GoChat API参考¶
该文档根据 godoc 规范生成,涵盖了 pkg 目录下核心对外暴露的包、接口、函数及结构体的使用说明。
包 core (核心类型)¶
包含整个项目的基础接口约束、数据载体及领域模型。
接口 Client¶
大语言模型的基础交互接口。
type Client interface {
// Chat 执行阻塞式的聊天请求,一次性返回所有的信息。
Chat(ctx context.Context, messages []Message, opts ...Option) (*Response, error)
// ChatStream 执行流式聊天请求,返回可供迭代的 Stream 对象。
ChatStream(ctx context.Context, messages []Message, opts ...Option) (*Stream, error)
}
结构体 Message¶
代表了对话上下文的原子级结构。
type Message struct {
Role string `json:"role"` // 角色: system, user, assistant, tool
Content []ContentBlock `json:"content"` // 多模态内容块切片
ToolCalls []ToolCall `json:"tool_calls"` // assistant发起的函数调用请求
ToolCallID string `json:"tool_call_id"` // 用于role为tool响应的回调ID
}
// 快速创建单一文本块消息
func NewUserMessage(text string) Message
func NewSystemMessage(text string) Message
// 获取内部所有的文本拼接内容
func (m Message) TextContent() string
结构体 ContentBlock¶
type ContentBlock struct {
Type ContentType // "text", "image", "file", "thinking"
Text string
MediaType string // MIME 类型,如 "image/png"
Data string // 资源的 Base64 编码数据
}
结构体 Response & Stream¶
type Response struct {
ID string
Model string
Content string // 合并后的最终文本
ReasoningContent string // 模型的思考(CoT)过程
Message Message // 返回的结构化消息
FinishReason string // 停止原因,如 "stop"
Usage *Usage // Token消耗情况
ToolCalls []ToolCall // 期望调用的工具
}
type Stream struct { ... }
// Next 驱动迭代推进,返回 false 表示迭代结束或发生错误。
func (s *Stream) Next() bool
// Event 获取当前游标指向的流事件对象。
func (s *Stream) Event() StreamEvent
// Close 关闭底层响应流避免连接泄漏。
func (s *Stream) Close() error
// Usage 如果流结束,获取 Token 的总耗费。
func (s *Stream) Usage() *Usage
函数修饰符 Option¶
func WithModel(model string) Option
func WithTemperature(t float64) Option
func WithMaxTokens(n int) Option
func WithTools(tools ...Tool) Option
func WithSystemPrompt(prompt string) Option
func WithThinking(budget int) Option
包 client (LLM客户端)¶
该目录下的子包分别为各个特定的厂商提供了 core.Client 的实现。
client/openai¶
支持所有兼容标准 OpenAI 协议(/v1/chat/completions)的模型端点。
client/anthropic¶
高度集成了 Anthropic 特有的 API 机制与版本请求头,原生地支持其独有的思考模型协议及图像传输标准。
client/ollama¶
连接本地部署的 Ollama 服务,内部自适应处理了其非流式模式下仍返回流式格式(NDJSON)的独有设计。
(其它如 azureopenai、deepseek、qwen 包也提供相同的抽象构造入口。)
包 embedding (向量化模型)¶
提供文本嵌入接口、本地 ONNX 加载引擎、分词器和动态并发调度的处理器。
接口 Provider & MultimodalProvider¶
type Provider interface {
Embed(ctx context.Context, texts []string) ([][]float32, error)
Dimension() int // 获取返回的特征维度
}
type MultimodalProvider interface {
Provider
EmbedImages(ctx context.Context, images [][]byte) ([][]float32, error)
}
工厂构造方法¶
// 通过模型文件路径创建模型提供商,自动识别 BERT、BGE、Sentence-BERT 类型
func NewProvider(modelPath string) (Provider, error)
// 便利化工厂方法,若本地不存在会自动从 HF 镜像源流式下载模型并进行初始化
func WithBEG(modelName, modelPath string) (Provider, error)
func WithBERT(modelName, modelPath string) (Provider, error)
func WithCLIP(modelName, modelPath string) (MultimodalProvider, error)
BatchProcessor¶
通过内存级的文本哈希缓存、并发管道分发策略实现海量文本的大吞吐量 Embedding 计算。
func NewBatchProcessor(provider Provider, options BatchOptions) *BatchProcessor
// 携带进度条回调执行批处理操作
func (bp *BatchProcessor) ProcessWithProgress(ctx context.Context, texts []string, callback ProgressCallback) ([][]float32, error)
包 pipeline (工作流引擎)¶
提供用于大模型应用的高阶流程调度架构,彻底支持强类型安全泛型。
Pipeline[T]¶
核心的管线驱动机。
type Pipeline[T any] struct { ... }
// 初始化类型安全的 Pipeline
func New[T any]() *Pipeline[T]
// 组装处理步骤
func (p *Pipeline[T]) AddStep(step Step[T]) *Pipeline[T]
// 开始执行,阻塞并在其中一个失败时快速返回
func (p *Pipeline[T]) Execute(ctx context.Context, state T) error
接口 Step[T] & Hook[T]¶
// 所有的处理组件必须实现该接口
type Step[T any] interface {
Name() string
Execute(ctx context.Context, state T) error
}
// 拦截生命周期进行 AOP 监控
type Hook[T any] interface {
OnStepStart(ctx context.Context, step Step[T], state T)
OnStepError(ctx context.Context, step Step[T], state T, err error)
OnStepComplete(ctx context.Context, step Step[T], state T)
}
控制流¶
支持流程控制分支及终止跳出的辅助内置 Step。