Files
code-utils/service/ai/provider.go
2026-08-15 17:18:00 +08:00

34 lines
975 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package ai 定义 AI 聊天 Provider 抽象与工厂:
// 上层只依赖 Provider 接口,通过 New 按设置创建讯飞星火 Lite 或 DeepSeek 实例。
package ai
import "context"
// Message 是一条对话消息role: system | user | assistant
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
// Chunk 是流式返回的一段增量内容Err 非空表示流异常中止。
// Usage 在流结束时可选附带 token 用量(若服务商提供)。
type Chunk struct {
Content string
Err error
Usage *Usage
}
// Usage 一次调用的 token 用量。
type Usage struct {
PromptTokens int64
CompletionTokens int64
Estimated bool
}
// Provider 是 AI 服务商的统一抽象。
type Provider interface {
Name() string
// ChatStream 发起流式对话,返回增量内容通道;通道关闭即流结束。
ChatStream(ctx context.Context, messages []Message) (<-chan Chunk, error)
}