Kacper Łukawski
Allow overriding the model with env var
e1233ed
import logging
from collections.abc import AsyncIterable
from pathlib import Path
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from app.agent import MODEL, run_agent
from app.models import AgentRequest, TextChunk
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger(__name__)
app = FastAPI()
_STATIC = Path(__file__).parent / "static"
app.mount("/static", StaticFiles(directory=_STATIC), name="static")
@app.get("/config")
async def config():
return {"model": MODEL}
@app.get("/")
async def index() -> FileResponse:
return FileResponse(_STATIC / "index.html")
@app.post("/agent")
async def handle_agent_call(request: AgentRequest) -> AsyncIterable[TextChunk]:
try:
async for chunk in run_agent(request.input):
yield chunk
except Exception as exc:
logger.exception("Agent error during streaming")
yield TextChunk(type="error", content=str(exc))