llm

package
v0.2.21 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssetSource

type AssetSource string

AssetSource defines the way the asset is provided.

const (
	SourceURL    AssetSource = "url"
	SourceBase64 AssetSource = "base64"
	SourceRaw    AssetSource = "raw"
)

type BackoffAdvisor added in v0.2.9

type BackoffAdvisor interface {
	// AdviseBackoff returns a suggested backoff duration and whether the error is
	// retryable for the given attempt index (0-based). Returning retry=false means
	// the error should not be retried by this advisor.
	AdviseBackoff(err error, attempt int) (delay time.Duration, retry bool)
}

BackoffAdvisor is an optional interface that a Model may implement to advise the caller about retry/backoff policy for specific provider/model errors.

When implemented, the core retry loop may consult AdviseBackoff to decide whether to retry on a given error and how long to wait before the next attempt. The returned duration is a suggested delay; if retry is false the advice is ignored.

type CallSpan added in v0.2.0

type CallSpan struct {
	StartedAt time.Time `json:"startedat"`
	EndedAt   time.Time `json:"endedat"`
}

func (*CallSpan) SetEnd added in v0.2.0

func (c *CallSpan) SetEnd(t time.Time)

type Choice

type Choice struct {
	// Index is the index of the choice.
	Index int `json:"index"`

	// Message is the generated message.
	Message Message `json:"message"`

	// FinishReason is the reason why the generation stopped.
	FinishReason string `json:"finish_reason,omitempty"`
}

Choice represents a single response choice from a chat-based LLM.

type ContentItem

type ContentItem struct {
	Name string `json:"name,omitempty"`

	// Type indicates the type of the content.
	Type ContentType `json:"type"`

	// Source indicates how the asset is provided (url, base64, raw bytes).
	Source AssetSource `json:"source"`

	// Data is the actual content of the asset.
	// - For SourceURL: URL as string.
	// - For SourceBase64: Base64-encoded data.
	// - For SourceRaw: Raw binary data (usually base64 encoded or omitted in JSON).
	Data string `json:"data,omitempty"`

	MimeType string `json:"mimeType,omitempty"`

	// Metadata is optional structured metadata (e.g., for video timestamps, image detail levels).
	Metadata map[string]interface{} `json:"metadata,omitempty"`

	// Legacy fields for backward compatibility
	Text string `json:"text,omitempty"`
}

ContentItem is a universal representation of any content asset in the message.

func NewBinaryContent

func NewBinaryContent(data []byte, mimeType, name string) ContentItem

NewBinaryContent creates a new binary content item from raw data.

func NewContentItem

func NewContentItem(contentType ContentType) ContentItem

NewContentItem creates a new content item with the specified type.

func NewImageContent

func NewImageContent(imageURL string, detail string) ContentItem

NewImageContent creates a new image content item.

func NewTextContent

func NewTextContent(text string) ContentItem

NewTextContent creates a new text content item.

type ContentType

type ContentType string

ContentType defines the supported asset types.

const (
	ContentTypeText   ContentType = "text"
	ContentTypeImage  ContentType = "image"
	ContentTypeVideo  ContentType = "video"
	ContentTypePDF    ContentType = "pdf"
	ContentTypeAudio  ContentType = "audio"
	ContentTypeBinary ContentType = "binary"

	// Legacy content types for backward compatibility
	ContentTypeImageURL ContentType = "image_url"
)

type Finder

type Finder interface {
	Find(ctx context.Context, id string) (Model, error)
}

type FunctionCall

type FunctionCall struct {
	// Name is the name of the function to call.
	Name string `json:"name"`

	// Arguments is a JSON string containing the arguments to pass to the function.
	Arguments string `json:"arguments"`
}

FunctionCall represents a function call made by the assistant. Deprecated: Use ToolCall instead.

func NewFunctionCall

func NewFunctionCall(name string, args map[string]interface{}) FunctionCall

NewFunctionCall creates a FunctionCall with the given name and arguments.

type GenerateRequest

