Skip to content

Commit bf0faf8

Browse files
committed
working on session history
1 parent 8e9ea76 commit bf0faf8

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

Backend/app.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
API entrypoint for backend API.
33
"""
4-
from fastapi import FastAPI
4+
from fastapi import FastAPI, HTTPException
55
from fastapi.middleware.cors import CORSMiddleware
66

77
import uuid
@@ -53,3 +53,43 @@ def run_cosmic_works_ai_agent(request: AIRequest):
5353

5454
# Run the agent with the provided prompt.
5555
return { "message": agent_pool[session_id].run(prompt), "session_id": session_id }
56+
57+
58+
# ========================
59+
import os
60+
from dotenv import load_dotenv
61+
from azure.cosmos import CosmosClient
62+
from pydantic import BaseModel
63+
from typing import List
64+
65+
load_dotenv()
66+
67+
# Your existing Cosmos DB client and container setup
68+
CONNECTION_STRING = os.environ.get("COSMOS_DB_CONNECTION_STRING")
69+
client = CosmosClient.from_connection_string(CONNECTION_STRING)
70+
db = client.get_database_client("cosmic_works")
71+
chat_session_container = db.get_container_client("chat_session")
72+
73+
# Define the model for a Chat Session response
74+
class ChatSessionResponse(BaseModel):
75+
session_id: str
76+
title: str
77+
78+
@app.get("/sessions") #, response_model=List[ChatSessionResponse])
79+
def list_sessions():
80+
"""
81+
Endpoint to list all chat sessions.
82+
"""
83+
try:
84+
# Query to get all sessions in the chat_session_container
85+
query = "SELECT c.session_id, c.title FROM c"
86+
sessions = list(chat_session_container.query_items(
87+
query=query,
88+
enable_cross_partition_query=True
89+
))
90+
91+
# Convert the sessions into a list of ChatSessionResponse objects
92+
session_responses = [ChatSessionResponse(session_id=session['session_id'], title=session['title']) for session in sessions]
93+
return session_responses
94+
except Exception as e:
95+
raise HTTPException(status_code=500, detail=f"Failed to retrieve sessions: {str(e)}")

Backend/cosmic_works/cosmic_works_ai_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ def run(self, prompt: str) -> str:
123123
result = self.agent_executor.invoke(full_prompt)
124124
response = result["output"]
125125

126-
# Update chat history with new interaction
126+
# Update session chat history with new interaction
127127
self.chat_session.history.append({"role": "user", "content": prompt})
128128
self.chat_session.history.append({"role": "assistant", "content": response})
129129

130-
# Save updated chat history to Cosmos DB
131-
chat_session_container.upsert_item(self.chat_session.dict())
130+
# Save updated session chat history to Cosmos DB
131+
chat_session_container.upsert_item(self.chat_session.model_dump())
132132

133133
return response
134134

0 commit comments

Comments
 (0)