-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquickstart_simple_agent.py
88 lines (67 loc) · 2.56 KB
/
quickstart_simple_agent.py
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
from dagent import DecisionNode, FunctionNode
import logging
"""
This example demonstrates the main concepts of the dagent library:
1. Function Nodes: Represent individual operations in the workflow.
2. Decision Nodes: Use AI models to make decisions and route the workflow.
3. Node Linking: Connect nodes to create a directed acyclic graph (DAG).
4. Compilation: Prepare the DAG for execution.
5. Execution: Run the workflow starting from an entry point.
"""
# Can enable logging below to save logs to file
# logging.basicConfig(level=logging.INFO,
# format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
# handlers=[logging.FileHandler('dagent_logs.log'), logging.StreamHandler()])
def add_two_nums(a: int, b: int) -> int:
"""A simple function to add two numbers."""
return a + b
def multiply_two_nums(a: int, b: int) -> int:
"""A simple function to multiply two numbers."""
return a * b
def print_result(prev_output: int) -> None:
"""
Print the result from a previous node.
Note: `prev_output` is automatically passed from the previous node.
"""
print(prev_output)
return prev_output
def entry_func(input: str) -> str:
"""Entry point function for the workflow."""
return input
def main():
# Setup Function Nodes
"""
FunctionNodes wrap regular Python functions, allowing them to be used in the DAG.
"""
add_two_nums_node = FunctionNode(func=add_two_nums)
multiply_two_nums_node = FunctionNode(func=multiply_two_nums)
print_result_node = FunctionNode(func=print_result)
entry_node = FunctionNode(func=entry_func)
# Setup Decision Node
"""
DecisionNodes use AI models to make routing decisions in the workflow.
"""
decision_node = DecisionNode(model='gpt-4-0125-preview', api_base=None)
# Link Nodes
"""
Nodes are connected by setting their `next_nodes` attribute.
This creates the structure of the directed acyclic graph (DAG).
"""
entry_node.next_nodes = [decision_node]
decision_node.next_nodes = [
add_two_nums_node,
multiply_two_nums_node,
]
add_two_nums_node.next_nodes = [print_result_node]
multiply_two_nums_node.next_nodes = [print_result_node]
# Compile the DAG
"""
Compilation prepares the DAG for execution, ensuring all nodes are properly linked.
"""
entry_node.compile(force_load=True)
# Execute the DAG
while True:
user_input = input("Enter your command: ")
entry_node.run(input=user_input)
if __name__ == "__main__":
main()