-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlanggraph-demo.py
More file actions
55 lines (43 loc) · 1.49 KB
/
langgraph-demo.py
File metadata and controls
55 lines (43 loc) · 1.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
"""
The required dependencies for this demo are:
- langgraph
- langgraph-cli[inmem]
- langchain
then use `langgraph dev` to run in the browser!
"""
import time
import random
from langgraph.graph import START, END, StateGraph
from typing_extensions import TypedDict
delay_range = (0.5, 1.0)
class DemoState(TypedDict):
message: str
#################################################################################################
# Nodes
#################################################################################################
def node_a(state: DemoState) -> DemoState:
time.sleep(random.uniform(*delay_range))
state['message'] += ' -> A'
return state
def node_b(state: DemoState) -> DemoState:
time.sleep(random.uniform(*delay_range))
state['message'] += ' -> B'
return state
def node_c(state: DemoState) -> DemoState:
time.sleep(random.uniform(*delay_range))
state['message'] += ' -> C'
return state
def route_c(state: DemoState) -> str:
return random.choice(['A', 'B', 'C', END])
#################################################################################################
# Graph
#################################################################################################
graph = StateGraph(DemoState)
graph.add_node('A', node_a)
graph.add_node('B', node_b)
graph.add_node('C', node_c)
graph.add_edge(START, 'A')
graph.add_edge('A', 'B')
graph.add_edge('B', 'C')
graph.add_conditional_edges('C', route_c, ['A', 'B', 'C', END])
app = graph.compile()