forked from epec254/rag_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_hello_world_driver_notebook.py
More file actions
133 lines (102 loc) · 4.38 KB
/
Copy path1_hello_world_driver_notebook.py
File metadata and controls
133 lines (102 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Databricks notebook source
# DBTITLE 1,Databricks RAG Studio Installer
# MAGIC %run ./wheel_installer
# COMMAND ----------
dbutils.library.restartPython()
# COMMAND ----------
# DBTITLE 1,Imports
import os
import mlflow
from databricks import rag_studio
### START: Ignore this code, temporary workarounds given the Private Preview state of the product
from mlflow.utils import databricks_utils as du
os.environ['MLFLOW_ENABLE_ARTIFACTS_PROGRESS_BAR'] = "false"
def parse_deployment_info(deployment_info):
browser_url = du.get_browser_hostname()
message = f"""Deployment of {deployment_info.model_name} version {deployment_info.model_version} initiated. This can take up to 15 minutes and the Review App & REST API will not work until this deployment finishes.
View status: https://{browser_url}/ml/endpoints/{deployment_info.endpoint_name}
Review App: {deployment_info.rag_app_url}"""
return message
### END: Ignore this code, temporary workarounds given the Private Preview state of the product
# COMMAND ----------
# DBTITLE 1,Setup
############
# Specify the full path to the chain notebook
############
# Assuming your chain notebook is in the current directory, this helper line grabs the current path, prepending /Workspace/
# Limitation: RAG Studio does not support logging chains stored in Repos
current_path = '/Workspace' + os.path.dirname(dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get())
chain_notebook_file = "1_hello_world_chain"
chain_notebook_path = f"{current_path}/{chain_notebook_file}"
print(f"Saving chain from: {chain_notebook_path}")
# COMMAND ----------
# DBTITLE 1,Log the model
############
# Log the chain to the Notebook's MLflow Experiment inside a Run
# The model is logged to the Notebook's MLflow Experiment as a run
############
logged_chain_info = rag_studio.log_model(code_path=chain_notebook_path)
print(f"MLflow Run: {logged_chain_info.run_id}")
print(f"Model URI: {logged_chain_info.model_uri}")
############
# If you see this error, go to your chain code and comment out all usage of `dbutils`
############
# ValueError: The file specified by 'code_path' uses 'dbutils' command which are not supported in a chain model. To ensure your code functions correctly, remove or comment out usage of 'dbutils' command.
# COMMAND ----------
# DBTITLE 1,Run the logged model locally
############
# You can test the model locally
# This is the same input that the REST API will accept once deployed.
############
example_input = {
"messages": [
{
"role": "user",
"content": "Hello world!!",
},
{
"role": "assistant",
"content": "Hello back.",
},
{
"role": "user",
"content": "Hello again.",
}
]
}
model = mlflow.langchain.load_model(logged_chain_info.model_uri)
model.invoke(example_input)
# COMMAND ----------
############
# Normally, you would now evaluate the chain, but lets skip ahead to deploying the chain so your stakeholders can use it via a chat UI.
############
# COMMAND ----------
############
# To deploy the model, first register the chain from the MLflow Run as a Unity Catalog model.
############
uc_catalog = "catalog"
uc_schema = "schema"
model_name = "hello_world"
uc_model_fqdn = f"{uc_catalog}.{uc_schema}.{model_name}"
mlflow.set_registry_uri('databricks-uc')
uc_registered_chain_info = mlflow.register_model(logged_chain_info.model_uri, uc_model_fqdn)
# COMMAND ----------
# DBTITLE 1,Deploy the model
############
# Deploy the chain to:
# 1) Review App so you & your stakeholders can chat with the chain & given feedback via a web UI.
# 2) Chain REST API endpoint to call the chain from your front end
# 3) Feedback REST API endpoint to pass feedback back from your front end.
############
deployment_info = rag_studio.deploy_model(uc_model_fqdn, uc_registered_chain_info.version)
print(parse_deployment_info(deployment_info))
# Note: It can take up to 15 minutes to deploy - we are working to reduce this time to seconds.
# COMMAND ----------
# DBTITLE 1,View deployments
############
# If you lost the deployment information captured above, you can find it using list_deployments()
############
deployments = rag_studio.list_deployments()
for deployment in deployments:
if deployment.model_name == uc_model_fqdn and deployment.model_version==uc_registered_chain_info.version:
print(parse_deployment_info(deployment))