schema

package
v0.9.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: Apache-2.0 Imports: 29 Imported by: 413

Documentation

Overview

Package schema defines core data structures and utilities for messages, streams, and tool schemas used across Eino.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoValue = errors.New("no value")

ErrNoValue is used during StreamReaderWithConvert to skip a streamItem, excluding it from the converted stream. e.g.

outStream = schema.StreamReaderWithConvert(s,

func(src string) (string, error) {
	if len(src) == 0 {
		return nil, schema.ErrNoValue
	}

	return src.Message, nil
})

outStream will filter out the empty string.

DO NOT use it under other circumstances.

View Source
var ErrRecvAfterClosed = errors.New("recv after stream closed")

ErrRecvAfterClosed indicates that StreamReader.Recv was unexpectedly called after StreamReader.Close. This error should not occur during normal use of StreamReader.Recv. If it does, please check your application code.

Functions

func GetSourceName added in v0.3.36

func GetSourceName(err error) (string, bool)

GetSourceName extracts the source stream name from a SourceEOF error. It returns the source name and a boolean indicating whether the error was a SourceEOF. If the error is not a SourceEOF, it returns an empty string and false.

func Pipe

func Pipe[T any](cap int) (*StreamReader[T], *StreamWriter[T])

Pipe creates a new stream with the given capacity that represented with StreamWriter and StreamReader. The capacity is the maximum number of items that can be buffered in the stream. e.g.

sr, sw := schema.Pipe[string](3)
go func() { // send data
	defer sw.Close()
	for i := 0; i < 10; i++ {
		sw.Send(i, nil)
	}
}

defer sr.Close()
for chunk, err := sr.Recv() {
	if errors.Is(err, io.EOF) {
		break
	}
	fmt.Println(chunk)
}

func Register added in v0.5.4

func Register[T any]()

Register registers a type for serialization. This is required for any type you intend to persist in a graph or ADK checkpoint. It automatically determines the type name and is the recommended method for registering new types.

It is recommended to call this in an `init()` function in the file where the type is declared.

What to Register:

  • Top-level types used as state (e.g., structs).
  • Concrete types that are assigned to interface fields.

What NOT to Register:

  • Struct fields with concrete types (e.g., `string`, `int`, other structs). These are inferred via reflection.

Serialization Rules:

The serialization behavior is based on Go's standard `encoding/gob` package. See https://pkg.go.dev/encoding/gob for detailed rules.

  • Only exported struct fields are serialized.
  • Functions and channels are not supported and will be ignored.

This function panics if registration fails.

func RegisterName added in v0.5.4

func RegisterName[T any](name string)

RegisterName registers a type with a specific name for serialization. This is required for any type you intend to persist in a graph or ADK checkpoint. Use this function to maintain backward compatibility by mapping a type to a previously used name. For new types, `Register` is preferred.

It is recommended to call this in an `init()` function in the file where the type is declared.

What to Register:

  • Top-level types used as state (e.g., structs).
  • Concrete types that are assigned to interface fields.

What NOT to Register:

  • Struct fields with concrete types (e.g., `string`, `int`, other structs). These are inferred via reflection.

Serialization Rules:

The serialization behavior is based on Go's standard `encoding/gob` package. See https://pkg.go.dev/encoding/gob for detailed rules.

  • Only exported struct fields are serialized.
  • Functions and channels are not supported and will be ignored.

This function panics if registration fails.

Types

type AgenticAllowedToolChoice

type AgenticAllowedToolChoice struct {
	// Tools is the list of allowed tools for the model to call.
	// Optional.
	Tools []*AllowedTool
}

AgenticAllowedToolChoice specifies a list of allowed tools for the model.

type AgenticForcedToolChoice

type AgenticForcedToolChoice struct {
	// Tools is the list of tools that the model must call.
	// Optional.
	Tools []*AllowedTool
}

AgenticForcedToolChoice specifies a list of tools that the model must call.

type AgenticMessage

type AgenticMessage struct {
	// Role is the message role.
	Role AgenticRoleType

	// ContentBlocks is the list of content blocks.
	ContentBlocks []*ContentBlock

	// ResponseMeta is the response metadata.
	ResponseMeta *AgenticResponseMeta

	// Extra is the additional information.
	Extra map[string]any
}

func ConcatAgenticMessages

func ConcatAgenticMessages(msgs []*AgenticMessage) (*AgenticMessage, error)

ConcatAgenticMessages concatenates a list of AgenticMessage chunks into a single AgenticMessage.

func ConcatAgenticMessagesArray

func ConcatAgenticMessagesArray(mas [][]*AgenticMessage) ([]*AgenticMessage, error)

ConcatAgenticMessagesArray concatenates multiple streams of AgenticMessage into a single slice of AgenticMessage.

func FunctionToolResultAgenticMessage

func FunctionToolResultAgenticMessage(callID, name, result string) *AgenticMessage

FunctionToolResultAgenticMessage represents a function tool result message with AgenticRoleType "user".

func SystemAgenticMessage

func SystemAgenticMessage(text string) *AgenticMessage

SystemAgenticMessage represents a message with AgenticRoleType "system".

func UserAgenticMessage

func UserAgenticMessage(text string) *AgenticMessage

UserAgenticMessage represents a message with AgenticRoleType "user".

func (*AgenticMessage) Format

func (m *AgenticMessage) Format(_ context.Context, vs map[string]any, formatType FormatType) ([]*AgenticMessage, error)

Format returns the agentic messages after rendering by the given formatType. It formats only the user input fields (UserInputText, UserInputImage, UserInputAudio, UserInputVideo, UserInputFile). e.g.

msg := &schema.AgenticMessage{
	Role: schema.AgenticRoleTypeUser,
	ContentBlocks: []*schema.ContentBlock{
		{Type: schema.ContentBlockTypeUserInputText, UserInputText: &schema.UserInputText{Text: "hello {name}"}},
	},
}
msgs, err := msg.Format(ctx, map[string]any{"name": "eino"}, schema.FString)
// msgs[0].ContentBlocks[0].UserInputText.Text will be "hello eino"

func (*AgenticMessage) String

func (m *AgenticMessage) String() string

String returns the string representation of AgenticMessage.

type AgenticMessagesTemplate

type AgenticMessagesTemplate interface {
	Format(ctx context.Context, vs map[string]any, formatType FormatType) ([]*AgenticMessage, error)
}

AgenticMessagesTemplate is the interface for agentic messages template. It's used to render a template to a list of agentic messages. e.g.

chatTemplate := prompt.FromAgenticMessages(
	&schema.AgenticMessage{
		Role: schema.AgenticRoleTypeSystem,
		ContentBlocks: []*schema.ContentBlock{
			{Type: schema.ContentBlockTypeUserInputText, UserInputText: &schema.UserInputText{Text: "you are an eino helper"}},
		},
	},
	schema.AgenticMessagesPlaceholder("history", false), // <= this will use the value of "history" in params
)
msgs, err := chatTemplate.Format(ctx, params)

func AgenticMessagesPlaceholder

func AgenticMessagesPlaceholder(key string, optional bool) AgenticMessagesTemplate

AgenticMessagesPlaceholder can render a placeholder to a list of agentic messages in params. e.g.

placeholder := AgenticMessagesPlaceholder("history", false)
params := map[string]any{
	"history": []*schema.AgenticMessage{
		&schema.AgenticMessage{
			Role: schema.AgenticRoleTypeSystem,
			ContentBlocks: []*schema.ContentBlock{
				{Type: schema.ContentBlockTypeUserInputText, UserInputText: &schema.UserInputText{Text: "you are an eino helper"}},
			},
		},
	},
}
chatTemplate := chatTpl := prompt.FromMessages(
	schema.AgenticMessagesPlaceholder("history", false), // <= this will use the value of "history" in params
)
msgs, err := chatTemplate.Format(ctx, params)

