llm

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedMessageType = errors.New("unexpected message type")

ErrUnexpectedMessageType is returned when a chat message is of an unexpected type.

Functions

func GetBufferString

func GetBufferString(messages []Message) (string, error)

GetBufferString gets the buffer string of messages.

Types

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type FunctionCallBehavior

type FunctionCallBehavior string

FunctionCallBehavior is the behavior to use when calling functions.

const (
	// FunctionCallBehaviorNone will not call any functions.
	FunctionCallBehaviorNone FunctionCallBehavior = "none"
	// FunctionCallBehaviorAuto will call functions automatically.
	FunctionCallBehaviorAuto FunctionCallBehavior = "auto"
)

type FunctionDefinition

type FunctionDefinition struct {
	// Name is the name of the function.
	Name string `json:"name"`
	// Description is a description of the function.
	Description string `json:"description"`
	// Parameters is a list of parameters for the function.
	Parameters any `json:"parameters,omitempty"`
	// Strict is a flag to indicate if the function should be called strictly. Only used for openai llm structured output.
	Strict bool `json:"strict,omitempty"`
}

FunctionDefinition is a definition of a function that can be called by the model.

type FunctionReference

type FunctionReference struct {
	// Name is the name of the function.
	Name string `json:"name"`
}

FunctionReference is a reference to a function.

type GenerateOption

type GenerateOption func(*GenerateOptions)

GenerateOption is a function that configures a GenerateOptions.

func WithCandidateCount

func WithCandidateCount(c int) GenerateOption

WithCandidateCount specifies the number of response candidates to generate.

func WithFrequencyPenalty

func WithFrequencyPenalty(frequencyPenalty float32) GenerateOption

WithFrequencyPenalty will add an option to set the frequency penalty for sampling.

func WithJSONMode

func WithJSONMode() GenerateOption

WithJSONMode will add an option to set the response format to JSON. This is useful for models that return structured data.

func WithLogProbes added in v0.2.0

func WithLogProbes(probe bool) GenerateOption

func WithMaxLength

func WithMaxLength(maxLength int) GenerateOption

WithMaxLength will add an option to set the maximum length of the generated text.

func WithMaxTokens

func WithMaxTokens(maxTokens int) GenerateOption

WithMaxTokens specifies the max number of tokens to generate.

func WithMetadata

func WithMetadata(metadata map[string]string) GenerateOption

WithMetadata will add an option to set metadata to include in the request. The meaning of this field is specific to the backend in use.

func WithMinLength

func WithMinLength(minLength int) GenerateOption

WithMinLength will add an option to set the minimum length of the generated text.

func WithModel

func WithModel(model string) GenerateOption

WithModel specifies which model name to use.

func WithN

func WithN(n int) GenerateOption

WithN will add an option to set how many chat completion choices to generate for each input message.

func WithOptions

func WithOptions(options GenerateOptions) GenerateOption

WithOptions specifies options.

func WithParallelToolCalls

func WithParallelToolCalls(parallel bool) GenerateOption

func WithPresencePenalty

func WithPresencePenalty(presencePenalty float32) GenerateOption

WithPresencePenalty will add an option to set the presence penalty for sampling.

func WithReasoningStreamingFunc added in v0.2.0

func WithReasoningStreamingFunc(streamingFunc func(ctx context.Context, chunk []byte) error) GenerateOption

WithReasoningStreamingFunc specifies the streaming function for reasoning to use.

func WithRepetitionPenalty

func WithRepetitionPenalty(repetitionPenalty float32) GenerateOption

WithRepetitionPenalty will add an option to set the repetition penalty for sampling.

func WithResponseMIMEType

func WithResponseMIMEType(responseMIMEType string) GenerateOption

WithResponseMIMEType will add an option to set the ResponseMIMEType Currently only supported by googleai llms.

func WithSeed

func WithSeed(seed int) GenerateOption

WithSeed will add an option to use deterministic sampling.

func WithStopWords

func WithStopWords(stopWords []string) GenerateOption

WithStopWords specifies a list of words to stop generation on.

func WithStreamingFunc

func WithStreamingFunc(streamingFunc func(ctx context.Context, chunk []byte) error) GenerateOption

WithStreamingFunc specifies the streaming function to use.

func WithTemperature

func WithTemperature(temperature float32) GenerateOption

WithTemperature specifies the model temperature, a hyperparameter that regulates the randomness, or creativity, of the AI's responses.

func WithToolChoice

func WithToolChoice(choice any) GenerateOption

WithToolChoice will add an option to set the choice of tool to use. It can either be "none", "auto" (the default behavior), or a specific tool as described in the ToolChoice type.

func WithTools

func WithTools(tools []Tool) GenerateOption

WithTools will add an option to set the tools to use.

func WithTopK

func WithTopK(topK int) GenerateOption

WithTopK will add an option to use top-k sampling.

func WithTopLogProbs added in v0.2.0

func WithTopLogProbs(top int) GenerateOption

func WithTopP

func WithTopP(topP float64) GenerateOption

WithTopP will add an option to use top-p sampling.

type GenerateOptions