type GenerateRequest struct {
	// Messages is the list of messages in the conversation.
	Messages []Message `json:"messages"`

	// Options contains additional options for the request.
	Options *Options `json:"options,omitempty"`

	// PreviousResponseID optionally links this request to a prior provider
	// response when the backend supports incremental continuation (e.g.,
	// OpenAI Responses API).
	PreviousResponseID string `json:"previous_response_id,omitempty"`
}

GenerateRequest represents a request to a chat-based LLM. It is designed to be compatible with various LLM providers.

type GenerateResponse

type GenerateResponse struct {
	// Choices contains the generated responses.
	Choices []Choice `json:"choices"`

	// Usage contains token usage information.
	Usage *Usage `json:"usage,omitempty"`
	Model string `json:"model,omitempty"`

	// ResponseID is a provider response identifier when available.
	ResponseID string `json:"response_id,omitempty"`
}

GenerateResponse represents a response from a chat-based LLM. It is designed to be compatible with various LLM providers.

type ImageURL

type ImageURL struct {
	// URL is the URL of the image.
	URL string `json:"url"`

	// Detail specifies the detail level for image analysis.
	// Options: "auto", "low", "high"
	Detail string `json:"detail,omitempty"`
}

ImageURL represents an image referenced by URL. Deprecated: Use ContentItem with ContentTypeImage and SourceURL instead.

type Matcher

type Matcher interface {
	Best(preferences *ModelPreferences) string
}

type Message

