From 1476b33a9b0de0081e57afd1debc641aecd3fa1b Mon Sep 17 00:00:00 2001 From: TimHoogervorst Date: Sun, 10 May 2026 15:56:01 +0200 Subject: [PATCH] Refactor chat endpoint: extract agent logic to run_agent function and update response handling --- agent_api/main.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/agent_api/main.py b/agent_api/main.py index 6dad68e..7612a16 100644 --- a/agent_api/main.py +++ b/agent_api/main.py @@ -1,32 +1,44 @@ from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel - from .core.llm import client app = FastAPI() +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) -# ---- request model ---- class ChatRequest(BaseModel): message: str + session_id: str | None = None + + +def run_agent(message: str, session_id: str | None = None): + response = client.chat.completions.create( + model="deepseek-chat", + messages=[ + {"role": "system", "content": "You are a helpful agent."}, + {"role": "user", "content": message} + ] + ) + return response.choices[0].message.content -# ---- basic health check ---- @app.get("/") def root(): return {"status": "ok"} -# ---- test LLM endpoint ---- @app.post("/chat") def chat(req: ChatRequest): - response = client.chat.completions.create( - model="deepseek-chat", - messages=[ - {"role": "user", "content": req.message} - ] - ) + response = run_agent(req.message, req.session_id) return { - "response": response.choices[0].message.content + "response": response, + "session_id": req.session_id } \ No newline at end of file