Skip to content

Commit 6250957

Browse files
committed
feat: add endpoint to get message history for a chat
1 parent c621a9c commit 6250957

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

server/api/v1/chat.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from server.features.embeddings import Embedder
1818
from server.features.extraction import extract_documents_from_pdfs
1919
from server.features.question_answering import question_answering
20-
from server.schemas.v1 import Chat, Files, Query
20+
from server.schemas.v1 import Chat, Files, Messages, Query
2121
from server.state import AppState
2222

2323

@@ -43,6 +43,15 @@ async def create_chat(self) -> Chat:
4343
"""
4444
return Chat()
4545

46+
@get('/{chat_id:str}')
47+
async def get_chat(self, redis: Annotated[RedisAsync, Dependency()], chat_id: str) -> Messages:
48+
"""
49+
Summary
50+
-------
51+
an endpoint for getting all chat messages
52+
"""
53+
return Messages(messages=await redis.get_messages(chat_id))
54+
4655
@delete('/{chat_id:str}')
4756
async def delete_chat(self, redis: Annotated[RedisAsync, Dependency()], chat_id: str) -> None:
4857
"""

server/schemas/v1/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
from server.schemas.v1.chat import Chat as Chat
33
from server.schemas.v1.embedding import Embedding as Embedding
44
from server.schemas.v1.files import Files as Files
5+
from server.schemas.v1.messages import Messages as Messages
56
from server.schemas.v1.query import Query as Query

server/schemas/v1/messages.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import Annotated
2+
3+
from msgspec import Meta, Struct
4+
5+
from server.features.chat.types import Message
6+
7+
8+
class Messages(Struct):
9+
"""
10+
Summary
11+
-------
12+
the message history schema
13+
14+
Attributes
15+
----------
16+
messages (list[Message]) : the message history
17+
"""
18+
19+
messages: Annotated[
20+
list[Message],
21+
Meta(
22+
examples=[
23+
[
24+
{
25+
'role': 'user',
26+
'content': 'What is the capital of Japan?',
27+
},
28+
{
29+
'role': 'assistant',
30+
'content': 'Tokyo.',
31+
},
32+
]
33+
]
34+
),
35+
]

0 commit comments

Comments
 (0)