type Message struct {
	// Role of the sender (user, assistant, system, etc.)
	Role MessageRole `json:"role"`

	// Name is the optional sender/tool name.
	Name string `json:"name,omitempty"`

	// Items contains multiple, diverse content assets.
	Items []ContentItem `json:"items,omitempty"`

	// ToolCalls represents structured function/tool calls.
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`

	// Legacy fields for backward compatibility
	Content      string        `json:"content,omitempty"`
	ToolCallId   string        `json:"tool_call_id,omitempty"`
	ContentItems []ContentItem `json:"content_items,omitempty"`
	FunctionCall *FunctionCall `json:"function_call,omitempty"`
}

Message is a generic message suitable for multiple content items and types.

func NewAssistantMessage

func NewAssistantMessage(content string) Message

NewAssistantMessage creates a new message with the "assistant" role. NewAssistantMessage creates a new message with the "assistant" role.

func NewAssistantMessageWithToolCalls

func NewAssistantMessageWithToolCalls(toolCalls ...ToolCall) Message

NewAssistantMessageWithToolCalls creates an assistant message that includes tool calls.

func NewMessageWithBinary added in v0.2.0

func NewMessageWithBinary(role MessageRole, data []byte, mimeType, content, name string) Message

NewMessageWithBinary creates a message that includes binary data and optional text prompt.

func NewSystemMessage

func NewSystemMessage(content string) Message

NewSystemMessage creates a new message with the "system" role. NewSystemMessage creates a new message with the "system" role.

func NewTextMessage added in v0.2.0

func NewTextMessage(role MessageRole, content string) Message

NewTextMessage creates a text-only message for the given role.

func NewToolMessage

func NewToolMessage(name, content string) Message

NewToolMessage creates a new message with the "tool" role. NewToolMessage creates a new message with the "tool" role.

func NewToolResultMessage

func NewToolResultMessage(call ToolCall) Message

NewToolResultMessage creates a tool role message with the given tool call's ID and result content.

func NewUserMessage

func NewUserMessage(content string) Message

NewUserMessage creates a new message with the "user" role. NewUserMessage creates a new message with the "user" role.

func NewUserMessageWithBinary

func NewUserMessageWithBinary(data []byte, mimeType, prompt, name string) Message

NewUserMessageWithBinary creates a new user message that includes binary data and optional text prompt.

func NewUserMessageWithImage

func NewUserMessageWithImage(text, imageURL string, detail string) Message

NewUserMessageWithImage creates a new message with the "user" role that includes both text and an image. NewUserMessageWithImage creates a new message with the "user" role that includes both text and an image.

type MessageRole

type MessageRole string

MessageRole represents the role of the message sender.

const (
	RoleSystem    MessageRole = "system"
	RoleUser      MessageRole = "user"
	RoleAssistant MessageRole = "assistant"
	RoleFunction  MessageRole = "function"
	RoleTool      MessageRole = "tool"
)

func (MessageRole) String added in v0.2.0

func (m MessageRole) String() string

type Messages added in v0.2.18

type Messages []Message

func (*Messages) Append added in v0.2.18

func (m *Messages) Append(msg Message)

type Model

type Model interface {
	Generate(ctx context.Context, request *GenerateRequest) (*GenerateResponse, error)
	Implements(feature string) bool
}

Model represents a Large Language Model client capable of generating content.

type ModelPreferences

type ModelPreferences struct {
	IntelligencePriority float64  `yaml:"intelligencePriority,omitempty" json:"intelligencePriority,omitempty"`
	SpeedPriority        float64  `yaml:"speedPriority,omitempty" json:"speedPriority,omitempty"`
	CostPriority         float64  `yaml:"costPriority,omitempty" json:"costPriority,omitempty"`
	Hints                []string `yaml:"hints,omitempty" json:"hints,omitempty"`
}

ModelPreferences expresses caller priorities (0..1) + optional name hints.

func NewModelPreferences

func NewModelPreferences(options ...ModelPreferencesOption) *ModelPreferences

type ModelPreferencesOption

type ModelPreferencesOption func(*ModelPreferences)

ModelPreferencesOption // is a functional option for ModelPreferences.

func WithPreferences

func WithPreferences(preferences *schema.ModelPreferences) ModelPreferencesOption

type ModelSelection added in v0.2.0

type ModelSelection struct {
	Model       string            `yaml:"model,omitempty" json:"model,omitempty"`
	Preferences *ModelPreferences `yaml:"modelPreferences,omitempty" json:"modelPreferences,omitempty"`
	Options     *Options          `yaml:"options,omitempty" json:"options,omitempty"`
}

type Options

type Options struct {
	// Model is the model to use.
	Model string `json:"model" yaml:"model"`

	// CandidateCount is the number of response candidates to generate.
	CandidateCount int `json:"candidate_count" yaml:"candidate_count"`

	// MaxTokens is the maximum number of tokens to generate.
	MaxTokens int `json:"max_tokens" yaml:"max_tokens"`

	// Temperature is the temperature for sampling, between 0 and 1.
	Temperature float64 `json:"temperature" yaml:"temperature"`

	// StopWords is a list of words to stop on.
	StopWords []string `json:"stop_words" yaml:"stop_words"`

	// TopK is the number of tokens to consider for top-k sampling.
	TopK int `json:"top_k" yaml:"top_k"`

	// TopP is the cumulative probability for top-p sampling.
	TopP float64 `json:"top_p" yaml:"top_p"`

	// Seed is a seed for deterministic sampling.
	Seed int `json:"seed" yaml:"seed"`

	// MinLength is the minimum length of the generated text.
	MinLength int `json:"min_length" yaml:"min_length"`

	// MaxLength is the maximum length of the generated text.
	MaxLength int `json:"max_length" yaml:"max_length"`

	// N is how many chat completion choices to generate for each input message.
	N int `json:"n" yaml:"n"`

	// RepetitionPenalty is the repetition penalty for sampling.
	RepetitionPenalty float64 `json:"repetition_penalty" yaml:"repetition_penalty"`

	// FrequencyPenalty is the frequency penalty for sampling.
	FrequencyPenalty float64 `json:"frequency_penalty" yaml:"frequency_penalty"`

	// PresencePenalty is the presence penalty for sampling.
	PresencePenalty float64 `json:"presence_penalty" yaml:"presence_penalty"`

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

	// Tools is a list of tools to use. Each tool can be a specific tool or a function.
	Tools []Tool `json:"tools,omitempty" yaml:"tools,omitempty"`

	// ToolChoice is the choice of tool to use: "none", "auto" (default), or a specific tool.
	ToolChoice ToolChoice `json:"tool_choice,omitempty" yaml:"tool_choice,omitempty"`

	// 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]interface{} `json:"metadata,omitempty" yaml:"metadata,omitempty"`

	// ResponseMIMEType MIME type of the generated candidate text.
	// Supported MIME types: text/plain (default), application/json.
	ResponseMIMEType string `json:"response_mime_type,omitempty" yaml:"response_mime_type,omitempty"`

	Thinking *Thinking `json:"thinking,omitempty" yaml:"thinking,omitempty"`
	// Reasoning configures the model's reasoning behavior, e.g. summarization of chain-of-thought.
	Reasoning *Reasoning `json:"reasoning,omitempty" yaml:"reasoning,omitempty"`

	// Stream enables streaming responses.
	Stream bool `json:"stream,omitempty" yaml:"stream,omitempty"`

	Mode string `json:"mode,omitempty" yaml:"mode,omitempty"`

	// ParallelToolCalls requests the provider to execute multiple tool calls
	// in parallel within a single reasoning step, when supported by the model.
	// This is honored by providers that implement base.CanExecToolsInParallel
	// (e.g., OpenAI). Others will ignore it.
	ParallelToolCalls bool `json:"parallel_tool_calls,omitempty" yaml:"parallelToolCalls,omitempty"`

	// ContinuationEnabled explicitly enables/disables provider continuation
	// via previous_response_id for models that support it. When nil, it
	// defaults to enabled if the model supports continuation.
	ContinuationEnabled bool `json:"continuationEnabled,omitempty" yaml:"continuationEnabled,omitempty"`
}

type Reasoning

type Reasoning struct {
	// Effort controls provider-native reasoning depth. For OpenAI o-series
	// valid values are: "low" | "medium" | "high".
	Effort  string `json:"effort,omitempty" yaml:"effort,omitempty"`
	Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
}

Reasoning specifies options for the model's internal reasoning process. Summary may be set to "auto" to request an automatic summary of chain-of-thought.

type StreamEvent

type StreamEvent struct {
	Response *GenerateResponse
	Err      error
}

StreamEvent represents a partial or complete event in a streaming LLM response. Response holds the generated partial or final content, Err indicates a streaming error.

type StreamingModel

type StreamingModel interface {
	// Stream sends a chat request with streaming enabled and returns a channel of StreamEvent.
	Stream(ctx context.Context, request *GenerateRequest) (<-chan StreamEvent, error)
}

StreamingModel is an optional interface for LLM providers that support streaming responses.

type Thinking

type Thinking struct {
	Type         string `json:"type" yaml:"type"`
	BudgetTokens int    `json:"budget_tokens,omitempty" yaml:"budget_tokens,omitempty"`
}

type Tool

type Tool struct {
	Ref     string `json:"ref,omitempty" yaml:"ref"`
	Pattern string `json:"pattern,omitempty" yaml:"pattern"`
	// Type is the type of the tool. Currently, only "function" is supported.
	Type string `json:"type" yaml:"type"`

	// Function is the function definition for this tool.
	// This follows the OpenAPI schema specification.
	Definition ToolDefinition `json:"definition" yaml:"definition"`
}

Tool represents a tool that can be used by an LLM. It follows the OpenAPI specification for defining tools.

func NewFunctionTool

func NewFunctionTool(definition ToolDefinition) Tool

NewFunctionTool creates a new Tool representing a callable function.

type ToolCall

type ToolCall struct {
	// ID is a unique identifier for the tool call.
	ID string `json:"id,omitempty"`

	// Name is the name of the tool to call.
	Name string `json:"name"`

	// Arguments contains the arguments to pass to the tool.
	Arguments map[string]interface{} `json:"arguments"`

	// Legacy fields for backward compatibility
	Type     string       `json:"type,omitempty"`
	Function FunctionCall `json:"function,omitempty"`

	Result string `json:"result,omitempty"`
	//Error tool call error
	Error string `json:"error,omitempty"`
}

ToolCall is a structured representation of a function/tool invocation.

func NewToolCall

func NewToolCall(id string, name string, args map[string]interface{}, result string) ToolCall

NewToolCall creates a ToolCall with the given function name and arguments. An ID is generated automatically and legacy fields are populated for backward compatibility.

type ToolChoice

type ToolChoice struct {
	// Type is the type of the tool choice. It can be "none", "auto", or "function".
	Type string `json:"type"`

	// Function is the function to call if Type is "function".
	Function *ToolChoiceFunction `json:"function,omitempty"`
}

ToolChoice represents a choice of tool to use. It can be "none", "auto", or a specific tool.

func NewAutoToolChoice

func NewAutoToolChoice() ToolChoice

NewAutoToolChoice creates a new ToolChoice with "auto" type.

func NewFunctionToolChoice

func NewFunctionToolChoice(name string) ToolChoice

NewFunctionToolChoice creates a new ToolChoice with "function" type and the given function name.

func NewNoneToolChoice

func NewNoneToolChoice() ToolChoice

NewNoneToolChoice creates a new ToolChoice with "none" type.

type ToolChoiceFunction

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

ToolChoiceFunction represents a function to call in a tool choice.

type ToolDefinition

type ToolDefinition struct {
	// Name is the name of the function to be called.
	Name string `json:"name" yaml:"name"`

	// Description is a description of what the function does.
	Description string `json:"description,omitempty" yaml:"description,omitempty"`

	// Parameters is a JSON Schema object that defines the input parameters the function accepts.
	// This follows the OpenAPI schema specification.
	Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`

	// Required is a list of required parameters.
	Required []string `json:"required,omitempty" yaml:"required"`

	OutputSchema map[string]interface{} `json:"output_schema,omitempty" yaml:"output_schema,omitempty"` // Output schema for the function
}

