Documentation
¶
Index ¶
- Constants
- func BuildAgentCallback(modelHandler *template.ModelCallbackHandler, ...) callbacks.Handler
- func SetReturnDirectly(ctx context.Context) error
- func WithChatModelOptions(opts ...model.Option) agent.AgentOption
- func WithToolList(tools ...tool.BaseTool) agent.AgentOptiondeprecated
- func WithToolOptions(opts ...tool.Option) agent.AgentOption
- func WithTools(ctx context.Context, tools ...tool.BaseTool) ([]agent.AgentOption, error)
- type Agent
- func (r *Agent) ExportGraph() (compose.AnyGraph, []compose.GraphAddNodeOpt)
- func (r *Agent) Generate(ctx context.Context, input []*schema.Message, opts ...agent.AgentOption) (*schema.Message, error)
- func (r *Agent) Stream(ctx context.Context, input []*schema.Message, opts ...agent.AgentOption) (output *schema.StreamReader[*schema.Message], err error)
- type AgentConfig
- type Iterator
- type MessageFuture
- type MessageModifier
Constants ¶
const ( GraphName = "ReActAgent" ModelNodeName = "ChatModel" ToolsNodeName = "Tools" )
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)))
func SetReturnDirectly ¶ added in v0.3.46
SetReturnDirectly is a helper function that can be called within a tool's execution. It signals the ReAct agent to stop further processing and return the result of the current tool call directly. This is useful when the tool's output is the final answer and no more steps are needed. Note: If multiple tools call this function in the same step, only the last call will take effect. This setting has a higher priority than the AgentConfig.ToolReturnDirectly.
func WithChatModelOptions ¶ added in v0.3.45
func WithChatModelOptions(opts ...model.Option) agent.AgentOption
WithChatModelOptions returns an agent option that specifies model.Option for the chat model in agent.
func WithToolList
deprecated
added in
v0.3.26
func WithToolList(tools ...tool.BaseTool) agent.AgentOption
Deprecated: this option changes tool list for ToolsNode ONLY, if your need to pass new ToolInfo list to chat model as well, use the new WithTools function instead. WithToolList returns an agent option that specifies the list of tools can be called which are BaseTool but must implement InvokableTool or StreamableTool.
func WithToolOptions ¶ added in v0.3.19
func WithToolOptions(opts ...tool.Option) agent.AgentOption
WithToolOptions returns an agent option that specifies tool.Option for the tools in agent.
func WithTools ¶ added in v0.4.8
WithTools is a convenience function that configures a React agent with a list of tools. It performs two essential operations:
- Extracts tool information for the chat model to understand available tools
- Registers the actual tool implementations for execution
Parameters:
- ctx: The context for the operation, used when calling Info() on each tool
- tools: A variadic list of tools that must implement either InvokableTool or StreamableTool interfaces
Returns:
- []agent.AgentOption: A slice containing exactly 2 agent options:
- Option 1: Configures the chat model with tool schemas via model.WithTools(toolInfos)
- Option 2: Registers the tool implementations via compose.WithToolList(tools...)
- error: Returns an error if any tool's Info() method fails
Usage Example:
ctx := context.Background()
agentOptions, err := WithTools(ctx, myTool1, myTool2, myTool3)
if err != nil {
return fmt.Errorf("failed to configure tools: %w", err)
}
agent, err := react.NewAgent(ctx, &react.AgentConfig{
ToolCallingModel: myModel,
// other config...
})
if err != nil {
return fmt.Errorf("failed to create agent: %w", err)
}
// Use the tool options with Generate or Stream methods
msg, err := agent.Generate(ctx, messages, agentOptions...)
// or
stream, err := agent.Stream(ctx, messages, agentOptions...)
Comparison with Related Functions:
- WithToolList: Only registers tool implementations, doesn't configure the chat model
- WithTools: Comprehensive setup that handles both chat model configuration and tool registration
Notes:
- The function always returns exactly 2 options when successful
- Both returned options should be applied to the agent for proper tool functionality
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 {
// ToolCallingModel is the chat model to be used for handling user messages with tool calling capability.
// This is the recommended model field to use.
ToolCallingModel model.ToolCallingChatModel
// Deprecated: Use ToolCallingModel instead.
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
// MessageRewriter modifies message in the state, before the ChatModel is called.
// It takes the messages stored accumulated in state, modify them, and put the modified version back into state.
// Useful for compressing message history to fit the model context window,
// or if you want to make changes to messages that take effect across multiple model calls.
// NOTE: if both MessageModifier and MessageRewriter are set, MessageRewriter will be called before MessageModifier.
MessageRewriter 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{}
// StreamToolCallChecker 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)
// GraphName is the graph name of the ReAct Agent.
// Optional. Default `ReActAgent`.
GraphName string
// ModelNodeName is the node name of the model node in the ReAct Agent graph.
// Optional. Default `ChatModel`.
ModelNodeName string
// ToolsNodeName is the node name of the tools node in the ReAct Agent graph.
// Optional. Default `Tools`.
ToolsNodeName string
}
AgentConfig is the config for ReAct agent.
type Iterator ¶ added in v0.3.23
type Iterator[T any] struct { // contains filtered or unexported fields }
type MessageFuture ¶ added in v0.3.23
type MessageFuture interface {
// GetMessages returns an iterator for retrieving messages generated during "agent.Generate" calls.
GetMessages() *Iterator[*schema.Message]
// GetMessageStreams returns an iterator for retrieving streaming messages generated during "agent.Stream" calls.
GetMessageStreams() *Iterator[*schema.StreamReader[*schema.Message]]
}
func WithMessageFuture ¶ added in v0.3.23
func WithMessageFuture() (agent.AgentOption, MessageFuture)
WithMessageFuture returns an agent option and a MessageFuture interface instance. The option configures the agent to collect messages generated during execution, while the MessageFuture interface allows users to asynchronously retrieve these messages.
type MessageModifier ¶
MessageModifier modify the input messages before the model is called.
func NewPersonaModifier
deprecated
func NewPersonaModifier(persona string) MessageModifier
Deprecated: This approach of adding persona involves unnecessary slice copying overhead. Instead, directly include the persona message in the input messages when calling Generate or Stream.
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)