react

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2024 License: Apache-2.0 Imports: 11 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildAgentCallback

func BuildAgentCallback(modelHandler *model.CallbackHandler, toolHandler *tool.CallbackHandler) callbacks.Handler

BuildAgentCallback builds a callback handler for agent. eg.

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. eg.

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, error)

NewAgent creates a react agent.

func (*Agent) Generate

func (r *Agent) Generate(ctx context.Context, input []*schema.Message, opts ...agent.AgentOption) (output *schema.Message, err error)

Generate generates a response from the agent.

func (*Agent) Stream

func (r *Agent) Stream(ctx context.Context, input []*schema.Message, opts ...agent.AgentOption) (
	output *schema.StreamReader[*schema.Message], err error)

Stream calls the agent and returns a stream response.

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.
	ToolReturnDirectly map[string]struct{}
}

AgentConfig is the config for react agent.

type MessageModifier

type MessageModifier func(ctx context.Context, input []*schema.Message) []*schema.Message

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL