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
+
1
6
import os
2
- import mysql .connector
3
7
import datetime
4
8
from dotenv import load_dotenv
9
+ import mysql .connector
5
10
from together import Together
6
11
from groq import Groq
7
12
20
25
21
26
def create_tables () -> None :
22
27
"""
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.
25
31
"""
26
32
try :
27
33
conn = mysql .connector .connect (** db_config )
@@ -77,7 +83,8 @@ def insert_chat_history(start_time: datetime.datetime, is_stream: int) -> None:
77
83
"""
78
84
Insert a new row into the ChatDB.Chat_history table.
79
85
: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
81
88
"""
82
89
try :
83
90
conn = mysql .connector .connect (** db_config )
@@ -107,15 +114,15 @@ def get_latest_chat_id() -> int:
107
114
cursor = conn .cursor ()
108
115
cursor .execute (
109
116
"""
110
- SELECT chat_id FROM ChatDB.Chat_history
117
+ SELECT chat_id FROM ChatDB.Chat_history
111
118
ORDER BY chat_id DESC LIMIT 1
112
119
"""
113
120
)
114
121
chat_id = cursor .fetchone ()[0 ]
115
122
return chat_id if chat_id else None
116
123
except mysql .connector .Error as err :
117
124
print (f"Error: { err } " )
118
- return None
125
+ return 0
119
126
finally :
120
127
cursor .close ()
121
128
conn .close ()
@@ -151,8 +158,10 @@ def generate_llm_response(
151
158
) -> str :
152
159
"""
153
160
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
156
165
:return: Assistant's response as a string
157
166
"""
158
167
bot_response = ""
@@ -194,7 +203,8 @@ def chat_session() -> None:
194
203
print ("Welcome to the chatbot! Type '/stop' to end the conversation." )
195
204
196
205
conversation_history = []
197
- start_time = datetime .datetime .now ()
206
+ start_time = datetime .datetime .now (datetime .timezone .utc )
207
+
198
208
chat_id_pk = None
199
209
api_service = "Groq" # or "Together"
200
210
@@ -215,22 +225,22 @@ def chat_session() -> None:
215
225
else :
216
226
if user_input .lower () == "/stop" :
217
227
is_stream = 2 # End of conversation
218
- current_time = datetime .datetime .now ()
228
+ current_time = datetime .datetime .now (datetime . timezone . utc )
219
229
insert_chat_history (current_time , is_stream )
220
230
break
221
231
222
232
bot_response = generate_llm_response (conversation_history , api_service )
223
233
conversation_history .append ({"role" : "assistant" , "content" : bot_response })
224
234
225
235
is_stream = 0 # Continuation of conversation
226
- current_time = datetime .datetime .now ()
236
+ current_time = datetime .datetime .now (datetime . timezone . utc )
227
237
insert_chat_history (current_time , is_stream )
228
238
insert_chat_data (chat_id_pk , user_input , bot_response )
229
239
230
240
if len (conversation_history ) > 1000 :
231
241
conversation_history = conversation_history [- 3 :]
232
242
233
243
234
- # Example of starting a chat session
244
+ # starting a chat session
235
245
create_tables ()
236
246
chat_session ()
0 commit comments