ai

package
v0.106.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	APIChatCompletions = "openai-completions"
	APIResponses       = "openai-responses"
	APIMessages        = "anthropic-messages"
)
View Source
const (
	NormalizeMaxDim = 2000

	NormalizeMaxBytes = 5 * 1024 * 1024

	NormalizeMaxPixels = 64_000_000
)
View Source
const DefaultMaxAttempts = 8
View Source
const ImageTokenFloor = 85

Variables

This section is empty.

Functions

func DecodeImageSize

func DecodeImageSize(data []byte) (w, h int, ok bool)

func ImageTokens

func ImageTokens(w, h int) int

func IsContextLimit

func IsContextLimit(err error) bool

func NormalizeImage

func NormalizeImage(ext string, data []byte) (string, []byte)

func PartTokens

func PartTokens(p ContentPart) int

Types

type Anthropic

type Anthropic struct{ *OpenAI }

func NewAnthropic

func NewAnthropic(baseURL, apiKey string) *Anthropic

func (*Anthropic) Clone

func (c *Anthropic) Clone() Client

func (*Anthropic) Complete

func (c *Anthropic) Complete(ctx context.Context, req Request) (string, Usage, error)

func (*Anthropic) Models

func (c *Anthropic) Models(ctx context.Context) ([]ModelInfo, error)

func (*Anthropic) Stream

func (c *Anthropic) Stream(ctx context.Context, req Request, onText, onThink func(string), onToolCall func(id, name, args string)) (Message, Usage, error)

type CacheOptions

type CacheOptions struct {
	Retention       string
	SessionAffinity bool
	ControlFormat   string
	SupportsLong    bool
}

type Client

type Client interface {
	Models(context.Context) ([]ModelInfo, error)
	Stream(context.Context, Request, func(string), func(string), func(id, name, args string)) (Message, Usage, error)
	Complete(context.Context, Request) (string, Usage, error)

	Clone() Client

	SetCacheKey(string)

	Endpoint() string
}

func NewClient

func NewClient(opts ClientOptions) (Client, error)

type ClientOptions

type ClientOptions struct {
	API        string
	BaseURL    string
	APIKey     string
	MaxRetries int
	Cache      CacheOptions
}

type ContentPart

type ContentPart struct {
	Type     string `json:"type"`
	Text     string `json:"text,omitempty"`
	ImageURL *struct {
		URL string `json:"url"`
	} `json:"image_url,omitempty"`
	W int `json:"w,omitempty"`
	H int `json:"h,omitempty"`
}

func ImagePart

func ImagePart(ext string, data []byte) ContentPart

func (ContentPart) DecodeDimensions

func (p ContentPart) DecodeDimensions() (w, h int, ok bool)

type Cost added in v0.103.0

type Cost struct {
	Input      float64 `json:"input"`
	Output     float64 `json:"output"`
	CacheRead  float64 `json:"cacheRead"`
	CacheWrite float64 `json:"cacheWrite"`
	Total      float64 `json:"total"`
}

func CalculateCost added in v0.103.0

func CalculateCost(u Usage, rates TokenRates) (Cost, bool)

type HTTPError

type HTTPError struct {
	Status      string
	Body        string
	StatusCode  int
	ShouldRetry string

	RetryAfter    time.Duration
	RetryAfterSet bool
}

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Message

type Message struct {
	Role       string        `json:"role"`
	Content    string        `json:"content"`
	Parts      []ContentPart `json:"-"`
	ToolCalls  []ToolCall    `json:"tool_calls,omitempty"`
	ToolCallID string        `json:"tool_call_id,omitempty"`

	Name string `json:"name,omitempty"`

	Authored bool `json:"authored,omitempty"`

	SentAt *time.Time `json:"sent_at,omitempty"`

	Usage *Usage `json:"usage,omitempty"`

	Model string `json:"model,omitempty"`

	StopReason    StopReason `json:"stop_reason,omitempty"`
	RawStopReason string     `json:"raw_stop_reason,omitempty"`

	RewoundFrom string `json:"rewound_from,omitempty"`
}

func (Message) ContentParts added in v0.103.0

func (m Message) ContentParts() []ContentPart

func (Message) MarshalJSON

func (m Message) MarshalJSON() ([]byte, error)

func (Message) TextContent

func (m Message) TextContent() string

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

type ModelInfo

type ModelInfo struct {
	ID                  string   `json:"id"`
	ContextLength       int      `json:"context_length,omitempty"`
	MaxCompletionTokens int      `json:"max_completion_tokens,omitempty"`
	ReasoningEfforts    []string `json:"reasoning_efforts,omitempty"`
	Pricing             *Pricing `json:"pricing,omitempty"`

	InputModalities []string `json:"input_modalities,omitempty"`
}

func (ModelInfo) SupportsVision

func (mi ModelInfo) SupportsVision() bool

type OpenAI

type OpenAI struct {
	BaseURL string
	APIKey  string
	HTTP    *http.Client

	MaxRetries int

	OnRetry func(RetryEvent)

	CacheKey                   string
	CacheRetention             string
	CacheSessionAffinity       bool
	CacheControlFormat         string
	SupportsLongCacheRetention bool
}

