Documentation
¶
Index ¶
- Variables
- func GetSourceName(err error) (string, bool)
- func Pipe[T any](cap int) (*StreamReader[T], *StreamWriter[T])
- func Register[T any]()
- func RegisterName[T any](name string)
- type ChatMessageAudioURLdeprecated
- type ChatMessageFileURLdeprecated
- type ChatMessageImageURLdeprecated
- type ChatMessagePartdeprecated
- type ChatMessagePartType
- type ChatMessageVideoURLdeprecated
- type CompletionTokensDetails
- type ConvertOption
- type DataType
- type Document
- func (d *Document) DSLInfo() map[string]any
- func (d *Document) DenseVector() []float64
- func (d *Document) ExtraInfo() string
- func (d *Document) Score() float64
- func (d *Document) SparseVector() map[int]float64
- func (d *Document) String() string
- func (d *Document) SubIndexes() []string
- func (d *Document) WithDSLInfo(dslInfo map[string]any) *Document
- func (d *Document) WithDenseVector(vector []float64) *Document
- func (d *Document) WithExtraInfo(extraInfo string) *Document
- func (d *Document) WithScore(score float64) *Document
- func (d *Document) WithSparseVector(sparse map[int]float64) *Document
- func (d *Document) WithSubIndexes(indexes []string) *Document
- type FormatType
- type FunctionCall
- type ImageURLDetail
- type LogProb
- type LogProbs
- type Message
- func AssistantMessage(content string, toolCalls []ToolCall) *Message
- func ConcatMessageArray(mas [][]*Message) ([]*Message, error)
- func ConcatMessageStream(s *StreamReader[*Message]) (*Message, error)
- func ConcatMessages(msgs []*Message) (*Message, error)
- func SystemMessage(content string) *Message
- func ToolMessage(content string, toolCallID string, opts ...ToolMessageOption) *Message
- func UserMessage(content string) *Message
- type MessageInputAudio
- type MessageInputFile
- type MessageInputImage
- type MessageInputPart
- type MessageInputVideo
- type MessageJSONParseConfig
- type MessageJSONParser
- type MessageOutputAudio
- type MessageOutputImage
- type MessageOutputPart
- type MessageOutputVideo
- type MessageParseFrom
- type MessageParser
- type MessagePartCommon
- type MessagesTemplate
- type ParameterInfo
- type ParamsOneOf
- type PromptTokenDetails
- type ResponseMeta
- type RoleType
- type SourceEOF
- type StreamReader
- func InternalMergeNamedStreamReaders[T any](srs []*StreamReader[T], names []string) *StreamReader[T]
- func MergeNamedStreamReaders[T any](srs map[string]*StreamReader[T]) *StreamReader[T]
- func MergeStreamReaders[T any](srs []*StreamReader[T]) *StreamReader[T]
- func StreamReaderFromArray[T any](arr []T) *StreamReader[T]
- func StreamReaderWithConvert[T, D any](sr *StreamReader[T], convert func(T) (D, error), opts ...ConvertOption) *StreamReader[D]
- type StreamWriter
- type TokenUsage
- type ToolCall
- type ToolChoice
- type ToolInfo
- type ToolMessageOption
- type TopLogProb
Constants ¶
This section is empty.
Variables ¶
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.
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
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
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 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 ChatMessageAudioURL is used to represent an audio part in a chat message. Choose either URL or URI. If your model implementation supports it, URL could be used to embed inline audio data as defined in 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 ChatMessageFileURL is used to represent an file part in a chat message. 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 ChatMessageImageURL is used to represent an image part in a chat message. Choose either URL or URI. If your model implementation supports it, URL could be used to 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 the input of the model, use MessageInputPart, For the output of the model, use MessageOutputPart ChatMessagePart is the part in a chat message.
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 ChatMessageVideoURL is used to represent an video part in a chat message. Choose either URL or URI. If your model implementation supports it, URL could be used to embed inline video data as defined in 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 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.
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
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
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
ExtraInfo returns the extra info of the document. can use doc.WithExtraInfo() to set the extra info.
func (*Document) Score ¶
Score returns the score of the document. can use doc.WithScore() to set the score.
func (*Document) SparseVector ¶ added in v0.3.4
SparseVector returns the sparse vector of the document, key indices -> value vector. can use doc.WithSparseVector() to set the sparse vector.
func (*Document) SubIndexes ¶
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
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
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
WithExtraInfo sets the extra info of the document. can use doc.ExtraInfo() to get the extra info.
func (*Document) WithScore ¶
WithScore sets the score of the document. can use doc.Score() to get the score.
func (*Document) WithSparseVector ¶ added in v0.3.4
WithSparseVector sets the sparse vector of the document, key indices -> value vector. can use doc.SparseVector() to get the sparse vector.
func (*Document) WithSubIndexes ¶
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 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 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 ¶
AssistantMessage represents a message with Role "assistant".
func ConcatMessageArray ¶ added in v0.5.0
func ConcatMessageStream ¶ added in v0.3.23
func ConcatMessageStream(s *StreamReader[*Message]) (*Message, error)
func ConcatMessages ¶
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 ¶
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 ¶
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 ¶
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"`
}
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)
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" )
type MessageParser ¶
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 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 freamwork 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.
- use NewParamsOneOfByParams(): an intuitive way to describe the parameters that covers most of the use-cases.
- 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"`
}
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 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.
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]
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 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"`
}