File size: 20,691 Bytes
ccbdd61
32f5b77
 
 
 
ac253c3
 
32f5b77
 
95c52e2
 
 
 
 
 
 
 
c9c27b8
274670b
 
 
dd82d0a
95c52e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32f5b77
c101c53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0614630
 
 
 
 
 
 
ac253c3
0614630
 
 
 
 
 
 
 
 
 
 
 
fb65c41
76271c7
 
3d87c18
32f5b77
76271c7
 
 
 
 
ac253c3
 
 
 
 
 
 
 
 
60f68c4
c101c53
 
95c52e2
 
 
ac253c3
c9c27b8
 
 
 
 
 
c101c53
c9c27b8
c101c53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9c27b8
 
 
 
 
 
c101c53
c9c27b8
95c52e2
 
 
c101c53
95c52e2
c101c53
32f5b77
 
 
fb65c41
32f5b77
 
c101c53
32f5b77
 
 
 
60f68c4
32f5b77
c101c53
 
60f68c4
32f5b77
c101c53
 
32f5b77
 
 
c101c53
32f5b77
3d87c18
0614630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d87c18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from utils import TEMP_DIR, message_dict

from haystack.dataclasses import ChatMessage
from haystack.components.generators.chat import OpenAIChatGenerator

import sqlite3

chat_generator = OpenAIChatGenerator(model="gpt-4o")
response = None

def example_question_generator(session_hash):
    example_response = None
    example_messages = [
        ChatMessage.from_system(
            "You are a helpful and knowledgeable agent who has access to an SQLite database which has a table called 'data_source'."
        )
    ]

    session_path = 'file_upload'

    dir_path = TEMP_DIR / str(session_hash) / str(session_path)
    connection = sqlite3.connect(f'{dir_path}/data_source.db')
    print("Querying questions");
    cur=connection.execute('select * from data_source')
    columns = [i[0] for i in cur.description]
    print("QUESTION COLUMNS")
    print(columns)
    cur.close()
    connection.close()

    example_messages.append(ChatMessage.from_user(text=f"""We have a SQLite database with the following {columns}. 

                                                  We also have an AI agent with access to the same database that will be performing data analysis.

                                                  Please return an array of seven strings, each one being a question for our data analysis agent

                                                  that we can suggest that you believe will be insightful or helpful to a data analysis looking for

                                                  data insights. Return nothing more than the array of questions because I need that specific data structure

                                                  to process your response. No other response type or data structure will work."""))

    example_response = chat_generator.run(messages=example_messages)

    return example_response["replies"][0].text

def sql_example_question_generator(session_hash, db_tables, db_name):
    example_response = None
    example_messages = [
        ChatMessage.from_system(
            f"You are a helpful and knowledgeable agent who has access to an PostgreSQL database called {db_name}."
        )
    ]

    example_messages.append(ChatMessage.from_user(text=f"""We have a PostgreSQL database with the following tables: {db_tables}. 

                                                  We also have an AI agent with access to the same database that will be performing data analysis.

                                                  Please return an array of seven strings, each one being a question for our data analysis agent

                                                  that we can suggest that you believe will be insightful or helpful to a data analysis looking for

                                                  data insights. Return nothing more than the array of questions because I need that specific data structure

                                                  to process your response. No other response type or data structure will work."""))

    example_response = chat_generator.run(messages=example_messages)

    return example_response["replies"][0].text

def doc_db_example_question_generator(session_hash, db_collections, db_name, db_schema):
    example_response = None
    example_messages = [
        ChatMessage.from_system(
            f"You are a helpful and knowledgeable agent who has access to an MongoDB NoSQL document database called {db_name}."
        )
    ]

    example_messages.append(ChatMessage.from_user(text=f"""We have a MongoDB NoSQL document database with the following collections: {db_collections}.

                                                  The schema of these collections is: {db_schema}. 

                                                  We also have an AI agent with access to the same database that will be performing data analysis.

                                                  Please return an array of seven strings, each one being a question for our data analysis agent

                                                  that we can suggest that you believe will be insightful or helpful to a data analysis looking for

                                                  data insights. Return nothing more than the array of questions because I need that specific data structure

                                                  to process your response. No other response type or data structure will work."""))

    example_response = chat_generator.run(messages=example_messages)

    return example_response["replies"][0].text

