cot

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CoTAgent

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

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

func NewCoTAgent(config CoTConfig) *CoTAgent

NewCoTAgent creates a new Chain-of-Thought agent

func (*CoTAgent) Invoke

Invoke executes the Chain-of-Thought reasoning

func (*CoTAgent) RunGenerator added in v0.2.0

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

Stream executes Chain-of-Thought with streaming

func (*CoTAgent) WithCallbacks

WithCallbacks adds callback handlers

func (*CoTAgent) WithConfig

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

type CoTExample struct {
	Question string
	Steps    []string
	Answer   string
}

CoTExample represents an example for few-shot Chain-of-Thought

Jump to

Keyboard shortcuts

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