Documentation
¶
Index ¶
- type CoTAgent
- func (c *CoTAgent) Invoke(ctx context.Context, input *agentcore.AgentInput) (*agentcore.AgentOutput, error)
- func (c *CoTAgent) RunGenerator(ctx context.Context, input *agentcore.AgentInput) agentcore.Generator[*agentcore.AgentOutput]
- func (c *CoTAgent) Stream(ctx context.Context, input *agentcore.AgentInput) (<-chan agentcore.StreamChunk[*agentcore.AgentOutput], error)
- func (c *CoTAgent) WithCallbacks(callbacks ...agentcore.Callback) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
- func (c *CoTAgent) WithConfig(config agentcore.RunnableConfig) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
- type CoTConfig
- type CoTExample
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CoTAgent ¶
CoTAgent implements Chain-of-Thought reasoning pattern.
Chain-of-Thought (CoT) prompts the model to break down complex problems into intermediate reasoning steps, making the problem-solving process more transparent and accurate. This agent: - Encourages step-by-step reasoning - Shows intermediate calculations and logic - Improves accuracy on complex tasks - Provides interpretable reasoning traces
func NewCoTAgent ¶
NewCoTAgent creates a new Chain-of-Thought agent
func (*CoTAgent) Invoke ¶
func (c *CoTAgent) Invoke(ctx context.Context, input *agentcore.AgentInput) (*agentcore.AgentOutput, error)
Invoke executes the Chain-of-Thought reasoning
func (*CoTAgent) RunGenerator ¶ added in v0.2.0
func (c *CoTAgent) RunGenerator(ctx context.Context, input *agentcore.AgentInput) agentcore.Generator[*agentcore.AgentOutput]
RunGenerator 使用 Generator 模式执行 Chain-of-Thought(实验性功能)
相比 Stream,RunGenerator 提供零分配的流式执行,在每个主要步骤后 yield 中间结果:
- LLM 生成初始推理步骤后 yield
- 执行工具后 yield(如果使用工具)
- LLM 基于工具结果生成最终答案后 yield
性能优势:
- 零内存分配(无 channel、goroutine 开销)
- 支持早期终止(用户可以在任意步骤 break)
- 更低延迟(无 channel 发送/接收开销)
使用示例:
for output, err := range agent.RunGenerator(ctx, input) {
if err != nil {
log.Error("step failed", err)
continue
}
fmt.Printf("Step type: %s\n", output.Metadata["step_type"])
if output.Status == interfaces.StatusSuccess {
break // 找到最终答案
}
}
注意:此方法会触发 LLM 回调,但不触发 Agent 级别的回调(OnStart/OnFinish)
func (*CoTAgent) Stream ¶
func (c *CoTAgent) Stream(ctx context.Context, input *agentcore.AgentInput) (<-chan agentcore.StreamChunk[*agentcore.AgentOutput], error)
Stream executes Chain-of-Thought with streaming
func (*CoTAgent) WithCallbacks ¶
func (c *CoTAgent) WithCallbacks(callbacks ...agentcore.Callback) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
WithCallbacks adds callback handlers
func (*CoTAgent) WithConfig ¶
func (c *CoTAgent) WithConfig(config agentcore.RunnableConfig) agentcore.Runnable[*agentcore.AgentInput, *agentcore.AgentOutput]
WithConfig configures the agent
type CoTConfig ¶
type CoTConfig struct {
Name string // Agent name
Description string // Agent description
LLM llm.Client // LLM client
Tools []interfaces.Tool // Available tools (optional)
MaxSteps int // Maximum reasoning steps
// CoT-specific settings
ShowStepNumbers bool // Show step numbers in reasoning
RequireJustification bool // Require justification for each step
FinalAnswerFormat string // Format for final answer
ExampleFormat string // Example CoT format to show model
// Prompting strategy
ZeroShot bool // Use zero-shot CoT ("Let's think step by step")
FewShot bool // Use few-shot CoT with examples
FewShotExamples []CoTExample // Examples for few-shot learning
}
CoTConfig configuration for Chain-of-Thought agent
type CoTExample ¶
CoTExample represents an example for few-shot Chain-of-Thought