File size: 1,279 Bytes
2a675e4
 
9601875
48b9027
2a675e4
 
 
 
 
 
 
48b9027
2a675e4
 
 
 
 
 
 
 
 
 
9601875
2a675e4
 
 
2cefdc2
9601875
2a675e4
 
 
 
 
 
 
 
48b9027
2a675e4
 
48b9027
 
 
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
import json
import uuid
from datetime import datetime
from typing import Dict, Tuple

import boto3

dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
all_chats_table = dynamodb.Table("ChatbotTibetanAllChats")


def store_message_pair(chat_id: str, msg_pair: Dict[str, Tuple[str, str]]):
    """Store the chat history to DynamoDB

    Args:
        chat_id: The ID of the chat
        msg_pair: tuple with 2 items (user_message, bot_response)
        lang: The language of the msg_pair
        order: The order of the msg_pair in chat history
    """

    # Add the new message to the chat history
    msg_pair_id = uuid.uuid4().hex[:10]
    response = all_chats_table.put_item(
        Item={
            "msg_pair_id": msg_pair_id,
            "msg_pair": json.dumps(msg_pair, ensure_ascii=False),
            "created_at": datetime.now().isoformat(),
            "chat_id": chat_id,
        }
    )
    return response


if __name__ == "__main__":
    # Replace with your own DynamoDB table name and chat ID
    chat_id = str(uuid.uuid4())

    # Replace with your own chat history (list of tuples or list of dictionaries)
    msg_pair = {"bo": ("hello", "hello"), "en": ("hello", "hello")}
    response = store_message_pair(chat_id, msg_pair)
    print(response)