-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
270 lines (236 loc) · 9.69 KB
/
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
264
265
266
267
268
269
270
import time
from google.cloud import bigquery
import streamlit as st
from vertexai.generative_models import FunctionDeclaration, GenerativeModel, Part, Tool
BIGQUERY_DATASET_ID = "bigquery-public-data.thelook_ecommerce"
list_datasets_func = FunctionDeclaration(
name="list_datasets",
description="Get a list of datasets that will help answer the user's question",
parameters={
"type": "object",
"properties": {},
},
)
list_tables_func = FunctionDeclaration(
name="list_tables",
description="List tables in a dataset that will help answer the user's question",
parameters={
"type": "object",
"properties": {
"dataset_id": {
"type": "string",
"description": "Dataset ID to fetch tables from.",
}
},
"required": [
"dataset_id",
],
},
)
get_table_func = FunctionDeclaration(
name="get_table",
description="Get information about a table, including the description, schema, and number of rows that will help answer the user's question. Always use the fully qualified dataset and table names.",
parameters={
"type": "object",
"properties": {
"table_id": {
"type": "string",
"description": "Fully qualified ID of the table to get information about",
}
},
"required": [
"table_id",
],
},
)
sql_query_func = FunctionDeclaration(
name="sql_query",
description="Get information from data in BigQuery using SQL queries",
parameters={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "SQL query on a single line that will help give quantitative answers to the user's question when run on a BigQuery dataset and table. In the SQL query, always use the fully qualified dataset and table names.",
}
},
"required": [
"query",
],
},
)
sql_query_tool = Tool(
function_declarations=[
list_datasets_func,
list_tables_func,
get_table_func,
sql_query_func,
],
)
model = GenerativeModel(
"gemini-1.0-pro",
generation_config={"temperature": 0},
tools=[sql_query_tool],
)
st.set_page_config(
page_title="SQL Talk with BigQuery",
page_icon="vertex-ai.png",
layout="wide",
)
col1, col2 = st.columns([8, 1])
with col1:
st.title("SQL Talk with BigQuery")
with col2:
st.image("vertex-ai.png")
st.subheader("Powered by Function Calling in Gemini")
st.markdown(
"[Source Code](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/gemini/function-calling/sql-talk-app/) • [Documentation](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/function-calling) • [Codelab](https://codelabs.developers.google.com/codelabs/gemini-function-calling) • [Sample Notebook](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/intro_function_calling.ipynb)"
)
with st.expander("Sample prompts", expanded=True):
st.write(
"""
- What kind of information is in this database?
- What percentage of orders are returned?
- How is inventory distributed across our regional distribution centers?
- Do customers typically place more than one order?
- Which product categories have the highest profit margins?
"""
)
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"].replace("$", "\$")) # noqa: W605
try:
with st.expander("Function calls, parameters, and responses"):
st.markdown(message["backend_details"])
except KeyError:
pass
if prompt := st.chat_input("Ask me about information in the database..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
chat = model.start_chat()
client = bigquery.Client()
prompt += """
Please give a concise, high-level summary followed by detail in
plain language about where the information in your response is
coming from in the database. Only use information that you learn
from BigQuery, do not make up information.
"""
response = chat.send_message(prompt)
response = response.candidates[0].content.parts[0]
print(response)
api_requests_and_responses = []
backend_details = ""
function_calling_in_process = True
while function_calling_in_process:
try:
params = {}
for key, value in response.function_call.args.items():
params[key] = value
print(response.function_call.name)
print(params)
if response.function_call.name == "list_datasets":
api_response = client.list_datasets()
api_response = BIGQUERY_DATASET_ID
api_requests_and_responses.append(
[response.function_call.name, params, api_response]
)
if response.function_call.name == "list_tables":
api_response = client.list_tables(params["dataset_id"])
api_response = str([table.table_id for table in api_response])
api_requests_and_responses.append(
[response.function_call.name, params, api_response]
)
if response.function_call.name == "get_table":
api_response = client.get_table(params["table_id"])
api_response = api_response.to_api_repr()
api_requests_and_responses.append(
[
response.function_call.name,
params,
[
str(api_response.get("description", "")),
str(
[
column["name"]
for column in api_response["schema"]["fields"]
]
),
],
]
)
api_response = str(api_response)
if response.function_call.name == "sql_query":
job_config = bigquery.QueryJobConfig(
maximum_bytes_billed=100000000
) # Data limit per query job
try:
cleaned_query = (
params["query"]
.replace("\\n", " ")
.replace("\n", "")
.replace("\\", "")
)
query_job = client.query(cleaned_query, job_config=job_config)
api_response = query_job.result()
api_response = str([dict(row) for row in api_response])
api_response = api_response.replace("\\", "").replace("\n", "")
api_requests_and_responses.append(
[response.function_call.name, params, api_response]
)
except Exception as e:
api_response = f"{str(e)}"
api_requests_and_responses.append(
[response.function_call.name, params, api_response]
)
print(api_response)
response = chat.send_message(
Part.from_function_response(
name=response.function_call.name,
response={
"content": api_response,
},
),
)
response = response.candidates[0].content.parts[0]
backend_details += "- Function call:\n"
backend_details += (
" - Function name: ```"
+ str(api_requests_and_responses[-1][0])
+ "```"
)
backend_details += "\n\n"
backend_details += (
" - Function parameters: ```"
+ str(api_requests_and_responses[-1][1])
+ "```"
)
backend_details += "\n\n"
backend_details += (
" - API response: ```"
+ str(api_requests_and_responses[-1][2])
+ "```"
)
backend_details += "\n\n"
with message_placeholder.container():
st.markdown(backend_details)
except AttributeError:
function_calling_in_process = False
time.sleep(3)
full_response = response.text
with message_placeholder.container():
st.markdown(full_response.replace("$", "\$")) # noqa: W605
with st.expander("Function calls, parameters, and responses:"):
st.markdown(backend_details)
st.session_state.messages.append(
{
"role": "assistant",
"content": full_response,
"backend_details": backend_details,
}
)