42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import boto3
|
|
def write_memory(user,timestamp,role,content):
|
|
dynamodb = boto3.resource('dynamodb')
|
|
|
|
table = dynamodb.Table('br-edu-ifsp-ifsp-ret-memoria-tabela-chatbot-editais-dev') # Replace 'YourTableName' with your actual table name
|
|
|
|
item_data = {
|
|
'UserId': user, # Replace with your partition key attribute and value
|
|
'Timestamp': timestamp, # Replace with your sort key attribute and value (if applicable)
|
|
'role': role,
|
|
'content': content
|
|
}
|
|
|
|
try:
|
|
response = table.put_item(Item=item_data)
|
|
return response
|
|
except Exception as e:
|
|
return "Error adding item:"+str(e)
|
|
def read_memory(userid):
|
|
dynamodb = boto3.resource('dynamodb')
|
|
table = dynamodb.Table('br-edu-ifsp-ifsp-ret-memoria-tabela-chatbot-editais-dev')
|
|
|
|
# Query parameters
|
|
|
|
try:
|
|
response = table.query(
|
|
KeyConditionExpression=boto3.dynamodb.conditions.Key('UserId').eq(userid),
|
|
ScanIndexForward=False, # Descending order
|
|
Limit=30 # Get only the latest item
|
|
)
|
|
|
|
items = response.get('Items', [])
|
|
if items:
|
|
latest_items = items
|
|
return latest_items
|
|
else:
|
|
return []
|
|
|
|
except Exception as e:
|
|
print("Error querying DynamoDB:", str(e))
|
|
|