models

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package models constructs provider-neutral model generators from a reusable collection of provider configurations.

Applications configure provider protocols and credentials once, then resolve llm.Generator values from llm.ModelInfo. Catalog stores immutable model metadata, protocol compatibility, and optional per-million-token pricing independently of credentials; resolved generators apply model compatibility and attach category costs to usage. FactoryRegistry supports immutable custom API registration and explicitly configured mixed-protocol provider routes. CatalogManager atomically publishes validated immutable snapshots from static baselines, persisted provider overlays, and conditional provider sources. BuiltinCatalog provides generated, validated metadata from the versioned source in models/catalogsource. Built-in profiles configure OpenRouter, Groq, DeepSeek, xAI, Cerebras, and Fireworks endpoints and protocol compatibility. CredentialStore defines concurrency-safe persistence, while CredentialManager coalesces provider OAuth refreshes and can be attached to Collection. AnthropicOAuth and CodexOAuth provide non-interactive PKCE exchange and refresh primitives; their default subscription endpoints/client IDs are provider-private, unofficial, unstable, and replaceable. Applications own browser, callback, and prompt UI. The concrete provider packages remain available for protocol-specific configuration that this package does not expose.

Index

Examples

Constants

View Source
const (
	ProviderOpenRouter = "openrouter"
	ProviderGroq       = "groq"
	ProviderDeepSeek   = "deepseek"
	ProviderXAI        = "xai"
	ProviderCerebras   = "cerebras"
	ProviderFireworks  = "fireworks"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API string

API identifies a provider wire protocol.

const (
	// OpenAIResponses selects OpenAI's Responses API.
	OpenAIResponses API = "openai-responses"
	// OpenAIChatCompletions selects the OpenAI-compatible Chat Completions API.
	OpenAIChatCompletions API = "openai-chat-completions"
	// OpenAICodexResponses selects the ChatGPT subscription Codex Responses API.
	OpenAICodexResponses API = "openai-codex-responses"
	// AnthropicMessages selects the Anthropic-compatible Messages API.
	AnthropicMessages API = "anthropic-messages"
	// GeminiGenerateContent selects the Gemini Developer API GenerateContent protocol.
	GeminiGenerateContent API = "gemini-generate-content"
)

type AnthropicOAuth

type AnthropicOAuth struct {
	HTTPClient   *http.Client
	AuthorizeURL string
	TokenURL     string
	ClientID     string
	Now          func() time.Time
}

AnthropicOAuth implements Anthropic's provider-private Claude subscription PKCE flow without owning a browser, callback server, or user interface. Its default endpoint and client ID are unofficial, unstable, and replaceable via the corresponding fields. It retains but never closes HTTPClient and starts no background work. Do not mutate its fields during use; HTTPClient and Now must support any concurrent calls made by the application.

func (AnthropicOAuth) Begin

func (o AnthropicOAuth) Begin(redirectURI string) (OAuthAuthorization, error)

Begin creates an Anthropic authorization URL and caller-owned PKCE state. Applications must retain the returned value and compare it during Exchange.

func (AnthropicOAuth) Exchange

func (o AnthropicOAuth) Exchange(ctx context.Context, authorization OAuthAuthorization, code, state string) (StoredCredential, error)

Exchange validates the retained authorization and callback values before exchanging an Anthropic authorization code. Invalid inputs perform no I/O.

func (AnthropicOAuth) Refresh

Refresh exchanges a current Anthropic OAuth refresh token. The returned credential is caller-owned.

func (AnthropicOAuth) RefreshConfig

func (o AnthropicOAuth) RefreshConfig(provider string, before time.Duration) CredentialRefreshConfig

RefreshConfig returns a CredentialManager configuration that retains a copy of o and invokes its Anthropic refresh flow.

type AvailableCredential

type AvailableCredential struct {
	Provider   string
	Type       CredentialType
	ExpiresAt  time.Time
	Attributes map[string]string
}

AvailableCredential contains no credential secrets.

type Catalog

type Catalog struct {
	// contains filtered or unexported fields
}

Catalog is an immutable collection of model metadata. It is safe for concurrent use.

Example
package main

import (
	"fmt"

	llm "github.com/XiaoConstantine/llm-go"
	"github.com/XiaoConstantine/llm-go/models"
)

func main() {
	catalog, err := models.NewCatalog(llm.Model{
		Provider:        "openai",
		ID:              "gpt-model",
		Name:            "GPT Model",
		API:             llm.APIOpenAIResponses,
		Capabilities:    []llm.Capability{llm.CapabilityStreaming, llm.CapabilityTools},
		ContextWindow:   128_000,
		MaxOutputTokens: 16_384,
		Reasoning:       true,
		Compatibility: &llm.ModelCompatibility{OpenAIResponses: &llm.OpenAIResponsesCompatibility{
			StrictTools: llm.CompatibilityEnabled,
		}},
	})
	if err != nil {
		panic(err)
	}

	info, ok := catalog.Model("openai", "gpt-model")
	fmt.Println(ok, info.Name, info.ContextWindow)
}
Output:
true GPT Model 128000

func BuiltinCatalog

func BuiltinCatalog() *Catalog

BuiltinCatalog returns the immutable generated model catalog. Catalog read methods return independently owned values.

Example
package main

import (
	"fmt"

	"github.com/XiaoConstantine/llm-go/models"
)

func main() {
	model, ok := models.BuiltinCatalog().Model(models.ProviderDeepSeek, "deepseek-v4-flash")
	fmt.Println(ok, model.Name, model.ContextWindow, model.Reasoning)
}
Output:
true DeepSeek V4 Flash 1000000 true

func NewCatalog

func NewCatalog(models ...llm.Model) (*Catalog, error)

NewCatalog constructs a Catalog. Provider, ID, and API are required and each provider/model pair must be unique. Name defaults to ID. Generation is added as the first capability when omitted.

func (*Catalog) Model

func (c *Catalog) Model(provider, model string) (llm.Model, bool)

Model looks up one model by provider and model ID. The returned value and its capability slice are owned by the caller.

func (*Catalog) Models

func (c *Catalog) Models(provider string) []llm.Model

Models returns catalog entries for provider in catalog order. An empty provider returns all entries. The returned values and their capability slices are owned by the caller.

type CatalogAvailabilityFilter

type CatalogAvailabilityFilter func(context.Context, AvailableCredential, []llm.Model) ([]llm.Model, error)

CatalogAvailabilityFilter selects available models using secret-free metadata. It must honor ctx, may be called concurrently, and receives owned inputs.

type CatalogFetchRequest

type CatalogFetchRequest struct {
	Provider     string
	ETag         string
	LastModified time.Time
	CheckedAt    time.Time
	FetchedAt    time.Time
	Force        bool
	Credential   *StoredCredential
}

CatalogFetchRequest supplies prior validators and current credential material. Sources must not retain Credential and must honor ctx.

type CatalogFetchResponse

type CatalogFetchResponse struct {
	Models       []llm.Model
	NotModified  bool
	ETag         string
	LastModified time.Time
}

CatalogFetchResponse is either NotModified or a complete provider model list.

type CatalogManager

type CatalogManager struct {
	// contains filtered or unexported fields
}

CatalogManager atomically publishes immutable catalog snapshots. It is safe for concurrent use. Refresh performs all work before returning, leaves no background goroutines, and therefore requires no Close method.

Example
package main

import (
	"context"
	"fmt"
	"time"

	llm "github.com/XiaoConstantine/llm-go"
	"github.com/XiaoConstantine/llm-go/models"
)

func main() {
	baseline, _ := models.NewCatalog(llm.Model{Provider: "acme", ID: "static", API: llm.APIOpenAIResponses})
	store, _ := models.NewMemoryCatalogStore(nil)
	manager, _ := models.NewCatalogManager(models.CatalogManagerConfig{
		Baseline: baseline, Store: store,
		Providers: []models.CatalogProvider{{Provider: "acme", Source: models.CatalogSourceFunc(func(ctx context.Context, request models.CatalogFetchRequest) (models.CatalogFetchResponse, error) {
			if err := ctx.Err(); err != nil {
				return models.CatalogFetchResponse{}, err
			}
			if request.ETag == `"v1"` {
				return models.CatalogFetchResponse{NotModified: true}, nil
			}
			return models.CatalogFetchResponse{Models: []llm.Model{{Provider: "acme", ID: "dynamic", API: llm.APIOpenAIChatCompletions}}, ETag: `"v1"`}, nil
		})}},
		Now: func() time.Time { return time.Unix(1, 0) },
	})
	manager.Refresh(context.Background(), models.CatalogRefreshOptions{})
	_, dynamic := manager.Model("acme", "dynamic")
	fmt.Println(dynamic)
}
Output:
true

func NewCatalogManager

func NewCatalogManager(config CatalogManagerConfig) (*CatalogManager, error)

NewCatalogManager validates config and owns snapshots of its baseline and provider metadata. It retains configured stores, managers, sources, filters, and callbacks, which must remain concurrency-safe while in use.

func (*CatalogManager) Available

func (m *CatalogManager) Available(ctx context.Context, provider string) ([]llm.Model, error)

Available returns caller-owned models whose providers have a usable managed credential. Filter callbacks receive only secret-free credential metadata.

Example
package main

import (
	"context"
	"fmt"

	llm "github.com/XiaoConstantine/llm-go"
	"github.com/XiaoConstantine/llm-go/models"
)

func main() {
	baseline, _ := models.NewCatalog(llm.Model{Provider: "acme", ID: "basic", API: llm.APIOpenAIResponses})
	credentials, _ := models.NewMemoryCredentialStore(map[string]models.StoredCredential{
		"acme": {Type: models.CredentialAPIKey, APIKey: "secret", Attributes: map[string]string{"tier": "basic"}},
	})
	managerCredentials, _ := models.NewCredentialManager(credentials)
	manager, _ := models.NewCatalogManager(models.CatalogManagerConfig{Baseline: baseline, Credentials: managerCredentials})
	available, _ := manager.Available(context.Background(), "acme")
	fmt.Println(available[0].ID)
}
Output:
basic

func (*CatalogManager) Model

func (m *CatalogManager) Model(provider, model string) (llm.Model, bool)

Model returns a caller-owned model from the current snapshot.

func (*CatalogManager) Models

func (m *CatalogManager) Models(provider string) []llm.Model

Models returns caller-owned models from the current snapshot. An empty provider returns all models.

func (*CatalogManager) Refresh

Refresh restores persisted state before optional network work. Omitted Providers refreshes configured dynamic providers; selective unknown/static providers and duplicate provider entries return deterministic skipped results. It runs selected providers concurrently and waits for all of them. A newer refresh supersedes and cancels older work for the same provider.

func (*CatalogManager) Snapshot

func (m *CatalogManager) Snapshot() *Catalog

Snapshot returns the current immutable catalog snapshot.

type CatalogManagerConfig

type CatalogManagerConfig struct {
	Baseline    *Catalog
	Store       CatalogStore
	Credentials *CredentialManager
	Providers   []CatalogProvider
	Now         func() time.Time
}

CatalogManagerConfig configures immutable snapshot publication.

type CatalogProvider

type CatalogProvider struct {
	Provider string
	Source   CatalogSource
	Filter   CatalogAvailabilityFilter
}

CatalogProvider configures a dynamic source and/or availability filter.

type CatalogRefreshOptions

type CatalogRefreshOptions struct {
	Providers []string
	Force     bool
	NoNetwork bool
}

CatalogRefreshOptions controls selective, force, and restore-only refreshes.

type CatalogRefreshResult

type CatalogRefreshResult struct{ Providers []ProviderRefreshResult }

CatalogRefreshResult contains provider results in selection order.

func (CatalogRefreshResult) ErrorMap

func (r CatalogRefreshResult) ErrorMap() map[string]error

ErrorMap returns the last non-nil error for each provider in the result.

type CatalogSource

type CatalogSource interface {
	Fetch(context.Context, CatalogFetchRequest) (CatalogFetchResponse, error)
}

CatalogSource fetches one provider's complete dynamic model list. Implementations must honor ctx, be safe for concurrent calls across manager operations, and transfer ownership of returned response storage to the caller.

type CatalogSourceFunc

type CatalogSourceFunc func(context.Context, CatalogFetchRequest) (CatalogFetchResponse, error)

CatalogSourceFunc adapts a function to CatalogSource.

func (CatalogSourceFunc) Fetch

Fetch calls f with the supplied context and request.

type CatalogStore

type CatalogStore interface {
	Read(context.Context, string) (CatalogStoreEntry, bool, error)
	Write(context.Context, string, CatalogStoreEntry) error
}

CatalogStore persists provider-scoped catalog snapshots. Implementations must own inputs/outputs, honor ctx, and never commit after cancellation.

type CatalogStoreEntry

type CatalogStoreEntry struct {
	Models       []llm.Model
	ETag         string
	LastModified time.Time
	CheckedAt    time.Time
	FetchedAt    time.Time
}

CatalogStoreEntry is one provider-scoped persisted dynamic catalog snapshot.

type CodexOAuth

type CodexOAuth struct {
	HTTPClient   *http.Client
	AuthorizeURL string
	TokenURL     string
	ClientID     string
	// Originator identifies the private browser flow and defaults to "llm-go",
	// matching the Codex client. It can be replaced for deployments that have an
	// assigned originator.
	Originator string
	Now        func() time.Time
}

CodexOAuth implements OpenAI's provider-private ChatGPT Codex PKCE flow without owning interactive UI. Its default endpoint and client ID are unofficial, unstable, and replaceable via the corresponding fields. It retains but never closes HTTPClient and starts no background work. Do not mutate its fields during use; HTTPClient and Now must support any concurrent calls made by the application.

func (CodexOAuth) Begin

func (o CodexOAuth) Begin(redirectURI string) (OAuthAuthorization, error)

Begin creates a Codex authorization URL and caller-owned PKCE state. Applications must retain the returned value and compare it during Exchange.

func (CodexOAuth) Exchange

func (o CodexOAuth) Exchange(ctx context.Context, authorization OAuthAuthorization, code, state string) (StoredCredential, error)

Exchange validates the retained authorization and callback values before exchanging a Codex authorization code. Invalid inputs perform no I/O.

func (CodexOAuth) Refresh

func (o CodexOAuth) Refresh(ctx context.Context, current StoredCredential) (StoredCredential, error)

Refresh exchanges a current Codex OAuth refresh token. The returned credential is caller-owned.

func (CodexOAuth) RefreshConfig

func (o CodexOAuth) RefreshConfig(provider string, before time.Duration) CredentialRefreshConfig

RefreshConfig returns a CredentialManager configuration that retains a copy of o and invokes its Codex refresh flow.

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

Collection is an immutable set of provider configurations. It is safe for concurrent use when its configured HTTP clients and credential resolvers are safe for concurrent use.

Example
package main

import (
	"fmt"

	llm "github.com/XiaoConstantine/llm-go"
	"github.com/XiaoConstantine/llm-go/models"
)

func main() {
	collection, err := models.New(models.ProviderConfig{
		ID:     "openai",
		API:    models.OpenAIResponses,
		APIKey: "key",
	})
	if err != nil {
		panic(err)
	}

	generator, err := collection.Generator(llm.ModelInfo{
		Provider:     "openai",
		Model:        "gpt-model",
		Capabilities: []llm.Capability{llm.CapabilityStreaming, llm.CapabilityTools},
	})
	if err != nil {
		panic(err)
	}

	info := generator.Info()
	fmt.Println(info.Provider, info.Model)
}
Output:
openai gpt-model

func New

func New(configs ...ProviderConfig) (*Collection, error)

New constructs a Collection. At least one provider is required. Provider IDs must be unique after surrounding whitespace is removed.

func NewWithCredentialManager

func NewWithCredentialManager(manager *CredentialManager, configs ...ProviderConfig) (*Collection, error)

NewWithCredentialManager constructs a Collection with the default registry.

func NewWithCredentialManagerAndRegistry

func NewWithCredentialManagerAndRegistry(manager *CredentialManager, registry *FactoryRegistry, configs ...ProviderConfig) (*Collection, error)

NewWithCredentialManagerAndRegistry constructs an immutable Collection.

func NewWithRegistry

func NewWithRegistry(registry *FactoryRegistry, configs ...ProviderConfig) (*Collection, error)

NewWithRegistry constructs a Collection using registry and no managed credentials.

func (*Collection) Generator

func (c *Collection) Generator(info llm.ModelInfo) (llm.Generator, error)

Generator constructs a provider-neutral generator for info. Provider and Model are required. Capabilities are validated by the selected protocol implementation.

func (*Collection) GeneratorContext

func (c *Collection) GeneratorContext(ctx context.Context, info llm.ModelInfo) (llm.Generator, error)

GeneratorContext constructs a generator after resolving any managed credential. Credential refresh honors ctx.

func (*Collection) GeneratorFor

func (c *Collection) GeneratorFor(model llm.Model) (llm.Generator, error)

GeneratorFor constructs a provider-neutral generator from a catalog model. The model's API must match its configured provider protocol.

type CredentialManager

type CredentialManager struct {
	// contains filtered or unexported fields
}

CredentialManager resolves credentials and refreshes OAuth credentials under the store's serialized provider update. It is safe for concurrent use when its store and refreshers are safe for concurrent use.

func NewCredentialManager

func NewCredentialManager(store CredentialStore, configs ...CredentialRefreshConfig) (*CredentialManager, error)

NewCredentialManager constructs a manager. Refresh configuration is optional; providers without one can resolve API keys and unexpired OAuth credentials. Provider IDs must be unique and canonical, and RefreshBefore must not be negative.

func (*CredentialManager) Resolve

func (m *CredentialManager) Resolve(ctx context.Context, provider string) (StoredCredential, bool, error)

Resolve returns the current credential for provider. It refreshes an OAuth credential when its access token is empty or its nonzero expiry is within the configured RefreshBefore interval. Concurrent Resolve calls through one manager are coalesced, including refresh failures. A refresh failure leaves the stored credential unchanged.

type CredentialMetadata

type CredentialMetadata struct {
	Provider string
	Type     CredentialType
}

CredentialMetadata describes a stored credential without exposing secrets.

type CredentialRefreshConfig

type CredentialRefreshConfig struct {
	Provider      string
	RefreshBefore time.Duration
	Refresh       CredentialRefresher
}

CredentialRefreshConfig configures OAuth refresh for one provider.

type CredentialRefresher

type CredentialRefresher func(ctx context.Context, current StoredCredential) (StoredCredential, error)

CredentialRefresher exchanges or renews an OAuth credential. It must honor ctx and must not call any method on a CredentialManager or CredentialStore. The returned credential must have type CredentialOAuth and a usable access token.

type CredentialResolver

type CredentialResolver func(ctx context.Context, rejectedAccessToken string) (Credentials, error)

CredentialResolver returns current token-based provider credentials. rejectedAccessToken is nonempty after a provider rejects a token with HTTP 401. A resolver must be safe for concurrent use.

type CredentialStore

type CredentialStore interface {
	Read(ctx context.Context, provider string) (StoredCredential, bool, error)
	List(ctx context.Context) ([]CredentialMetadata, error)
	Modify(ctx context.Context, provider string, update func(StoredCredential, bool) (*StoredCredential, error)) error
	Delete(ctx context.Context, provider string) error
}

CredentialStore persists one credential per provider. Modify must serialize updates for a provider, including across store instances backed by the same storage. Implementations must honor context cancellation while waiting for storage and must not commit an update after its context is done. A Modify callback must not call Modify or Delete on any CredentialStore.

type CredentialType

type CredentialType string

CredentialType identifies the representation of a stored credential.

const (
	CredentialAPIKey CredentialType = "api_key"
	CredentialOAuth  CredentialType = "oauth"
)

type Credentials

type Credentials struct {
	AccessToken string
	AccountID   string
}

Credentials contains current token-based provider credentials. AccountID may be empty when the selected protocol can derive it from AccessToken.

type FactoryCredentialResolver

type FactoryCredentialResolver func(context.Context) (StoredCredential, bool, error)

FactoryCredentialResolver returns a current provider credential. Custom factories may retain the callback but not returned secret-bearing values.

type FactoryRegistration

type FactoryRegistration struct {
	API     llm.API
	Factory GeneratorFactory
}

FactoryRegistration registers one nonempty API.

type FactoryRegistry

type FactoryRegistry struct {
	// contains filtered or unexported fields
}

FactoryRegistry is an immutable API-to-factory snapshot safe for concurrent use.

func NewFactoryRegistry

func NewFactoryRegistry(registrations ...FactoryRegistration) (*FactoryRegistry, error)

NewFactoryRegistry returns the five built-in factories plus registrations. Registering an API already present, including a built-in API, is an error.

Example
package main

import (
	"context"
	"fmt"

	llm "github.com/XiaoConstantine/llm-go"
	"github.com/XiaoConstantine/llm-go/models"
)

type exampleGenerator struct{ info llm.ModelInfo }

func (g *exampleGenerator) Info() llm.ModelInfo { return g.info }
func (g *exampleGenerator) Generate(context.Context, llm.Request) (*llm.Response, error) {
	return &llm.Response{Message: llm.Message{Role: llm.RoleAssistant}}, nil
}
func (g *exampleGenerator) Stream(context.Context, llm.Request) (llm.Stream, error) {
	return nil, fmt.Errorf("streaming is not implemented")
}

func main() {
	const customAPI llm.API = "acme-generate"
	registry, _ := models.NewFactoryRegistry(models.FactoryRegistration{API: customAPI, Factory: func(ctx context.Context, config models.GeneratorFactoryConfig) (llm.Generator, error) {
		if err := ctx.Err(); err != nil {
			return nil, err
		}
		return &exampleGenerator{info: llm.ModelInfo{Provider: config.Provider, Model: config.Model.Model, API: customAPI, Capabilities: []llm.Capability{llm.CapabilityGeneration}}}, nil
	}})
	collection, _ := models.NewWithRegistry(registry, models.ProviderConfig{
		ID: "acme", API: models.OpenAIChatCompletions,
		AdditionalAPIs: []models.ProviderAPIConfig{{API: customAPI}},
	})
	generator, _ := collection.Generator(llm.ModelInfo{Provider: "acme", Model: "special", API: customAPI})
	fmt.Println(generator.Info().API)
}
Output:
acme-generate

func (*FactoryRegistry) APIs

func (r *FactoryRegistry) APIs() []llm.API

APIs returns registered APIs in stable order.

type GeneratorFactory

type GeneratorFactory func(context.Context, GeneratorFactoryConfig) (llm.Generator, error)

GeneratorFactory constructs a generator for one API. It may be called concurrently, must honor ctx, and must not mutate config storage.

type GeneratorFactoryConfig

type GeneratorFactoryConfig struct {
	Provider           string
	API                llm.API
	Model              llm.ModelInfo
	APIKey             string
	Credentials        Credentials
	ResolveCredentials CredentialResolver
	ResolveCredential  FactoryCredentialResolver
	BaseURL            string
	HTTPClient         *http.Client
	Headers            http.Header
}

GeneratorFactoryConfig is an owned construction snapshot. Model, Headers, Credentials, and returned resolver values do not alias Collection internals.

type MemoryCatalogStore

type MemoryCatalogStore struct {
	// contains filtered or unexported fields
}

MemoryCatalogStore is a concurrency-safe in-memory CatalogStore.

func NewMemoryCatalogStore

func NewMemoryCatalogStore(initial map[string]CatalogStoreEntry) (*MemoryCatalogStore, error)

NewMemoryCatalogStore returns a store that owns a validated copy of initial.

func (*MemoryCatalogStore) Read

Read returns an owned copy of one provider entry.

func (*MemoryCatalogStore) Write

func (s *MemoryCatalogStore) Write(ctx context.Context, provider string, entry CatalogStoreEntry) error

Write validates and stores an owned copy of entry.

type MemoryCredentialStore

type MemoryCredentialStore struct {
	// contains filtered or unexported fields
}

MemoryCredentialStore is an in-memory CredentialStore. It is safe for concurrent use. Its zero value is not usable; use NewMemoryCredentialStore.

func NewMemoryCredentialStore

func NewMemoryCredentialStore(initial map[string]StoredCredential) (*MemoryCredentialStore, error)

NewMemoryCredentialStore constructs an in-memory store from initial values. The input map and credential attributes are copied. A nil map creates an empty store.

func (*MemoryCredentialStore) Delete

func (s *MemoryCredentialStore) Delete(ctx context.Context, provider string) error

Delete implements CredentialStore. Deleting an unknown provider succeeds.

func (*MemoryCredentialStore) List

List implements CredentialStore. Results are sorted by provider ID.

func (*MemoryCredentialStore) Modify

func (s *MemoryCredentialStore) Modify(ctx context.Context, provider string, update func(StoredCredential, bool) (*StoredCredential, error)) error

Modify implements CredentialStore. Returning nil from update deletes the credential. If update returns an error, the stored value is unchanged.

func (*MemoryCredentialStore) Read

Read implements CredentialStore.

type OAuthAuthorization

type OAuthAuthorization struct {
	URL         string
	State       string
	Verifier    string
	RedirectURI string
}

OAuthAuthorization is the non-interactive first half of an authorization-code PKCE flow. Applications present URL and retain State and Verifier until the redirect code is received.

type ProviderAPIConfig

type ProviderAPIConfig struct {
	API                llm.API
	APIKey             string
	Credentials        Credentials
	ResolveCredentials CredentialResolver
	BaseURL            string
	HTTPClient         *http.Client
	Headers            http.Header
}

ProviderAPIConfig configures one additional protocol route for a provider. Zero-valued connection and credential fields do not inherit from the default route; configure each route explicitly.

type ProviderConfig

type ProviderConfig struct {
	ID                 string
	API                API
	APIKey             string
	Credentials        Credentials
	ResolveCredentials CredentialResolver
	BaseURL            string
	HTTPClient         *http.Client
	Headers            http.Header
	// AdditionalAPIs explicitly enables model-selected protocols for this
	// provider. Its storage is copied by Collection construction.
	AdditionalAPIs []ProviderAPIConfig
}

ProviderConfig configures one provider. ID is the provider name used by llm.ModelInfo and llm.Error. API selects its wire protocol. APIKey configures API-key protocols. Credentials configures static token credentials for Codex Responses or Anthropic subscription OAuth. ResolveCredentials is supported only by Codex Responses; use CredentialManager for live Anthropic OAuth.

BaseURL, HTTPClient, and Headers are forwarded to the selected provider implementation. New copies Headers. The caller remains responsible for safe concurrent use of HTTPClient. A nil HTTPClient ultimately uses http.DefaultClient, so operations should have a context deadline when an unbounded request is not acceptable.

type ProviderProfile

type ProviderProfile struct {
	// contains filtered or unexported fields
}

ProviderProfile contains the protocol defaults for a built-in provider. Its fields are exposed through accessors so profiles remain immutable values.

func BuiltinProvider

func BuiltinProvider(id string) (ProviderProfile, bool)

BuiltinProvider returns protocol defaults for a canonical provider ID.

Example
package main

import (
	"fmt"

	llm "github.com/XiaoConstantine/llm-go"
	"github.com/XiaoConstantine/llm-go/models"
)

func main() {
	profile, ok := models.BuiltinProvider(models.ProviderDeepSeek)
	if !ok {
		panic("missing provider profile")
	}
	config := profile.Config("api-key")
	collection, err := models.New(config)
	if err != nil {
		panic(err)
	}
	generator, err := collection.Generator(llm.ModelInfo{Provider: models.ProviderDeepSeek, Model: "deepseek-model"})
	if err != nil {
		panic(err)
	}
	fmt.Println(generator.Info().Provider, profile.BaseURL())
}
Output:
deepseek https://api.deepseek.com

func BuiltinProviders

func BuiltinProviders() []ProviderProfile

BuiltinProviders returns all built-in profiles in stable order. The returned slice is owned by the caller.

func (ProviderProfile) API

func (p ProviderProfile) API() API

API returns the provider's wire protocol.

func (ProviderProfile) BaseURL

func (p ProviderProfile) BaseURL() string

BaseURL returns the provider's default API base URL.

func (ProviderProfile) Config

func (p ProviderProfile) Config(apiKey string) ProviderConfig

Config constructs an independently owned provider configuration. Callers may override its BaseURL, HTTPClient, or Headers before passing it to New.

func (ProviderProfile) ID

func (p ProviderProfile) ID() string

ID returns the canonical provider ID.

type ProviderRefreshResult

type ProviderRefreshResult struct {
	Provider    string
	Restored    bool
	Fetched     bool
	NotModified bool
	Published   bool
	Skipped     bool
	Err         error
}

ProviderRefreshResult describes one selected provider refresh.

type StoredCredential

type StoredCredential struct {
	Type         CredentialType
	APIKey       string
	AccessToken  string
	RefreshToken string
	AccountID    string
	ExpiresAt    time.Time
	Attributes   map[string]string
}

StoredCredential contains provider credentials. API-key credentials use APIKey. OAuth credentials require either AccessToken or RefreshToken and may include AccountID and ExpiresAt. Attributes holds provider-specific values. Stores must copy Attributes on input and output.

func (StoredCredential) Validate

func (c StoredCredential) Validate() error

Validate reports whether a stored credential is internally consistent.

Directories

Path Synopsis
cmd
cataloggen command

Jump to

Keyboard shortcuts

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