deliberation

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

包 deliberation 提供智能体行动前的多步推理与审议引擎。

概述

本包用于解决"智能体在执行工具调用前如何进行深度思考"的问题。 通过可配置的审议模式,让智能体在行动前经历理解、评估、规划、 自我批判等推理步骤,从而提升决策质量与置信度。

核心模型

  • DeliberationMode:审议模式枚举,支持 immediate(直接执行)、 deliberate(完整推理循环)和 adaptive(上下文自适应)。
  • Engine:审议引擎,驱动多轮推理循环并管理置信度阈值。
  • ThoughtProcess:单步推理记录,包含类型(understand / evaluate / plan / critique)、内容与置信度。
  • Decision:审议最终产出,包含动作、工具选择、参数与推理依据。
  • Reasoner:基于 LLM 的推理接口,由外部注入具体实现。

主要能力

  • 多轮迭代推理:在置信度未达阈值时自动触发新一轮思考。
  • 自我批判:当 EnableSelfCritique 开启时,对低置信度决策进行反思。
  • 超时控制:通过 MaxThinkingTime 限制单次审议的最大耗时。
  • 运行时模式切换:支持在运行期间动态调整审议模式。

与 agent 包协同

deliberation 作为 agent 决策管线的前置环节:agent 在调用工具前 将任务提交给 Engine.Deliberate,获取经过推理验证的 Decision 后 再执行实际操作,从而在效率与决策质量之间取得平衡。

Index

Constants

This section is empty.

Variables

View Source
var ErrDecisionRejected = errors.New("decision rejected by human reviewer")

ErrDecisionRejected is returned when a human reviewer rejects a low-confidence decision.

Functions

This section is empty.

Types

type ApprovalOption added in v1.0.0

type ApprovalOption struct {
	ID    string
	Label string
}

ApprovalOption is a single choice presented to the reviewer.

type ApprovalRequest added in v1.0.0

type ApprovalRequest struct {
	Title       string
	Description string
	Data        any
	Options     []ApprovalOption
	Timeout     time.Duration
}

ApprovalRequest describes what the human needs to review.

type ApprovalResponse added in v1.0.0

type ApprovalResponse struct {
	Action   string // "approve", "reject", "modify"
	Feedback string
	Data     any
}

ApprovalResponse carries the human reviewer's decision.

type Decision

type Decision struct {
	Action     string         `json:"action"`
	Tool       string         `json:"tool,omitempty"`
	Parameters map[string]any `json:"parameters,omitempty"`
	Reasoning  string         `json:"reasoning"`
	Confidence float64        `json:"confidence"`
}

决定是审议后的最后决定。

type DeliberationConfig

type DeliberationConfig struct {
	Mode               DeliberationMode `json:"mode"`
	MaxThinkingTime    time.Duration    `json:"max_thinking_time"`
	MinConfidence      float64          `json:"min_confidence"`
	EnableSelfCritique bool             `json:"enable_self_critique"`
	MaxIterations      int              `json:"max_iterations"`
}

Deconfig 配置审议引擎 。

func DefaultDeliberationConfig

func DefaultDeliberationConfig() DeliberationConfig

默认De ReleaseConfig 返回默认配置 。

type DeliberationMode

type DeliberationMode string

审议 模式控制代理在行动前的理性.

const (
	ModeImmediate  DeliberationMode = "immediate"  // Direct tool execution
	ModeDeliberate DeliberationMode = "deliberate" // Full reasoning cycle
	ModeAdaptive   DeliberationMode = "adaptive"   // Context-dependent
)

type DeliberationResult

type DeliberationResult struct {
	TaskID          string           `json:"task_id"`
	Thoughts        []ThoughtProcess `json:"thoughts"`
	Decision        *Decision        `json:"decision"`
	TotalTime       time.Duration    `json:"total_time"`
	Iterations      int              `json:"iterations"`
	FinalConfidence float64          `json:"final_confidence"`
}

审议结果载有审议结果。

type Engine

type Engine struct {
	OnThought  func(ThoughtProcess)
	OnDecision func(Decision)
	// contains filtered or unexported fields
}

Engine provides deliberation capabilities.

func NewEngine

func NewEngine(config DeliberationConfig, reasoner Reasoner, logger *zap.Logger) *Engine

NewEngine创造了一个新的审议引擎.

func (*Engine) Deliberate

func (e *Engine) Deliberate(ctx context.Context, task Task) (*DeliberationResult, error)