type AgenticResponseMeta

type AgenticResponseMeta struct {
	// TokenUsage is the token usage.
	TokenUsage *TokenUsage

	// OpenAIExtension is the extension for OpenAI.
	OpenAIExtension *openai.ResponseMetaExtension

	// GeminiExtension is the extension for Gemini.
	GeminiExtension *gemini.ResponseMetaExtension

	// ClaudeExtension is the extension for Claude.
	ClaudeExtension *claude.ResponseMetaExtension

	// Extension is the extension for other models, supplied by the component implementer.
	Extension any
}

func (*AgenticResponseMeta) String

func (a *AgenticResponseMeta) String() string

String returns the string representation of AgenticResponseMeta.

type AgenticRoleType

type AgenticRoleType string
const (
	AgenticRoleTypeSystem    AgenticRoleType = "system"
	AgenticRoleTypeUser      AgenticRoleType = "user"
	AgenticRoleTypeAssistant AgenticRoleType = "assistant"
)

type AgenticToolChoice

type AgenticToolChoice struct {
	// Type is the tool choice mode.
	Type ToolChoice

	// Allowed optionally specifies the list of tools that the model is permitted to call.
	// Optional.
	Allowed *AgenticAllowedToolChoice

	// Forced optionally specifies the list of tools that the model is required to call.
	// Optional.
	Forced *AgenticForcedToolChoice
}

type AllowedMCPTool

type AllowedMCPTool struct {
	// ServerLabel is the label of the MCP server.
	ServerLabel string
	// Name is the name of the MCP tool.
	Name string
}

AllowedMCPTool contains the information for identifying an MCP tool.

type AllowedServerTool

type AllowedServerTool struct {
	// Name is the name of the server tool.
	Name string
}

AllowedServerTool contains the information for identifying a server tool.

type AllowedTool

type AllowedTool struct {
	// FunctionName specifies a function tool by name.
	FunctionName string

	// MCPTool specifies an MCP tool.
	MCPTool *AllowedMCPTool

	// ServerTool specifies a server tool.
	ServerTool *AllowedServerTool
}

AllowedTool represents a tool that the model is allowed or forced to call. Exactly one of FunctionName, MCPTool, or ServerTool must be specified.

type AssistantGenAudio

type AssistantGenAudio struct {
	// URL is the HTTP/HTTPS link.
	URL string

	// Base64Data is the binary data in Base64 encoded string format.
	Base64Data string

	// MIMEType is the mime type, e.g. "audio/wav".
	MIMEType string
}

func (*AssistantGenAudio) String

func (a *AssistantGenAudio) String() string

String returns the string representation of AssistantGenAudio.

type AssistantGenImage

type AssistantGenImage struct {
	// URL is the HTTP/HTTPS link.
	URL string

	// Base64Data is the binary data in Base64 encoded string format.
	Base64Data string

	// MIMEType is the mime type, e.g. "image/png".
	MIMEType string
}

func (*AssistantGenImage) String

func (a *AssistantGenImage) String() string

String returns the string representation of AssistantGenImage.

type AssistantGenText

type AssistantGenText struct {
	// Text is the generated text.
	Text string

	// OpenAIExtension is the extension for OpenAI.
	OpenAIExtension *openai.AssistantGenTextExtension

	// ClaudeExtension is the extension for Claude.
	ClaudeExtension *claude.AssistantGenTextExtension

	// Extension is the extension for other models, supplied by the component implementer.
	Extension any
}

func (*AssistantGenText) String

func (a *AssistantGenText) String() string

String returns the string representation of AssistantGenText.

type AssistantGenVideo

type AssistantGenVideo struct {
	// URL is the HTTP/HTTPS link.
	URL string

	// Base64Data is the binary data in Base64 encoded string format.
	Base64Data string

	// MIMEType is the mime type, e.g. "video/mp4".
	MIMEType string
}

func (*AssistantGenVideo) String

func (a *AssistantGenVideo) String() string

String returns the string representation of AssistantGenVideo.

type ChatMessageAudioURL deprecated

type ChatMessageAudioURL struct {
	// URL can either be a traditional URL or a special URL conforming to RFC-2397 (https://www.rfc-editor.org/rfc/rfc2397).
	// double check with model implementations for detailed instructions on how to use this.
	URL string `json:"url,omitempty"`
	URI string `json:"uri,omitempty"`

	// MIMEType is the mime type of the audio, eg. "audio/wav" or "audio/ogg".
	MIMEType string `json:"mime_type,omitempty"`
	// Extra is used to store extra information for the audio url.
	Extra map[string]any `json:"extra,omitempty"`
}

Deprecated: This struct is deprecated as the MultiContent field is deprecated. For the audio input part of the model, use MessageInputAudio. For the audio output part of the model, use MessageOutputAudio. Choose either URL or URI. If supported, URL may embed inline audio data per RFC-2397.

type ChatMessageFileURL deprecated

type ChatMessageFileURL struct {
	URL string `json:"url,omitempty"`
	URI string `json:"uri,omitempty"`

	// MIMEType is the mime type of the file, eg. "application/pdf", "text/plain".
	MIMEType string `json:"mime_type,omitempty"`
	// Name is the name of the file.
	Name string `json:"name,omitempty"`

	// Extra is used to store extra information for the file url.
	Extra map[string]any `json:"extra,omitempty"`
}

Deprecated: This struct is deprecated as the MultiContent field is deprecated. For the file input part of the model, use MessageInputFile. Choose either URL or URI.

type ChatMessageImageURL deprecated

type ChatMessageImageURL struct {
	// URL can either be a traditional URL or a special URL conforming to RFC-2397 (https://www.rfc-editor.org/rfc/rfc2397).
	// double check with model implementations for detailed instructions on how to use this.
	URL string `json:"url,omitempty"`

	URI string `json:"uri,omitempty"`
	// Detail is the quality of the image url.
	Detail ImageURLDetail `json:"detail,omitempty"`

	// MIMEType is the mime type of the image, eg. "image/png".
	MIMEType string `json:"mime_type,omitempty"`
	// Extra is used to store extra information for the image url.
	Extra map[string]any `json:"extra,omitempty"`
}

Deprecated: This struct is deprecated as the MultiContent field is deprecated. For the image input part of the model, use MessageInputImage. For the image output part of the model, use MessageOutputImage. Choose either URL or URI. If your model implementation supports it, URL could embed inline image data as defined in RFC-2397.

type ChatMessagePart deprecated

type ChatMessagePart struct {
	// Type is the type of the part, eg. "text", "image_url", "audio_url", "video_url", "file_url".
	Type ChatMessagePartType `json:"type,omitempty"`

	// Text is the text of the part, it's used when Type is "text".
	Text string `json:"text,omitempty"`

	// ImageURL is the image url of the part, it's used when Type is "image_url".
	ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
	// AudioURL is the audio url of the part, it's used when Type is "audio_url".
	AudioURL *ChatMessageAudioURL `json:"audio_url,omitempty"`
	// VideoURL is the video url of the part, it's used when Type is "video_url".
	VideoURL *ChatMessageVideoURL `json:"video_url,omitempty"`
	// FileURL is the file url of the part, it's used when Type is "file_url".
	FileURL *ChatMessageFileURL `json:"file_url,omitempty"`
}

Deprecated: This struct is deprecated as the MultiContent field is deprecated. For model input, use MessageInputPart. For model output, use MessageOutputPart.

