schema

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

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"`                // "text" 或 "image_url"
	Text     string    `json:"text,omitempty"`      // type="text"
	ImageURL *ImageURL `json:"image_url,omitempty"` // type="image_url"
}

ContentPart 多模态内容片段

func ImagePart

func ImagePart(url string) ContentPart

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

func ImagePartBase64

func ImagePartBase64(mediaType, base64Data string) ContentPart

ImagePartBase64 创建图片片段(通过 base64 数据)

func TextPart

func TextPart(text string) ContentPart

TextPart 创建文本片段

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

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 深拷贝 Clone 深拷贝

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

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

Jump to

Keyboard shortcuts

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