-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlanggraph_auto_schema_agent.py
More file actions
282 lines (226 loc) · 8.8 KB
/
langgraph_auto_schema_agent.py
File metadata and controls
282 lines (226 loc) · 8.8 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""LangGraph agent that relies on auto-derived step schemas from @control tools.
This example demonstrates the SDK flow we want:
1. Define tool-like functions with Python type hints.
2. Decorate them with ``@control()``.
3. Call ``agent_control.init(...)`` without explicit ``steps=...``.
4. Let the SDK auto-discover decorated functions and derive JSON Schemas.
Run:
cd examples/langchain
uv run langgraph_auto_schema_agent.py
Prerequisite:
Start the Agent Control server (`cd server && make run`) so @control()
evaluations can execute successfully at runtime.
"""
from __future__ import annotations
import asyncio
import json
import os
import re
from typing import Annotated, Literal, TypedDict
import agent_control
from agent_control import ControlViolationError, control, get_registered_steps
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage
from langchain_core.tools import tool
from langgraph.graph import END, START, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode
from pydantic import BaseModel, Field
AGENT_NAME = "LangGraph Auto Schema Demo"
AGENT_DESCRIPTION = "LangGraph tool routing with @control auto step schema derivation"
class AgentState(TypedDict):
"""LangGraph state object."""
messages: Annotated[list[BaseMessage], add_messages]
class OrderStatus(BaseModel):
"""Structured result for order status lookups."""
order_id: str = Field(description="External order identifier")
status: Literal["processing", "shipped", "delivered"]
estimated_delivery_days: int | None = Field(
default=None,
ge=0,
description="Days until delivery; null when already delivered",
)
history: list[str] = Field(default_factory=list)
class RefundDecision(BaseModel):
"""Structured result for refund checks."""
order_id: str
reason: Literal["damaged", "late", "cancelled"]
approved: bool
approved_amount: float = Field(ge=0)
class OrderStatusPayload(TypedDict):
"""Tool payload returned to LangGraph."""
order_id: str
status: Literal["processing", "shipped", "delivered"]
estimated_delivery_days: int | None
history: list[str]
class RefundDecisionPayload(TypedDict):
"""Tool payload returned to LangGraph."""
order_id: str
reason: Literal["damaged", "late", "cancelled"]
approved: bool
approved_amount: float
def _parse_order_id(user_text: str) -> str:
"""Extract a stable order identifier from user text."""
match = re.search(r"(?:order\s*)?(\d{4,8})", user_text)
if match:
return f"ORD-{match.group(1)}"
return "ORD-1001"
async def _lookup_order_status(order_id: str, include_history: bool = False) -> OrderStatus:
"""Fetch fulfillment status for an order."""
base = OrderStatus(order_id=order_id, status="shipped", estimated_delivery_days=2)
if include_history:
base.history = [
"Label created",
"Picked up by carrier",
"Arrived at regional hub",
]
return base
setattr(_lookup_order_status, "name", "lookup_order_status")
setattr(_lookup_order_status, "tool_name", "lookup_order_status")
_lookup_order_status_checked = control()(_lookup_order_status)
async def _issue_refund(
order_id: str,
reason: Literal["damaged", "late", "cancelled"],
requested_amount: float | None = None,
) -> RefundDecision:
"""Evaluate refund eligibility for an order."""
approved_amount = requested_amount if requested_amount is not None else 25.0
is_approved = approved_amount <= 100.0
return RefundDecision(
order_id=order_id,
reason=reason,
approved=is_approved,
approved_amount=approved_amount if is_approved else 0.0,
)
setattr(_issue_refund, "name", "issue_refund")
setattr(_issue_refund, "tool_name", "issue_refund")
_issue_refund_checked = control()(_issue_refund)
@tool("lookup_order_status")
async def lookup_order_status(order_id: str, include_history: bool = False) -> OrderStatusPayload:
"""LangGraph tool wrapper for order status."""
result = await _lookup_order_status_checked(
order_id=order_id,
include_history=include_history,
)
return {
"order_id": result.order_id,
"status": result.status,
"estimated_delivery_days": result.estimated_delivery_days,
"history": result.history,
}
@tool("issue_refund")
async def issue_refund(
order_id: str,
reason: Literal["damaged", "late", "cancelled"],
requested_amount: float | None = None,
) -> RefundDecisionPayload:
"""LangGraph tool wrapper for refund decisions."""
result = await _issue_refund_checked(
order_id=order_id,
reason=reason,
requested_amount=requested_amount,
)
return {
"order_id": result.order_id,
"reason": result.reason,
"approved": result.approved,
"approved_amount": result.approved_amount,
}
def _build_graph():
"""Build a simple deterministic LangGraph flow with ToolNode."""
tool_node = ToolNode([lookup_order_status, issue_refund])
def planner(state: AgentState) -> dict[str, list[AIMessage]]:
last_message = state["messages"][-1]
user_text = str(last_message.content)
lower = user_text.lower()
order_id = _parse_order_id(user_text)
if "refund" in lower:
reason: Literal["damaged", "late", "cancelled"] = "late"
if "damaged" in lower:
reason = "damaged"
elif "cancel" in lower:
reason = "cancelled"
requested_amount: float | None = 49.0 if "49" in lower else None
tool_call = {
"name": "issue_refund",
"args": {
"order_id": order_id,
"reason": reason,
"requested_amount": requested_amount,
},
"id": "call-refund-1",
"type": "tool_call",
}
else:
include_history = "history" in lower or "timeline" in lower
tool_call = {
"name": "lookup_order_status",
"args": {
"order_id": order_id,
"include_history": include_history,
},
"id": "call-status-1",
"type": "tool_call",
}
return {"messages": [AIMessage(content="", tool_calls=[tool_call])]} # type: ignore[arg-type]
def finalize(state: AgentState) -> dict[str, list[AIMessage]]:
tool_message = next(
message for message in reversed(state["messages"]) if isinstance(message, ToolMessage)
)
return {
"messages": [
AIMessage(content=f"Tool `{tool_message.name}` returned: {tool_message.content}")
]
}
graph = StateGraph(AgentState)
graph.add_node("planner", planner)
graph.add_node("tools", tool_node)
graph.add_node("finalize", finalize)
graph.add_edge(START, "planner")
graph.add_edge("planner", "tools")
graph.add_edge("tools", "finalize")
graph.add_edge("finalize", END)
return graph.compile()
def _print_auto_derived_steps() -> None:
"""Show the step schemas auto-derived from @control-decorated functions."""
print("\nAuto-derived step schemas from @control():")
for step in get_registered_steps():
print("-" * 80)
print(json.dumps(step, indent=2, sort_keys=True))
async def main() -> None:
"""Run the demo end-to-end."""
try:
await run_demo_session()
finally:
await agent_control.ashutdown()
async def run_demo_session() -> None:
"""Initialize the SDK and run the LangGraph scenarios."""
print("Initializing Agent Control (no explicit steps passed)...")
agent_control.init(
agent_name=AGENT_NAME,
agent_description=AGENT_DESCRIPTION,
server_url=os.getenv("AGENT_CONTROL_URL"),
)
_print_auto_derived_steps()
app = _build_graph()
scenarios = [
"Track order 1001 and include its history",
"Issue a refund for order 2048 because it was late (49 dollars)",
]
print("\nRunning LangGraph scenarios...")
for prompt in scenarios:
print("=" * 80)
print(f"User: {prompt}")
try:
result = await app.ainvoke({"messages": [HumanMessage(content=prompt)]})
final_message = result["messages"][-1]
print(f"Assistant: {final_message.content}")
except ControlViolationError as exc:
print(f"Assistant: Request blocked by control rules: {exc.message}")
except RuntimeError as exc:
print(
"Assistant: Control evaluation is unavailable. "
f"Start the Agent Control server and retry. Details: {exc}"
)
break
if __name__ == "__main__":
asyncio.run(main())