type ChatMessagePartType

type ChatMessagePartType string

ChatMessagePartType is the type of the part in a chat message.

const (
	// ChatMessagePartTypeText means the part is a text.
	ChatMessagePartTypeText ChatMessagePartType = "text"
	// ChatMessagePartTypeImageURL means the part is an image url.
	ChatMessagePartTypeImageURL ChatMessagePartType = "image_url"
	// ChatMessagePartTypeAudioURL means the part is an audio url.
	ChatMessagePartTypeAudioURL ChatMessagePartType = "audio_url"
	// ChatMessagePartTypeVideoURL means the part is a video url.
	ChatMessagePartTypeVideoURL ChatMessagePartType = "video_url"
	// ChatMessagePartTypeFileURL means the part is a file url.
	ChatMessagePartTypeFileURL ChatMessagePartType = "file_url"
)

type ChatMessageVideoURL deprecated

type ChatMessageVideoURL struct {
	// URL can either be a traditional URL or a special URL conforming to RFC-2397 (https://www.rfc-editor.org/rfc/rfc2397).
	// double check with model implementations for detailed instructions on how to use this.
	URL string `json:"url,omitempty"`
	URI string `json:"uri,omitempty"`

	// MIMEType is the mime type of the video, eg. "video/mp4".
	MIMEType string `json:"mime_type,omitempty"`
	// Extra is used to store extra information for the video url.
	Extra map[string]any `json:"extra,omitempty"`
}

Deprecated: This struct is deprecated as the MultiContent field is deprecated. For the video input part of the model, use MessageInputVideo. For the video output part of the model, use MessageOutputVideo. Choose either URL or URI. If supported, URL may embed inline video data per RFC-2397.

type CompletionTokensDetails added in v0.7.9

type CompletionTokensDetails struct {
	// ReasoningTokens tokens generated by the model for reasoning.
	// This is currently supported by OpenAI, Gemini, ARK and Qwen  chat models.
	// For other models, this field will be 0.
	ReasoningTokens int `json:"reasoning_tokens,omitempty"`
}

type ContentBlock

type ContentBlock struct {
	Type ContentBlockType

	// Reasoning contains the reasoning content generated by the model.
	Reasoning *Reasoning

	// UserInputText contains the text content provided by the user.
	UserInputText *UserInputText

	// UserInputImage contains the image content provided by the user.
	UserInputImage *UserInputImage

	// UserInputAudio contains the audio content provided by the user.
	UserInputAudio *UserInputAudio

	// UserInputVideo contains the video content provided by the user.
	UserInputVideo *UserInputVideo

	// UserInputFile contains the file content provided by the user.
	UserInputFile *UserInputFile

	// AssistantGenText contains the text content generated by the model.
	AssistantGenText *AssistantGenText

	// AssistantGenImage contains the image content generated by the model.
	AssistantGenImage *AssistantGenImage

	// AssistantGenAudio contains the audio content generated by the model.
	AssistantGenAudio *AssistantGenAudio

	// AssistantGenVideo contains the video content generated by the model.
	AssistantGenVideo *AssistantGenVideo

	// FunctionToolCall contains the invocation details for a user-defined tool.
	FunctionToolCall *FunctionToolCall

	// FunctionToolResult contains the result returned from a user-defined tool call.
	FunctionToolResult *FunctionToolResult

	// ServerToolCall contains the invocation details for a provider built-in tool executed on the model server.
	ServerToolCall *ServerToolCall

	// ServerToolResult contains the result returned from a provider built-in tool executed on the model server.
	ServerToolResult *ServerToolResult

	// MCPToolCall contains the invocation details for an MCP tool managed by the model server.
	MCPToolCall *MCPToolCall

	// MCPToolResult contains the result returned from an MCP tool managed by the model server.
	MCPToolResult *MCPToolResult

	// MCPListToolsResult contains the list of available MCP tools reported by the model server.
	MCPListToolsResult *MCPListToolsResult

	// MCPToolApprovalRequest contains the user approval request for an MCP tool call when required.
	MCPToolApprovalRequest *MCPToolApprovalRequest

	// MCPToolApprovalResponse contains the user's approval decision for an MCP tool call.
	MCPToolApprovalResponse *MCPToolApprovalResponse

	// StreamingMeta contains metadata for streaming responses.
	StreamingMeta *StreamingMeta

	// Extra contains additional information for the content block.
	Extra map[string]any
}

func NewContentBlock

func NewContentBlock[T contentBlockVariant](content *T) *ContentBlock

NewContentBlock creates a new ContentBlock with the given content.

func NewContentBlockChunk

func NewContentBlockChunk[T contentBlockVariant](content *T, meta *StreamingMeta) *ContentBlock

NewContentBlockChunk creates a new ContentBlock with the given content and streaming metadata.

func (*ContentBlock) String

func (b *ContentBlock) String() string

String returns the string representation of ContentBlock.

type ContentBlockType

type ContentBlockType string
const (
	ContentBlockTypeReasoning               ContentBlockType = "reasoning"
	ContentBlockTypeUserInputText           ContentBlockType = "user_input_text"
	ContentBlockTypeUserInputImage          ContentBlockType = "user_input_image"
	ContentBlockTypeUserInputAudio          ContentBlockType = "user_input_audio"
	ContentBlockTypeUserInputVideo          ContentBlockType = "user_input_video"
	ContentBlockTypeUserInputFile           ContentBlockType = "user_input_file"
	ContentBlockTypeAssistantGenText        ContentBlockType = "assistant_gen_text"
	ContentBlockTypeAssistantGenImage       ContentBlockType = "assistant_gen_image"
	ContentBlockTypeAssistantGenAudio       ContentBlockType = "assistant_gen_audio"
	ContentBlockTypeAssistantGenVideo       ContentBlockType = "assistant_gen_video"
	ContentBlockTypeFunctionToolCall        ContentBlockType = "function_tool_call"
	ContentBlockTypeFunctionToolResult      ContentBlockType = "function_tool_result"
	ContentBlockTypeServerToolCall          ContentBlockType = "server_tool_call"
	ContentBlockTypeServerToolResult        ContentBlockType = "server_tool_result"
	ContentBlockTypeMCPToolCall             ContentBlockType = "mcp_tool_call"
	ContentBlockTypeMCPToolResult           ContentBlockType = "mcp_tool_result"
	ContentBlockTypeMCPListToolsResult      ContentBlockType = "mcp_list_tools_result"
	ContentBlockTypeMCPToolApprovalRequest  ContentBlockType = "mcp_tool_approval_request"
	ContentBlockTypeMCPToolApprovalResponse ContentBlockType = "mcp_tool_approval_response"
)

type ConvertOption added in v0.7.14

type ConvertOption func(*convertOptions)

func WithErrWrapper added in v0.7.14

func WithErrWrapper(wrapper func(error) error) ConvertOption

WithErrWrapper wraps the first error encountered in a stream reader during conversion by StreamReaderWithConvert. The error returned by the convert function will not be wrapped. If the returned err is nil or is ErrNoValue, the stream chunk will be ignored

type DataType

type DataType string

DataType is the type of the parameter. It must be one of the following values: "object", "number", "integer", "string", "array", "null", "boolean", which is the same as the type of the parameter in JSONSchema.

const (
	Object  DataType = "object"
	Number  DataType = "number"
	Integer DataType = "integer"
	String  DataType = "string"
	Array   DataType = "array"
	Null    DataType = "null"
	Boolean DataType = "boolean"
)

Supported JSONSchema data types for tool parameters.

type Document