Deliberate performs reasoning before action.

func (*Engine) GetMode

func (e *Engine) GetMode() DeliberationMode

GetMode returns the current deliberation mode.

func (*Engine) SetMode

func (e *Engine) SetMode(mode DeliberationMode)

设置模式在运行时更改审议模式 。

type HITLBridge added in v1.0.0

type HITLBridge struct {
	// contains filtered or unexported fields
}

HITLBridge connects deliberation to human approval.

func NewHITLBridge added in v1.0.0

func NewHITLBridge(engine *Engine, requester InterruptRequester, config HITLConfig, logger *zap.Logger) *HITLBridge

NewHITLBridge creates a bridge between the deliberation engine and human review.

func (*HITLBridge) DeliberateWithApproval added in v1.0.0

func (b *HITLBridge) DeliberateWithApproval(ctx context.Context, task Task) (*DeliberationResult, error)

DeliberateWithApproval runs deliberation and escalates to a human reviewer when the final confidence falls below the configured threshold.

type HITLConfig added in v1.0.0

type HITLConfig struct {
	Enabled             bool
	ConfidenceThreshold float64       // below this, escalate to human
	InterruptTimeout    time.Duration // how long to wait for human response
}

HITLConfig configures human-in-the-loop escalation.

func DefaultHITLConfig added in v1.0.0

func DefaultHITLConfig() HITLConfig

DefaultHITLConfig returns sensible defaults for HITL escalation.

type HITLInterruptAdapter added in v1.0.0

type HITLInterruptAdapter struct {
	// contains filtered or unexported fields
}

HITLInterruptAdapter adapts hitl.InterruptManager to the InterruptRequester interface. This is the only file in the deliberation package that imports agent/hitl, keeping the rest of the package loosely coupled.

func NewHITLInterruptAdapter added in v1.0.0

func NewHITLInterruptAdapter(manager *hitl.InterruptManager) *HITLInterruptAdapter

NewHITLInterruptAdapter wraps an InterruptManager for use with HITLBridge.

func (*HITLInterruptAdapter) RequestApproval added in v1.0.0

func (a *HITLInterruptAdapter) RequestApproval(ctx context.Context, opts ApprovalRequest) (*ApprovalResponse, error)

RequestApproval translates an ApprovalRequest into a hitl.InterruptOptions call and maps the hitl.Response back to an ApprovalResponse.

type InterruptRequester added in v1.0.0

type InterruptRequester interface {
	RequestApproval(ctx context.Context, opts ApprovalRequest) (*ApprovalResponse, error)
}

InterruptRequester is a local interface for HITL integration. Matches the subset of hitl.InterruptManager needed here. Using a local interface (§15) to keep deliberation loosely coupled.

type LLMReasoner added in v1.0.0

type LLMReasoner struct {
	// contains filtered or unexported fields
}

LLMReasoner implements Reasoner using an llm.Provider.

func NewLLMReasoner added in v1.0.0

func NewLLMReasoner(provider llm.Provider, model string, logger *zap.Logger) *LLMReasoner

NewLLMReasoner creates a new LLM-backed Reasoner.

func (*LLMReasoner) Think added in v1.0.0

func (r *LLMReasoner) Think(ctx context.Context, prompt string) (string, float64, error)

Think sends a reasoning prompt to the LLM and returns the response content along with a confidence score extracted from the response.

type Reasoner

type Reasoner interface {
	Think(ctx context.Context, prompt string) (string, float64, error)
}

基于LLM的推理的理性界面.

type Task

type Task struct {
	ID             string         `json:"id"`
	Description    string         `json:"description"`
	Goal           string         `json:"goal"`
	Context        map[string]any `json:"context,omitempty"`
	AvailableTools []string       `json:"available_tools,omitempty"`
	SuggestedTool  string         `json:"suggested_tool,omitempty"`
	Parameters     map[string]any `json:"parameters,omitempty"`
}

任务是一项需要审议的任务。

type ThoughtProcess

type ThoughtProcess struct {
	ID         string    `json:"id"`
	Step       int       `json:"step"`
	Type       string    `json:"type"` // understand, evaluate, plan, critique
	Content    string    `json:"content"`
	Confidence float64   `json:"confidence"`
	Timestamp  time.Time `json:"timestamp"`
}

ThoughtProcess代表了单一的推理步骤.

Jump to

Keyboard shortcuts

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