1
1
import os
2
+ import time
2
3
from typing import Any , Dict , Union , cast
3
4
4
5
from azure .cosmos .aio import ContainerProxy , CosmosClient
@@ -36,8 +37,11 @@ async def post_chat_history(auth_claims: Dict[str, Any]):
36
37
id = request_json .get ("id" )
37
38
answers = request_json .get ("answers" )
38
39
title = answers [0 ][0 ][:50 ] + "..." if len (answers [0 ][0 ]) > 50 else answers [0 ][0 ]
40
+ timestamp = int (time .time () * 1000 )
39
41
40
- await container .upsert_item ({"id" : id , "entra_oid" : entra_oid , "title" : title , "answers" : answers })
42
+ await container .upsert_item (
43
+ {"id" : id , "entra_oid" : entra_oid , "title" : title , "answers" : answers , "timestamp" : timestamp }
44
+ )
41
45
42
46
return jsonify ({}), 201
43
47
except Exception as error :
@@ -64,7 +68,7 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
64
68
continuation_token = request_json .get ("continuation_token" )
65
69
66
70
res = container .query_items (
67
- query = "SELECT c.id, c.entra_oid, c.title, c._ts FROM c WHERE c.entra_oid = @entra_oid ORDER BY c._ts DESC" ,
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" ,
68
72
parameters = [dict (name = "@entra_oid" , value = entra_oid )],
69
73
max_item_count = count ,
70
74
)
@@ -79,7 +83,14 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
79
83
80
84
items = []
81
85
async for item in page :
82
- items .append (item )
86
+ items .append (
87
+ {
88
+ "id" : item .get ("id" ),
89
+ "entra_oid" : item .get ("entra_oid" ),
90
+ "title" : item .get ("title" , "untitled" ),
91
+ "timestamp" : item .get ("timestamp" ),
92
+ }
93
+ )
83
94
84
95
# If there are no page, StopAsyncIteration is raised
85
96
except StopAsyncIteration :
@@ -111,7 +122,18 @@ async def get_chat_history_session(path: str, auth_claims: Dict[str, Any]):
111
122
112
123
try :
113
124
res = await container .read_item (item = path , partition_key = entra_oid )
114
- return jsonify (res ), 200
125
+ return (
126
+ jsonify (
127
+ {
128
+ "id" : res .get ("id" ),
129
+ "entra_oid" : res .get ("entra_oid" ),
130
+ "title" : res .get ("title" , "untitled" ),
131
+ "timestamp" : res .get ("timestamp" ),
132
+ "answers" : res .get ("answers" , []),
133
+ }
134
+ ),
135
+ 200 ,
136
+ )
115
137
except Exception as error :
116
138
return error_response (error , f"/chat_history/items/{ path } " )
117
139
0 commit comments