type Document struct {
	// ID is the unique identifier of the document.
	ID string `json:"id"`
	// Content is the content of the document.
	Content string `json:"content"`
	// MetaData is the metadata of the document, can be used to store extra information.
	MetaData map[string]any `json:"meta_data"`
}

Document is a piece of text with metadata.

func (*Document) DSLInfo added in v0.3.4

func (d *Document) DSLInfo() map[string]any

DSLInfo returns the dsl info of the document. can use doc.WithDSLInfo() to set the dsl info.

func (*Document) DenseVector added in v0.3.4

func (d *Document) DenseVector() []float64

DenseVector returns the dense vector of the document. can use doc.WithDenseVector() to set the dense vector.

func (*Document) ExtraInfo added in v0.3.4

func (d *Document) ExtraInfo() string

ExtraInfo returns the extra info of the document. can use doc.WithExtraInfo() to set the extra info.

func (*Document) Score

func (d *Document) Score() float64

Score returns the score of the document. can use doc.WithScore() to set the score.

func (*Document) SparseVector added in v0.3.4

func (d *Document) SparseVector() map[int]float64

SparseVector returns the sparse vector of the document, key indices -> value vector. can use doc.WithSparseVector() to set the sparse vector.

func (*Document) String

func (d *Document) String() string

String returns the content of the document.

func (*Document) SubIndexes

func (d *Document) SubIndexes() []string

SubIndexes returns the sub indexes of the document. can use doc.WithSubIndexes() to set the sub indexes.

func (*Document) WithDSLInfo added in v0.3.4

func (d *Document) WithDSLInfo(dslInfo map[string]any) *Document

WithDSLInfo sets the dsl info of the document. can use doc.DSLInfo() to get the dsl info.

func (*Document) WithDenseVector added in v0.3.4

func (d *Document) WithDenseVector(vector []float64) *Document

WithDenseVector sets the dense vector of the document. can use doc.DenseVector() to get the dense vector.

func (*Document) WithExtraInfo added in v0.3.4

func (d *Document) WithExtraInfo(extraInfo string) *Document

WithExtraInfo sets the extra info of the document. can use doc.ExtraInfo() to get the extra info.

func (*Document) WithScore

func (d *Document) WithScore(score float64) *Document

WithScore sets the score of the document. can use doc.Score() to get the score.

func (*Document) WithSparseVector added in v0.3.4

func (d *Document) WithSparseVector(sparse map[int]float64) *Document

WithSparseVector sets the sparse vector of the document, key indices -> value vector. can use doc.SparseVector() to get the sparse vector.

func (*Document) WithSubIndexes

func (d *Document) WithSubIndexes(indexes []string) *Document

WithSubIndexes sets the sub indexes of the document. can use doc.SubIndexes() to get the sub indexes, useful for search engine to use sub indexes to search.

type FormatType

type FormatType uint8

FormatType used by MessageTemplate.Format

const (
	// FString Supported by pyfmt(github.com/slongfield/pyfmt), which is an implementation of https://peps.python.org/pep-3101/.
	FString FormatType = 0
	// GoTemplate https://pkg.go.dev/text/template.
	GoTemplate FormatType = 1
	// Jinja2 Supported by gonja(github.com/nikolalohinski/gonja), which is a implementation of https://jinja.palletsprojects.com/en/3.1.x/templates/.
	Jinja2 FormatType = 2
)

type FunctionCall

type FunctionCall struct {
	// Name is the name of the function to call, it can be used to identify the specific function.
	Name string `json:"name,omitempty"`
	// Arguments is the arguments to call the function with, in JSON format.
	Arguments string `json:"arguments,omitempty"`
}

FunctionCall is the function call in a message. It's used in Assistant Message.

type FunctionToolCall

type FunctionToolCall struct {
	// CallID is the unique identifier for the tool call.
	CallID string

	// Name specifies the function tool invoked.
	Name string

	// Arguments is the JSON string arguments for the function tool call.
	Arguments string
}

func (*FunctionToolCall) String

func (f *FunctionToolCall) String() string

String returns the string representation of FunctionToolCall.

type FunctionToolResult

type FunctionToolResult struct {
	// CallID is the unique identifier for the tool call.
	CallID string

	// Name specifies the function tool invoked.
	Name string

	// Result is the function tool result returned by the user
	Result string
}

func (*FunctionToolResult) String

func (f *FunctionToolResult) String() string

String returns the string representation of FunctionToolResult.

type ImageURLDetail

type ImageURLDetail string

ImageURLDetail is the detail of the image url.

const (
	// ImageURLDetailHigh means the high quality image url.
	ImageURLDetailHigh ImageURLDetail = "high"
	// ImageURLDetailLow means the low quality image url.
	ImageURLDetailLow ImageURLDetail = "low"
	// ImageURLDetailAuto means the auto quality image url.
	ImageURLDetailAuto ImageURLDetail = "auto"
)

type LogProb added in v0.3.16

type LogProb struct {
	// Token represents the text of the token, which is a contiguous sequence of characters
	// (e.g., a word, part of a word, or punctuation) as understood by the tokenization process used by the language model.
	Token string `json:"token"`
	// LogProb is the log probability of this token, if it is within the top 20 most likely tokens.
	// Otherwise, the value `-9999.0` is used to signify that the token is very unlikely.
	LogProb float64 `json:"logprob"`
	// Bytes is a list of integers representing the UTF-8 bytes representation of the token.
	// Useful in instances where characters are represented by multiple tokens and
	// their byte representations must be combined to generate the correct text
	// representation. Can be `null` if there is no bytes representation for the token.
	Bytes []int64 `json:"bytes,omitempty"` // Omitting the field if it is null
	// TopLogProbs is a list of the most likely tokens and their log probability, at this token position.
	// In rare cases, there may be fewer than the number of requested top_logprobs returned.
	TopLogProbs []TopLogProb `json:"top_logprobs"`
}

LogProb represents the probability information for a token.

type LogProbs added in v0.3.16

type LogProbs struct {
	// Content is a list of message content tokens with log probability information.
	Content []LogProb `json:"content"`
}

LogProbs is the top-level structure containing the log probability information.

type MCPListToolsItem

type MCPListToolsItem struct {
	// Name is the name of the tool.
	Name string

	// Description is the description of the tool.
	Description string

	// InputSchema is the JSON schema that describes the tool input parameters.
	InputSchema *jsonschema.Schema
}

type MCPListToolsResult

type MCPListToolsResult struct {
	// ServerLabel is the MCP server label used to identify it in tool calls.
	ServerLabel string

	// Tools is the list of tools available on the server.
	Tools []*MCPListToolsItem

	// Error returned when the server fails to list tools.
	Error string
}

func (*MCPListToolsResult) String

func (m *MCPListToolsResult) String() string

String returns the string representation of MCPListToolsResult.

type MCPToolApprovalRequest

type MCPToolApprovalRequest struct {
	// ID is the approval request ID.
	ID string

	// Name is the name of the tool to run.
	Name string

	// Arguments is the JSON string arguments for the tool call.
	Arguments string

	// ServerLabel is the MCP server label used to identify it in tool calls.
	ServerLabel string
}

func (*MCPToolApprovalRequest) String

func (m *MCPToolApprovalRequest) String() string

String returns the string representation of MCPToolApprovalRequest.

type MCPToolApprovalResponse

type MCPToolApprovalResponse struct {
	// ApprovalRequestID is the approval request ID being responded to.
	ApprovalRequestID string

	// Approve indicates whether the request is approved.
	Approve bool

	// Reason is the rationale for the decision.
	// Optional.
	Reason string
}