func New

func New(baseURL, apiKey string) *OpenAI

func (*OpenAI) Clone

func (c *OpenAI) Clone() Client

func (*OpenAI) Complete

func (c *OpenAI) Complete(ctx context.Context, req Request) (string, Usage, error)

func (*OpenAI) Endpoint

func (c *OpenAI) Endpoint() string

func (*OpenAI) Models

func (c *OpenAI) Models(ctx context.Context) ([]ModelInfo, error)

func (*OpenAI) SetCacheKey

func (c *OpenAI) SetCacheKey(k string)

func (*OpenAI) SetCacheOptions

func (c *OpenAI) SetCacheOptions(o CacheOptions)

func (*OpenAI) SetMaxRetries

func (c *OpenAI) SetMaxRetries(n int)

func (*OpenAI) SetOnRetry

func (c *OpenAI) SetOnRetry(fn func(RetryEvent))

func (*OpenAI) Stream

func (c *OpenAI) Stream(ctx context.Context, req Request, onText, onThink func(string), onToolCall func(id, name, args string)) (Message, Usage, error)

type OutputLimitError added in v0.103.0

type OutputLimitError struct {
	Reason string
}

func (*OutputLimitError) Error added in v0.103.0

func (e *OutputLimitError) Error() string

type Pricing

type Pricing struct {
	Prompt          string `json:"prompt"`
	Completion      string `json:"completion"`
	InputCacheRead  string `json:"input_cache_read,omitempty"`
	InputCacheWrite string `json:"input_cache_write,omitempty"`
}

func (Pricing) Rates

func (p Pricing) Rates() *TokenRates

type Request

type Request struct {
	Model           string    `json:"model"`
	Messages        []Message `json:"messages"`
	Tools           []Tool    `json:"tools,omitempty"`
	MaxTokens       int       `json:"max_tokens,omitempty"`
	ReasoningEffort string    `json:"reasoning_effort,omitempty"`

	Temperature *float64 `json:"temperature,omitempty"`
	TopP        *float64 `json:"top_p,omitempty"`
	Stream      bool     `json:"stream"`

	PromptCacheKey       string `json:"prompt_cache_key,omitempty"`
	PromptCacheRetention string `json:"prompt_cache_retention,omitempty"`
	StreamOptions        *struct {
		IncludeUsage bool `json:"include_usage"`
	} `json:"stream_options,omitempty"`
}

type Responses

type Responses struct{ *OpenAI }

func NewResponses

func NewResponses(baseURL, apiKey string) *Responses

func (*Responses) Clone

func (c *Responses) Clone() Client

func (*Responses) Complete

func (c *Responses) Complete(ctx context.Context, req Request) (string, Usage, error)

func (*Responses) Stream

func (c *Responses) Stream(ctx context.Context, req Request, onText, onThink func(string), onToolCall func(id, name, args string)) (Message, Usage, error)

type RetryEvent

type RetryEvent struct {
	Attempt int
	Max     int
	Delay   time.Duration
	Err     error
}

type StopReason added in v0.103.0

type StopReason string
const (
	StopReasonStop    StopReason = "stop"
	StopReasonLength  StopReason = "length"
	StopReasonToolUse StopReason = "tool_use"
	StopReasonOther   StopReason = "other"
)

type TokenRates added in v0.103.0

type TokenRates struct {
	Input      float64 `json:"input"`
	Output     float64 `json:"output"`
	CacheRead  float64 `json:"cacheRead"`
	CacheWrite float64 `json:"cacheWrite"`
}

func (TokenRates) Valid added in v0.103.0

func (r TokenRates) Valid() bool

type Tool

type Tool struct {
	Type     string `json:"type"`
	Function struct {
		Name        string          `json:"name"`
		Description string          `json:"description"`
		Parameters  json.RawMessage `json:"parameters"`
	} `json:"function"`
}

func NewTool

func NewTool(name, desc, schema string) Tool

type ToolCall

type ToolCall struct {
	ID       string `json:"id"`
	Type     string `json:"type"`
	Function struct {
		Name      string `json:"name"`
		Arguments string `json:"arguments"`
	} `json:"function"`
	DurationMs int64 `json:"duration_ms,omitempty"`
	ExitCode   int   `json:"exit_code,omitempty"`
}

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`

	PromptTokensDetails *struct {
		CachedTokens int `json:"cached_tokens"`
	} `json:"prompt_tokens_details,omitempty"`
	PromptCacheHitTokens   int `json:"prompt_cache_hit_tokens,omitempty"`
	PromptCacheWriteTokens int `json:"prompt_cache_write_tokens,omitempty"`
}

func (*Usage) Add added in v0.103.0

func (u *Usage) Add(other Usage)

func (Usage) CacheWrite

func (u Usage) CacheWrite() int

func (Usage) Cached

func (u Usage) Cached() int

type UsageSummary added in v0.103.0

type UsageSummary struct {
	Total     Usage
	Models    map[string]Usage
	Subagents map[string]Usage
}

Jump to

Keyboard shortcuts

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