-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path4_fastapi_server.py
More file actions
94 lines (75 loc) · 2.91 KB
/
Copy path4_fastapi_server.py
File metadata and controls
94 lines (75 loc) · 2.91 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# For setup instructions, see: https://docs.snath.ai/guides/litellm_setup/
"""
Example 19: Deploying Lár as an API (FastAPI)
"How do I deploy this?"
Lár is a library, not a platform. This means you wrap it in standard Python web frameworks
like FastAPI, Flask, or Django.
This example shows how to turn an Agent into a REST API in < 50 lines.
Prerequisites:
pip install fastapi uvicorn
"""
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from lar import GraphState, GraphExecutor, LLMNode, AddValueNode
from dotenv import load_dotenv
# 1. Setup
load_dotenv()
app = FastAPI(title="Lár Agent API", version="1.0")
# 2. Define Request Schema
class AgentRequest(BaseModel):
task: str
user_id: str = "anon"
# 3. Build the Graph (Global Instance)
# We build the graph once at startup.
def build_agent_graph():
# End Node
final_node = AddValueNode(key="status", value="completed", next_node=None)
# Simple Agent
agent_node = LLMNode(
model_name="ollama/phi4", # Or "ollama/phi4", "claude-3-opus"
prompt_template="You are a helper API. Answer this request concisely: {task}",
output_key="response",
next_node=final_node
)
return agent_node
# Initialize
root_node = build_agent_graph()
executor = GraphExecutor()
# 4. Define the Endpoint
@app.post("/run")
async def run_agent(request: AgentRequest):
"""
Executes the agent deterministically.
"""
print(f"--> Received request: {request.task}")
try:
# Run the graph
initial_state = {"task": request.task, "user_id": request.user_id}
# We use list() to execute all steps synchronously for this simple example.
# For long-running jobs, use a background task queue (Celery/Bull).
result_log = list(executor.run_step_by_step(root_node, initial_state))
if not result_log:
return {"status": "error", "reason": "No steps executed"}
last_step = result_log[-1]
# Check for error in the last step
if last_step.get("outcome") == "error":
return {
"status": "failed",
"error": last_step.get("error_details", "Unknown error"),
"audit_log": result_log
}
final_state = last_step.get("state_snapshot", {})
return {
"status": "success",
"result": final_state.get("response"),
"steps": len(result_log),
"audit_log": result_log # Full "Glass Box" audit trail
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# 5. Run Server
if __name__ == "__main__":
print("Starting Lár API server on http://localhost:8000")
print("Test with: curl -X POST 'http://localhost:8000/run' -H 'Content-Type: application/json' -d '{\"task\": \"Hello!\"}'")
uvicorn.run(app, host="0.0.0.0", port=8000)