Skip to content

Commit

Permalink
Merge branch 'main' into inardini--batch-pipeline-rag
Browse files Browse the repository at this point in the history
  • Loading branch information
inardini authored Sep 19, 2024
2 parents 0c50c3e + 9de3321 commit 56a1cf4
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 112 deletions.
260 changes: 148 additions & 112 deletions gemini/function-calling/sql-talk-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,131 +140,167 @@
from BigQuery, do not make up information.
"""

response = chat.send_message(prompt)
response = response.candidates[0].content.parts[0]

print(response)
try:
response = chat.send_message(prompt)
response = response.candidates[0].content.parts[0]

api_requests_and_responses = []
backend_details = ""
print(response)

function_calling_in_process = True
while function_calling_in_process:
try:
params = {}
for key, value in response.function_call.args.items():
params[key] = value
api_requests_and_responses = []
backend_details = ""

print(response.function_call.name)
print(params)
function_calling_in_process = True
while function_calling_in_process:
try:
params = {}
for key, value in response.function_call.args.items():
params[key] = value

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]
)
print(response.function_call.name)
print(params)

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", "")
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]
)
except Exception as e:
api_response = f"{str(e)}"

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]
)

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)
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:
error_message = f"""
We're having trouble running this SQL query. This
could be due to an invalid query or the structure of
the data. Try rephrasing your question to help the
model generate a valid query. Details:
{str(e)}"""
st.error(error_message)
api_response = error_message
api_requests_and_responses.append(
[response.function_call.name, params, api_response]
)
st.session_state.messages.append(
{
"role": "assistant",
"content": error_message,
}
)

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
except AttributeError:
function_calling_in_process = False

time.sleep(3)
time.sleep(3)

full_response = response.text
with message_placeholder.container():
st.markdown(full_response.replace("$", r"\$")) # noqa: W605
with st.expander("Function calls, parameters, and responses:"):
st.markdown(backend_details)
full_response = response.text
with message_placeholder.container():
st.markdown(full_response.replace("$", r"\$")) # 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,
}
)
st.session_state.messages.append(
{
"role": "assistant",
"content": full_response,
"backend_details": backend_details,
}
)
except Exception as e:
print(e)
error_message = f"""
Something went wrong! We encountered an unexpected error while
trying to process your request. Please try rephrasing your
question. Details:
{str(e)}"""
st.error(error_message)
st.session_state.messages.append(
{
"role": "assistant",
"content": error_message,
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RN8N3O43QDT5"
},
"source": [
"<table align=\"left\">\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/prompts/prompt_optimizer/vertex_ai_prompt_optimizer_ui.ipynb\">\n",
" <img width=\"32px\" src=\"https://www.gstatic.com/pantheon/images/bigquery/welcome_page/colab-logo.svg\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
" </a>\n",
" </td>\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fprompts%2Fprompt_optimizer%2Fvertex_ai_prompt_optimizer_ui.ipynb\">\n",
" <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
" </a>\n",
" </td>\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/gemini/prompts/prompt_optimizer/vertex_ai_prompt_optimizer_ui.ipynb\">\n",
" <img src=\"https://www.gstatic.com/images/branding/gcpiconscolors/vertexai/v1/32px.svg\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/prompts/prompt_optimizer/vertex_ai_prompt_optimizer_ui.ipynb\">\n",
" <img width=\"32px\" src=\"https://upload.wikimedia.org/wikipedia/commons/9/91/Octicons-mark-github.svg\" alt=\"GitHub logo\"><br> View on GitHub\n",
" </a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down

0 comments on commit 56a1cf4

Please sign in to comment.