Documentation
¶
Overview ¶
Package semantic 提供语义函数功能
本包实现 Semantic Functions:
- Prompt 函数:基于 Prompt 模板定义的函数
- Native 函数:Go 原生函数
- 函数组合:函数链式调用
- 函数注册:统一函数管理
设计借鉴:
- Semantic Kernel: Semantic Functions
- LangChain: Prompt Templates + Chains
- OpenAI: Function Calling
Index ¶
- func Invoke(ctx context.Context, name string, input map[string]any) (any, error)
- func Register(fn Function) error
- type BoolOutputParser
- type CompositeFunction
- type Function
- type FunctionType
- type JSONOutputParser
- type ListOutputParser
- type NativeFunction
- type NativeFunctionConfig
- type OutputParser
- type Registry
- type SemanticFunction
- type SemanticFunctionConfig
- type StringOutputParser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BoolOutputParser ¶
type BoolOutputParser struct{}
BoolOutputParser 布尔输出解析器
func (*BoolOutputParser) Parse ¶
func (p *BoolOutputParser) Parse(output string) (any, error)
Parse 解析输出
判定逻辑区分两类肯定词,避免此前 HasPrefix 造成的假阳性(如 "yesterday" 被判 true):
- ASCII 肯定词(true/yes/1):按非字母数字边界整词匹配首词, 即首词恰为该值,或该值后紧跟非字母数字字符(标点/空白),例如 "yes." / "true, ..."。
- CJK 肯定词(是/对/正确):中文无词边界,沿用前缀匹配以容忍 "是的" / "正确。" 等。
type CompositeFunction ¶
type CompositeFunction struct {
// contains filtered or unexported fields
}
CompositeFunction 组合函数 将多个函数组合成一个
func Chain ¶
func Chain(name, description string, functions ...Function) *CompositeFunction
Chain 创建函数链
func NewCompositeFunction ¶
func NewCompositeFunction(name, description string, functions ...Function) *CompositeFunction
NewCompositeFunction 创建组合函数
func (*CompositeFunction) Description ¶
func (f *CompositeFunction) Description() string
Description 返回函数描述
func (*CompositeFunction) InputSchema ¶
func (f *CompositeFunction) InputSchema() *llm.Schema
InputSchema 返回输入 Schema
func (*CompositeFunction) OutputSchema ¶
func (f *CompositeFunction) OutputSchema() *llm.Schema
OutputSchema 返回输出 Schema
type Function ¶
type Function interface {
// Name 函数名称
Name() string
// Description 函数描述
Description() string
// InputSchema 输入 Schema
InputSchema() *llm.Schema
// OutputSchema 输出 Schema
OutputSchema() *llm.Schema
// Invoke 调用函数
Invoke(ctx context.Context, input map[string]any) (any, error)
}
Function 语义函数接口
type FunctionType ¶
type FunctionType string
FunctionType 函数类型
const ( // FunctionTypeSemantic 语义函数(基于 Prompt) FunctionTypeSemantic FunctionType = "semantic" // FunctionTypeNative Native 函数 FunctionTypeNative FunctionType = "native" // FunctionTypeComposite 组合函数 FunctionTypeComposite FunctionType = "composite" )
type JSONOutputParser ¶
JSONOutputParser JSON 输出解析器
type ListOutputParser ¶
type ListOutputParser struct {
// Separator 分隔符
Separator string
}
ListOutputParser 列表输出解析器
type NativeFunction ¶
type NativeFunction struct {
// contains filtered or unexported fields
}
NativeFunction Native 函数 直接封装 Go 函数
func NewFunc ¶
func NewFunc[I, O any](name, description string, fn func(ctx context.Context, input I) (O, error)) *NativeFunction
NewFunc 从普通函数创建 Native 函数。
该便捷构造为保持简洁签名不返回 error,但绝不能吞掉底层 NewNativeFunction 的错误后 返回 nil(否则调用方对 nil 解引用会在后续 Invoke 处发生隐蔽 panic)。 因此当底层构造失败(如 name 为空)时,本函数 panic 并附带清晰的错误信息。 由泛型签名保证 fn 非 nil 且为函数类型,故唯一可能的失败为 name 为空。
func NewNativeFunction ¶
func NewNativeFunction(config NativeFunctionConfig) (*NativeFunction, error)
NewNativeFunction 创建 Native 函数
func (*NativeFunction) Description ¶
func (f *NativeFunction) Description() string
Description 返回函数描述
func (*NativeFunction) InputSchema ¶
func (f *NativeFunction) InputSchema() *llm.Schema
InputSchema 返回输入 Schema
func (*NativeFunction) OutputSchema ¶
func (f *NativeFunction) OutputSchema() *llm.Schema
OutputSchema 返回输出 Schema
type NativeFunctionConfig ¶
type NativeFunctionConfig struct {
Name string
Description string
Fn any
InputSchema *llm.Schema
OutputSchema *llm.Schema
}
NativeFunctionConfig Native 函数配置
type OutputParser ¶
OutputParser 输出解析器接口
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry 函数注册表
type SemanticFunction ¶
type SemanticFunction struct {
// contains filtered or unexported fields
}
SemanticFunction 语义函数 使用 Prompt 模板和 LLM 实现的函数
func NewSemanticFunction ¶
func NewSemanticFunction(config SemanticFunctionConfig) (*SemanticFunction, error)
NewSemanticFunction 创建语义函数
func (*SemanticFunction) Description ¶
func (f *SemanticFunction) Description() string
Description 返回函数描述
func (*SemanticFunction) InputSchema ¶
func (f *SemanticFunction) InputSchema() *llm.Schema
InputSchema 返回输入 Schema
func (*SemanticFunction) OutputSchema ¶
func (f *SemanticFunction) OutputSchema() *llm.Schema
OutputSchema 返回输出 Schema
type SemanticFunctionConfig ¶
type SemanticFunctionConfig struct {
// Name 函数名称
Name string
// Description 函数描述
Description string
// Prompt Prompt 模板
Prompt string
// SystemPrompt 系统提示
SystemPrompt string
// LLM LLM 提供者
LLM llm.Provider
// Model 模型名称
Model string
// Temperature 温度
Temperature float64
// MaxTokens 最大 token 数
MaxTokens int
// InputSchema 输入 Schema
InputSchema *llm.Schema
// OutputSchema 输出 Schema
OutputSchema *llm.Schema
// OutputParser 输出解析器
OutputParser OutputParser
}
SemanticFunctionConfig 语义函数配置