func (*MCPToolApprovalResponse) String

func (m *MCPToolApprovalResponse) String() string

String returns the string representation of MCPToolApprovalResponse.

type MCPToolCall

type MCPToolCall struct {
	// ServerLabel is the MCP server label used to identify it in tool calls
	ServerLabel string

	// ApprovalRequestID is the approval request ID.
	ApprovalRequestID string

	// CallID is the unique ID of the tool call.
	CallID string

	// Name is the name of the tool to run.
	Name string

	// Arguments is the JSON string arguments for the tool call.
	Arguments string
}

func (*MCPToolCall) String

func (m *MCPToolCall) String() string

String returns the string representation of MCPToolCall.

type MCPToolCallError

type MCPToolCallError struct {
	// Code is the error code.
	Code *int64

	// Message is the error message.
	Message string
}

type MCPToolResult

type MCPToolResult struct {
	// ServerLabel is the MCP server label used to identify it in tool calls
	ServerLabel string

	// CallID is the unique ID of the tool call.
	CallID string

	// Name is the name of the tool to run.
	Name string

	// Result is the JSON string with the tool result.
	Result string

	// Error returned when the server fails to run the tool.
	Error *MCPToolCallError
}

func (*MCPToolResult) String

func (m *MCPToolResult) String() string

String returns the string representation of MCPToolResult.

type Message

