-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathChatbot-App.py
263 lines (187 loc) · 7.18 KB
/
Chatbot-App.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Databricks notebook source
# MAGIC %pip install gradio==3.38.0 fastapi==0.104 uvicorn
# MAGIC %pip install typing-extensions --upgrade
# COMMAND ----------
dbutils.library.restartPython()
# COMMAND ----------
endpoint_name = "sample_agent"
DESCRIPTION = f"""
# Chatbot powered by Databricks
Sample agent that uses text to sql.
"""
# COMMAND ----------
# MAGIC %md ### Helper functions
# COMMAND ----------
import requests
import json
workspaceUrl = spark.conf.get('spark.databricks.workspaceUrl')
databricks_token = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiToken().get()
chatbot_model_serving_endpoint = f'https://{workspaceUrl}/serving-endpoints/{endpoint_name}/invocations'
message_thread = []
def reset_thread():
global message_thread
message_thread = []
reset_thread()
# COMMAND ----------
def submit_prompt(prompt):
global message_thread
headers = {'Authorization': f'Bearer {databricks_token}', 'Content-Type': 'application/json'}
message_thread.append(
{
"role": "user",
"content": prompt
}
)
payload = {
"messages": message_thread
}
response = requests.post(chatbot_model_serving_endpoint, headers=headers, json=payload)
if response.status_code == 200:
resp = response.json()
bot_message = resp['choices'][0]['message']['content']
message_thread = resp['thread']
return bot_message
else:
error = response.json()
raise ValueError(f'Error submitting job: {error}')
# COMMAND ----------
def generate_output(message: str,
chat_history: list[tuple[str, str]],
# system_prompt: str,
max_new_tokens: int = 300,
temperature: float = 0.8,
top_p: float = 0.95,
top_k: int = 50):
output = submit_prompt(message)
return output
# COMMAND ----------
# MAGIC %md
# MAGIC ### Let's host it in gradio
# COMMAND ----------
import json
from dataclasses import dataclass
import uvicorn
from fastapi import FastAPI
# COMMAND ----------
@dataclass
class ProxySettings:
proxy_url: str
port: str
url_base_path: str
class DatabricksApp:
def __init__(self, port):
# self._app = data_app
self._port = port
import IPython
self._dbutils = IPython.get_ipython().user_ns["dbutils"]
self._display_html = IPython.get_ipython().user_ns["displayHTML"]
self._context = json.loads(self._dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
# need to do this after the context is set
self._cloud = self.get_cloud()
# create proxy settings after determining the cloud
self._ps = self.get_proxy_settings()
self._fastapi_app = self._make_fastapi_app(root_path=self._ps.url_base_path.rstrip("/"))
self._streamlit_script = None
# after everything is set print out the url
def _make_fastapi_app(self, root_path) -> FastAPI:
fast_api_app = FastAPI(root_path=root_path)
@fast_api_app.get("/")
def read_main():
return {
"routes": [
{"method": "GET", "path": "/", "summary": "Landing"},
{"method": "GET", "path": "/status", "summary": "App status"},
{"method": "GET", "path": "/dash", "summary": "Sub-mounted Dash application"},
]
}
@fast_api_app.get("/status")
def get_status():
return {"status": "ok"}
return fast_api_app
def get_proxy_settings(self) -> ProxySettings:
if self._cloud.lower() not in ["aws", "azure"]:
raise Exception("only supported in aws or azure")
org_id = self._context["tags"]["orgId"]
org_shard = ""
# org_shard doesnt need a suffix of "." for dnsname its handled in building the url
if self._cloud.lower() == "azure":
org_shard_id = int(org_id) % 20
org_shard = f".{org_shard_id}"
cluster_id = self._context["tags"]["clusterId"]
url_base_path = f"/driver-proxy/o/{org_id}/{cluster_id}/{self._port}"
from dbruntime.databricks_repl_context import get_context
host_name = get_context().browserHostName
proxy_url = f"https://{host_name}/driver-proxy/o/{org_id}/{cluster_id}/{self._port}/"
return ProxySettings(
proxy_url=proxy_url,
port=self._port,
url_base_path=url_base_path
)
@property
def app_url_base_path(self):
return self._ps.url_base_path
def mount_gradio_app(self, gradio_app):
import gradio as gr
# gradio_app.queue()
gr.mount_gradio_app(self._fastapi_app, gradio_app, f"/gradio")
# self._fastapi_app.mount("/gradio", gradio_app)
self.display_url(self.get_gradio_url())
def get_cloud(self):
if self._context["extraContext"]["api_url"].endswith("azuredatabricks.net"):
return "azure"
return "aws"
def get_gradio_url(self):
# must end with a "/" for it to not redirect
return f'<a href="{self._ps.proxy_url}gradio/">Click to go to Gradio App!</a>'
def display_url(self, url):
self._display_html(url)
def run(self):
print(self.app_url_base_path)
uvicorn.run(self._fastapi_app, host="0.0.0.0", port=self._port)
# COMMAND ----------
import gradio as gr
import random
import time
def process_example(message: str, history: str):
# system_prompt, max_new_tokens, temperature, top_p, top_k
output = generate_output(message, history)
return output
chatbot = gr.Chatbot(height=500)
details = gr.JSON()
with gr.Blocks(theme=gr.themes.Soft()) as demo:
with gr.Row():
gr.HTML(
show_label=False,
value="<img src='https://databricks.gallerycdn.vsassets.io/extensions/databricks/databricks/0.3.15/1686753455931/Microsoft.VisualStudio.Services.Icons.Default' height='40' width='40'/><div font size='1'></div>",
)
gr.Markdown(DESCRIPTION)
tabbed = gr.TabbedInterface([chatbot, details], ["Chat", "JSON"])
#chatbot = gr.Chatbot(height=500)
msg = gr.Textbox(label='User Question'
# , value='Ask your question'
)
clear = gr.ClearButton([msg, chatbot, details])
clear.click(reset_thread)
def respond(message, chat_history):
bot_message = process_example(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history, message_thread
msg.submit(fn=respond,
inputs=[msg, chatbot],
outputs=[msg, chatbot, details])
# COMMAND ----------
app_port = 8766
# COMMAND ----------
print(spark.conf.get("spark.databricks.clusterUsageTags.clusterOwnerOrgId"))
# COMMAND ----------
cluster_id = dbutils.notebook.entry_point.getDbutils().notebook().getContext().clusterId().getOrElse(None)
workspace_id = spark.conf.get("spark.databricks.clusterUsageTags.clusterOwnerOrgId")
print(f"Use this URL to access the chatbot app: ")
print(f"https://dbc-dp-{workspace_id}.cloud.databricks.com/driver-proxy/o/{workspace_id}/{cluster_id}/{app_port}/gradio/")
# COMMAND ----------
dbx_app = DatabricksApp(app_port)
# demo.queue()
dbx_app.mount_gradio_app(demo)
import nest_asyncio
nest_asyncio.apply()
dbx_app.run()