File size: 1,061 Bytes
1bc2486
e9b356a
c63bddb
 
 
e9b356a
ac86483
c63bddb
e1233ed
e9b356a
c63bddb
1bc2486
 
 
 
c63bddb
3863be0
 
c63bddb
 
 
ac86483
c63bddb
 
e1233ed
 
 
 
 
c63bddb
 
 
 
 
 
e9b356a
3863be0
65a9b89
3863be0
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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))