def chatbot_with_fc(message, history, session_hash):
    from functions import sqlite_query_func, table_generation_func, regression_func, scatter_chart_generation_func, \
        line_chart_generation_func,bar_chart_generation_func,pie_chart_generation_func,histogram_generation_func
    import tools.tools as tools

    available_functions = {"sql_query_func": sqlite_query_func,"table_generation_func":table_generation_func,
                           "line_chart_generation_func":line_chart_generation_func,"bar_chart_generation_func":bar_chart_generation_func,
                           "scatter_chart_generation_func":scatter_chart_generation_func, "pie_chart_generation_func":pie_chart_generation_func,
                           "histogram_generation_func":histogram_generation_func,
                           "regression_func":regression_func }
    
    session_path = 'file_upload'

    dir_path = TEMP_DIR / str(session_hash) / str(session_path)
    connection = sqlite3.connect(f'{dir_path}/data_source.db')
    cur=connection.execute('select * from data_source')
    columns = [i[0] for i in cur.description]
    cur.close()
    connection.close()

    if message_dict[session_hash]['file_upload'] != None:
        message_dict[session_hash]['file_upload'].append(ChatMessage.from_user(message))
    else:
        messages = [
            ChatMessage.from_system(
                f"""You are a helpful and knowledgeable agent who has access to an SQLite database which has a table called 'data_source' that contains the following columns: {columns}. 

                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 should display in our chat window.

                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 should display in our chat window.

                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 should display in our chat window.

                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 should display in our chat window.

                You also have access to a pie chart function, called pie_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a pie chart and returns an iframe that we should display in our chat window.

                You also have access to a histogram function, called histogram_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a histogram and returns an iframe that we should display in our chat window.

                You also have access to a linear regression function, called regression_func, that can take a query.csv file generated from our sql query and a list of column names for our independent and dependent variables and return a regression data string and a regression chart which is returned as an iframe.

                Could you please always display the generated charts, tables, and visualizations as part of your output?"""
            )
        ]
        messages.append(ChatMessage.from_user(message))
        message_dict[session_hash]['file_upload'] = messages

    response = chat_generator.run(messages=message_dict[session_hash]['file_upload'], generation_kwargs={"tools": tools.data_file_tools_call(session_hash)})

    while True:
        # if OpenAI response is a tool call
        if response and response["replies"][0].meta["finish_reason"] == "tool_calls" or response["replies"][0].tool_calls:
            function_calls = response["replies"][0].tool_calls
            for function_call in function_calls:
                message_dict[session_hash]['file_upload'].append(ChatMessage.from_assistant(tool_calls=[function_call]))
                ## Parse function calling information
                function_name = function_call.tool_name
                function_args = function_call.arguments

                ## Find the corresponding function and call it with the given arguments
                function_to_call = available_functions[function_name]
                function_response = function_to_call(**function_args, session_hash=session_hash, session_folder='file_upload')
                print(function_name)
                ## Append function response to the messages list using `ChatMessage.from_tool`
                message_dict[session_hash]['file_upload'].append(ChatMessage.from_tool(tool_result=function_response['reply'], origin=function_call))
                response = chat_generator.run(messages=message_dict[session_hash]['file_upload'], generation_kwargs={"tools": tools.data_file_tools_call(session_hash)})

        # Regular Conversation
        else:
            message_dict[session_hash]['file_upload'].append(response["replies"][0])
            break

    return response["replies"][0].text

def sql_chatbot_with_fc(message, history, session_hash, db_url, db_port, db_user, db_pass, db_name, db_tables):
    from functions import sql_query_func, table_generation_func, regression_func, scatter_chart_generation_func, \
        line_chart_generation_func,bar_chart_generation_func,pie_chart_generation_func,histogram_generation_func
    import tools.tools as tools

    available_functions = {"sql_query_func": sql_query_func,"table_generation_func":table_generation_func,
                           "line_chart_generation_func":line_chart_generation_func,"bar_chart_generation_func":bar_chart_generation_func,
                           "scatter_chart_generation_func":scatter_chart_generation_func, "pie_chart_generation_func":pie_chart_generation_func,
                           "histogram_generation_func":histogram_generation_func,
                           "regression_func":regression_func }

    if message_dict[session_hash]['sql'] != None:
        message_dict[session_hash]['sql'].append(ChatMessage.from_user(message))
    else:
        messages = [
            ChatMessage.from_system(
                f"""You are a helpful and knowledgeable agent who has access to an PostgreSQL database which has a series of tables called {db_tables}. 

                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 should display in our chat window.

                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 should display in our chat window.

                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 should display in our chat window.

                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 should display in our chat window.

                You also have access to a pie chart function, called pie_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a pie chart and returns an iframe that we should display in our chat window.

                You also have access to a histogram function, called histogram_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a histogram and returns an iframe that we should display in our chat window.

                You also have access to a linear regression function, called regression_func, that can take a query.csv file generated from our sql query and a list of column names for our independent and dependent variables and return a regression data string and a regression chart which is returned as an iframe.

                Could you please always display the generated charts, tables, and visualizations as part of your output?"""
            )
        ]
        messages.append(ChatMessage.from_user(message))
        message_dict[session_hash]['sql'] = messages

    response = chat_generator.run(messages=message_dict[session_hash]['sql'], generation_kwargs={"tools": tools.sql_tools_call(db_tables)})

    while True:
        # if OpenAI response is a tool call
        if response and response["replies"][0].meta["finish_reason"] == "tool_calls" or response["replies"][0].tool_calls:
            function_calls = response["replies"][0].tool_calls
            for function_call in function_calls:
                message_dict[session_hash]['sql'].append(ChatMessage.from_assistant(tool_calls=[function_call]))
                ## Parse function calling information
                function_name = function_call.tool_name
                function_args = function_call.arguments

                ## Find the corresponding function and call it with the given arguments
                function_to_call = available_functions[function_name]
                function_response = function_to_call(**function_args, session_hash=session_hash, db_url=db_url, 
                                                     db_port=db_port, db_user=db_user, db_pass=db_pass, db_name=db_name, session_folder='sql')
                print(function_name)
                ## Append function response to the messages list using `ChatMessage.from_tool`
                message_dict[session_hash]['sql'].append(ChatMessage.from_tool(tool_result=function_response['reply'], origin=function_call))
                response = chat_generator.run(messages=message_dict[session_hash]['sql'], generation_kwargs={"tools": tools.sql_tools_call(db_tables)})

        # Regular Conversation
        else:
            message_dict[session_hash]['sql'].append(response["replies"][0])
            break
        
    return response["replies"][0].text

