Update functions/chat_functions.py
Browse files- functions/chat_functions.py +93 -93
functions/chat_functions.py
CHANGED
@@ -1,93 +1,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 |
-
print("CHATBOT FUNCTIONS")
|
28 |
-
from functions import sqlite_query_func
|
29 |
-
from pipelines import rag_pipeline_func
|
30 |
-
import tools
|
31 |
-
import importlib
|
32 |
-
importlib.reload(tools)
|
33 |
-
|
34 |
-
available_functions = {"sql_query_func": sqlite_query_func, "rag_pipeline_func": rag_pipeline_func}
|
35 |
-
messages.append(ChatMessage.from_user(message))
|
36 |
-
response = chat_generator.run(messages=messages, generation_kwargs={"tools": tools.tools})
|
37 |
-
|
38 |
-
while True:
|
39 |
-
# if OpenAI response is a tool call
|
40 |
-
if response and response["replies"][0].meta["finish_reason"] == "tool_calls":
|
41 |
-
function_calls = json.loads(response["replies"][0].
|
42 |
-
for function_call in function_calls:
|
43 |
-
## Parse function calling information
|
44 |
-
function_name = function_call["function"]["name"]
|
45 |
-
function_args = json.loads(function_call["function"]["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_function`
|
51 |
-
messages.append(ChatMessage.from_function(content=function_response['reply'], name=function_name))
|
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].
|
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 |
-
print("UPLOAD VALUE")
|
89 |
-
print(upload_value)
|
90 |
-
process_data_upload(upload_value)
|
91 |
-
return [], []
|
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 |
+
print("CHATBOT FUNCTIONS")
|
28 |
+
from functions import sqlite_query_func
|
29 |
+
from pipelines import rag_pipeline_func
|
30 |
+
import tools
|
31 |
+
import importlib
|
32 |
+
importlib.reload(tools)
|
33 |
+
|
34 |
+
available_functions = {"sql_query_func": sqlite_query_func, "rag_pipeline_func": rag_pipeline_func}
|
35 |
+
messages.append(ChatMessage.from_user(message))
|
36 |
+
response = chat_generator.run(messages=messages, generation_kwargs={"tools": tools.tools})
|
37 |
+
|
38 |
+
while True:
|
39 |
+
# if OpenAI response is a tool call
|
40 |
+
if response and response["replies"][0].meta["finish_reason"] == "tool_calls":
|
41 |
+
function_calls = json.loads(response["replies"][0].text)
|
42 |
+
for function_call in function_calls:
|
43 |
+
## Parse function calling information
|
44 |
+
function_name = function_call["function"]["name"]
|
45 |
+
function_args = json.loads(function_call["function"]["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_function`
|
51 |
+
messages.append(ChatMessage.from_function(content=function_response['reply'], name=function_name))
|
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 |
+
print("UPLOAD VALUE")
|
89 |
+
print(upload_value)
|
90 |
+
process_data_upload(upload_value)
|
91 |
+
return [], []
|
92 |
+
|
93 |
+
|