Refactor API structure: move chat functionality to v1 router, implement dependency injection for OpenAI client, and set up application state management
Build and Push Agent API / build (push) Successful in 6s
Build and Push Agent API / build (push) Successful in 6s
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
from fastapi import APIRouter, Body, Depends
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
from api.dependencies import get_llm_client
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class ChatRequest(BaseModel):
|
||||
message: str
|
||||
session_id: str | None = None
|
||||
|
||||
|
||||
def run_agent(client: OpenAI, message: str, session_id: str | None = None) -> str:
|
||||
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
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def root():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.post("/chat")
|
||||
def chat(req: ChatRequest, client: OpenAI = Depends(get_llm_client)):
|
||||
response = run_agent(client, req.message, req.session_id)
|
||||
return {"response": response, "session_id": req.session_id}
|
||||
|
||||
|
||||
@router.get("/models")
|
||||
def list_models():
|
||||
return {
|
||||
"object": "list",
|
||||
"data": [
|
||||
{
|
||||
"id": "agent-model",
|
||||
"object": "model",
|
||||
"created": 0,
|
||||
"owned_by": "local-agent",
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/chat/completions")
|
||||
def chat_completions(
|
||||
payload: dict = Body(...),
|
||||
client: OpenAI = Depends(get_llm_client),
|
||||
):
|
||||
messages = payload["messages"]
|
||||
user_message = messages[-1]["content"]
|
||||
response = run_agent(client, user_message)
|
||||
|
||||
return {
|
||||
"id": "chatcmpl-local",
|
||||
"object": "chat.completion",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {"role": "assistant", "content": response},
|
||||
}
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user