Documentation
¶
Index ¶
- func BuildAgentCallback(modelHandler *template.ModelCallbackHandler, ...) callbacks.Handler
- type Agent
- func (r *Agent) ExportGraph() (compose.AnyGraph, []compose.GraphAddNodeOpt)
- func (r *Agent) Generate(ctx context.Context, input []*schema.Message, opts ...agent.AgentOption) (output *schema.Message, err error)
- func (r *Agent) Stream(ctx context.Context, input []*schema.Message, opts ...agent.AgentOption) (output *schema.StreamReader[*schema.Message], err error)
- type AgentConfig
- type MessageModifier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildAgentCallback ¶
func BuildAgentCallback(modelHandler *template.ModelCallbackHandler, toolHandler *template.ToolCallbackHandler) callbacks.Handler
BuildAgentCallback builds a callback handler for agent. e.g.
callback := BuildAgentCallback(modelHandler, toolHandler)
agent, err := react.NewAgent(ctx, &AgentConfig{})
agent.Generate(ctx, input, agent.WithComposeOptions(compose.WithCallbacks(callback)))
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent is the ReAct agent. ReAct agent is a simple agent that handles user messages with a chat model and tools. ReAct will call the chat model, if the message contains tool calls, it will call the tools. if the tool is configured to return directly, ReAct will return directly. otherwise, ReAct will continue to call the chat model until the message contains no tool calls. e.g.
agent, err := ReAct.NewAgent(ctx, &react.AgentConfig{})
if err != nil {...}
msg, err := agent.Generate(ctx, []*schema.Message{{Role: schema.User, Content: "how to build agent with eino"}})
if err != nil {...}
println(msg.Content)
func NewAgent ¶
func NewAgent(ctx context.Context, config *AgentConfig) (_ *Agent, err error)
NewAgent creates a ReAct agent that feeds tool response into next round of Chat Model generation.
IMPORTANT!! For models that don't output tool calls in the first streaming chunk (e.g. Claude) the default StreamToolCallChecker may not work properly since it only checks the first chunk for tool calls. In such cases, you need to implement a custom StreamToolCallChecker that can properly detect tool calls.
func (*Agent) ExportGraph ¶ added in v0.3.11
func (r *Agent) ExportGraph() (compose.AnyGraph, []compose.GraphAddNodeOpt)
ExportGraph exports the underlying graph from Agent, along with the []compose.GraphAddNodeOpt to be used when adding this graph to another graph.
type AgentConfig ¶
type AgentConfig struct {
// Model is the chat model to be used for handling user messages.
Model model.ChatModel
// ToolsConfig is the config for tools node.
ToolsConfig compose.ToolsNodeConfig
// MessageModifier.
// modify the input messages before the model is called, it's useful when you want to add some system prompt or other messages.
MessageModifier MessageModifier
// MaxStep.
// default 12 of steps in pregel (node num + 10).
MaxStep int `json:"max_step"`
// Tools that will make agent return directly when the tool is called.
// When multiple tools are called and more than one tool is in the return directly list, only the first one will be returned.
ToolReturnDirectly map[string]struct{}
// StreamOutputHandler is a function to determine whether the model's streaming output contains tool calls.
// Different models have different ways of outputting tool calls in streaming mode:
// - Some models (like OpenAI) output tool calls directly
// - Others (like Claude) output text first, then tool calls
// This handler allows custom logic to check for tool calls in the stream.
// It should return:
// - true if the output contains tool calls and agent should continue processing
// - false if no tool calls and agent should stop
// Note: This field only needs to be configured when using streaming mode
// Note: The handler MUST close the modelOutput stream before returning
// Optional. By default, it checks if the first chunk contains tool calls.
// Note: The default implementation does not work well with Claude, which typically outputs tool calls after text content.
// Note: If your ChatModel doesn't output tool calls first, you can try adding prompts to constrain the model from generating extra text during the tool call.
StreamToolCallChecker func(ctx context.Context, modelOutput *schema.StreamReader[*schema.Message]) (bool, error)
}
AgentConfig is the config for ReAct agent.
type MessageModifier ¶
MessageModifier modify the input messages before the model is called.
func NewPersonaModifier ¶
func NewPersonaModifier(persona string) MessageModifier
NewPersonaModifier add the system prompt as persona before the model is called. example:
persona := "You are an expert in golang."
config := AgentConfig{
Model: model,
MessageModifier: NewPersonaModifier(persona),
}
agent, err := NewAgent(ctx, config)
if err != nil {return}
msg, err := agent.Generate(ctx, []*schema.Message{{Role: schema.User, Content: "how to build agent with eino"}})
if err != nil {return}
println(msg.Content)