Documentation
¶
Overview ¶
Package parsers defines constants used for parsing agent outputs, particularly for ReAct (Reasoning and Acting) pattern and other reasoning frameworks.
Index ¶
- Constants
- Variables
- type BaseOutputParser
- type BooleanOutputParser
- type ChainOutputParser
- type EnumOutputParser
- type FieldSchema
- type JSONOutputParser
- type ListOutputParser
- type OutputParser
- type ReActOutput
- type ReActOutputParser
- func (p *ReActOutputParser) GetFormatInstructions() string
- func (p *ReActOutputParser) Parse(ctx context.Context, text string) (*ReActOutput, error)
- func (p *ReActOutputParser) ParseWithRetry(ctx context.Context, text string, maxRetries int) (*ReActOutput, error)
- func (p *ReActOutputParser) Validate(parsed *ReActOutput) error
- type RegexOutputParser
- type StructuredOutputParser
Constants ¶
const ( // FieldThought represents the reasoning/thinking step FieldThought = "thought" // FieldAction represents the action to take FieldAction = "action" // FieldActionInput represents the input parameters for the action FieldActionInput = "action_input" // FieldObservation represents the result of the action FieldObservation = "observation" // FieldFinalAnswer represents the final answer to the query FieldFinalAnswer = "final_answer" // FieldAnswer represents a general answer field FieldAnswer = "answer" )
ReAct Pattern Field Names define the fields used in ReAct reasoning.
const ( // MarkerThought is the prefix for thought sections MarkerThought = "Thought:" // MarkerAction is the prefix for action sections MarkerAction = "Action:" // MarkerActionInput is the prefix for action input sections MarkerActionInput = "Action Input:" // MarkerObservation is the prefix for observation sections MarkerObservation = "Observation:" // MarkerFinalAnswer is the prefix for final answer sections MarkerFinalAnswer = "Final Answer:" )
ReAct Pattern Markers define the text markers used to identify ReAct components.
const ( // MarkerQuestion represents a question marker MarkerQuestion = "Question:" // MarkerPlan represents a planning marker MarkerPlan = "Plan:" // MarkerStep represents a step marker MarkerStep = "Step:" // MarkerReasoning represents a reasoning marker MarkerReasoning = "Reasoning:" // MarkerConclusion represents a conclusion marker MarkerConclusion = "Conclusion:" )
Alternative Pattern Markers provide variations commonly seen in outputs.
const ( // FieldReasoning represents reasoning steps FieldReasoning = "reasoning" // FieldSteps represents sequential steps FieldSteps = "steps" // FieldConclusion represents the conclusion FieldConclusion = "conclusion" // FieldConfidence represents confidence score FieldConfidence = "confidence" )
Chain-of-Thought (CoT) Pattern Constants
const ( // FieldBranch represents a reasoning branch FieldBranch = "branch" // FieldScore represents a score/evaluation FieldScore = "score" // FieldPath represents a solution path FieldPath = "path" // FieldEvaluation represents an evaluation result FieldEvaluation = "evaluation" )
Tree-of-Thought (ToT) Pattern Constants
const ( // FieldCritique represents a self-critique FieldCritique = "critique" // FieldImprovement represents an improvement suggestion FieldImprovement = "improvement" // FieldRevision represents a revised output FieldRevision = "revision" // FieldFeedback represents feedback FieldFeedback = "feedback" )
Self-Criticism Pattern Constants
const ( // ErrTypeInvalidFormat indicates invalid format ErrTypeInvalidFormat = "invalid_format" // ErrTypeMissingField indicates a required field is missing ErrTypeMissingField = "missing_field" // ErrTypeInvalidJSON indicates invalid JSON ErrTypeInvalidJSON = "invalid_json" // ErrTypeUnexpectedStructure indicates unexpected structure ErrTypeUnexpectedStructure = "unexpected_structure" )
Parsing Error Types
const ( // FormatJSON represents JSON output format FormatJSON = "json" // FormatText represents plain text format FormatText = "text" // FormatMarkdown represents markdown format FormatMarkdown = "markdown" // FormatStructured represents structured format FormatStructured = "structured" )
Output Format Types
const ( // ModeStrict indicates strict parsing (fail on errors) ModeStrict = "strict" // ModeLenient indicates lenient parsing (best effort) ModeLenient = "lenient" // ModeAuto indicates automatic mode detection ModeAuto = "auto" )
Parsing Modes
Variables ¶
Functions ¶
This section is empty.
Types ¶
type BaseOutputParser ¶
type BaseOutputParser[T any] struct { // contains filtered or unexported fields }
BaseOutputParser 提供 OutputParser 的基础实现
func NewBaseOutputParser ¶
func NewBaseOutputParser[T any]() *BaseOutputParser[T]
NewBaseOutputParser 创建基础输出解析器
func (*BaseOutputParser[T]) GetFormatInstructions ¶
func (p *BaseOutputParser[T]) GetFormatInstructions() string
GetFormatInstructions 需要由子类实现
func (*BaseOutputParser[T]) Parse ¶
func (p *BaseOutputParser[T]) Parse(ctx context.Context, text string) (T, error)
Parse 需要由子类实现
func (*BaseOutputParser[T]) ParseWithPrompt ¶
func (p *BaseOutputParser[T]) ParseWithPrompt(ctx context.Context, text, prompt string) (T, error)
ParseWithPrompt 默认实现:忽略 prompt,直接调用 Parse
type BooleanOutputParser ¶
type BooleanOutputParser struct {
*BaseOutputParser[bool]
// contains filtered or unexported fields
}
BooleanOutputParser 布尔输出解析器
解析是/否类型的输出
func NewBooleanOutputParser ¶
func NewBooleanOutputParser() *BooleanOutputParser
NewBooleanOutputParser 创建布尔输出解析器
func (*BooleanOutputParser) GetFormatInstructions ¶
func (p *BooleanOutputParser) GetFormatInstructions() string
GetFormatInstructions 获取格式化指令
type ChainOutputParser ¶
type ChainOutputParser[T any] struct { // contains filtered or unexported fields }
ChainOutputParser 链式输出解析器
尝试多个解析器,使用第一个成功的
func NewChainOutputParser ¶
func NewChainOutputParser[T any](parsers ...OutputParser[T]) *ChainOutputParser[T]
NewChainOutputParser 创建链式输出解析器
func (*ChainOutputParser[T]) GetFormatInstructions ¶
func (p *ChainOutputParser[T]) GetFormatInstructions() string
GetFormatInstructions 获取格式化指令
func (*ChainOutputParser[T]) Parse ¶
func (p *ChainOutputParser[T]) Parse(ctx context.Context, text string) (T, error)
Parse 尝试所有解析器
func (*ChainOutputParser[T]) ParseWithPrompt ¶
func (p *ChainOutputParser[T]) ParseWithPrompt(ctx context.Context, text, prompt string) (T, error)
ParseWithPrompt 带提示的解析
type EnumOutputParser ¶
type EnumOutputParser struct {
*BaseOutputParser[string]
// contains filtered or unexported fields
}
EnumOutputParser 枚举输出解析器
限制输出必须是预定义的枚举值之一
func NewEnumOutputParser ¶
func NewEnumOutputParser(allowedValues []string, caseSensitive bool) *EnumOutputParser
NewEnumOutputParser 创建枚举输出解析器
func (*EnumOutputParser) GetFormatInstructions ¶
func (p *EnumOutputParser) GetFormatInstructions() string
GetFormatInstructions 获取格式化指令
type FieldSchema ¶
type FieldSchema struct {
Name string // 字段名称
Type string // 字段类型
Description string // 字段描述
Required bool // 是否必需
}
FieldSchema 字段模式定义
type JSONOutputParser ¶
type JSONOutputParser[T any] struct { *BaseOutputParser[T] // contains filtered or unexported fields }
JSONOutputParser JSON 输出解析器
解析 LLM 输出中的 JSON 内容
func NewJSONOutputParser ¶
func NewJSONOutputParser[T any](strict bool) *JSONOutputParser[T]
NewJSONOutputParser 创建 JSON 输出解析器
func (*JSONOutputParser[T]) GetFormatInstructions ¶
func (p *JSONOutputParser[T]) GetFormatInstructions() string
GetFormatInstructions 获取格式化指令
type ListOutputParser ¶
type ListOutputParser struct {
*BaseOutputParser[[]string]
// contains filtered or unexported fields
}
ListOutputParser 列表输出解析器
解析列表格式的输出(如逗号分隔、换行分隔等)
func NewListOutputParser ¶
func NewListOutputParser(separator string) *ListOutputParser
NewListOutputParser 创建列表输出解析器
func (*ListOutputParser) GetFormatInstructions ¶
func (p *ListOutputParser) GetFormatInstructions() string
GetFormatInstructions 获取格式化指令
type OutputParser ¶
type OutputParser[T any] interface { // Parse 解析文本输出为结构化数据 Parse(ctx context.Context, text string) (T, error) // ParseWithPrompt 带提示信息的解析(用于错误恢复) ParseWithPrompt(ctx context.Context, text, prompt string) (T, error) // GetFormatInstructions 获取格式化指令 // 这些指令会添加到 prompt 中,告诉 LLM 如何格式化输出 GetFormatInstructions() string // GetType 获取输出类型描述 GetType() string }
OutputParser 定义输出解析器接口
借鉴 LangChain 的 OutputParser 设计,提供结构化的 LLM 输出解析 泛型参数 T 指定输出类型
type ReActOutput ¶
type ReActOutput struct {
FinalAnswer string `json:"final_answer,omitempty"`
Thought string `json:"thought,omitempty"`
Action string `json:"action,omitempty"`
ActionInput map[string]interface{} `json:"action_input,omitempty"`
}
ReActOutput ReAct 解析器的输出结构
type ReActOutputParser ¶
type ReActOutputParser struct {
*BaseOutputParser[*ReActOutput]
// contains filtered or unexported fields
}
ReActOutputParser 解析 ReAct Agent 的输出
解析格式:
Thought: <思考内容> Action: <工具名称> Action Input: <JSON 格式的输入> 或 Final Answer: <最终答案>
func NewReActOutputParser ¶
func NewReActOutputParser() *ReActOutputParser
NewReActOutputParser 创建 ReAct 输出解析器
func (*ReActOutputParser) GetFormatInstructions ¶
func (p *ReActOutputParser) GetFormatInstructions() string
GetFormatInstructions 返回格式说明
func (*ReActOutputParser) Parse ¶
func (p *ReActOutputParser) Parse(ctx context.Context, text string) (*ReActOutput, error)
Parse 解析 ReAct 输出
func (*ReActOutputParser) ParseWithRetry ¶
func (p *ReActOutputParser) ParseWithRetry(ctx context.Context, text string, maxRetries int) (*ReActOutput, error)
ParseWithRetry 带重试的解析
func (*ReActOutputParser) Validate ¶
func (p *ReActOutputParser) Validate(parsed *ReActOutput) error
Validate 验证解析结果
type RegexOutputParser ¶
type RegexOutputParser struct {
*BaseOutputParser[map[string]string]
// contains filtered or unexported fields
}
RegexOutputParser 正则表达式输出解析器
使用正则表达式提取输出
func NewRegexOutputParser ¶
func NewRegexOutputParser(patterns map[string]string) *RegexOutputParser
NewRegexOutputParser 创建正则表达式输出解析器
func (*RegexOutputParser) GetFormatInstructions ¶
func (p *RegexOutputParser) GetFormatInstructions() string
GetFormatInstructions 获取格式化指令
type StructuredOutputParser ¶
type StructuredOutputParser[T any] struct { *BaseOutputParser[T] // contains filtered or unexported fields }
StructuredOutputParser 结构化输出解析器
支持自定义字段的结构化解析
func NewStructuredOutputParser ¶
func NewStructuredOutputParser[T any](schema map[string]FieldSchema) *StructuredOutputParser[T]
NewStructuredOutputParser 创建结构化输出解析器
func (*StructuredOutputParser[T]) GetFormatInstructions ¶
func (p *StructuredOutputParser[T]) GetFormatInstructions() string
GetFormatInstructions 获取格式化指令