Documentation
¶
Index ¶
- Constants
- func FormatMessages(messages []*Message) string
- func PrintMessages(messages []*Message)
- type ContentPart
- func AudioPart(format, base64Data string) ContentPart
- func FilePart(url string) ContentPart
- func ImagePart(url string) ContentPart
- func ImagePartBase64(mediaType, base64Data string) ContentPart
- func InlineDataPart(mediaType, base64Data string) ContentPart
- func TextPart(text string) ContentPart
- func VideoPart(url string) ContentPart
- type Document
- type FunctionCall
- type ImageURL
- type IndexItem
- type InlineData
- type InputAudio
- type MediaURL
- type Message
- type OutputAudio
- type OutputImage
- type RoleType
- type StreamReader
- type Tool
- type ToolCall
- type ToolResult
- type ToolResultContent
- type Usage
Constants ¶
const ( ContentTypeText = "text" ContentTypeImageURL = "image_url" ContentTypeInputAudio = "input_audio" ContentTypeVideoURL = "video_url" ContentTypeFileURL = "file_url" ContentTypeInlineData = "inline_data" )
内容类型常量
Variables ¶
This section is empty.
Functions ¶
func FormatMessages ¶
FormatMessages 标准化格式化 []*Message 为可读字符串
func PrintMessages ¶
func PrintMessages(messages []*Message)
Types ¶
type ContentPart ¶
type ContentPart struct {
Type string `json:"type"` // 内容类型,见 ContentType* 常量
Text string `json:"text,omitempty"` // type="text"
ImageURL *ImageURL `json:"image_url,omitempty"` // type="image_url"
InputAudio *InputAudio `json:"input_audio,omitempty"` // type="input_audio"
VideoURL *MediaURL `json:"video_url,omitempty"` // type="video_url"
FileURL *MediaURL `json:"file_url,omitempty"` // type="file_url"
InlineData *InlineData `json:"inline_data,omitempty"` // type="inline_data"
}
ContentPart 多模态内容片段
func AudioPart ¶ added in v1.1.0
func AudioPart(format, base64Data string) ContentPart
AudioPart 创建音频片段(通过 base64 数据)
func ImagePartBase64 ¶
func ImagePartBase64(mediaType, base64Data string) ContentPart
ImagePartBase64 创建图片片段(通过 base64 数据)
func InlineDataPart ¶ added in v1.1.0
func InlineDataPart(mediaType, base64Data string) ContentPart
InlineDataPart 创建内联数据片段(通用 base64)
type Document ¶ added in v1.1.0
type Document struct {
ID string `json:"id"` // 唯一标识
Content string `json:"content"` // 文本内容
MetaData map[string]any `json:"meta_data,omitempty"` // 开放元数据
}
Document 通用文档类型,用于非对话数据存储和检索 适用场景:用户画像、企业知识库、产品文档等
type FunctionCall ¶
type ImageURL ¶
type ImageURL struct {
URL string `json:"url"` // http(s)://... 或 data:image/png;base64,...
Detail string `json:"detail,omitempty"` // "low", "high", "auto"
}
ImageURL 图片信息
type InlineData ¶ added in v1.1.0
type InlineData struct {
MediaType string `json:"media_type"` // MIME 类型:"image/png", "audio/mp3", "video/mp4", "application/pdf"
Data string `json:"data"` // base64 编码的数据
}
InlineData 内联二进制数据(通用 base64 编码)
type InputAudio ¶ added in v1.1.0
type InputAudio struct {
Data string `json:"data"` // base64 编码的音频数据
Format string `json:"format"` // 音频格式:"wav", "mp3"
}
InputAudio 用户输入的音频数据
type MediaURL ¶ added in v1.1.0
type MediaURL struct {
URL string `json:"url"` // http(s)://... 或 data:xxx;base64,...
}
MediaURL 通用媒体资源引用(视频、文件等)
type Message ¶
type Message struct {
Role RoleType `json:"role"`
Content string `json:"content,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
// 消息发送者的名称(可选)
Name string `json:"name,omitempty"`
// 当设置为 true 时,表示这条消息是未完成的,模型需要继续生成这条消息的剩余内容。(可选)
Partial bool `json:"partial,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
// 多模态内容:当非空时,序列化由 adapter 处理,Content 字段被忽略
// JSON 序列化标记为 "-",各 provider adapter 自行转换为对应格式
ContentParts []ContentPart `json:"-"`
// 输出侧多模态:模型生成的非文本内容
OutputImages []OutputImage `json:"output_images,omitempty"`
OutputAudio *OutputAudio `json:"output_audio,omitempty"`
// tool 消息专用:关联到哪个 ToolCall
ToolCallID string `json:"tool_call_id,omitempty"`
// tool 消息专用:标记工具执行是否出错(Anthropic API 原生支持 is_error)
IsError bool `json:"is_error,omitempty"`
Usage *Usage `json:"-"`
}
func AssistantMessage ¶
AssistantMessage 返回一个role为assistant的信息
func ToolResultsMessage ¶
func ToolResultsMessage(results []ToolResult) []*Message
ToolResultsMessage 创建一组 tool 角色的消息,每个 ToolResult 对应一条独立消息
func UserMultimodalMessage ¶
func UserMultimodalMessage(parts ...ContentPart) *Message
UserMultimodalMessage 创建包含图片/文本混合内容的用户消息
msg := UserMultimodalMessage(
TextPart("请描述这张图片"),
ImagePartBase64("image/png", screenshotBase64),
)
func (*Message) HasOutputAudio ¶ added in v1.1.0
HasOutputAudio 是否包含输出音频
func (*Message) HasOutputImages ¶ added in v1.1.0
HasOutputImages 是否包含输出图片
func (*Message) TextContent ¶
TextContent 统一获取纯文本内容 优先从 ContentParts 中提取所有文本片段拼接,否则返回 Content
type OutputAudio ¶ added in v1.1.0
type OutputAudio struct {
Data string `json:"data"` // base64 编码的音频数据
Format string `json:"format"` // 音频格式:"mp3", "wav", "opus"
}
OutputAudio 模型输出的音频
type OutputImage ¶ added in v1.1.0
type OutputImage struct {
URL string `json:"url,omitempty"` // 图片 URL
Base64 string `json:"base64,omitempty"` // base64 编码的图片数据
RevisedPrompt string `json:"revised_prompt,omitempty"` // 模型修订后的 prompt(如 DALL·E)
}
OutputImage 模型输出的图片
type StreamReader ¶
type StreamReader struct {
Usage Usage
// contains filtered or unexported fields
}
StreamReader 流式消息读取器
func NewStreamReaderWithBuffer ¶
func NewStreamReaderWithBuffer(bufSize int) *StreamReader
NewStreamReaderWithBuffer 带缓冲大小
func (*StreamReader) Recv ¶
func (sr *StreamReader) Recv() (*Message, error)
Recv 从stream流中接收一个值。
for {
msg, err := reader.Recv()
if errors.Is(err, io.EOF){
break
}
print(msg.Content)
}
Recv 从流中接收一条消息,符合 Go 标准流式读取风格
func (*StreamReader) Send ¶
func (sr *StreamReader) Send(msg Message)
func (*StreamReader) SendWithContext ¶
func (sr *StreamReader) SendWithContext(ctx context.Context, msg Message) bool
SendWithContext 带上下文的发送,可被取消或超时实现超时控制 返回 true 表示发送成功,false 表示被取消
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters any `json:"parameters"` // JSON Schema 对象
}
Tool 统一工具定义
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Index int `json:"index,omitempty"`
Function FunctionCall `json:"function"`
}
type ToolResult ¶
type ToolResult struct {
CallID string `json:"call_id"`
Content string `json:"content"`
IsError bool `json:"is_error"`
ContentParts []ContentPart `json:"content_parts,omitempty"` // 多模态工具结果
}
ToolResult 工具执行结果(仅用于工具执行层内部传递,不作为 Message 字段)
func NewToolResult ¶
func NewToolResult(callID, content string, isError bool) ToolResult
NewToolResult 便捷构造一个结果条目
type ToolResultContent ¶ added in v1.1.0
type ToolResultContent struct {
Content string // 文本内容
ContentParts []ContentPart // 多模态内容片段
}
ToolResultContent 工具返回多模态结果时使用的结构体 工具 Handler 返回此类型时,Execute() 会将其 ContentParts 填充到 ToolResult 中
用法:
func MyTool(ctx context.Context, args map[string]any) (any, error) {
return &schema.ToolResultContent{
Content: "截图已保存",
ContentParts: []schema.ContentPart{
schema.TextPart("截图已保存"),
schema.ImagePartBase64("image/png", base64Data),
},
}, nil
}
type Usage ¶
type Usage struct {
PromptTokens uint64 `json:"prompt_tokens"`
CompletionTokens uint64 `json:"completion_tokens"`
TotalTokens uint64 `json:"total_tokens"`
CachedTokens uint64 `json:"cached_tokens"`
PromptTokensDetails struct {
CachedTokens uint64 `json:"cached_tokens"`
} `json:"prompt_tokens_details"`
}