-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhuman_approval_agent.py
More file actions
68 lines (50 loc) · 1.96 KB
/
Copy pathhuman_approval_agent.py
File metadata and controls
68 lines (50 loc) · 1.96 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
import restate
from google.adk import Runner
from google.adk.agents.llm_agent import Agent
from google.adk.apps import App
from google.genai.types import Content, Part
from restate.ext.adk import RestatePlugin, restate_context, RestateSessionService
from utils.models import ClaimPrompt, InsuranceClaim
from utils.utils import request_review, parse_agent_response
APP_NAME = "agents"
# <start_here>
async def human_approval(claim: InsuranceClaim) -> str:
"""Ask for human approval for high-value claims."""
# Create an awakeable for human approval
approval_id, approval_promise = restate_context().awakeable(type_hint=str)
# Request human review
await restate_context().run_typed(
"Request review",
request_review,
claim=claim,
awakeable_id=approval_id,
)
# Wait for human approval
return await approval_promise
# <end_here>
agent = Agent(
model="gemini-2.5-flash",
name="claim_approval_agent",
instruction="""You are an insurance claim evaluation agent. Use these rules:
- if the amount is more than 1000, ask for human approval using tools;
- if the amount is less than 1000, decide by yourself.""",
tools=[human_approval],
)
app = App(name=APP_NAME, root_agent=agent, plugins=[RestatePlugin()])
runner = Runner(app=app, session_service=RestateSessionService())
agent_service = restate.VirtualObject("HumanClaimApprovalAgent")
@agent_service.handler()
async def run(ctx: restate.ObjectContext, req: ClaimPrompt) -> str | None:
events = runner.run_async(
user_id=ctx.key(),
session_id=req.session_id,
new_message=Content(role="user", parts=[Part.from_text(text=req.message)]),
)
return await parse_agent_response(events)
if __name__ == "__main__":
import hypercorn
import asyncio
restate_app = restate.app(services=[agent_service])
conf = hypercorn.Config()
conf.bind = ["0.0.0.0:9080"]
asyncio.run(hypercorn.asyncio.serve(restate_app, conf))