Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CAI-44] Chatbot save one session per day #1194

Merged
merged 35 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ac9b39a
feat(chatbot): session GSI
batdevis Oct 8, 2024
36c89af
feat(chatbot): docker compose
batdevis Oct 9, 2024
a2c16c7
fix(chatbot): dynamodb and redis for local development with docker co…
batdevis Oct 9, 2024
c633df6
Merge branch 'chatbot/docker-compose-complete' into chatbot/sessions-…
batdevis Oct 9, 2024
08a7fec
chore(chatbot):remove duplicate imports
batdevis Oct 9, 2024
7fe9101
chore(chatbot): linting
batdevis Oct 9, 2024
9b2eb61
fix(chatbot):create index in docker
batdevis Oct 9, 2024
b251837
chore(chatbot): llamaindex index id
batdevis Oct 10, 2024
4ecd4b3
fix(chatbot): create vector index with all docs
batdevis Oct 10, 2024
a84581c
Merge branch 'main' into chatbot/docker-compose-complete
batdevis Oct 10, 2024
ea7d3db
chore(chatbot): terraform lint
batdevis Oct 10, 2024
28695e3
fix(chatbot): terraform syntax
batdevis Oct 10, 2024
238edfd
chore(chatbot): remove dynamodb options
batdevis Oct 10, 2024
5f63560
chore(chatbot): from global to local secondary index
batdevis Oct 10, 2024
a8f4c9a
feat(chatbot): find or create session
batdevis Oct 11, 2024
9f9e218
Merge branch 'main' into chatbot/sessions-query-by-date
batdevis Oct 11, 2024
859c298
Merge branch 'main' into chatbot/docker-compose-complete
batdevis Oct 11, 2024
f43a771
chore: merge main
batdevis Oct 11, 2024
d96a9f9
chore: remove old var
batdevis Oct 11, 2024
9526c17
chore: merge main
batdevis Oct 11, 2024
a5177df
Update apps/chatbot/docker/compose.yaml
batdevis Oct 11, 2024
4daccf8
chore: remove logs
batdevis Oct 11, 2024
f5bdfd3
Merge branch 'chatbot/docker-compose-complete' of github.com:pagopa/d…
batdevis Oct 11, 2024
c123b5c
fix(chatbot): compose vars
batdevis Oct 13, 2024
aa59ca5
Merge branch 'main' into chatbot/docker-compose-complete
batdevis Oct 13, 2024
6d11e67
Merge branch 'chatbot/docker-compose-complete' into chatbot/sessions-…
batdevis Oct 13, 2024
5e07dbe
Update modules
mdciri Oct 16, 2024
da8a41c
Update config prompts
mdciri Oct 16, 2024
b57d55c
Update env example
mdciri Oct 16, 2024
62fffa1
Merge branch 'main' into chatbot/docker-compose-complete
batdevis Oct 16, 2024
f7b05e6
Merge branch 'languages/chatbot/cai-198' into chatbot/docker-compose-…
batdevis Oct 16, 2024
278d56d
redis admin port
batdevis Oct 16, 2024
91c782a
chore: add env example var
batdevis Oct 16, 2024
ee03467
Merge branch 'chatbot/docker-compose-complete' into chatbot/sessions-…
batdevis Oct 16, 2024
d56c07d
chore: merge main
batdevis Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/chatbot/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CHB_WEBSITE_URL=...
CHB_REDIS_INDEX_NAME=...
CHB_LLAMAINDEX_INDEX_ID=...
CHB_DOCUMENTATION_DIR=...
CHB_USE_PRESIDIO=...
CHB_GOOGLE_API_KEY=...
CHB_PROVIDER=...
CHB_MODEL_ID=...
Expand All @@ -26,3 +25,4 @@ CHB_ENGINE_USE_ASYNC=True
CHB_ENGINE_USE_STREAMING=...
CHB_QUERY_TABLE_PREFIX=chatbot-local
CHB_DYNAMODB_URL=http://locahost:8080
CHB_USE_PRESIDIO=True
56 changes: 38 additions & 18 deletions apps/chatbot/src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uuid
import boto3
import datetime
import time
import jwt
from typing import Annotated
from boto3.dynamodb.conditions import Key
Expand Down Expand Up @@ -68,13 +69,13 @@ async def query_creation (
query: Query,
authorization: Annotated[str | None, Header()] = None
):
now = datetime.datetime.now(datetime.UTC)
userId = current_user_id(authorization)
session = find_or_create_session(userId)
session = find_or_create_session(userId, now=now)
answer = chatbot.generate(query.question)

now = datetime.datetime.now(datetime.timezone.utc).isoformat()
if query.queriedAt is None:
queriedAt = now
queriedAt = now.isoformat()
else:
queriedAt = query.queriedAt

Expand All @@ -83,7 +84,7 @@ async def query_creation (
"sessionId": session['id'],
"question": query.question,
"answer": answer,
"createdAt": now,
"createdAt": now.isoformat(),
"queriedAt": queriedAt
}

Expand All @@ -109,24 +110,41 @@ def current_user_id(authorization: str):
return decoded['cognito:username']


def find_or_create_session(userId: str):
def find_or_create_session(userId: str, now: datetime.datetime):
# TODO: return if userId is None
if userId is None:
userId = '-'
now = datetime.datetime.now(datetime.timezone.utc).isoformat()
# TODO: calculate title
# TODO: find last session based on SESSION_MAX_DURATION_MINUTES
# TODO: if it's None, create it.
body = {
"id": '1',#f'{uuid.uuid4()}',
"title": "last session",
"userId": userId,
"createdAt": now
}

SESSION_MAX_DURATION_DAYS = int(os.getenv('SESSION_MAX_DURATION_DAYS', '1'))
datetimeLimit = now - datetime.timedelta(SESSION_MAX_DURATION_DAYS - 1)
startOfDay = datetime.datetime.combine(datetimeLimit, datetime.time.min)
# trovare una sessione con createdAt > datetimeLimit
try:
table_sessions.put_item(Item = body)
db_response = table_sessions.query(
KeyConditionExpression=Key("userId").eq(userId) &
Key('createdAt').gt(startOfDay.isoformat()),
IndexName='SessionsByCreatedAtIndex',
ScanIndexForward=False,
Limit=1
)
except (BotoCoreError, ClientError) as e:
raise HTTPException(status_code=422, detail=f"[find_or_create_session] body: {body}, error: {e}")
raise HTTPException(status_code=422, detail=f"[find_or_create_session] userId: {userId}, error: {e}")

items = db_response.get('Items', [])
if len(items) == 0:
body = {
"id": f'{uuid.uuid4()}',
"title": now.strftime("%Y-%m-%d"),
"userId": userId,
"createdAt": now.isoformat()
}
try:
table_sessions.put_item(Item = body)
except (BotoCoreError, ClientError) as e:
raise HTTPException(status_code=422, detail=f"[find_or_create_session] body: {body}, error: {e}")

else:
body = items[0]

return body

Expand Down Expand Up @@ -154,7 +172,9 @@ async def sessions_fetching(

try:
db_response = table_sessions.query(
KeyConditionExpression=Key("userId").eq(userId)
KeyConditionExpression=Key("userId").eq(userId),
IndexName='SessionsByCreatedAtIndex',
ScanIndexForward=False
)
except (BotoCoreError, ClientError) as e:
raise HTTPException(status_code=422, detail=f"[sessions_fetching] userId: {userId}, error: {e}")
Expand Down
Loading