Skip to content

Commit 276528d

Browse files
authored
Add files via upload
made changes suggested by auto-checker
1 parent 7c49052 commit 276528d

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

neural_network/chatbot/chat_db.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
"""
2+
credits : https://medium.com/google-developer-experts/beyond-live-sessions-building-persistent-memory-chatbots-with-langchain-gemini-pro-and-firebase-19d6f84e21d3
3+
4+
"""
5+
16
import os
2-
import mysql.connector
37
import datetime
48
from dotenv import load_dotenv
9+
import mysql.connector
510
from together import Together
611
from groq import Groq
712

@@ -20,8 +25,9 @@
2025

2126
def create_tables() -> None:
2227
"""
23-
Create the ChatDB.Chat_history and ChatDB.Chat_data tables if they do not exist.
24-
Also, create a trigger to update is_stream in Chat_data when Chat_history.is_stream is updated.
28+
Create the ChatDB.Chat_history and ChatDB.Chat_data tables
29+
if they do not exist.Also, create a trigger to update is_stream
30+
in Chat_data when Chat_history.is_stream is updated.
2531
"""
2632
try:
2733
conn = mysql.connector.connect(**db_config)
@@ -77,7 +83,8 @@ def insert_chat_history(start_time: datetime.datetime, is_stream: int) -> None:
7783
"""
7884
Insert a new row into the ChatDB.Chat_history table.
7985
:param start_time: Timestamp of when the chat started
80-
:param is_stream: Indicator of whether the conversation is ongoing, starting, or ending
86+
:param is_stream: Indicator of whether the conversation is
87+
ongoing, starting, or ending
8188
"""
8289
try:
8390
conn = mysql.connector.connect(**db_config)
@@ -107,15 +114,15 @@ def get_latest_chat_id() -> int:
107114
cursor = conn.cursor()
108115
cursor.execute(
109116
"""
110-
SELECT chat_id FROM ChatDB.Chat_history
117+
SELECT chat_id FROM ChatDB.Chat_history
111118
ORDER BY chat_id DESC LIMIT 1
112119
"""
113120
)
114121
chat_id = cursor.fetchone()[0]
115122
return chat_id if chat_id else None
116123
except mysql.connector.Error as err:
117124
print(f"Error: {err}")
118-
return None
125+
return 0
119126
finally:
120127
cursor.close()
121128
conn.close()
@@ -151,8 +158,10 @@ def generate_llm_response(
151158
) -> str:
152159
"""
153160
Generate a response from the LLM based on the conversation history.
154-
:param conversation_history: List of dictionaries representing the conversation so far
155-
:param api_service: Choose between "Together" or "Groq" as the API service
161+
:param conversation_history: List of dictionaries representing
162+
the conversation so far
163+
:param api_service: Choose between "Together" or "Groq" as the
164+
API service
156165
:return: Assistant's response as a string
157166
"""
158167
bot_response = ""
@@ -194,7 +203,8 @@ def chat_session() -> None:
194203
print("Welcome to the chatbot! Type '/stop' to end the conversation.")
195204

196205
conversation_history = []
197-
start_time = datetime.datetime.now()
206+
start_time = datetime.datetime.now(datetime.timezone.utc)
207+
198208
chat_id_pk = None
199209
api_service = "Groq" # or "Together"
200210

@@ -215,22 +225,22 @@ def chat_session() -> None:
215225
else:
216226
if user_input.lower() == "/stop":
217227
is_stream = 2 # End of conversation
218-
current_time = datetime.datetime.now()
228+
current_time = datetime.datetime.now(datetime.timezone.utc)
219229
insert_chat_history(current_time, is_stream)
220230
break
221231

222232
bot_response = generate_llm_response(conversation_history, api_service)
223233
conversation_history.append({"role": "assistant", "content": bot_response})
224234

225235
is_stream = 0 # Continuation of conversation
226-
current_time = datetime.datetime.now()
236+
current_time = datetime.datetime.now(datetime.timezone.utc)
227237
insert_chat_history(current_time, is_stream)
228238
insert_chat_data(chat_id_pk, user_input, bot_response)
229239

230240
if len(conversation_history) > 1000:
231241
conversation_history = conversation_history[-3:]
232242

233243

234-
# Example of starting a chat session
244+
# starting a chat session
235245
create_tables()
236246
chat_session()

0 commit comments

Comments
 (0)