type GenerateOptions struct {
	// Model is the model to use.
	Model string `json:"model"`
	// CandidateCount is the number of response candidates to generate.
	CandidateCount int `json:"candidate_count"`
	// MaxTokens is the maximum number of tokens to generate.
	MaxTokens int `json:"max_tokens"`
	// Temperature is the temperature for sampling, between 0 and 1.
	Temperature float32 `json:"temperature"`
	// StopWords is a list of words to stop on.
	StopWords []string `json:"stop_words"`
	// StreamingFunc is a function to be called for each chunk of a streaming response.
	// Return an error to stop streaming early.
	StreamingFunc          func(ctx context.Context, chunk []byte) error `json:"-"`
	ReasoningStreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
	// TopK is the number of tokens to consider for top-k sampling.
	TopK int `json:"top_k"`
	// TopP is the cumulative probability for top-p sampling.
	TopP float64 `json:"top_p"`
	// Seed is a seed for deterministic sampling.
	Seed int `json:"seed"`
	// MinLength is the minimum length of the generated text.
	MinLength int `json:"min_length"`
	// MaxLength is the maximum length of the generated text.
	MaxLength int `json:"max_length"`
	// N is how many chat completion choices to generate for each input message.
	N int `json:"n"`
	// RepetitionPenalty is the repetition penalty for sampling.
	RepetitionPenalty float32 `json:"repetition_penalty"`
	// FrequencyPenalty is the frequency penalty for sampling.
	FrequencyPenalty float32 `json:"frequency_penalty"`
	// PresencePenalty is the presence penalty for sampling.
	PresencePenalty float32 `json:"presence_penalty"`

	// JSONMode is a flag to enable JSON mode.
	JSONMode bool `json:"json"`

	// Tools is a list of tools to use. Each tool can be a specific tool or a function.
	Tools []Tool `json:"tools,omitempty"`
	// ParallelToolCalls Whether to enable parallel function calling during tool use.
	ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
	// ToolChoice is the choice of tool to use, it can either be "none", "auto" (the default behavior), or a specific tool as described in the ToolChoice type.
	ToolChoice any `json:"tool_choice"`

	// Metadata is a map of metadata to include in the request.
	// The meaning of this field is specific to the backend in use.
	Metadata map[string]string `json:"metadata,omitempty"`

	// ResponseMIMEType MIME type of the generated candidate text.
	// Supported MIME types are: text/plain: (default) Text output.
	// application/json: JSON response in the response candidates.
	ResponseMIMEType string `json:"response_mime_type,omitempty"`

	LogProbs bool `json:"logprobs,omitempty"`
	// TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each
	// token position, each with an associated log probability.
	// logprobs must be set to true if this parameter is used.
	TopLogProbs int `json:"top_logprobs,omitempty"`
}

GenerateOptions is a set of options for calling models. Not all models support all options.

func DefaultGenerateOption

func DefaultGenerateOption() *GenerateOptions

type Generation

type Generation struct {
	// Text is the generated text.
	Role             string `json:"role"`
	Content          string `json:"content"`
	StopReason       string `json:"stop_reason"`
	ReasoningContent string `json:"reasoning_content"`
	// GenerationInfo prepared field
	GenerationInfo map[string]any `json:"generation_info"`
	// ToolCalls is a list of tool calls the model asks to invoke.
	ToolCalls []ToolCall
	Usage     *Usage
	LogProbs  *openai.ChatCompletionStreamChoiceLogprobs
}

type LLM

type LLM interface {
	Generate(ctx context.Context, prompt string, options ...GenerateOption) (*Generation, error)
	GenerateContent(ctx context.Context, messages []Message, options ...GenerateOption) (*Generation, error)
}

type Message

type Message struct {
	Role       MessageType
	Name       string     `json:"name,omitempty"`
	ToolCallId string     `json:"tool_call_id,omitempty"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
	// Content is the content of the message.
	Content string `json:"content,omitempty"`
}

Message is a message sent by an assistant.

func NewAssistantMessage

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

func NewSystemMessage

func NewSystemMessage(name, content string) *Message

func NewToolMessage

func NewToolMessage(id, content string) *Message

func NewUserMessage

func NewUserMessage(name, content string) *Message

type MessageType

type MessageType string

MessageType is the type of chat message.

const (
	// MessageTypeUser is a message sent by a human.
	MessageTypeUser MessageType = "user"
	// MessageTypeSystem is a message sent by the system.
	MessageTypeSystem MessageType = "system"
	// MessageTypeAssistant is a message sent by the assistant.
	MessageTypeAssistant MessageType = "assistant"
	// MessageTypeTool is a message sent by a tool.
	MessageTypeTool MessageType = "tool"
)

type Tool

type Tool struct {
	// Type is the type of the tool.
	Type string `json:"type"`
	// Function is the function to call.
	Function *FunctionDefinition `json:"function,omitempty"`
}

Tool is a tool that can be used by the model.

type ToolCall

type ToolCall struct {
	// ID is the unique identifier of the tool call.
	ID string `json:"id"`
	// Type is the type of the tool call. Typically, this would be "function".
	Type string `json:"type"`
	// Function is the function call to be executed.
	Function *FunctionCall `json:"function,omitempty"`
}

ToolCall is a call to a tool (as requested by the model) that should be executed.

type ToolChoice

type ToolChoice struct {
	// Type is the type of the tool.
	Type string `json:"type"`
	// Function is the function to call (if the tool is a function).
	Function *FunctionReference `json:"function,omitempty"`
}

ToolChoice is a specific tool to use.

type Usage

type Usage struct {
	CompletionTokens int `json:"completion_tokens,omitempty"`
	PromptTokens     int `json:"prompt_tokens,omitempty"`
	TotalTokens      int `json:"total_tokens,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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