-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_example.py
More file actions
49 lines (37 loc) · 1.26 KB
/
Copy pathrunner_example.py
File metadata and controls
49 lines (37 loc) · 1.26 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
"""
Runner Example - Central Entry Point
Demonstrates Runner.run(), Runner.arun(), RunConfig, and RunHooks.
Uses StateGraphAgent (no API key required).
"""
from agentensemble import Runner, RunConfig, RunHooks, StateGraphAgent
def main():
# Simple graph: start -> end
def start_node(state):
return {"context": {**state.context, "started": True}, "result": f"Processed: {state.query}"}
agent = StateGraphAgent(
name="demo",
nodes={"start": start_node},
max_iterations=5,
)
agent._route = lambda state, current: "end" # start -> done
# Basic run
result = Runner.run(agent, "Hello world")
print("Result:", result["result"])
# With RunConfig and hooks
started = []
ended = []
def on_start(query, kwargs):
started.append(query)
print(f" [hook] on_start: {query}")
def on_end(res):
ended.append(res)
print(f" [hook] on_end: {res.get('result', '')[:50]}...")
config = RunConfig(
hooks=RunHooks(on_start=on_start, on_end=on_end),
context={"extra": "value"},
)
result = Runner.run(agent, "With hooks", config=config)
print("Result:", result["result"])
assert len(started) == 1 and len(ended) == 1
if __name__ == "__main__":
main()