model

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package model defines LoopKit's provider-agnostic model domain types.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTable = Table{
	"claude-3-5-sonnet-20241022": {
		Input:     3.0,
		Output:    15.0,
		CacheRead: 1.5,
	},
	"gpt-4o": {
		Input:     2.5,
		Output:    10.0,
		CacheRead: 1.25,
	},
	"gpt-4o-mini": {
		Input:     0.15,
		Output:    0.6,
		CacheRead: 0.075,
	},
	"gemini-1.5-pro": {
		Input:     1.25,
		Output:    5.0,
		CacheRead: 0.625,
	},
	"gemini-1.5-flash": {
		Input:     0.075,
		Output:    0.3,
		CacheRead: 0.0375,
	},
}

DefaultTable contains pricing for common language models. Prices are in USD per million tokens.

Functions

This section is empty.

Types

type Chunk

type Chunk struct {
	// Delta is the incremental content in this chunk.
	Delta Part `json:"delta"`
	// Usage is the cumulative usage (only set in the final chunk; nil otherwise).
	Usage *Usage `json:"usage,omitempty"`
}

Chunk represents a streamed chunk of a response.

type Message

type Message struct {
	// Role indicates who sent this message.
	Role Role `json:"role"`
	// Content is the list of content parts in this message.
	Content []Part `json:"content"`
}

Message represents a message in a conversation.

type Part

type Part struct {
	// Kind indicates the type of content in this part.
	Kind PartKind `json:"kind"`
	// Text contains plain text content (used with PartKindText).
	Text string `json:"text,omitempty"`
	// ImageURL contains a URL to an image (used with PartKindImage).
	ImageURL string `json:"image_url,omitempty"`
	// ImageData contains base64-encoded image data (used with PartKindImage).
	ImageData string `json:"image_data,omitempty"`
	// ImageMIME contains the MIME type of the image (used with PartKindImage).
	ImageMIME string `json:"image_mime,omitempty"`
	// ToolUseID is the unique identifier of a tool invocation (used with PartKindToolUse).
	ToolUseID string `json:"tool_use_id,omitempty"`
	// ToolName is the name of the tool being invoked (used with PartKindToolUse).
	ToolName string `json:"tool_name,omitempty"`
	// ToolArgs contains the JSON arguments for the tool invocation (used with PartKindToolUse).
	ToolArgs json.RawMessage `json:"tool_args,omitempty"`
	// ToolResultID is the ID of the tool invocation this result corresponds to (used with PartKindToolResult).
	ToolResultID string `json:"tool_result_id,omitempty"`
	// ToolResultContent contains the result content from the tool (used with PartKindToolResult).
	ToolResultContent string `json:"tool_result_content,omitempty"`
	// IsError indicates whether this tool result is an error (used with PartKindToolResult).
	IsError bool `json:"is_error,omitempty"`
}

Part represents a single piece of content within a message.

type PartKind

type PartKind string

PartKind represents the type of content in a message part.

const (
	// PartKindText is plain text content.
	PartKindText PartKind = "text"
	// PartKindImage is image content.
	PartKindImage PartKind = "image"
	// PartKindToolUse is a tool invocation.
	PartKindToolUse PartKind = "tool_use"
	// PartKindToolResult is a tool result/response.
	PartKindToolResult PartKind = "tool_result"
)

type PricePerMillionTokens

type PricePerMillionTokens struct {
	// Input is the cost per million input tokens.
	Input float64
	// Output is the cost per million output tokens.
	Output float64
	// CacheRead is the cost per million cache-read tokens.
	CacheRead float64
}

PricePerMillionTokens represents pricing for a model in USD per million tokens.

type Request

type Request struct {
	// Model is the model identifier (e.g., "claude-3-5-sonnet-20241022", "gpt-4o").
	Model string `json:"model"`
	// System is optional system/instruction context prepended to the messages.
	System string `json:"system,omitempty"`
	// Messages is the conversation history.
	Messages []Message `json:"messages"`
	// Tools is the list of tools the model can invoke.
	Tools []ToolSchema `json:"tools,omitempty"`
	// Temperature controls randomness in model outputs (0 to 1, where 1 is max randomness).
	Temperature *float64 `json:"temperature,omitempty"`
	// MaxTokens is the maximum number of tokens to generate in the response.
	MaxTokens int `json:"max_tokens,omitempty"`
	// StopSeqs are sequences that will cause generation to stop early.
	StopSeqs []string `json:"stop_seqs,omitempty"`
}

Request represents an API request to invoke a language model.

type Response

type Response struct {
	// Message is the model's response message.
	Message Message `json:"message"`
	// StopReason indicates why generation stopped.
	StopReason StopReason `json:"stop_reason"`
	// Usage is the token usage and cost information.
	Usage Usage `json:"usage"`
}

Response represents an API response from a language model.

type Role

type Role string

Role represents the role of a message participant in a conversation.

const (
	// RoleSystem is the system/instruction role.
	RoleSystem Role = "system"
	// RoleUser is the user message role.
	RoleUser Role = "user"
	// RoleAssistant is the assistant/model response role.
	RoleAssistant Role = "assistant"
	// RoleTool is the tool result response role.
	RoleTool Role = "tool"
)

type StopReason

type StopReason string

StopReason indicates why model generation stopped.

const (
	// StopReasonEndTurn indicates the model finished its turn naturally.
	StopReasonEndTurn StopReason = "end_turn"
	// StopReasonToolUse indicates the model invoked a tool.
	StopReasonToolUse StopReason = "tool_use"
	// StopReasonMaxTokens indicates generation stopped due to reaching max_tokens.
	StopReasonMaxTokens StopReason = "max_tokens"
	// StopReasonStopSeq indicates generation stopped due to a stop sequence.
	StopReasonStopSeq StopReason = "stop_seq"
)

type Table

type Table map[string]PricePerMillionTokens

Table maps model IDs to their pricing information.

func (Table) Cost

func (t Table) Cost(modelID string, u Usage) (float64, bool)

Cost calculates the USD cost for a model and usage. Returns (0, false) if the model is not in the pricing table.

type ToolSchema

type ToolSchema struct {
	// Name is the unique identifier of the tool.
	Name string `json:"name"`
	// Description explains what the tool does.
	Description string `json:"description"`
	// InputSchema is the JSON schema describing the tool's input parameters.
	InputSchema json.RawMessage `json:"input_schema"`
}

ToolSchema describes a tool that the model can invoke.

type Usage

type Usage struct {
	// InputTokens is the number of input tokens processed.
	InputTokens int `json:"input_tokens"`
	// OutputTokens is the number of output tokens generated.
	OutputTokens int `json:"output_tokens"`
	// CacheReadTokens is the number of tokens read from cache (omitted if zero).
	CacheReadTokens int `json:"cache_read_tokens,omitempty"`
	// CostUSD is the cost of the request in USD (omitted if zero).
	CostUSD float64 `json:"cost_usd,omitempty"`
}

Usage tracks token usage and cost for a request or response.

func (Usage) Add

func (u Usage) Add(other Usage) Usage

Add returns a new Usage with all fields summed from u and other.

Jump to

Keyboard shortcuts

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