Upload 11 files
Browse filespackage updates and improvements
- __init__.py +1 -1
- functions/chat_functions.py +91 -93
- pipelines/pipelines.py +0 -32
- tools.py +2 -2
__init__.py
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
from .
|
2 |
|
3 |
__all__ = ["data_url"]
|
|
|
1 |
+
from .app import data_url
|
2 |
|
3 |
__all__ = ["data_url"]
|
functions/chat_functions.py
CHANGED
@@ -1,93 +1,91 @@
|
|
1 |
-
from data_sources import process_data_upload
|
2 |
-
|
3 |
-
import gradio as gr
|
4 |
-
import json
|
5 |
-
|
6 |
-
from haystack.dataclasses import ChatMessage
|
7 |
-
from haystack.components.generators.chat import OpenAIChatGenerator
|
8 |
-
|
9 |
-
import os
|
10 |
-
from getpass import getpass
|
11 |
-
from dotenv import load_dotenv
|
12 |
-
|
13 |
-
load_dotenv()
|
14 |
-
|
15 |
-
if "OPENAI_API_KEY" not in os.environ:
|
16 |
-
os.environ["OPENAI_API_KEY"] = getpass("Enter OpenAI API key:")
|
17 |
-
|
18 |
-
chat_generator = OpenAIChatGenerator(model="gpt-4o")
|
19 |
-
response = None
|
20 |
-
messages = [
|
21 |
-
ChatMessage.from_system(
|
22 |
-
"You are a helpful and knowledgeable agent who has access to an SQL database which has a table called 'data_source'"
|
23 |
-
)
|
24 |
-
]
|
25 |
-
|
26 |
-
def chatbot_with_fc(message, history):
|
27 |
-
|
28 |
-
from
|
29 |
-
|
30 |
-
import
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
## Parse function calling information
|
44 |
-
function_name = function_call
|
45 |
-
function_args =
|
46 |
-
|
47 |
-
## Find the correspoding function and call it with the given arguments
|
48 |
-
function_to_call = available_functions[function_name]
|
49 |
-
function_response = function_to_call(**function_args)
|
50 |
-
## Append function response to the messages list using `ChatMessage.
|
51 |
-
messages.append(ChatMessage.
|
52 |
-
response = chat_generator.run(messages=messages, generation_kwargs={"tools": tools.tools})
|
53 |
-
|
54 |
-
# Regular Conversation
|
55 |
-
else:
|
56 |
-
messages.append(response["replies"][0])
|
57 |
-
break
|
58 |
-
return response["replies"][0].text
|
59 |
-
|
60 |
-
css= ".file_marker .large{min-height:50px !important;}"
|
61 |
-
|
62 |
-
with gr.Blocks(css=css) as demo:
|
63 |
-
title = gr.HTML("<h1 style='text-align:center;'>Virtual Data Analyst</h1>")
|
64 |
-
description = gr.HTML("<p style='text-align:center;'>Upload a CSV file and chat with our virtual data analyst to get insights on your data set</p>")
|
65 |
-
file_output = gr.File(label="CSV File", show_label=True, elem_classes="file_marker", file_types=['.csv'])
|
66 |
-
|
67 |
-
@gr.render(inputs=file_output)
|
68 |
-
def data_options(filename):
|
69 |
-
print(filename)
|
70 |
-
if filename:
|
71 |
-
bot = gr.Chatbot(type='messages', label="CSV Chat Window", show_label=True, render=False, visible=True, elem_classes="chatbot")
|
72 |
-
chat = gr.ChatInterface(
|
73 |
-
fn=chatbot_with_fc,
|
74 |
-
type='messages',
|
75 |
-
chatbot=bot,
|
76 |
-
title="Chat with your data file",
|
77 |
-
examples=[
|
78 |
-
["Describe the dataset"],
|
79 |
-
["List the columns in the dataset"],
|
80 |
-
["What could this data be used for?"],
|
81 |
-
],
|
82 |
-
)
|
83 |
-
|
84 |
-
process_upload(filename)
|
85 |
-
|
86 |
-
def process_upload(upload_value):
|
87 |
-
if upload_value:
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
1 |
+
from data_sources import process_data_upload
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import json
|
5 |
+
|
6 |
+
from haystack.dataclasses import ChatMessage
|
7 |
+
from haystack.components.generators.chat import OpenAIChatGenerator
|
8 |
+
|
9 |
+
import os
|
10 |
+
from getpass import getpass
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
if "OPENAI_API_KEY" not in os.environ:
|
16 |
+
os.environ["OPENAI_API_KEY"] = getpass("Enter OpenAI API key:")
|
17 |
+
|
18 |
+
chat_generator = OpenAIChatGenerator(model="gpt-4o")
|
19 |
+
response = None
|
20 |
+
messages = [
|
21 |
+
ChatMessage.from_system(
|
22 |
+
"You are a helpful and knowledgeable agent who has access to an SQL database which has a table called 'data_source'"
|
23 |
+
)
|
24 |
+
]
|
25 |
+
|
26 |
+
def chatbot_with_fc(message, history):
|
27 |
+
from functions import sqlite_query_func
|
28 |
+
from pipelines import rag_pipeline_func
|
29 |
+
import tools
|
30 |
+
import importlib
|
31 |
+
importlib.reload(tools)
|
32 |
+
|
33 |
+
available_functions = {"sql_query_func": sqlite_query_func, "rag_pipeline_func": rag_pipeline_func}
|
34 |
+
messages.append(ChatMessage.from_user(message))
|
35 |
+
response = chat_generator.run(messages=messages, generation_kwargs={"tools": tools.tools})
|
36 |
+
|
37 |
+
while True:
|
38 |
+
# if OpenAI response is a tool call
|
39 |
+
if response and response["replies"][0].meta["finish_reason"] == "tool_calls":
|
40 |
+
function_calls = response["replies"][0].tool_calls
|
41 |
+
for function_call in function_calls:
|
42 |
+
messages.append(ChatMessage.from_assistant(tool_calls=[function_call]))
|
43 |
+
## Parse function calling information
|
44 |
+
function_name = function_call.tool_name
|
45 |
+
function_args = function_call.arguments
|
46 |
+
|
47 |
+
## Find the correspoding function and call it with the given arguments
|
48 |
+
function_to_call = available_functions[function_name]
|
49 |
+
function_response = function_to_call(**function_args)
|
50 |
+
## Append function response to the messages list using `ChatMessage.from_tool`
|
51 |
+
messages.append(ChatMessage.from_tool(tool_result=function_response['reply'], origin=function_call))
|
52 |
+
response = chat_generator.run(messages=messages, generation_kwargs={"tools": tools.tools})
|
53 |
+
|
54 |
+
# Regular Conversation
|
55 |
+
else:
|
56 |
+
messages.append(response["replies"][0])
|
57 |
+
break
|
58 |
+
return response["replies"][0].text
|
59 |
+
|
60 |
+
css= ".file_marker .large{min-height:50px !important;}"
|
61 |
+
|
62 |
+
with gr.Blocks(css=css) as demo:
|
63 |
+
title = gr.HTML("<h1 style='text-align:center;'>Virtual Data Analyst</h1>")
|
64 |
+
description = gr.HTML("<p style='text-align:center;'>Upload a CSV file and chat with our virtual data analyst to get insights on your data set</p>")
|
65 |
+
file_output = gr.File(label="CSV File", show_label=True, elem_classes="file_marker", file_types=['.csv'])
|
66 |
+
|
67 |
+
@gr.render(inputs=file_output)
|
68 |
+
def data_options(filename):
|
69 |
+
print(filename)
|
70 |
+
if filename:
|
71 |
+
bot = gr.Chatbot(type='messages', label="CSV Chat Window", show_label=True, render=False, visible=True, elem_classes="chatbot")
|
72 |
+
chat = gr.ChatInterface(
|
73 |
+
fn=chatbot_with_fc,
|
74 |
+
type='messages',
|
75 |
+
chatbot=bot,
|
76 |
+
title="Chat with your data file",
|
77 |
+
examples=[
|
78 |
+
["Describe the dataset"],
|
79 |
+
["List the columns in the dataset"],
|
80 |
+
["What could this data be used for?"],
|
81 |
+
],
|
82 |
+
)
|
83 |
+
|
84 |
+
process_upload(filename)
|
85 |
+
|
86 |
+
def process_upload(upload_value):
|
87 |
+
if upload_value:
|
88 |
+
process_data_upload(upload_value)
|
89 |
+
return [], []
|
90 |
+
|
91 |
+
|
|
|
|
pipelines/pipelines.py
CHANGED
@@ -16,30 +16,7 @@ load_dotenv()
|
|
16 |
|
17 |
if "OPENAI_API_KEY" not in os.environ:
|
18 |
os.environ["OPENAI_API_KEY"] = getpass("Enter OpenAI API key:")
|
19 |
-
'''
|
20 |
-
prompt = PromptBuilder(template="""Please generate an SQL query. The query should answer the following Question: {{question}};
|
21 |
-
The query is to be answered for the table is called 'data_source' with the following
|
22 |
-
Columns: {{columns}};
|
23 |
-
Answer:""")
|
24 |
-
sql_query = SQLQuery('data_source.db')
|
25 |
-
llm = OpenAIGenerator(model="gpt-4")
|
26 |
-
|
27 |
-
sql_pipeline = Pipeline()
|
28 |
-
sql_pipeline.add_component("prompt", prompt)
|
29 |
-
sql_pipeline.add_component("llm", llm)
|
30 |
-
sql_pipeline.add_component("sql_querier", sql_query)
|
31 |
-
|
32 |
-
sql_pipeline.connect("prompt", "llm")
|
33 |
-
sql_pipeline.connect("llm.replies", "sql_querier.queries")
|
34 |
|
35 |
-
# If you want to draw the pipeline, uncomment below 👇
|
36 |
-
sql_pipeline.show()
|
37 |
-
print("PIPELINE RUNNING")
|
38 |
-
result = sql_pipeline.run({"prompt": {"question": "On which days of the week are average sales highest?",
|
39 |
-
"columns": columns}})
|
40 |
-
|
41 |
-
print(result["sql_querier"]["results"][0])
|
42 |
-
'''
|
43 |
from haystack.components.builders import PromptBuilder
|
44 |
from haystack.components.generators import OpenAIGenerator
|
45 |
|
@@ -49,8 +26,6 @@ sql_query = SQLiteQuery('data_source.db')
|
|
49 |
connection = sqlite3.connect('data_source.db')
|
50 |
cur=connection.execute('select * from data_source')
|
51 |
columns = [i[0] for i in cur.description]
|
52 |
-
print("COLUMNS 2")
|
53 |
-
print(columns)
|
54 |
cur.close()
|
55 |
|
56 |
#Rag Pipeline
|
@@ -96,13 +71,6 @@ conditional_sql_pipeline.connect("router.sql", "sql_querier.queries")
|
|
96 |
conditional_sql_pipeline.connect("router.go_to_fallback", "fallback_prompt.question")
|
97 |
conditional_sql_pipeline.connect("fallback_prompt", "fallback_llm")
|
98 |
|
99 |
-
question = "When is my birthday?"
|
100 |
-
result = conditional_sql_pipeline.run({"prompt": {"question": question,
|
101 |
-
"columns": columns},
|
102 |
-
"router": {"question": question},
|
103 |
-
"fallback_prompt": {"columns": columns}})
|
104 |
-
|
105 |
-
|
106 |
def rag_pipeline_func(question: str, columns: str):
|
107 |
result = conditional_sql_pipeline.run({"prompt": {"question": question,
|
108 |
"columns": columns},
|
|
|
16 |
|
17 |
if "OPENAI_API_KEY" not in os.environ:
|
18 |
os.environ["OPENAI_API_KEY"] = getpass("Enter OpenAI API key:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
from haystack.components.builders import PromptBuilder
|
21 |
from haystack.components.generators import OpenAIGenerator
|
22 |
|
|
|
26 |
connection = sqlite3.connect('data_source.db')
|
27 |
cur=connection.execute('select * from data_source')
|
28 |
columns = [i[0] for i in cur.description]
|
|
|
|
|
29 |
cur.close()
|
30 |
|
31 |
#Rag Pipeline
|
|
|
71 |
conditional_sql_pipeline.connect("router.go_to_fallback", "fallback_prompt.question")
|
72 |
conditional_sql_pipeline.connect("fallback_prompt", "fallback_llm")
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
def rag_pipeline_func(question: str, columns: str):
|
75 |
result = conditional_sql_pipeline.run({"prompt": {"question": question,
|
76 |
"columns": columns},
|
tools.py
CHANGED
@@ -37,7 +37,7 @@ tools = [
|
|
37 |
"parameters": {
|
38 |
"type": "object",
|
39 |
"properties": {
|
40 |
-
"
|
41 |
"type": "array",
|
42 |
"description": "The query to use in the search. Infer this from the user's message. It should be a question or a statement",
|
43 |
"items": {
|
@@ -45,7 +45,7 @@ tools = [
|
|
45 |
}
|
46 |
}
|
47 |
},
|
48 |
-
"required": ["
|
49 |
},
|
50 |
},
|
51 |
}
|
|
|
37 |
"parameters": {
|
38 |
"type": "object",
|
39 |
"properties": {
|
40 |
+
"queries": {
|
41 |
"type": "array",
|
42 |
"description": "The query to use in the search. Infer this from the user's message. It should be a question or a statement",
|
43 |
"items": {
|
|
|
45 |
}
|
46 |
}
|
47 |
},
|
48 |
+
"required": ["question"],
|
49 |
},
|
50 |
},
|
51 |
}
|