pechawa commited on
Commit
2a675e4
·
1 Parent(s): 1ff37d9

feat: store user and bot msg pair in db

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -1
  2. store.py +45 -0
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio==3.26.0
2
- requests==2.28.2
 
 
1
  gradio==3.26.0
2
+ requests==2.28.2
3
+ boto3==1.26.113
store.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import uuid
3
+ from typing import Tuple
4
+
5
+ import boto3
6
+
7
+ dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
8
+ all_chats_table = dynamodb.Table("ChatbotTibetanAllChats")
9
+
10
+
11
+ def store_message_pair(chat_id: str, msg_pair: Tuple[str, str], lang: str, order: int):
12
+ """Store the chat history to DynamoDB
13
+
14
+ Args:
15
+ chat_id: The ID of the chat
16
+ msg_pair: tuple with 2 items (user_message, bot_response)
17
+ lang: The language of the msg_pair
18
+ order: The order of the msg_pair in chat history
19
+ """
20
+
21
+ # Add the new message to the chat history
22
+ msg_pair_id = uuid.uuid4().hex[:4]
23
+ response = all_chats_table.put_item(
24
+ Item={
25
+ "msg_pair_id": msg_pair_id,
26
+ "msg_pair": json.dumps(msg_pair),
27
+ "lang": lang,
28
+ "order": order,
29
+ "chat_id": chat_id,
30
+ }
31
+ )
32
+ return response
33
+
34
+
35
+ if __name__ == "__main__":
36
+ # Replace with your own DynamoDB table name and chat ID
37
+ chat_id = uuid.uuid4().hex[:4]
38
+
39
+ # Replace with your own chat history (list of tuples or list of dictionaries)
40
+ chat_history = [
41
+ ("User", "Hello, how are you?"),
42
+ ("Chatbot", "I am fine, thank you!"),
43
+ ]
44
+ for i, msg_pair in enumerate(chat_history):
45
+ store_message_pair(chat_id, msg_pair, "en", i)