-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathlangchain_langgraph_multi_agent.py
More file actions
91 lines (72 loc) · 2.49 KB
/
langchain_langgraph_multi_agent.py
File metadata and controls
91 lines (72 loc) · 2.49 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
"""
LangGraph Multi-Agent Example (Python, optional)
Demonstrates (conceptually):
- Multi-agent / graph orchestration with cascadeflow
- Tool binding + high-risk verifier policy
Notes:
- Requires optional dependency: `pip install langgraph`
- This file is an example only; it is not required for cascadeflow core usage.
Setup:
export OPENAI_API_KEY="sk-..."
pip install -U langchain-core langchain-openai langgraph
python examples/langchain_langgraph_multi_agent.py
"""
import os
from langchain_openai import ChatOpenAI
from cascadeflow.langchain import CascadeFlow
def main() -> None:
if not os.environ.get("OPENAI_API_KEY"):
raise SystemExit("Set OPENAI_API_KEY first.")
# Optional: import only when used.
from langgraph.graph import END, StateGraph # type: ignore
drafter = ChatOpenAI(model="gpt-4o-mini", temperature=0.2)
verifier = ChatOpenAI(model="gpt-4o", temperature=0.2)
base = CascadeFlow(
drafter=drafter,
verifier=verifier,
quality_threshold=0.7,
cost_tracking_provider="langsmith",
)
tools = [
{
"name": "get_weather",
"description": "Read-only: get the current weather for a location.",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"],
},
},
{
"name": "delete_user",
"description": "HIGH RISK: permanently deletes a user account (irreversible).",
"parameters": {
"type": "object",
"properties": {"user_id": {"type": "string"}},
"required": ["user_id"],
},
},
]
cascade = base.bind_tools(tools)
def planner(state: dict) -> dict:
msg = cascade.invoke(
state["input"],
config={
"tags": ["example", "langgraph", "planner"],
"metadata": {"example": "langgraph-multi-agent", "agent": "planner"},
},
)
return {**state, "result": msg.content}
graph = StateGraph(dict)
graph.add_node("planner", planner)
graph.set_entry_point("planner")
graph.add_edge("planner", END)
app = graph.compile()
out = app.invoke(
{
"input": "Plan steps to fetch weather for Berlin. If any destructive action is needed, propose it but do not execute."
}
)
print(out.get("result"))
if __name__ == "__main__":
main()