ToolDefinition represents a function that can be called by an LLM. It follows the OpenAPI specification for defining functions.

func ToolDefinitionFromMcpTool

func ToolDefinitionFromMcpTool(tool *mcpschema.Tool) *ToolDefinition

ToolDefinitionFromMcpTool convert mcp tool into llm tool

func (*ToolDefinition) Normalize added in v0.2.6

func (d *ToolDefinition) Normalize()

Normalize ensures provider-agnostic schema validity: - parameters is always a JSON object with type=object and properties=object - output_schema is always a JSON object with type=object and properties=object

type Usage

type Usage struct {
	// PromptTokens is the number of tokens used in the prompt.
	PromptTokens int `json:"prompt_tokens"`

	// CompletionTokens is the number of tokens used in the completion.
	CompletionTokens int `json:"completion_tokens"`

	// TotalTokens is the total number of tokens used.
	TotalTokens int `json:"total_tokens"`

	// ContextTokens is the list of token IDs used in the model context (Ollama-specific).
	ContextTokens []int `json:"context_tokens,omitempty"`

	CachedTokens int `json:"cached_tokens,omitempty"`

	ReasoningTokens int `json:"reasoning_tokens,omitempty"`

	AudioTokens int `json:"audio_tokens,omitempty"`

	// Provider-detailed usage (OpenAI-compatible)
	// Prompt-level cached tokens (from prompt_tokens_details.cached_tokens)
	PromptCachedTokens int `json:"prompt_cached_tokens,omitempty"`
	// Prompt-level audio tokens (from prompt_tokens_details.audio_tokens)
	PromptAudioTokens int `json:"prompt_audio_tokens,omitempty"`
	// Completion-level reasoning tokens (from completion_tokens_details.reasoning_tokens)
	CompletionReasoningTokens int `json:"completion_reasoning_tokens,omitempty"`
	// Completion-level audio tokens (from completion_tokens_details.audio_tokens)
	CompletionAudioTokens int `json:"completion_audio_tokens,omitempty"`
	// Speculative decoding accepted/rejected prediction tokens
	AcceptedPredictionTokens int `json:"accepted_prediction_tokens,omitempty"`
	RejectedPredictionTokens int `json:"rejected_prediction_tokens,omitempty"`
}

Usage contains token usage information.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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