Documentation
¶
Index ¶
Constants ¶
This section is empty.
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"` // "text" 或 "image_url"
Text string `json:"text,omitempty"` // type="text"
ImageURL *ImageURL `json:"image_url,omitempty"` // type="image_url"
}
ContentPart 多模态内容片段
func ImagePartBase64 ¶
func ImagePartBase64(mediaType, base64Data string) ContentPart
ImagePartBase64 创建图片片段(通过 base64 数据)
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 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:"-"`
// tool 消息专用:关联到哪个 ToolCall
ToolCallID string `json:"tool_call_id,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) TextContent ¶
TextContent 统一获取纯文本内容 优先从 ContentParts 中提取所有文本片段拼接,否则返回 Content
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"`
}
ToolResult 工具执行结果(仅用于工具执行层内部传递,不作为 Message 字段)
func NewToolResult ¶
func NewToolResult(callID, content string, isError bool) ToolResult
NewToolResult 便捷构造一个结果条目
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"`
}
Click to show internal directories.
Click to hide internal directories.