| 29 | } |
| 30 | |
| 31 | func 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 | } |