Skip to content

Commit fbb5135

Browse files
committed
CosmosDB v2 changes
1 parent 6fac970 commit fbb5135

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

app/backend/chat_history/cosmosdb.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,33 @@ async def post_chat_history(auth_claims: Dict[str, Any]):
3939
title = answers[0][0][:50] + "..." if len(answers[0][0]) > 50 else answers[0][0]
4040
timestamp = int(time.time() * 1000)
4141

42+
# Insert the session item:
4243
await container.upsert_item(
43-
{"id": id, "entra_oid": entra_oid, "title": title, "answers": answers, "timestamp": timestamp}
44+
{
45+
"id": id,
46+
"session_id": id,
47+
"entra_oid": entra_oid,
48+
"type": "session",
49+
"title": title,
50+
"timestamp": timestamp,
51+
}
4452
)
4553

54+
# Now insert a message item for each question/response pair:
55+
for ind, message_pair in enumerate(zip(answers[::2], answers[1::2])):
56+
# TODO: Can I do a batch upsert?
57+
await container.upsert_item(
58+
{
59+
"id": f"{id}-{ind}",
60+
"session_id": id,
61+
"entra_oid": entra_oid,
62+
"type": "message",
63+
"question": message_pair[0],
64+
"response": message_pair[1],
65+
"timestamp": timestamp,
66+
}
67+
)
68+
4669
return jsonify({}), 201
4770
except Exception as error:
4871
return error_response(error, "/chat_history")
@@ -68,8 +91,9 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
6891
continuation_token = request_json.get("continuation_token")
6992

7093
res = container.query_items(
71-
query="SELECT c.id, c.entra_oid, c.title, c.timestamp FROM c WHERE c.entra_oid = @entra_oid ORDER BY c.timestamp DESC",
72-
parameters=[dict(name="@entra_oid", value=entra_oid)],
94+
query="SELECT c.id, c.entra_oid, c.title, c.timestamp FROM c WHERE c.entra_oid = @entra_oid AND c.type = @type ORDER BY c.timestamp DESC",
95+
parameters=[dict(name="@entra_oid", value=entra_oid), dict(name="@type", value="session")],
96+
partition_key=entra_oid,
7397
max_item_count=count,
7498
)
7599

app/frontend/src/components/LoginButton/LoginButton.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const LoginButton = () => {
3535
})
3636
.catch(error => console.log(error))
3737
.then(async () => {
38+
debugger;
3839
setLoggedIn(await checkLoggedIn(instance));
3940
setUsername((await getUsername(instance)) ?? "");
4041
});

infra/main.bicep

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ param cosmosDbLocation string = ''
6969
param cosmosDbAccountName string = ''
7070
param cosmosDbThroughput int = 400
7171
param chatHistoryDatabaseName string = 'chat-database'
72-
param chatHistoryContainerName string = 'chat-history'
72+
param chatHistoryContainerName string = 'chat-history-container'
7373

7474
// https://learn.microsoft.com/azure/ai-services/openai/concepts/models?tabs=python-secure%2Cstandard%2Cstandard-chat-completions#standard-deployment-model-availability
7575
@description('Location for the OpenAI resource group')
@@ -800,24 +800,25 @@ module cosmosDb 'br/public:avm/res/document-db/database-account:0.6.1' = if (use
800800
name: chatHistoryContainerName
801801
paths: [
802802
'/entra_oid'
803+
'/session_id'
803804
]
804805
indexingPolicy: {
805806
indexingMode: 'consistent'
806807
automatic: true
807808
includedPaths: [
808809
{
809-
path: '/*'
810+
path: '/entra_oid/?'
810811
}
811-
]
812-
excludedPaths: [
813812
{
814-
path: '/title/?'
813+
path: '/session_id/?'
815814
}
816815
{
817-
path: '/answers/*'
816+
path: '/timestamp/?'
818817
}
818+
]
819+
excludedPaths: [
819820
{
820-
path: '/"_etag"/?'
821+
path: '/*'
821822
}
822823
]
823824
}

0 commit comments

Comments
 (0)