-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_workflow_example.py
More file actions
51 lines (38 loc) · 1.35 KB
/
Copy pathgraph_workflow_example.py
File metadata and controls
51 lines (38 loc) · 1.35 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
"""
Graph Workflow Example
WorkflowGraph for composable, graph-based workflows with conditional edges.
"""
from agentensemble import WorkflowGraph, Edge
def analyze_node(state):
"""Analyze the query."""
return {"context": {**state.get("context", {}), "analyzed": True}}
def search_node(state):
"""Simulate search step."""
q = state.get("query", "")
ctx = state.get("context", {})
return {"context": {**ctx, "search_result": f"Results for: {q}"}}
def summarize_node(state):
"""Produce final summary."""
ctx = state.get("context", {})
return {
"result": f"Summary: {ctx.get('search_result', 'N/A')}",
"current_node": "end",
}
def main():
graph = (
WorkflowGraph(entry="start", exit_nodes=["end"])
.add_node("start", lambda s: {"current_node": "analyze", "context": s.get("context", {})})
.add_node("analyze", analyze_node)
.add_node("search", search_node)
.add_node("summarize", summarize_node)
.add_edge("start", "analyze")
.add_edge("analyze", "search")
.add_edge("search", "summarize")
.add_edge("summarize", "end")
)
# Run workflow
result = graph.run("What is multi-agent AI?")
print("Result:", result["result"])
print("Metadata:", result["metadata"].get("nodes_visited", []))
if __name__ == "__main__":
main()