ai

package
v0.107.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AttachmentImage = "image"
	AttachmentFile  = "file"
)

AttachmentKind names the provider-neutral attachment classes.

View Source
const (
	APIChatCompletions = "openai-completions"
	APIResponses       = "openai-responses"
	APIMessages        = "anthropic-messages"
	APIGemini          = "google-generative-ai"
	APIGoogleGemini    = "google-gemini"
)
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 AttachmentData added in v0.107.0

func AttachmentData(p ContentPart) (mimeType, value string, isData bool, err error)

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 ObserveRuntimeDiagnostic added in v0.107.0

func ObserveRuntimeDiagnostic(ctx context.Context, event RuntimeDiagnostic)

func PartTokens

func PartTokens(p ContentPart) int

func SupportedAPI added in v0.107.0

func SupportedAPI(api string) bool

func WithRuntimeDiagnosticObserver added in v0.107.0

func WithRuntimeDiagnosticObserver(ctx context.Context, observe func(RuntimeDiagnostic)) context.Context

func WithSearchObserver added in v0.107.0

func WithSearchObserver(ctx context.Context, observer func(HostedSearch)) context.Context

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
	IsFullURL  bool
	ModelsURL  string
	Headers    map[string]string
}

type ContentPart

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

func FilePart added in v0.107.0

func FilePart(mimeType, filename string, data []byte) ContentPart

func ImagePart

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

func (ContentPart) Attachment added in v0.107.0

func (p ContentPart) Attachment() bool

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 Gemini added in v0.107.0

type Gemini struct {
	BaseURL    string
	APIKey     string
	IsFullURL  bool
	ModelsURL  string
	Headers    http.Header
	HTTP       *http.Client
	MaxRetries int
	OnRetry    func(RetryEvent)
	// contains filtered or unexported fields
}

Gemini implements Google's native generateContent API while exposing the same provider-neutral Client interface as the other adapters.

func NewGemini added in v0.107.0

func NewGemini(baseURL, apiKey string) *Gemini

func NewGeminiWithOptions added in v0.107.0

func NewGeminiWithOptions(baseURL, apiKey string, isFullURL bool, modelsURL string, headers map[string]string) *Gemini

func (*Gemini) Clone added in v0.107.0

func (c *Gemini) Clone() Client

func (*Gemini) Complete added in v0.107.0

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

func (*Gemini) Endpoint added in v0.107.0

func (c *Gemini) Endpoint() string

func (*Gemini) Models added in v0.107.0

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

func (*Gemini) SetCacheKey added in v0.107.0

func (c *Gemini) SetCacheKey(string)

func (*Gemini) SetMaxRetries added in v0.107.0

func (c *Gemini) SetMaxRetries(n int)

func (*Gemini) SetOnRetry added in v0.107.0

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

func (*Gemini) Stream added in v0.107.0

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

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 HostedSearch added in v0.107.0

type HostedSearch struct {
	Type     string         `json:"type"`
	ID       string         `json:"id"`
	Provider string         `json:"provider,omitempty"`
	Status   string         `json:"status"`
	Queries  []string       `json:"queries"`
	Sources  []SearchSource `json:"sources"`
	Error    string         `json:"error,omitempty"`
}

type Message

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

	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
	IsFullURL bool
	ModelsURL string
	Headers   http.Header
	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 NewWithOptions added in v0.107.0

func NewWithOptions(baseURL, apiKey string, full bool, modelsURL string, headers map[string]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 {
	NativeWebSearch      bool      `json:"-"`
	NativeSearchProvider string    `json:"-"`
	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 RuntimeDiagnostic added in v0.107.0

type RuntimeDiagnostic struct {
	Kind        string
	Provider    string
	From        string
	To          string
	Endpoint    string
	Attempt     int
	TargetIndex int
	Error       string
}

RuntimeDiagnostic carries routing facts only; headers and credentials are excluded.

type SearchSource added in v0.107.0

type SearchSource struct {
	URL        string `json:"url"`
	Title      string `json:"title,omitempty"`
	SourceType string `json:"sourceType,omitempty"`
}

type StopReason added in v0.103.0

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

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