MCPcopy Create free account
hub / github.com/cloudwego/eino-examples / RegisterSimpleStateGraph

Function RegisterSimpleStateGraph

devops/debug/graph/state_graph.go:31–83  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

29}
30
31func RegisterSimpleStateGraph(ctx context.Context) {
32 stateFunction := func(ctx context.Context) *nodeState {
33 s := &nodeState{
34 Messages: make([]string, 0, 3),
35 }
36 return s
37 }
38
39 sg := compose.NewGraph[string, string](compose.WithGenLocalState(stateFunction))
40
41 _ = sg.AddLambdaNode("node_1", compose.InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
42 return input + " process by node_1,", nil
43 }), compose.WithStatePreHandler(func(ctx context.Context, input string, state *nodeState) (string, error) {
44 state.Messages = append(state.Messages, input)
45 return input, nil
46 }))
47
48 _ = sg.AddLambdaNode("node_2", compose.InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
49 return input + " process by node_2,", nil
50 }), compose.WithStatePreHandler(func(ctx context.Context, input string, state *nodeState) (string, error) {
51 state.Messages = append(state.Messages, input)
52 return input, nil
53 }))
54
55 _ = sg.AddLambdaNode("node_3", compose.InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
56 return input + " process by node_3,", nil
57 }), compose.WithStatePreHandler(func(ctx context.Context, input string, state *nodeState) (string, error) {
58 state.Messages = append(state.Messages, input)
59 return input, nil
60 }))
61
62 _ = sg.AddEdge(compose.START, "node_1")
63
64 _ = sg.AddEdge("node_1", "node_2")
65
66 _ = sg.AddEdge("node_2", "node_3")
67
68 _ = sg.AddEdge("node_3", compose.END)
69
70 r, err := sg.Compile(ctx)
71 if err != nil {
72 logs.Errorf("compile state graph failed, err=%v", err)
73 return
74 }
75
76 message, err := r.Invoke(ctx, "eino state graph test")
77 if err != nil {
78 logs.Errorf("invoke state graph failed, err=%v", err)
79 return
80 }
81
82 logs.Infof("eino simple state graph output is: %v", message)
83}

Callers 1

mainFunction · 0.92

Calls 4

ErrorfFunction · 0.92
InfofFunction · 0.92
CompileMethod · 0.65
InvokeMethod · 0.45

Tested by

no test coverage detected