Fixes for chart generation
Browse filesShould allow for larger tables to load. Adding additional data scrubbing to account for LLM format variation
- functions/chart_functions.py +26 -4
- functions/chat_functions.py +1 -1
- functions/sqlite_functions.py +1 -1
- tools.py +2 -3
functions/chart_functions.py
CHANGED
|
@@ -6,6 +6,7 @@ import pandas as pd
|
|
| 6 |
from utils import TEMP_DIR
|
| 7 |
import os
|
| 8 |
import ast
|
|
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
load_dotenv()
|
|
@@ -22,12 +23,20 @@ def llm_chart_data_scrub(data, layout):
|
|
| 22 |
else:
|
| 23 |
data_list.append(data)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
data_dict = {}
|
| 26 |
for data_obj in data_list:
|
| 27 |
if isinstance(data_obj, str):
|
| 28 |
data_obj = data_obj.replace("\n", "")
|
| 29 |
-
|
| 30 |
-
data_obj =
|
|
|
|
|
|
|
|
|
|
| 31 |
data_dict = ast.literal_eval(data_obj)
|
| 32 |
else:
|
| 33 |
data_dict = data_obj
|
|
@@ -38,6 +47,11 @@ def llm_chart_data_scrub(data, layout):
|
|
| 38 |
layout_obj = layout
|
| 39 |
|
| 40 |
if layout_obj and isinstance(layout_obj, str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
layout_dict = ast.literal_eval(layout_obj)
|
| 42 |
else:
|
| 43 |
layout_dict = layout_obj
|
|
@@ -348,6 +362,7 @@ def table_generation_func(session_hash):
|
|
| 348 |
try:
|
| 349 |
dir_path = TEMP_DIR / str(session_hash)
|
| 350 |
csv_query_path = f'{dir_path}/query.csv'
|
|
|
|
| 351 |
|
| 352 |
df = pd.read_csv(csv_query_path)
|
| 353 |
print(df)
|
|
@@ -355,12 +370,19 @@ def table_generation_func(session_hash):
|
|
| 355 |
html_table = df.to_html()
|
| 356 |
print(html_table)
|
| 357 |
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
except Exception as e:
|
| 361 |
print("TABLE ERROR")
|
| 362 |
print(e)
|
| 363 |
-
reply = f"""There was an error generating the Pandas DataFrame table
|
| 364 |
The error is {e},
|
| 365 |
You should probably try again.
|
| 366 |
"""
|
|
|
|
| 6 |
from utils import TEMP_DIR
|
| 7 |
import os
|
| 8 |
import ast
|
| 9 |
+
import json
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
load_dotenv()
|
|
|
|
| 23 |
else:
|
| 24 |
data_list.append(data)
|
| 25 |
|
| 26 |
+
false_replace = [':false', ': false']
|
| 27 |
+
false_value = ':False'
|
| 28 |
+
true_replace = [':true', ': true']
|
| 29 |
+
true_value = ':True'
|
| 30 |
+
|
| 31 |
data_dict = {}
|
| 32 |
for data_obj in data_list:
|
| 33 |
if isinstance(data_obj, str):
|
| 34 |
data_obj = data_obj.replace("\n", "")
|
| 35 |
+
for replace in false_replace:
|
| 36 |
+
data_obj = data_obj.replace(replace, false_value)
|
| 37 |
+
for replace in true_replace:
|
| 38 |
+
data_obj = data_obj.replace(replace, true_value)
|
| 39 |
+
print(data_obj)
|
| 40 |
data_dict = ast.literal_eval(data_obj)
|
| 41 |
else:
|
| 42 |
data_dict = data_obj
|
|
|
|
| 47 |
layout_obj = layout
|
| 48 |
|
| 49 |
if layout_obj and isinstance(layout_obj, str):
|
| 50 |
+
for replace in false_replace:
|
| 51 |
+
layout_obj = layout_obj.replace(replace, false_value)
|
| 52 |
+
for replace in true_replace:
|
| 53 |
+
layout_obj = layout_obj.replace(replace, true_value)
|
| 54 |
+
print(layout_obj)
|
| 55 |
layout_dict = ast.literal_eval(layout_obj)
|
| 56 |
else:
|
| 57 |
layout_dict = layout_obj
|
|
|
|
| 362 |
try:
|
| 363 |
dir_path = TEMP_DIR / str(session_hash)
|
| 364 |
csv_query_path = f'{dir_path}/query.csv'
|
| 365 |
+
table_path = f'{dir_path}/table.html'
|
| 366 |
|
| 367 |
df = pd.read_csv(csv_query_path)
|
| 368 |
print(df)
|
|
|
|
| 370 |
html_table = df.to_html()
|
| 371 |
print(html_table)
|
| 372 |
|
| 373 |
+
with open(table_path, "w") as file:
|
| 374 |
+
file.write(html_table)
|
| 375 |
+
|
| 376 |
+
table_url = f'{root_url}/gradio_api/file/temp/{session_hash}/table.html'
|
| 377 |
+
|
| 378 |
+
iframe = '<div style=overflow:auto;><iframe\n scrolling="yes"\n width="1000px"\n height="500px"\n src="' + table_url + '"\n frameborder="0"\n allowfullscreen\n></iframe>\n</div>'
|
| 379 |
+
print(iframe)
|
| 380 |
+
return {"reply": iframe}
|
| 381 |
|
| 382 |
except Exception as e:
|
| 383 |
print("TABLE ERROR")
|
| 384 |
print(e)
|
| 385 |
+
reply = f"""There was an error generating the Pandas DataFrame table results.
|
| 386 |
The error is {e},
|
| 387 |
You should probably try again.
|
| 388 |
"""
|
functions/chat_functions.py
CHANGED
|
@@ -52,7 +52,7 @@ def chatbot_with_fc(message, history, session_hash):
|
|
| 52 |
messages = [
|
| 53 |
ChatMessage.from_system(
|
| 54 |
"""You are a helpful and knowledgeable agent who has access to an SQLite database which has a table called 'data_source'.
|
| 55 |
-
You also have access to a function, called table_generation_func, that
|
| 56 |
You also have access to a scatter plot function, called scatter_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a scatter plot and returns an iframe that we can display in our chat window.
|
| 57 |
You also have access to a line chart function, called line_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a line chart and returns an iframe that we can display in our chat window.
|
| 58 |
You also have access to a bar graph function, called line_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a bar graph and returns an iframe that we can display in our chat window.
|
|
|
|
| 52 |
messages = [
|
| 53 |
ChatMessage.from_system(
|
| 54 |
"""You are a helpful and knowledgeable agent who has access to an SQLite database which has a table called 'data_source'.
|
| 55 |
+
You also have access to a function, called table_generation_func, that can take a query.csv file generated from our sql query and returns an iframe that we can display in our chat window.
|
| 56 |
You also have access to a scatter plot function, called scatter_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a scatter plot and returns an iframe that we can display in our chat window.
|
| 57 |
You also have access to a line chart function, called line_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a line chart and returns an iframe that we can display in our chat window.
|
| 58 |
You also have access to a bar graph function, called line_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a bar graph and returns an iframe that we can display in our chat window.
|
functions/sqlite_functions.py
CHANGED
|
@@ -35,7 +35,7 @@ def sqlite_query_func(queries: List[str], session_hash):
|
|
| 35 |
result = sql_query.run(queries, session_hash)
|
| 36 |
if len(result["results"][0]) > 1000:
|
| 37 |
print("QUERY TOO LARGE")
|
| 38 |
-
return {"reply": "query result too large to be processed by llm, the query results are in our query.csv file"}
|
| 39 |
else:
|
| 40 |
return {"reply": result["results"][0]}
|
| 41 |
|
|
|
|
| 35 |
result = sql_query.run(queries, session_hash)
|
| 36 |
if len(result["results"][0]) > 1000:
|
| 37 |
print("QUERY TOO LARGE")
|
| 38 |
+
return {"reply": "query result too large to be processed by llm, the query results are in our query.csv file. If you need to display the results directly, perhaps use the table_generation_func function."}
|
| 39 |
else:
|
| 40 |
return {"reply": result["results"][0]}
|
| 41 |
|
tools.py
CHANGED
|
@@ -401,9 +401,8 @@ def tools_call(session_hash):
|
|
| 401 |
"description": f"""This an table generation tool useful to format data as a table from queried data from our SQL table called 'data_source'.
|
| 402 |
Takes no parameters as it uses data queried in our query.csv file to build the table.
|
| 403 |
Call this function after running our SQLite query and generating query.csv.
|
| 404 |
-
Returns an
|
| 405 |
-
function
|
| 406 |
-
Do not edit the string returned by the function in any way when displaying to the user.""",
|
| 407 |
"parameters": {},
|
| 408 |
},
|
| 409 |
},
|
|
|
|
| 401 |
"description": f"""This an table generation tool useful to format data as a table from queried data from our SQL table called 'data_source'.
|
| 402 |
Takes no parameters as it uses data queried in our query.csv file to build the table.
|
| 403 |
Call this function after running our SQLite query and generating query.csv.
|
| 404 |
+
Returns an iframe string which will be displayed inline in our chat window. Do not edit the iframe string returned
|
| 405 |
+
from the table_generation_func function in any way and always display the iframe fully to the user in the chat window.""",
|
|
|
|
| 406 |
"parameters": {},
|
| 407 |
},
|
| 408 |
},
|