From 4c758f773344c5f4fff4d0157bc25d2e647a536e Mon Sep 17 00:00:00 2001 From: TimHoogervorst Date: Sun, 10 May 2026 13:09:58 +0200 Subject: [PATCH] Refactor agent API structure and add core functionality with FastAPI integration --- agent-api/Dockerfile | 11 ----------- agent-api/app.py | 37 ------------------------------------- agent-api/requirements.txt | 3 --- agent_api/__init__.py | 0 agent_api/core/__init__.py | 0 agent_api/core/config.py | 7 +++++++ agent_api/core/llm.py | 7 +++++++ agent_api/main.py | 32 ++++++++++++++++++++++++++++++++ docker/Dockerfile | 13 +++++++++++++ requirements.txt | 4 ++++ 10 files changed, 63 insertions(+), 51 deletions(-) delete mode 100644 agent-api/Dockerfile delete mode 100644 agent-api/app.py delete mode 100644 agent-api/requirements.txt create mode 100644 agent_api/__init__.py create mode 100644 agent_api/core/__init__.py create mode 100644 agent_api/core/config.py create mode 100644 agent_api/core/llm.py create mode 100644 agent_api/main.py create mode 100644 docker/Dockerfile create mode 100644 requirements.txt diff --git a/agent-api/Dockerfile b/agent-api/Dockerfile deleted file mode 100644 index 9d07a1c..0000000 --- a/agent-api/Dockerfile +++ /dev/null @@ -1,11 +0,0 @@ -FROM python:3.11-slim - -WORKDIR /app - -COPY requirements.txt . - -RUN pip install --no-cache-dir -r requirements.txt - -COPY . . - -CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/agent-api/app.py b/agent-api/app.py deleted file mode 100644 index 2753e08..0000000 --- a/agent-api/app.py +++ /dev/null @@ -1,37 +0,0 @@ -from fastapi import FastAPI -from openai import OpenAI -import os - -app = FastAPI() - -# DeepSeek OpenAI-compatible endpoint -client = OpenAI( - api_key=os.getenv("DEEPSEEK_API_KEY"), - base_url="https://api.deepseek.com" -) - -@app.post("/v1/chat/completions") -async def chat(data: dict): - - messages = data["messages"] - - repo_context = """ - This assistant has access to the repository. - Help the user understand the codebase. - This repo is building an chatbot, currently still in early stages. The main files are: - - app.py: The main FastAPI application that defines the API endpoints and integrates with the Deep seek API. - - agent.py: Contains the logic for the chatbot agent, including how it processes messages and generates responses. - """ - - messages.insert(0, { - "role": "system", - "content": repo_context - }) - - response = client.chat.completions.create( - model="deepseek-v4-flash", - messages=messages, - temperature=0.3 - ) - - return response.model_dump() \ No newline at end of file diff --git a/agent-api/requirements.txt b/agent-api/requirements.txt deleted file mode 100644 index 4e42a51..0000000 --- a/agent-api/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -fastapi -openai -uvicorn \ No newline at end of file diff --git a/agent_api/__init__.py b/agent_api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/agent_api/core/__init__.py b/agent_api/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/agent_api/core/config.py b/agent_api/core/config.py new file mode 100644 index 0000000..61f89df --- /dev/null +++ b/agent_api/core/config.py @@ -0,0 +1,7 @@ +from dotenv import load_dotenv +from pathlib import Path +import os + +load_dotenv(Path(__file__).resolve().parent.parent.parent / ".env") + +DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY") \ No newline at end of file diff --git a/agent_api/core/llm.py b/agent_api/core/llm.py new file mode 100644 index 0000000..b567c56 --- /dev/null +++ b/agent_api/core/llm.py @@ -0,0 +1,7 @@ +from openai import OpenAI +from .config import DEEPSEEK_API_KEY + +client = OpenAI( + api_key=DEEPSEEK_API_KEY, + base_url="https://api.deepseek.com" +) \ No newline at end of file diff --git a/agent_api/main.py b/agent_api/main.py new file mode 100644 index 0000000..6dad68e --- /dev/null +++ b/agent_api/main.py @@ -0,0 +1,32 @@ +from fastapi import FastAPI +from pydantic import BaseModel + +from .core.llm import client + +app = FastAPI() + + +# ---- request model ---- +class ChatRequest(BaseModel): + message: str + + +# ---- 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} + ] + ) + + return { + "response": response.choices[0].message.content + } \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..ca53a08 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.11-slim + +WORKDIR /app + +COPY requirements.txt /app/requirements.txt + +RUN pip install --no-cache-dir -r requirements.txt + +COPY . /app + +ENV PYTHONPATH=/app + +CMD ["uvicorn", "agent-api.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c9db552 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +fastapi +openai +uvicorn +python-dotenv \ No newline at end of file