-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatBot4.py
248 lines (201 loc) · 8.34 KB
/
ChatBot4.py
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
import os
import uuid
import pyodbc
from typing import List, Dict
import cohere
# Assuming 'cohere' is the package name for Cohere API
co = cohere.Client("D14bT4Bm9SoiXE5ioVryf2DGOyIw1yjm1ccR0giQ")
class DatabaseQuery:
def __init__(self, server: str, database: str, username: str, password: str):
self.server = server
self.database = database
self.username = username
self.password = password
self.connection = self.create_connection()
def create_connection(self):
conn_str = f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={self.server};DATABASE={self.database};UID={self.username};PWD={self.password}"
try:
connection = pyodbc.connect(conn_str)
print("Database connection established.")
return connection
except pyodbc.Error as e:
print(f"Error connecting to SQL Server: {e}")
raise
# return pyodbc.connect(conn_str)
def execute_query(self, query: str):
"""
Executes a SQL query on the connected MSSQL database.
Parameters:
query (str): The SQL query to execute.
Returns:
List[Dict[str, Any]]: A list of dictionaries representing the query results.
"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
columns = [column[0] for column in cursor.description]
result = [dict(zip(columns, row)) for row in cursor.fetchall()]
# result = cursor.fetchall()
cursor.close()
return result
except pyodbc.Error as e:
print(f"Error executing query: {e}")
raise
class Vectorstore:
def __init__(self, database_query: DatabaseQuery):
self.database_query = database_query
def retrieve_from_database(self, query: str) -> List[Dict[str, str]]:
"""
Retrieves data from MSSQL database based on the given query.
Parameters:
query (str): The SQL query to retrieve data from the database.
Returns:
List[Dict[str, str]]: A list of dictionaries representing the retrieved data rows.
"""
print(f"Retrieving data from database with query: {query}")
results = self.database_query.execute_query(query)
# Format results as needed (assuming each row is a dictionary with column names as keys)
formatted_results = []
for row in results:
formatted_results.append(dict(row))
return formatted_results
class Chatbot:
def __init__(self, vectorstore: Vectorstore):
self.vectorstore = vectorstore
self.conversation_id = str(uuid.uuid4())
def run(self):
while True:
message = input("User: ")
if message.lower() == "quit":
print("Ending chat.")
break
else:
print(f"User: {message}")
# For testing, use a predefined query instead of user input
query = "SELECT TOP (1000) [user_id], [username], [email], [password] FROM [Trial1].[dbo].[Users]"
# Query database based on user message
try:
db_results = self.vectorstore.retrieve_from_database(query)
except Exception as e:
print(f"Error retrieving from database: {e}")
continue
# Prepare database results for chatbot response
documents = []
for result in db_results:
documents.append({
"user_id": result["user_id"], # Adjust as per your database schema
"username": result["username"], # Adjust as per your database schema
"email": result["email"], # Adjust as per your database schema
"password": result["password"], # Adjust as per your database schema
# Add other fields as necessary
})
try:
# Use Cohere to process user message and retrieve relevant documents
response = co.chat_stream(
message=message,
model="command-r",
documents=documents,
conversation_id=self.conversation_id,
)
except Exception as e:
print(f"Error during Cohere chat_stream: {e}")
continue
# Print chatbot response
print("\nChatbot:")
citations = []
cited_documents = []
for event in response:
if event.event_type == "text-generation":
print(event.text, end="")
elif event.event_type == "citation-generation":
citations.extend(event.citations)
elif event.event_type == "search-results":
cited_documents = event.documents
if citations:
print("\n\nCITATIONS:")
for citation in citations:
print(citation)
print("\nDOCUMENTS:")
for document in cited_documents:
print(document)
print(f"\n{'-'*100}\n")
if __name__ == "__main__":
# Replace these with your MSSQL server details
server = 'localhost'
database = 'Trial1'
username = 'sa'
password = 'Pratham72'
# Create an instance of DatabaseQuery
database_query = DatabaseQuery(server, database, username, password)
# Create an instance of Vectorstore
vectorstore = Vectorstore(database_query)
# Create an instance of Chatbot
chatbot = Chatbot(vectorstore)
# Run the chatbot
chatbot.run()
# Close database connection
database_query.connection.close()
# class Chatbot:
# def __init__(self, vectorstore: Vectorstore):
# self.vectorstore = vectorstore
# self.conversation_id = str(uuid.uuid4())
# def run(self):
# while True:
# message = input("User: ")
# if message.lower() == "quit":
# print("Ending chat.")
# break
# else:
# print(f"User: {message}")
# # Query database based on user message
# db_results = self.vectorstore.retrieve_from_database(message)
# # Prepare database results for chatbot response
# documents = []
# for result in db_results:
# documents.append({
# "user_id": result["user_id"], # Adjust as per your database schema
# "username": result["username"], # Adjust as per your database schema
# # Add other fields as necessary
# })
# # Use Cohere to process user message and retrieve relevant documents
# response = co.chat_stream(
# message=message,
# model="command-r",
# documents=documents,
# conversation_id=self.conversation_id,
# )
# # Print chatbot response
# print("\nChatbot:")
# citations = []
# cited_documents = []
# for event in response:
# if event.event_type == "text-generation":
# print(event.text, end="")
# elif event.event_type == "citation-generation":
# citations.extend(event.citations)
# elif event.event_type == "search-results":
# cited_documents = event.documents
# if citations:
# print("\n\nCITATIONS:")
# for citation in citations:
# print(citation)
# print("\nDOCUMENTS:")
# for document in cited_documents:
# print(document)
# print(f"\n{'-'*100}\n")
# if __name__ == "__main__":
# # Replace these with your MSSQL server details
# server = 'localhost'
# database = 'Trial1'
# username = 'sa'
# password = 'Pratham72'
# # Create an instance of DatabaseQuery
# database_query = DatabaseQuery(server, database, username, password)
# # Create an instance of Vectorstore
# vectorstore = Vectorstore(database_query)
# # Create an instance of Chatbot
# chatbot = Chatbot(vectorstore)
# # Run the chatbot
# chatbot.run()
# # Close database connection
# database_query.connection.close()