react

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCachedReActAgent

func NewCachedReActAgent(config ReActConfig, cacheConfig *performance.CacheConfig) agentcore.Agent

NewCachedReActAgent creates a ReAct agent with caching enabled This wraps a ReActAgent with performance.CachedAgent for automatic result caching. Cached ReAct agents are ideal for scenarios with repeated reasoning patterns or queries.

Example:

config := react.ReActConfig{
    Name:        "cached-react",
    Description: "ReAct agent with caching",
    LLM:         llmClient,
    Tools:       tools,
    MaxSteps:    10,
}
cachedAgent := react.NewCachedReActAgent(config, nil)

Or with custom cache config:

cacheConfig := &performance.CacheConfig{
    TTL:     5 * time.Minute,
    MaxSize: 500,
}
cachedAgent := react.NewCachedReActAgent(config, cacheConfig)

Types

type ReActAgent

type ReActAgent struct {
	*agentcore.BaseAgent
	// contains filtered or unexported fields
}

ReActAgent ReAct (Reasoning + Acting) Agent

实现 LangChain 的 ReAct 模式,通过思考-行动-观察循环解决问题: 1. Thought: 分析当前情况 2. Action: 决定使用哪个工具 3. Observation: 执行工具并观察结果 4. 循环直到得出最终答案

func NewReActAgent

func NewReActAgent(config ReActConfig) *ReActAgent

NewReActAgent 创建 ReAct Agent

func (*ReActAgent) Invoke

Invoke 执行 ReAct Agent(含完整回调)

func (*ReActAgent) InvokeFast

func (r *ReActAgent) InvokeFast(ctx context.Context, input *agentcore.AgentInput) (*agentcore.AgentOutput, error)

InvokeFast 快速执行(绕过回调)

用于热路径优化,直接执行核心逻辑,跳过所有回调 性能提升:避免回调遍历和接口调用开销

使用场景: - Chain 内部调用 - 嵌套 Agent 调用 - 高频循环场景 - 不需要监控和追踪的内部调用

注意:此方法不会触发任何回调(OnStart/OnFinish/OnLLMStart/OnToolStart 等)

func (*ReActAgent) RunGenerator added in v0.2.0

RunGenerator 使用 Generator 模式执行 ReAct Agent(实验性功能)

相比 Stream,RunGenerator 提供零分配的流式执行,在每个推理步骤后 yield 中间结果:

  • 每次 LLM 调用后 yield(Thought 步骤)
  • 每次工具执行后 yield(Action 步骤)
  • 最终答案后 yield(Final Answer)

性能优势:

  • 零内存分配(无 channel、goroutine 开销)
  • 支持早期终止(用户可以在任意步骤 break)
  • 更低延迟(无 channel 发送/接收开销)

使用示例:

for output, err := range agent.RunGenerator(ctx, input) {
    if err != nil {
        log.Error("step failed", err)
        continue  // 可以选择继续或 break
    }
    fmt.Printf("Step %d: %s\n", output.Metadata["current_step"], output.Message)
    if output.Status == interfaces.StatusSuccess {
        break  // 找到最终答案,提前终止
    }
}

注意:此方法不会触发 Agent 级别的回调(OnStart/OnFinish),但会触发 LLM 和 Tool 回调

func (*ReActAgent) Stream

Stream 流式执行 ReAct Agent

func (*ReActAgent) WithCallbacks

WithCallbacks 添加回调处理器

func (*ReActAgent) WithConfig

WithConfig 配置 Agent

type ReActConfig

type ReActConfig struct {
	Name         string            // Agent 名称
	Description  string            // Agent 描述
	LLM          llm.Client        // LLM 客户端
	Tools        []interfaces.Tool // 可用工具列表
	MaxSteps     int               // 最大步数
	StopPattern  []string          // 停止模式
	PromptPrefix string            // Prompt 前缀
	PromptSuffix string            // Prompt 后缀
	FormatInstr  string            // 格式说明
}

ReActConfig ReAct Agent 配置

Jump to

Keyboard shortcuts

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