Skip to content

请教一下,为什么我这样写的react使用流式会报错呢 #172

@ppd324

Description

@ppd324
func NewReactAgent(tools []tool.BaseTool, model model.BaseChatModel) (*ReactAgent, error) {
	graph := compose.NewGraph[string, string](compose.WithGenLocalState(func(ctx context.Context) *state {
		return &state{
			Messages:    []*schema.Message{},
			ShortMemory: shortMemory,
		}
	}))
	graph.AddLambdaNode("add_prompt", compose.InvokableLambda(func(ctx context.Context, input string) (output []*schema.Message, err error) {
		output = []*schema.Message{
			{
				Role:    "system",
				Content: prompt,
			},
			{
				Role:    "user",
				Content: input,
			},
		}
		return
	}))
	graph.AddChatModelNode("chat_model", model, compose.WithStatePreHandler(ModelStateAdd))
	toolNode, err := compose.NewToolNode(context.Background(), &compose.ToolsNodeConfig{
		Tools: tools,
	})
	if err != nil {
		slog.Error("new tool node error", "err", err)
		return nil, err
	}
	graph.AddToolsNode("tools_call", toolNode, compose.WithStatePreHandler(ToolCallStateAdd))
	graph.AddLambdaNode("done", compose.StreamableLambda(func(ctx context.Context, input *schema.StreamReader[*schema.Message]) (output *schema.StreamReader[string], err error) {
		return schema.StreamReaderWithConvert(input, func(t *schema.Message) (string, error) {
			return t.Content, nil
		}), nil

	}))
	graph.AddEdge(compose.START, "add_prompt")
	graph.AddEdge("add_prompt", "chat_model")
	//func(ctx context.Context, in *schema.Message) (endNode string, err error) {
	//	if len(in.ToolCalls) > 0 {
	//		return "tools_call", nil
	//	}
	//	return "done", nil
	//
	//}, map[string]bool{
	//	"tools_call": true,
	//	"done":       true,
	//})
	graph.AddBranch("chat_model", compose.NewStreamGraphBranch(func(ctx context.Context, in *schema.StreamReader[*schema.Message]) (endNode string, err error) {
		if toolCall, err := firstChunkStreamToolCallChecker(ctx, in); err != nil {
			return "", nil
		} else if toolCall {
			return "tools_call", nil
		}
		return "done", nil
	}, map[string]bool{
		"tools_call": true,
		"done":       true,
	}))
	//graph.AddBranch("tools_call", compose.NewStreamGraphBranch(func(ctx context.Context, msgsStream *schema.StreamReader[[]*schema.Message]) (endNode string, err error) {
	//	msgsStream.Close()
	//
	//	return "chat_model", nil
	//}, map[string]bool{"chat_model": true}))
	graph.AddEdge("tools_call", "chat_model")
	graph.AddEdge("done", compose.END)

	executor, err := graph.Compile(context.Background())
	if err != nil {
		panic(err)
	}
	return &ReactAgent{
		tools:    tools,
		model:    model,
		executor: executor,
	}, nil
}

panic: graph edge[chat_model]-[done]: start node's output type[*schema.Message] and end node's input type[*schema.StreamReader[*github.com/cloudwego/eino/schema.Message]] mismatch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions