File size: 1,183 Bytes
2043afa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Agent suggestion API routes."""

from __future__ import annotations

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field

from ...models import PolypharmacyAction, PolypharmacyObservation
from ...services.groq_agent import suggest_action_from_observation

router = APIRouter(prefix="/agent", tags=["agent"])


class AgentSuggestRequest(BaseModel):
    observation: PolypharmacyObservation
    model_name: str | None = None


class AgentSuggestResponse(BaseModel):
    action: PolypharmacyAction
    source: str = Field(default="groq")


@router.post("/suggest", response_model=AgentSuggestResponse)
def suggest_agent_action(payload: AgentSuggestRequest) -> AgentSuggestResponse:
    """Return a model-suggested action for the current observation."""
    try:
        action = suggest_action_from_observation(
            payload.observation, model_name=payload.model_name
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc
    except Exception as exc:
        raise HTTPException(status_code=500, detail=f"Model call failed: {exc}") from exc
    return AgentSuggestResponse(action=action)