in dbai_src/dbai.py [0:0]
def ask(self, question, chat):
"""main interface for interacting in multi-turn chat mode. """
prompt = question + \
f"\n The dataset_id is {self.dataset_id}" + \
self.system_prompt
response = chat.send_message(prompt)
response = response.candidates[0].content.parts[0]
intermediate_steps = []
function_calling_in_process = True
while function_calling_in_process:
try:
function_name, params = response.function_call.name, {}
for key, value in response.function_call.args.items():
params[key] = value
api_response = ''
if function_name == "list_tables":
api_response = self.api_list_tables()
if function_name == "get_table_metadata":
api_response = self.api_get_table_metadata(
params["table_id"]
)
if function_name == "sql_query":
api_response = self.execute_sql_query(params["query"])
# if function_name == "plot_chart":
# fig = api_plot_chart(params)
# st.plotly_chart(fig)#, use_container_width=True)
# api_response = "here is the plot of the data."
if function_name == "plot_chart_auto":
print(type(params['code']), params['code'])
local_namespace = {}
# Execute the code string in the local namespace
exec( # pylint: disable=exec-used
params['code'].replace('\r\n', '\n'),
globals(),
local_namespace
)
# Access the 'fig' variable from the local namespace
fig = local_namespace['fig']
st.plotly_chart(fig) # use_container_width=True)
api_response = "here is the plot of the data shown below \
in separate tab."
response = chat.send_message(
Part.from_function_response(
name=function_name,
response={
"content": api_response,
},
),
)
response = response.candidates[0].content.parts[0]
intermediate_steps.append({
'function_name': function_name,
'function_params': params,
'API_response': api_response,
'response': response
})
except AttributeError:
function_calling_in_process = False
return Response(text=response.text, interim_steps=intermediate_steps)