schema

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

ErrNoValue is the error returned when the value is not found. used in convert function when has WithInputKey option.

Functions

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

Types

type ChatMessageAudioURL

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

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

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

ChatMessageFileURL is used to represent an file part in a chat message. Choose either URL or URI.

type ChatMessageImageURL

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

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

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

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

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

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 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 OpenAPI v3.0.

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

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

func (d *Document) Score() float64

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

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

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

Vector returns the vector of the document. can use doc.WithVector() to set the vector.

func (*Document) VikingDSLInfo

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

VikingDSLInfo returns the dsl info of the document. can use doc.WithVikingDSLInfo() to set the dsl info.

func (*Document) VikingExtraInfo

func (d *Document) VikingExtraInfo() string

VikingExtraInfo returns the extra info of the document. can use doc.WithVikingExtraInfo() to set 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) 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.

func (*Document) WithVector

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

WithVector sets the vector of the document. can use doc.Vector() to get the vector.

func (*Document) WithVikingDSLInfo

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

WithVikingDSLInfo sets the dsl info of the document. can use doc.VikingDSLInfo() to get the dsl info.

func (*Document) WithVikingExtraInfo

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

WithVikingExtraInfo sets the extra info of the document. can use doc.VikingExtraInfo() to get the extra info.

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 Message

type Message struct {
	Role    RoleType `json:"role"`
	Content string   `json:"content"`

	// if MultiContent is not empty, use this instead of Content
	// if MultiContent is empty, use Content
	MultiContent []ChatMessagePart `json:"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"`

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

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

func AssistantMessage

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

AssistantMessage represents a message with Role "assistant".

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) *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 renderring 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 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)

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

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

func NewMessageJSONParser

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

NewMessageJSONParser creates a new MessageJSONParser.

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 {
	// deprecated: use NewParamsOneOfByParams instead, Params will no longer be exported in the future.
	Params map[string]*ParameterInfo

	// deprecated: use NewParamsOneOfByOpenAPIV3 instead, OpenAPIV3 will no longer be exported in the future.
	OpenAPIV3 *openapi3.Schema
}

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 Params: an intuitive way to describe the parameters that covers most of the use-cases.
  2. use OpenAPIV3: a formal way to describe the parameters that strictly adheres to OpenAPIV3.0 specification. See https://github.com/getkin/kin-openapi/blob/master/openapi3/schema.go.

func NewParamsOneOfByOpenAPIV3

func NewParamsOneOfByOpenAPIV3(openAPIV3 *openapi3.Schema) *ParamsOneOf

NewParamsOneOfByOpenAPIV3 creates a ParamsOneOf with *openapi3.Schema.

func NewParamsOneOfByParams

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

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

func (*ParamsOneOf) ToOpenAPIV3

func (p *ParamsOneOf) ToOpenAPIV3() (*openapi3.Schema, error)

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

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

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

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)) *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(). eg.

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

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

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

type TokenUsage

type TokenUsage struct {
	// PromptTokens is the number of tokens in the prompt.
	PromptTokens int `json:"prompt_tokens"`
	// CompletionTokens is the number of tokens in the completion.
	CompletionTokens int `json:"completion_tokens"`
	// TotalTokens is the total number of tokens in the request.
	TotalTokens int `json:"total_tokens"`
}

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

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

ToolInfo is the information of a tool.

Jump to

Keyboard shortcuts

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