import json import logging import httpx from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse, StreamingResponse from fastapi.staticfiles import StaticFiles from pydantic import BaseModel from typing import List, Generator logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) app = FastAPI(title="LocalFoodAI Chat") OLLAMA_URL = "http://localhost:11434/api/chat" MODEL_NAME = "llama3.1:8b" # Mount static files to serve the frontend app.mount("/static", StaticFiles(directory="static"), name="static") class ChatMessage(BaseModel): role: str content: str class ChatRequest(BaseModel): messages: List[ChatMessage] @app.get("/", response_class=HTMLResponse) async def read_root(): """Serve the chat interface HTML""" try: with open("static/index.html", "r", encoding="utf-8") as f: return HTMLResponse(content=f.read()) except FileNotFoundError: return HTMLResponse(content="
static/index.html not found. Please create the frontend.
") @app.post("/chat") async def chat_endpoint(request: ChatRequest): """Proxy chat requests to the local Ollama instance with streaming support""" payload = { "model": MODEL_NAME, "messages": [msg.model_dump() for msg in request.messages], "stream": True # Enable streaming for a better UI experience } async def generate_response(): try: async with httpx.AsyncClient() as client: async with client.stream("POST", OLLAMA_URL, json=payload, timeout=120.0) as response: if response.status_code != 200: error_detail = await response.aread() logger.error(f"Error communicating with Ollama: {error_detail}") yield f"data: {json.dumps({'error': 'Error communicating with local LLM.'})}\n\n" return async for line in response.aiter_lines(): if line: data = json.loads(line) if "message" in data and "content" in data["message"]: content = data["message"]["content"] yield f"data: {json.dumps({'content': content})}\n\n" if data.get("done"): break except Exception as e: logger.error(f"Unexpected error during stream: {e}") yield f"data: {json.dumps({'error': str(e)})}\n\n" return StreamingResponse(generate_response(), media_type="text/event-stream") if __name__ == "__main__": import uvicorn uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)