File size: 1,430 Bytes
509faa9
 
 
40ee601
509faa9
40ee601
 
509faa9
 
40ee601
 
509faa9
 
 
 
 
 
40ee601
 
509faa9
 
 
 
 
 
40ee601
509faa9
 
 
 
 
 
4cf95d1
509faa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
from fastapi import FastAPI, HTTPException, Request
from contextlib import asynccontextmanager
from pydantic import BaseModel, Field

from src.graph import workflow


class ResearchRequest(BaseModel):
    query: str = Field(..., min_length=1, description="The user's research question.")


class ResearchResponse(BaseModel):
    report: str
    objectives: list[str]
    is_approved: bool
    improvements: list[str]
    retries_used: int


@asynccontextmanager
async def lifespan(app: FastAPI):
    print("Research assistant graph compiled and ready.")
    app.state.research_graph = workflow.compile()
    yield
    print("Shutting down.")


app = FastAPI(title="Multi-Agent Research Assistant", lifespan=lifespan)


@app.post("/research", response_model=ResearchResponse)
async def run_research(payload: ResearchRequest, request: Request):
    research_graph = request.app.state.research_graph 

    try:
        result = await research_graph.ainvoke({
            "user_query": payload.query,
            "retry": 0,
        })
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Research pipeline failed: {e}")

    return ResearchResponse(
        report=result.get("written_report", ""),
        objectives=result.get("objectives", []),
        is_approved=result.get("is_approved", False),
        improvements=result.get("improvements", []),
        retries_used=result.get("retry", 0),
    )