schema

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
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

func FormatMessages(messages []*Message) string

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 FilePart added in v1.1.0

func FilePart(url string) ContentPart

FilePart 创建文件片段(通过 URL)

func ImagePart

func ImagePart(url string) ContentPart

ImagePart 创建图片片段(通过 URL)

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)

func TextPart

func TextPart(text string) ContentPart

TextPart 创建文本片段

func VideoPart added in v1.1.0

func VideoPart(url string) ContentPart

VideoPart 创建视频片段(通过 URL)

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 FunctionCall struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

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 IndexItem added in v1.1.0

type IndexItem interface {
	GetID() string
	GetContent() string
	GetEmbedding() []float32
}

IndexItem 向量索引项接口 用于 HNSW 索引的泛化,不绑定具体数据模型

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

func AssistantMessage(content, reasoningContent string) *Message

AssistantMessage 返回一个role为assistant的信息

func SystemMessage

func SystemMessage(content string) *Message

SystemMessage 返回一个role为system的信息

func ToolResultsMessage

func ToolResultsMessage(results []ToolResult) []*Message

ToolResultsMessage 创建一组 tool 角色的消息,每个 ToolResult 对应一条独立消息

func UserMessage

func UserMessage(content string) *Message

UserMessage 返回一个role为user的信息

func UserMultimodalMessage

func UserMultimodalMessage(parts ...ContentPart) *Message

UserMultimodalMessage 创建包含图片/文本混合内容的用户消息

msg := UserMultimodalMessage(
    TextPart("请描述这张图片"),
    ImagePartBase64("image/png", screenshotBase64),
)

func (*Message) Clone

func (m *Message) Clone() Message

Clone 深拷贝

func (*Message) HasOutputAudio added in v1.1.0

func (m *Message) HasOutputAudio() bool

HasOutputAudio 是否包含输出音频

func (*Message) HasOutputImages added in v1.1.0

func (m *Message) HasOutputImages() bool

HasOutputImages 是否包含输出图片

func (*Message) ImageCount

func (m *Message) ImageCount() int

ImageCount 返回消息中的图片数量

func (*Message) IsMultimodal

func (m *Message) IsMultimodal() bool

IsMultimodal 是否包含多模态内容

func (*Message) TextContent

func (m *Message) TextContent() string

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 RoleType

type RoleType string
const (
	AssistantRole RoleType = "assistant"
	UserRole      RoleType = "user"
	SystemRole    RoleType = "system"
	ToolRole      RoleType = "tool"
)

type StreamReader

type StreamReader struct {
	Usage Usage
	// contains filtered or unexported fields
}

StreamReader 流式消息读取器

func NewStreamReader

func NewStreamReader() *StreamReader

NewStreamReader 创建默认带缓冲的流读取器

func NewStreamReaderWithBuffer

func NewStreamReaderWithBuffer(bufSize int) *StreamReader

NewStreamReaderWithBuffer 带缓冲大小

func (*StreamReader) Close

func (sr *StreamReader) Close()

Close 安全关闭

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 表示被取消

func (*StreamReader) SetError

func (sr *StreamReader) SetError(err error)

SetError 内部设置错误

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"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL