Documentation
¶
Index ¶
- func NewCachedReActAgent(config ReActConfig, cacheConfig *performance.CacheConfig) agentcore.Agent
- type ReActAgent
- func (r *ReActAgent) Invoke(ctx context.Context, input *agentcore.AgentInput) (*agentcore.AgentOutput, error)
- func (r *ReActAgent) InvokeFast(ctx context.Context, input *agentcore.AgentInput) (*agentcore.AgentOutput, error)
- func (r *ReActAgent) RunGenerator(ctx context.Context, input *agentcore.AgentInput) agentcore.Generator[*agentcore.AgentOutput]
- func (r *ReActAgent) Stream(ctx context.Context, input *agentcore.AgentInput) (<-chan agentcore.StreamChunk[*agentcore.AgentOutput], error)
- func (r *ReActAgent) WithCallbacks(callbacks ...agentcore.Callback) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
- func (r *ReActAgent) WithConfig(config agentcore.RunnableConfig) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
- type ReActConfig
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 ¶
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 ¶
func (r *ReActAgent) Invoke(ctx context.Context, input *agentcore.AgentInput) (*agentcore.AgentOutput, error)
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
func (r *ReActAgent) RunGenerator(ctx context.Context, input *agentcore.AgentInput) agentcore.Generator[*agentcore.AgentOutput]
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 ¶
func (r *ReActAgent) Stream(ctx context.Context, input *agentcore.AgentInput) (<-chan agentcore.StreamChunk[*agentcore.AgentOutput], error)
Stream 流式执行 ReAct Agent
func (*ReActAgent) WithCallbacks ¶
func (r *ReActAgent) WithCallbacks(callbacks ...agentcore.Callback) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
WithCallbacks 添加回调处理器
func (*ReActAgent) WithConfig ¶
func (r *ReActAgent) WithConfig(config agentcore.RunnableConfig) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
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 配置