def doc_db_chatbot_with_fc(message, history, session_hash, db_connection_string, db_name, db_collections, db_schema):
    from functions import doc_db_query_func, table_generation_func, regression_func, scatter_chart_generation_func, \
        line_chart_generation_func,bar_chart_generation_func,pie_chart_generation_func,histogram_generation_func
    import tools.tools as tools

    available_functions = {"doc_db_query_func": doc_db_query_func,"table_generation_func":table_generation_func,
                           "line_chart_generation_func":line_chart_generation_func,"bar_chart_generation_func":bar_chart_generation_func,
                           "scatter_chart_generation_func":scatter_chart_generation_func, "pie_chart_generation_func":pie_chart_generation_func,
                           "histogram_generation_func":histogram_generation_func,
                           "regression_func":regression_func }

    if message_dict[session_hash]['doc_db'] != None:
        message_dict[session_hash]['doc_db'].append(ChatMessage.from_user(message))
    else:
        messages = [
            ChatMessage.from_system(
                f"""You are a helpful and knowledgeable agent who has access to an NoSQL MongoDB Document database which has a series of collections called {db_collections}.

                The schema of these collections is: {db_schema}.

                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 should display in our chat window.

                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 should display in our chat window.

                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 should display in our chat window.

                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 should display in our chat window.

                You also have access to a pie chart function, called pie_chart_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a pie chart and returns an iframe that we should display in our chat window.

                You also have access to a histogram function, called histogram_generation_func, that can take a query.csv file generated from our sql query and uses plotly dictionaries to generate a histogram and returns an iframe that we should display in our chat window.

                You also have access to a linear regression function, called regression_func, that can take a query.csv file generated from our sql query and a list of column names for our independent and dependent variables and return a regression data string and a regression chart which is returned as an iframe.

                Could you please always display the generated charts, tables, and visualizations as part of your output?"""
            )
        ]
        messages.append(ChatMessage.from_user(message))
        message_dict[session_hash]['doc_db'] = messages

    response = chat_generator.run(messages=message_dict[session_hash]['doc_db'], generation_kwargs={"tools": tools.doc_db_tools_call(db_collections)})

    while True:
        # if OpenAI response is a tool call
        if response and response["replies"][0].meta["finish_reason"] == "tool_calls" or response["replies"][0].tool_calls:
            function_calls = response["replies"][0].tool_calls
            for function_call in function_calls:
                message_dict[session_hash]['doc_db'].append(ChatMessage.from_assistant(tool_calls=[function_call]))
                ## Parse function calling information
                function_name = function_call.tool_name
                function_args = function_call.arguments

                ## Find the corresponding function and call it with the given arguments
                function_to_call = available_functions[function_name]
                function_response = function_to_call(**function_args, session_hash=session_hash, connection_string=db_connection_string, 
                                                    doc_db_name=db_name, session_folder='doc_db')
                print(function_name)
                ## Append function response to the messages list using `ChatMessage.from_tool`
                message_dict[session_hash]['doc_db'].append(ChatMessage.from_tool(tool_result=function_response['reply'], origin=function_call))
                response = chat_generator.run(messages=message_dict[session_hash]['doc_db'], generation_kwargs={"tools": tools.doc_db_tools_call(db_collections)})

        # Regular Conversation
        else:
            message_dict[session_hash]['doc_db'].append(response["replies"][0])
            break
        
    return response["replies"][0].text