-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b81df3f
commit 5ec4103
Showing
16 changed files
with
395 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from fastapi import APIRouter | ||
from typing import Optional | ||
from datetime import datetime | ||
import app.admin.chatlogs.store as store | ||
|
||
router = APIRouter(prefix="/chatlogs", tags=["chatlogs"]) | ||
|
||
|
||
@router.get("/") | ||
async def list_chatlogs( | ||
page: int = 1, | ||
limit: int = 10, | ||
start_date: Optional[datetime] = None, | ||
end_date: Optional[datetime] = None, | ||
): | ||
"""Get paginated chat conversation history with optional date filtering""" | ||
return await store.list_chatlogs(page, limit, start_date, end_date) | ||
|
||
|
||
@router.get("/{thread_id}") | ||
async def get_chat_thread(thread_id: str): | ||
"""Get complete conversation history for a specific thread""" | ||
conversation = await store.get_chat_thread(thread_id) | ||
if not conversation: | ||
return {"error": "Conversation not found"} | ||
|
||
return conversation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from pydantic import BaseModel | ||
from typing import Dict, List, Optional | ||
from datetime import datetime | ||
|
||
|
||
class ChatMessage(BaseModel): | ||
text: str | ||
context: Optional[Dict] = {} | ||
|
||
|
||
class ChatThreadInfo(BaseModel): | ||
thread_id: str | ||
date: datetime | ||
|
||
|
||
class BotNessage(BaseModel): | ||
text: str | ||
|
||
|
||
class ChatLog(BaseModel): | ||
user_message: ChatMessage | ||
bot_message: List[BotNessage] | ||
date: datetime | ||
context: Optional[Dict] = {} | ||
|
||
|
||
class ChatLogResponse(BaseModel): | ||
total: int | ||
page: int | ||
limit: int | ||
conversations: List[ChatThreadInfo] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from typing import List, Optional | ||
from datetime import datetime | ||
from app.database import client | ||
from .schemas import ChatLog, ChatLogResponse, ChatThreadInfo | ||
|
||
# Initialize MongoDB collection | ||
collection = client["chatbot"]["state"] | ||
|
||
|
||
async def list_chatlogs( | ||
page: int = 1, | ||
limit: int = 10, | ||
start_date: Optional[datetime] = None, | ||
end_date: Optional[datetime] = None, | ||
) -> ChatLogResponse: | ||
skip = (page - 1) * limit | ||
|
||
# Build query filter | ||
query = {} | ||
if start_date or end_date: | ||
query["date"] = {} | ||
if start_date: | ||
query["date"]["$gte"] = start_date | ||
if end_date: | ||
query["date"]["$lte"] = end_date | ||
|
||
# Get total count of unique threads for pagination | ||
pipeline = [ | ||
{"$match": query}, | ||
{"$group": {"_id": "$thread_id"}}, | ||
{"$count": "total"}, | ||
] | ||
result = await collection.aggregate(pipeline).to_list(1) | ||
total = result[0]["total"] if result else 0 | ||
|
||
# Get paginated results grouped by thread_id with latest date | ||
pipeline = [ | ||
{"$match": query}, | ||
{"$sort": {"date": -1}}, | ||
{ | ||
"$group": { | ||
"_id": "$thread_id", | ||
"thread_id": {"$first": "$thread_id"}, | ||
"date": {"$first": "$date"}, | ||
} | ||
}, | ||
{"$sort": {"date": -1}}, | ||
{"$skip": skip}, | ||
{"$limit": limit}, | ||
] | ||
|
||
conversations = [] | ||
async for doc in collection.aggregate(pipeline): | ||
conversations.append( | ||
ChatThreadInfo(thread_id=doc["thread_id"], date=doc["date"]) | ||
) | ||
|
||
return ChatLogResponse( | ||
total=total, page=page, limit=limit, conversations=conversations | ||
) | ||
|
||
|
||
async def get_chat_thread(thread_id: str) -> List[ChatLog]: | ||
"""Get complete conversation history for a specific thread""" | ||
|
||
cursor = collection.find({"thread_id": thread_id}).sort("date", 1) | ||
messages = await cursor.to_list(length=None) | ||
|
||
if not messages: | ||
return None | ||
|
||
chat_logs = [] | ||
for msg in messages: | ||
chat_logs.append( | ||
ChatLog( | ||
user_message=msg["user_message"], | ||
bot_message=msg["bot_message"], | ||
date=msg["date"], | ||
context=msg.get("context", {}), | ||
) | ||
) | ||
|
||
return chat_logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules/ | ||
.next/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.