type Message struct {
	Role RoleType `json:"role"`

	// Content is for user text input and model text output.
	Content string `json:"content"`

	// if MultiContent is not empty, use this instead of Content
	// if MultiContent is empty, use Content
	// Deprecated: Use UserInputMultiContent for user multimodal inputs and AssistantGenMultiContent for model multimodal outputs.
	MultiContent []ChatMessagePart `json:"multi_content,omitempty"`

	// UserInputMultiContent passes multimodal content provided by the user to the model.
	UserInputMultiContent []MessageInputPart `json:"user_input_multi_content,omitempty"`

	// AssistantGenMultiContent is for receiving multimodal output from the model.
	AssistantGenMultiContent []MessageOutputPart `json:"assistant_output_multi_content,omitempty"`

	Name string `json:"name,omitempty"`

	// only for AssistantMessage
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`

	// only for ToolMessage
	ToolCallID string `json:"tool_call_id,omitempty"`
	// only for ToolMessage
	ToolName string `json:"tool_name,omitempty"`

	ResponseMeta *ResponseMeta `json:"response_meta,omitempty"`

	// ReasoningContent is the thinking process of the model, which will be included when the model returns reasoning content.
	ReasoningContent string `json:"reasoning_content,omitempty"`

	// customized information for model implementation
	Extra map[string]any `json:"extra,omitempty"`
}

Message denotes the data structure for model input and output, originating from either user input or model return. It supports both text-only and multimodal content.

For text-only input from a user, use the Content field:

&schema.Message{
	Role:    schema.User,
	Content: "What is the capital of France?",
}

For multimodal input from a user, use the UserInputMultiContent field. This allows combining text with other media like images:

&schema.Message{
	Role: schema.User,
	UserInputMultiContent: []schema.MessageInputPart{
		{Type: schema.ChatMessagePartTypeText, Text: "What is in this image?"},
		{Type: schema.ChatMessagePartTypeImageURL, Image: &schema.MessageInputImage{
			MessagePartCommon: schema.MessagePartCommon{
				URL: toPtr("https://example.com/cat.jpg"),
			},
			Detail: schema.ImageURLDetailHigh,
		}},
	},
}

When the model returns multimodal content, it is available in the AssistantGenMultiContent field:

&schema.Message{
	Role: schema.Assistant,
	AssistantGenMultiContent: []schema.MessageOutputPart{
		{Type: schema.ChatMessagePartTypeText, Text: "Here is the generated image:"},
		{Type: schema.ChatMessagePartTypeImage, Image: &schema.MessageOutputImage{
			MessagePartCommon: schema.MessagePartCommon{
				Base64Data: toPtr("base64_image_binary"),
				MIMEType:   "image/png",
			},
		}},
	},
}

func AssistantMessage

func AssistantMessage(content string, toolCalls []ToolCall) *Message

AssistantMessage represents a message with Role "assistant".

func ConcatMessageArray added in v0.5.0

func ConcatMessageArray(mas [][]*Message) ([]*Message, error)

ConcatMessageArray merges aligned slices of messages into a single slice, concatenating messages at the same index across the input arrays.

func ConcatMessageStream added in v0.3.23

func ConcatMessageStream(s *StreamReader[*Message]) (*Message, error)

ConcatMessageStream drains a stream of messages and returns a single concatenated message representing the merged content.

func ConcatMessages

func ConcatMessages(msgs []*Message) (*Message, error)

ConcatMessages concat messages with the same role and name. It will concat tool calls with the same index. It will return an error if the messages have different roles or names. It's useful for concatenating messages from a stream. e.g.

msgs := []*Message{}
for {
	msg, err := stream.Recv()
	if errors.Is(err, io.EOF) {
		break
	}
	if err != nil {...}
	msgs = append(msgs, msg)
}

concatedMsg, err := ConcatMessages(msgs) // concatedMsg.Content will be full content of all messages

func SystemMessage

func SystemMessage(content string) *Message

SystemMessage represents a message with Role "system".

func ToolMessage

func ToolMessage(content string, toolCallID string, opts ...ToolMessageOption) *Message

ToolMessage represents a message with Role "tool".

func UserMessage

func UserMessage(content string) *Message

UserMessage represents a message with Role "user".

func (*Message) Format

func (m *Message) Format(_ context.Context, vs map[string]any, formatType FormatType) ([]*Message, error)

Format returns the messages after rendering by the given formatType. e.g.

msg := schema.UserMessage("hello world, {name}")
msgs, err := msg.Format(ctx, map[string]any{"name": "eino"}, schema.FString) // <= this will render the content of msg by pyfmt
// msgs[0].Content will be "hello world, eino"

func (*Message) String

func (m *Message) String() string

String returns the string representation of the message. e.g.

msg := schema.UserMessage("hello world")
fmt.Println(msg.String()) // Output will be: `user: hello world``

msg := schema.Message{
	Role:    schema.Tool,
	Content: "{...}",
	ToolCallID: "callxxxx"
}
fmt.Println(msg.String())
Output will be:
	tool: {...}
	call_id: callxxxx

type MessageInputAudio added in v0.5.5

type MessageInputAudio struct {
	MessagePartCommon
}

MessageInputAudio is used to represent an audio part in message. Choose either URL or Base64Data.

type MessageInputFile added in v0.5.5

type MessageInputFile struct {
	MessagePartCommon

	// Name represents the filename.
	// Optional.
	Name string `json:"name,omitempty"`
}

MessageInputFile is used to represent a file part in message. Choose either URL or Base64Data.

type MessageInputImage added in v0.5.5

type MessageInputImage struct {
	MessagePartCommon

	// Detail is the quality of the image url.
	Detail ImageURLDetail `json:"detail,omitempty"`
}

MessageInputImage is used to represent an image part in message. Choose either URL or Base64Data.

type MessageInputPart added in v0.5.5

type MessageInputPart struct {
	Type ChatMessagePartType `json:"type"`

	Text string `json:"text,omitempty"`

	// Image is the image input of the part, it's used when Type is "image_url".
	Image *MessageInputImage `json:"image,omitempty"`

	// Audio  is the audio input of the part, it's used when Type is "audio_url".
	Audio *MessageInputAudio `json:"audio,omitempty"`

	// Video is the video input of the part, it's used when Type is "video_url".
	Video *MessageInputVideo `json:"video,omitempty"`

	// File is the file input of the part, it's used when Type is "file_url".
	File *MessageInputFile `json:"file,omitempty"`

	// Extra is used to store extra information.
	Extra map[string]any `json:"extra,omitempty"`
}

MessageInputPart represents the input part of message.

type MessageInputVideo added in v0.5.5

type MessageInputVideo struct {
	MessagePartCommon
}

MessageInputVideo is used to represent a video part in message. Choose either URL or Base64Data.

type MessageJSONParseConfig

type MessageJSONParseConfig struct {
	// parse from content or tool call, default is content.
	ParseFrom MessageParseFrom `json:"parse_from,omitempty"`

	// parse key path, default is empty.
	// must be a valid json path expression, eg: field.sub_field
	ParseKeyPath string `json:"parse_key_path,omitempty"`
}

MessageJSONParseConfig configures JSON parsing behavior for Message.

type MessageJSONParser

type MessageJSONParser[T any] struct {
	ParseFrom    MessageParseFrom
	ParseKeyPath string
}

MessageJSONParser is a parser that parses a message into an object T, using json unmarshal. eg of parse to single struct:

config := &MessageJSONParseConfig{
	ParseFrom: MessageParseFromToolCall,
}
parser := NewMessageJSONParser[GetUserParam](config)
param, err := parser.Parse(ctx, message)

eg of parse to slice of struct:

config := &MessageJSONParseConfig{
	ParseFrom: MessageParseFromToolCall,
}

parser := NewMessageJSONParser[GetUserParam](config) param, err := parser.Parse(ctx, message)

func (*MessageJSONParser[T]) Parse

func (p *MessageJSONParser[T]) Parse(ctx context.Context, m *Message) (parsed T, err error)

Parse parses a message into an object T.

type MessageOutputAudio added in v0.5.5

type MessageOutputAudio struct {
	MessagePartCommon
}

MessageOutputAudio is used to represent an audio part in message.

type MessageOutputImage added in v0.5.5

type MessageOutputImage struct {
	MessagePartCommon
}

MessageOutputImage is used to represent an image part in message.

type MessageOutputPart added in v0.5.5

type MessageOutputPart struct {
	// Type is the type of the part, eg. "text", "image_url", "audio_url", "video_url".
	Type ChatMessagePartType `json:"type"`

	// Text is the text of the part, it's used when Type is "text".
	Text string `json:"text,omitempty"`

	// Image is the image output of the part, used when Type is ChatMessagePartTypeImageURL.
	Image *MessageOutputImage `json:"image,omitempty"`

	// Audio is the audio output of the part, used when Type is ChatMessagePartTypeAudioURL.
	Audio *MessageOutputAudio `json:"audio,omitempty"`

	// Video is the video output of the part, used when Type is ChatMessagePartTypeVideoURL.
	Video *MessageOutputVideo `json:"video,omitempty"`

	// Extra is used to store extra information.
	Extra map[string]any `json:"extra,omitempty"`
}

MessageOutputPart represents a part of an assistant-generated message. It can contain text, or multimedia content like images, audio, or video.

type MessageOutputVideo added in v0.5.5

type MessageOutputVideo struct {
	MessagePartCommon
}

MessageOutputVideo is used to represent a video part in message.

type MessageParseFrom

type MessageParseFrom string

MessageParseFrom determines the source of the data to be parsed. default is content (Message.Content).

const (
	MessageParseFromContent  MessageParseFrom = "content"
	MessageParseFromToolCall MessageParseFrom = "tool_call"
)

MessageParseFrom indicates the source data used by the parser.

type MessageParser

type MessageParser[T any] interface {
	Parse(ctx context.Context, m *Message) (T, error)
}

MessageParser parses a Message into a strongly typed value.

func NewMessageJSONParser

func NewMessageJSONParser[T any](config *MessageJSONParseConfig) MessageParser[T]

NewMessageJSONParser creates a new MessageJSONParser.

type MessagePartCommon added in v0.5.5

type MessagePartCommon struct {
	// URL is primarily used for HTTP or HTTPS access links.
	// For data in the format 'data:[<mediatype>][;base64],<data>' (the 'data' URL Schema of RFC-2397 (https://www.rfc-editor.org/rfc/rfc2397)),
	// it is recommended to use Base64Data and MIMEType fields separately instead.
	URL *string `json:"url,omitempty"`

	// Base64Data represents the binary data in Base64 encoded string format.
	Base64Data *string `json:"base64data,omitempty"`

	// MIMEType is the mime type , eg."image/png",""audio/wav" etc.
	MIMEType string `json:"mime_type,omitempty"`

	// Extra is used to store extra information.
	Extra map[string]any `json:"extra,omitempty"`
}

MessagePartCommon represents the common abstract components for input and output of multi-modal types.

type MessagesTemplate

type MessagesTemplate interface {
	Format(ctx context.Context, vs map[string]any, formatType FormatType) ([]*Message, error)
}

MessagesTemplate is the interface for messages template. It's used to render a template to a list of messages. e.g.

chatTemplate := prompt.FromMessages(
	schema.SystemMessage("you are an eino helper"),
	schema.MessagesPlaceholder("history", false), // <= this will use the value of "history" in params
)
msgs, err := chatTemplate.Format(ctx, params)

func MessagesPlaceholder

func MessagesPlaceholder(key string, optional bool) MessagesTemplate

MessagesPlaceholder can render a placeholder to a list of messages in params. e.g.

placeholder := MessagesPlaceholder("history", false)
params := map[string]any{
	"history": []*schema.Message{{Role: "user", Content: "what is eino?"}, {Role: "assistant", Content: "eino is a great framework to build llm apps"}},
	"query": "how to use eino?",
}
chatTemplate := chatTpl := prompt.FromMessages(
	schema.SystemMessage("you are eino helper"),
	schema.MessagesPlaceholder("history", false), // <= this will use the value of "history" in params
)
msgs, err := chatTemplate.Format(ctx, params)

type ParameterInfo

type ParameterInfo struct {
	// The type of the parameter.
	Type DataType
	// The element type of the parameter, only for array.
	ElemInfo *ParameterInfo
	// The sub parameters of the parameter, only for object.
	SubParams map[string]*ParameterInfo
	// The description of the parameter.
	Desc string
	// The enum values of the parameter, only for string.
	Enum []string
	// Whether the parameter is required.
	Required bool
}

ParameterInfo is the information of a parameter. It is used to describe the parameters of a tool.

type ParamsOneOf

type ParamsOneOf struct {
	// contains filtered or unexported fields
}

ParamsOneOf is a union of the different methods user can choose which describe a tool's request parameters. User must specify one and ONLY one method to describe the parameters.

  1. use NewParamsOneOfByParams(): an intuitive way to describe the parameters that covers most of the use-cases.
  2. use NewParamsOneOfByJSONSchema(): a formal way to describe the parameters that strictly adheres to JSONSchema specification. See https://json-schema.org/draft/2020-12.

func NewParamsOneOfByJSONSchema added in v0.4.6

func NewParamsOneOfByJSONSchema(s *jsonschema.Schema) *ParamsOneOf

NewParamsOneOfByJSONSchema creates a ParamsOneOf with *jsonschema.Schema.

func NewParamsOneOfByParams

func NewParamsOneOfByParams(params map[string]*ParameterInfo) *ParamsOneOf

NewParamsOneOfByParams creates a ParamsOneOf with map[string]*ParameterInfo.

func (*ParamsOneOf) ToJSONSchema added in v0.4.6

func (p *ParamsOneOf) ToJSONSchema() (*jsonschema.Schema, error)

ToJSONSchema parses ParamsOneOf, converts the parameter description that user actually provides, into the format ready to be passed to Model.

type PromptTokenDetails added in v0.4.2

type PromptTokenDetails struct {
	// Cached tokens present in the prompt.
	CachedTokens int `json:"cached_tokens"`
}

PromptTokenDetails provides a breakdown of prompt token usage.

type Reasoning

type Reasoning struct {
	// Text is either the thought summary or the raw reasoning text itself.
	Text string

	// Signature contains encrypted reasoning tokens.
	// Required by some models when passing reasoning text back.
	Signature string
}

func (*Reasoning) String

func (r *Reasoning) String() string

String returns the string representation of Reasoning.

type ResponseMeta

type ResponseMeta struct {
	// FinishReason is the reason why the chat response is finished.
	// It's usually "stop", "length", "tool_calls", "content_filter", "null". This is defined by chat model implementation.
	FinishReason string `json:"finish_reason,omitempty"`
	// Usage is the token usage of the chat response, whether usage exists depends on whether the chat model implementation returns.
	Usage *TokenUsage `json:"usage,omitempty"`
	// LogProbs is Log probability information.
	LogProbs *LogProbs `json:"logprobs,omitempty"`
}

ResponseMeta collects meta information about a chat response.

type RoleType

type RoleType string

RoleType is the type of the role of a message.

const (
	// Assistant is the role of an assistant, means the message is returned by ChatModel.
	Assistant RoleType = "assistant"
	// User is the role of a user, means the message is a user message.
	User RoleType = "user"
	// System is the role of a system, means the message is a system message.
	System RoleType = "system"
	// Tool is the role of a tool, means the message is a tool call output.
	Tool RoleType = "tool"
)

type ServerToolCall

type ServerToolCall struct {
	// Name specifies the server-side tool invoked.
	// Supplied by the model server (e.g., `web_search` for OpenAI, `googleSearch` for Gemini).
	Name string

	// CallID is the unique identifier for the tool call.
	// Empty if not provided by the model server.
	CallID string

	// Arguments are the raw inputs to the server-side tool,
	// supplied by the component implementer.
	Arguments any
}

func (*ServerToolCall) String

func (s *ServerToolCall) String() string

String returns the string representation of ServerToolCall.

type ServerToolResult

type ServerToolResult struct {
	// Name specifies the server-side tool invoked.
	// Supplied by the model server (e.g., `web_search` for OpenAI, `googleSearch` for Gemini).
	Name string

	// CallID is the unique identifier for the tool call.
	// Empty if not provided by the model server.
	CallID string

	// Result refers to the raw output generated by the server-side tool,
	// supplied by the component implementer.
	Result any
}

func (*ServerToolResult) String

func (s *ServerToolResult) String() string

String returns the string representation of ServerToolResult.

type SourceEOF added in v0.3.36

type SourceEOF struct {
	// contains filtered or unexported fields
}

SourceEOF represents an EOF error from a specific source stream. It is only returned by the method Recv() of StreamReader created with MergeNamedStreamReaders when one of its source streams reaches EOF.

func (*SourceEOF) Error added in v0.3.36

func (e *SourceEOF) Error() string

type StreamReader

type StreamReader[T any] struct {
	// contains filtered or unexported fields
}

StreamReader the receiver of a stream. created by Pipe function. eg.

sr, sw := schema.Pipe[string](3)
// omit sending data
// most of time, reader is returned by function, and used in another function.

for chunk, err := sr.Recv() {
	if errors.Is(err, io.EOF) {
		break
	}
	if err != nil {
		// handle error
	}
	fmt.Println(chunk)
}

func InternalMergeNamedStreamReaders added in v0.3.44

func InternalMergeNamedStreamReaders[T any](srs []*StreamReader[T], names []string) *StreamReader[T]

InternalMergeNamedStreamReaders merges multiple readers with their names into a single multi-stream reader.

func MergeNamedStreamReaders added in v0.3.36

func MergeNamedStreamReaders[T any](srs map[string]*StreamReader[T]) *StreamReader[T]

MergeNamedStreamReaders merges multiple StreamReaders into one, preserving their names. When a source stream reaches EOF, the merged stream will return a SourceEOF error containing the name of the source stream that ended. This is useful when you need to track which source stream has completed. e.g.

sr1, sw1 := schema.Pipe[string](2)
sr2, sw2 := schema.Pipe[string](2)

namedStreams := map[string]*StreamReader[string]{
	"stream1": sr1,
	"stream2": sr2,
}

mergedSR := schema.MergeNamedStreamReaders(namedStreams)
defer mergedSR.Close()

for {
	chunk, err := mergedSR.Recv()
	if err != nil {
		if sourceName, ok := schema.GetSourceName(err); ok {
			fmt.Printf("Stream %s ended\n", sourceName)
			continue
		}
		if errors.Is(err, io.EOF) {
			break // All streams have ended
		}
		// Handle other errors
	}
	fmt.Println(chunk)
}

func MergeStreamReaders

func MergeStreamReaders[T any](srs []*StreamReader[T]) *StreamReader[T]

MergeStreamReaders merge multiple StreamReader into one. it's useful when you want to merge multiple streams into one. e.g.

sr1, sr2 := schema.Pipe[string](2)
defer sr1.Close()
defer sr2.Close()

sr := schema.MergeStreamReaders([]*schema.StreamReader[string]{sr1, sr2})

defer sr.Close()
for chunk, err := sr.Recv() {
	fmt.Println(chunk)
}

func StreamReaderFromArray

func StreamReaderFromArray[T any](arr []T) *StreamReader[T]

StreamReaderFromArray creates a StreamReader from a given slice of elements. It takes an array of type T and returns a pointer to a StreamReader[T]. This allows for streaming the elements of the array in a controlled manner. eg.

sr := schema.StreamReaderFromArray([]int{1, 2, 3})
defer sr.Close()

for chunk, err := sr.Recv() {
	fmt.Println(chunk)
}

func StreamReaderWithConvert

func StreamReaderWithConvert[T, D any](sr *StreamReader[T], convert func(T) (D, error), opts ...ConvertOption) *StreamReader[D]

StreamReaderWithConvert converts the stream reader to another stream reader.

eg.

intReader := StreamReaderFromArray([]int{1, 2, 3})
stringReader := StreamReaderWithConvert(sr, func(i int) (string, error) {
	return fmt.Sprintf("val_%d", i), nil
})

defer stringReader.Close() // Close the reader if you using Recv(), or may cause memory/goroutine leak.
s, err := stringReader.Recv()
fmt.Println(s) // Output: val_1

func (*StreamReader[T]) Close

func (sr *StreamReader[T]) Close()

Close safely closes the StreamReader. It should be called only once, as multiple calls may not work as expected. Notice: always remember to call Close() after using Recv(). e.g.

defer sr.Close()

for chunk, err := sr.Recv() {
	if errors.Is(err, io.EOF) {
		break
	}
	fmt.Println(chunk)
}

func (*StreamReader[T]) Copy

func (sr *StreamReader[T]) Copy(n int) []*StreamReader[T]

Copy creates a slice of new StreamReader. The number of copies, indicated by the parameter n, should be a non-zero positive integer. The original StreamReader will become unusable after Copy. eg.

sr := schema.StreamReaderFromArray([]int{1, 2, 3})
srs := sr.Copy(2)

sr1 := srs[0]
sr2 := srs[1]
defer sr1.Close()
defer sr2.Close()

chunk1, err1 := sr1.Recv()
chunk2, err2 := sr2.Recv()

func (*StreamReader[T]) Recv

func (sr *StreamReader[T]) Recv() (T, error)

Recv receives a value from the stream. eg.

for chunk, err := sr.Recv() {
	if errors.Is(err, io.EOF) {
		break
	}
	if err != nil {
	fmt.Println(chunk)
}

func (*StreamReader[T]) SetAutomaticClose added in v0.5.0

func (sr *StreamReader[T]) SetAutomaticClose()

SetAutomaticClose sets the StreamReader to automatically close when it's no longer reachable and ready to be GCed. NOT concurrency safe.

type StreamWriter

type StreamWriter[T any] struct {
	// contains filtered or unexported fields
}

StreamWriter the sender of a stream. created by Pipe function. eg.

sr, sw := schema.Pipe[string](3)
go func() { // send data
	defer sw.Close()
	for i := 0; i < 10; i++ {
		sw.Send(i, nil)
	}
}

func (*StreamWriter[T]) Close

func (sw *StreamWriter[T]) Close()

Close notify the receiver that the stream sender has finished. The stream receiver will get an error of io.EOF from StreamReader.Recv(). Notice: always remember to call Close() after sending all data. eg.

defer sw.Close()
for i := 0; i < 10; i++ {
	sw.Send(i, nil)
}

func (*StreamWriter[T]) Send

func (sw *StreamWriter[T]) Send(chunk T, err error) (closed bool)

Send sends a value to the stream. e.g.

closed := sw.Send(i, nil)
if closed {
	// the stream is closed
}

type StreamingMeta

type StreamingMeta struct {
	// Index specifies the index position of this block in the final response.
	Index int
}

type TokenUsage

type TokenUsage struct {
	// PromptTokens is the number of prompt tokens, including all the input tokens of this request.
	PromptTokens int `json:"prompt_tokens"`
	// PromptTokenDetails is a breakdown of the prompt tokens.
	PromptTokenDetails PromptTokenDetails `json:"prompt_token_details"`
	// CompletionTokens is the number of completion tokens.
	CompletionTokens int `json:"completion_tokens"`
	// TotalTokens is the total number of tokens.
	TotalTokens int `json:"total_tokens"`
	// CompletionTokensDetails is breakdown of completion tokens.
	CompletionTokensDetails CompletionTokensDetails `json:"completion_token_details"`
}

TokenUsage Represents the token usage of chat model request.

type ToolCall

type ToolCall struct {
	// Index is used when there are multiple tool calls in a message.
	// In stream mode, it's used to identify the chunk of the tool call for merging.
	Index *int `json:"index,omitempty"`
	// ID is the id of the tool call, it can be used to identify the specific tool call.
	ID string `json:"id"`
	// Type is the type of the tool call, default is "function".
	Type string `json:"type"`
	// Function is the function call to be made.
	Function FunctionCall `json:"function"`

	// Extra is used to store extra information for the tool call.
	Extra map[string]any `json:"extra,omitempty"`
}

ToolCall is the tool call in a message. It's used in Assistant Message when there are tool calls should be made.

type ToolChoice added in v0.3.8

type ToolChoice string

ToolChoice controls how the model calls tools (if any).

const (
	// ToolChoiceForbidden indicates that the model should not call any tools.
	// Corresponds to "none" in OpenAI Chat Completion.
	ToolChoiceForbidden ToolChoice = "forbidden"

	// ToolChoiceAllowed indicates that the model can choose to generate a message or call one or more tools.
	// Corresponds to "auto" in OpenAI Chat Completion.
	ToolChoiceAllowed ToolChoice = "allowed"

	// ToolChoiceForced indicates that the model must call one or more tools.
	// Corresponds to "required" in OpenAI Chat Completion.
	ToolChoiceForced ToolChoice = "forced"
)

type ToolInfo

type ToolInfo struct {
	// The unique name of the tool that clearly communicates its purpose.
	Name string
	// Used to tell the model how/when/why to use the tool.
	// You can provide few-shot examples as a part of the description.
	Desc string
	// Extra is the extra information for the tool.
	Extra map[string]any

	// The parameters the functions accepts (different models may require different parameter types).
	// can be described in two ways:
	//  - use params: schema.NewParamsOneOfByParams(params)
	//  - use jsonschema: schema.NewParamsOneOfByJSONSchema(jsonschema)
	// If is nil, signals that the tool does not need any input parameter
	*ParamsOneOf
}

ToolInfo is the information of a tool.

type ToolMessageOption added in v0.3.41

type ToolMessageOption func(*toolMessageOptions)

ToolMessageOption defines a option for ToolMessage

func WithToolName added in v0.3.41

func WithToolName(name string) ToolMessageOption

WithToolName returns a ToolMessageOption that sets the tool call name.

type TopLogProb added in v0.3.16

type TopLogProb struct {
	// Token represents the text of the token, which is a contiguous sequence of characters
	// (e.g., a word, part of a word, or punctuation) as understood by the tokenization process used by the language model.
	Token string `json:"token"`
	// LogProb is the log probability of this token, if it is within the top 20 most likely tokens.
	// Otherwise, the value `-9999.0` is used to signify that the token is very unlikely.
	LogProb float64 `json:"logprob"`
	// Bytes is a list of integers representing the UTF-8 bytes representation of the token.
	// Useful in instances where characters are represented by multiple tokens and
	// their byte representations must be combined to generate the correct text
	// representation. Can be `null` if there is no bytes representation for the token.
	Bytes []int64 `json:"bytes,omitempty"`
}

TopLogProb describes a likely token and its log probability at a position.

type UserInputAudio

type UserInputAudio struct {
	// URL is the HTTP/HTTPS link.
	URL string

	// Base64Data is the binary data in Base64 encoded string format.
	Base64Data string

	// MIMEType is the mime type, e.g. "audio/wav".
	MIMEType string
}

func (*UserInputAudio) String

func (u *UserInputAudio) String() string

String returns the string representation of UserInputAudio.

type UserInputFile

type UserInputFile struct {
	// URL is the HTTP/HTTPS link.
	URL string

	// Name is the filename.
	Name string

	// Base64Data is the binary data in Base64 encoded string format.
	Base64Data string

	// MIMEType is the mime type, e.g. "application/pdf".
	MIMEType string
}

func (*UserInputFile) String

func (u *UserInputFile) String() string

String returns the string representation of UserInputFile.

type UserInputImage

type UserInputImage struct {
	// URL is the HTTP/HTTPS link.
	URL string

	// Base64Data is the binary data in Base64 encoded string format.
	Base64Data string

	// MIMEType is the mime type, e.g. "image/png".
	MIMEType string

	// Detail is the quality of the image url.
	Detail ImageURLDetail
}

func (*UserInputImage) String

func (u *UserInputImage) String() string

String returns the string representation of UserInputImage.

type UserInputText

type UserInputText struct {
	// Text is the text content.
	Text string
}

func (*UserInputText) String

func (u *UserInputText) String() string

String returns the string representation of UserInputText.

type UserInputVideo

type UserInputVideo struct {
	// URL is the HTTP/HTTPS link.
	URL string

	// Base64Data is the binary data in Base64 encoded string format.
	Base64Data string

	// MIMEType is the mime type, e.g. "video/mp4".
	MIMEType string
}

func (*UserInputVideo) String

func (u *UserInputVideo) String() string

String returns the string representation of UserInputVideo.

Directories

Path Synopsis
Package claude defines constants for claude.
Package claude defines constants for claude.
Package gemini defines the extension for gemini.
Package gemini defines the extension for gemini.
Package openai defines constants for openai.
Package openai defines constants for openai.

Jump to

Keyboard shortcuts

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