From ad78ab2aca117b9038ec53128a08b69de40b63ca Mon Sep 17 00:00:00 2001 From: Devis Battisti Date: Fri, 20 Sep 2024 12:10:21 +0200 Subject: [PATCH] [CAI-155] Chatbot/queries fetching (#1131) * feat(chatbot): dynamodb infrastructure * feat(chatbot): dynamodb data attribute * fix(chatbot): use string type for map type on dynamodb terraform module * fix(chatbot): dynamodb terraform * feat(chatbot): dynamodb configuration * feat(chatbot): dynamodb table name * chore(chatbot): docker for local development * chore(chatbot): docker for local development * doc(chatbot): index creation readme * feat(chatbot): write to dynamodb * feat(chatbot): table name var * chore(chatbot): API improvements and refactor) * feat(chatbot): sessionId key in dynamodb * feat(chatbot): query by sessionId * chore(chatbot): remove aws region var * fix(chatbot):default session * fix(chatbot): import * fix(chatbot): region_name for local development * fix(chatbot) queriedAt field default * chore(chatbot): table name lambda env var * chore: terraform format * fix(chatbot): table prefix --------- Co-authored-by: christian-calabrese --- apps/chatbot/src/app/main.py | 45 +++++++++++-------- .../src/modules/chatbot/dynamodb.tf | 7 ++- .../src/modules/chatbot/lambda_chatbot.tf | 11 ++--- apps/infrastructure/src/variables.tf | 9 +++- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/apps/chatbot/src/app/main.py b/apps/chatbot/src/app/main.py index 6c8000feb4..eb9323b318 100644 --- a/apps/chatbot/src/app/main.py +++ b/apps/chatbot/src/app/main.py @@ -6,6 +6,7 @@ import uuid import boto3 import datetime +from boto3.dynamodb.conditions import Key from botocore.exceptions import BotoCoreError, ClientError from fastapi import FastAPI, HTTPException from starlette.middleware.cors import CORSMiddleware @@ -13,10 +14,11 @@ from src.modules.chatbot import Chatbot - params = yaml.safe_load(open("config/params.yaml", "r")) prompts = yaml.safe_load(open("config/prompts.yaml", "r")) +AWS_DEFAULT_REGION = os.getenv('CHB_AWS_DEFAULT_REGION', os.getenv('AWS_DEFAULT_REGION', None)) + class Query(BaseModel): question: str queriedAt: str | None = None @@ -24,17 +26,21 @@ class Query(BaseModel): if (os.getenv('environment', 'dev') == 'local'): profile_name='dummy' endpoint_url='http://localhost:8000' + region_name = AWS_DEFAULT_REGION boto3_session = boto3.session.Session( - profile_name = locals().get('profile_name', None) + profile_name = locals().get('profile_name', None), + region_name=locals().get('region_name', None) ) dynamodb = boto3_session.resource( 'dynamodb', - endpoint_url=locals().get('endpoint_url', None) + endpoint_url=locals().get('endpoint_url', None), + region_name=locals().get('region_name', None), ) -chatbot_queries = dynamodb.Table(os.getenv('CHB_QUERY_TABLE_NAME', 'chatbot-dev-queries')) +queries_table_name = f"{os.getenv('CHB_QUERY_TABLE_PREFIX', 'chatbot')}-queries" +chatbot_queries = dynamodb.Table(queries_table_name) app = FastAPI() app.add_middleware( @@ -57,15 +63,17 @@ async def query_creation (query: Query): now = datetime.datetime.now(datetime.timezone.utc).isoformat() # TODO: calculate sessionId + if query.queriedAt is None: + queriedAt = now + body = { "id": f'{uuid.uuid4()}', "sessionId": "1", "question": query.question, "answer": answer, "createdAt": now, - "queriedAt": query.queriedAt + "queriedAt": queriedAt } - # TODO: body validation try: chatbot_queries.put_item(Item = body) @@ -112,19 +120,18 @@ async def sessions_fetching(): return body @app.get("/queries") -async def queries_fetching(sessionId: str): - # TODO: dynamoDB integration - body = [ - { - "id": "", - "sessionId": sessionId, - "question": "", - "answer": "", - "createdAt": "", - "queriedAt": "" - } - ] - return body +async def queries_fetching(sessionId: str | None = None): + if sessionId is None: + # TODO: retrieve last user session + # sessionId = lastSessionId(userId) + sessionId = '1' + try: + db_response = chatbot_queries.query( + KeyConditionExpression=Key("sessionId").eq(sessionId) + ) + except (BotoCoreError, ClientError) as e: + raise HTTPException(status_code=422, detail='db error') + return db_response['Items'] @app.patch("/queries/{id}") async def query_feedback (badAnswer: bool): diff --git a/apps/infrastructure/src/modules/chatbot/dynamodb.tf b/apps/infrastructure/src/modules/chatbot/dynamodb.tf index ee9fab1e4a..3648d74b75 100644 --- a/apps/infrastructure/src/modules/chatbot/dynamodb.tf +++ b/apps/infrastructure/src/modules/chatbot/dynamodb.tf @@ -7,7 +7,8 @@ module "dynamodb_chatbot_queries" { deletion_protection_enabled = false name = "${local.prefix}-queries" - hash_key = "id" + hash_key = "sessionId" + range_key = "id" server_side_encryption_enabled = true attributes = [ @@ -15,5 +16,9 @@ module "dynamodb_chatbot_queries" { name = "id" type = "S" }, + { + name = "sessionId" + type = "S" + }, ] } diff --git a/apps/infrastructure/src/modules/chatbot/lambda_chatbot.tf b/apps/infrastructure/src/modules/chatbot/lambda_chatbot.tf index 0d470bc0e2..4a546e0879 100644 --- a/apps/infrastructure/src/modules/chatbot/lambda_chatbot.tf +++ b/apps/infrastructure/src/modules/chatbot/lambda_chatbot.tf @@ -15,10 +15,11 @@ locals { # Be extremely careful when changing the provider # both the generation and the embedding models would be changed # embeddings size change would break the application and requires reindexing - CHB_PROVIDER = "aws" - CHB_MODEL_ID = "mistral.mistral-large-2402-v1:0" - CHB_EMBED_MODEL_ID = "cohere.embed-multilingual-v3" - CHB_GOOGLE_API_KEY = module.google_api_key_ssm_parameter.ssm_parameter_name + CHB_PROVIDER = "aws" + CHB_MODEL_ID = "mistral.mistral-large-2402-v1:0" + CHB_EMBED_MODEL_ID = "cohere.embed-multilingual-v3" + CHB_GOOGLE_API_KEY = module.google_api_key_ssm_parameter.ssm_parameter_name + CHB_QUERY_TABLE_NAME = "${local.prefix}-queries" } } @@ -88,4 +89,4 @@ module "google_api_key_ssm_parameter" { type = "SecureString" secure_type = true ignore_value_changes = true -} \ No newline at end of file +} diff --git a/apps/infrastructure/src/variables.tf b/apps/infrastructure/src/variables.tf index 1c37ad3e05..2c9d2cd4d7 100644 --- a/apps/infrastructure/src/variables.tf +++ b/apps/infrastructure/src/variables.tf @@ -117,4 +117,11 @@ variable "chatbot_ecs_redis" { image_uri = "redis/redis-stack-server@sha256:887cf87cc744e4588ccade336d0dbb943e4e46330f738653ccb3a7a55df2f186" port = 6379 } -} \ No newline at end of file +} + +variable "CHB_QUERY_TABLE_NAME" { + type = string + description = "dynamodb table name for queries" + default = "chatbot-dev-queries" +} +