rdune71 commited on
Commit
11ba014
·
1 Parent(s): 3771a70

Implement chat endpoint and integrate with main API

Browse files
Files changed (2) hide show
  1. api/chat.py +34 -0
  2. api/main.py +2 -0
api/chat.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ from core.llm import LLMClient
4
+ from core.memory import save_user_state, load_user_state
5
+ import json
6
+
7
+ router = APIRouter()
8
+
9
+ llm_client = LLMClient(provider="ollama") # Default to Ollama
10
+
11
+ @router.post("/chat")
12
+ async def chat(user_id: str, message: str):
13
+ if not message:
14
+ raise HTTPException(status_code=400, detail="Message is required")
15
+
16
+ # Load user state from Redis
17
+ user_state = load_user_state(user_id)
18
+ conversation_history = json.loads(user_state.get("conversation", "[]")) if user_state else []
19
+
20
+ # Add user message to history
21
+ conversation_history.append({"role": "user", "content": message})
22
+
23
+ # Generate AI response
24
+ try:
25
+ response_stream = llm_client.generate(
26
+ prompt=message,
27
+ stream=True
28
+ )
29
+
30
+ # Stream response back
31
+ return StreamingResponse(response_stream, media_type="text/event-stream")
32
+
33
+ except Exception as e:
34
+ raise HTTPException(status_code=500, detail=f"LLM generation failed: {e}")
api/main.py CHANGED
@@ -1,10 +1,12 @@
1
  from fastapi import FastAPI
2
  from api.status import router as status_router
 
3
 
4
  app = FastAPI()
5
 
6
  # Mount status endpoint
7
  app.include_router(status_router, prefix="/api")
 
8
 
9
  @app.get("/")
10
  async def root():
 
1
  from fastapi import FastAPI
2
  from api.status import router as status_router
3
+ from api.chat import router as chat_router
4
 
5
  app = FastAPI()
6
 
7
  # Mount status endpoint
8
  app.include_router(status_router, prefix="/api")
9
+ app.include_router(chat_router, prefix="/api")
10
 
11
  @app.get("/")
12
  async def root():