Documentation
¶
Overview ¶
Package llmfactory constructs provider models from configuration, resolves assistant→model preferences (with optional per‑org overrides), and supports capability filtering and per‑org model restrictions.
Quick start
YAML configuration (providers, defaults, assistant mappings)
providers: - name: openai token: ${OPENAI_API_KEY} available_models: ["gpt-4o-mini", "gpt-4o"] open_ai: api_type: OPENAI - name: anthropic token: ${ANTHROPIC_API_KEY} available_models: ["claude-3-5-sonnet-20240620", "claude-3-5-haiku-latest"] open_ai: api_type: ANTHROPIC default_provider: openai assistant_models: default: ["openai/gpt-4o-mini"] coder: ["openai/gpt-4o", "anthropic/claude-3-5-sonnet-20240620"]
Load and get a model
fac, _ := llmfactory.Load("config.yaml") // Resolve model for the "coder" assistant, falling back to defaults llm, err := fac.GetModel(ctx, llmfactory.ModelOptions{AssistantName: "coder"}) _ = llm; _ = err
Enforce capabilities and per‑org restrictions
// Only providers that support JSON Schema + Tool Calls will be considered llm, _ = fac.GetModel(ctx, llmfactory.ModelOptions{ AssistantName: "coder", RequiredCapabilities: llms.CapabilityJSONSchema | llms.CapabilityToolCall, })
// Install a per‑org model filter (e.g., quotas) fac = fac.WithModelFilter(func(ctx context.Context, orgID, model string) bool { if orgID == "free-tier" && strings.Contains(model, "gpt-4o") { return false // block expensive models for this org } return true }) llm, _ = fac.GetModel(ctx, llmfactory.ModelOptions{AssistantName: "coder", OrgID: "free-tier"})
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NewLLM = CreateLLM
NewLLM is a wrapper for CreateLLM to allow for overriding the default implementation.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Providers specifies the list of providers to use
Providers []*ProviderConfig `json:"providers" yaml:"providers"`
// DefaultProvider specifies the default provider to use
DefaultProvider string `json:"default_provider" yaml:"default_provider"`
// AssistantModels specifies the mapping of assistants to models.
// key is the assistant name, value is the model name.
// The model name can be in the format of <provider_name>/<model_name>.
// Use `default: <model_name>` as the default model for assistants.
AssistantModels map[string][]string `json:"assistant_models" yaml:"assistant_models"`
// Orgs specifies the organizations configuration to override the global configuration.
Orgs map[string]*OrgConfig `json:"orgs_override" yaml:"orgs_override"`
// Skills specifies the skills configuration.
Skills *skills.Config `json:"skills,omitempty" yaml:"skills,omitempty"`
}
Config is the top-level factory configuration for providers, defaults, assistant→model mappings and optional per‑org overrides and skills.
type Factory ¶
type Factory interface {
// WithModelFilter sets a predicate used to restrict which models an org may use.
// Returns a new Factory with the filter applied.
WithModelFilter(filter ModelFilterFunc) Factory
// GetModel returns an LLM model that matches the given options.
//
// Resolution rules:
// - When ProviderType is set, a provider of that type is selected.
// - Otherwise, when AssistantName is set, the configured assistant model
// mapping (optionally per-org) is expanded into the preferred models.
// - The preferred models are tried in order; the first available and
// allowed model wins.
// - If no preferred model matches, the default model is returned.
//
// RequiredCapabilities, when non-zero, restricts the candidates to
// providers whose type supports ALL of the requested capabilities.
GetModel(ctx context.Context, opts ModelOptions) (llms.Model, error)
// Skills returns all loaded skills for the given agent sorted alphabetically by name.
// Use tags to filter skills by tags. The Skill must have all the tags provided.
Skills(agent string, tags ...string) skills.Skills
}
Factory is the interface for creating and managing LLM models. In multi-tenant environments, the OrgID is used to determine the LLM model to use for the organization. The factory can also be provided with a ModelFilterFunc to restrict which models an organization may use.
type HTTPClient ¶ added in v0.16.114
HTTPClient is primarily used to describe an *http.Client, but also supports custom implementations.
For bespoke implementations, prefer using an *http.Client with a custom transport. See http.RoundTripper for further information.
type ModelFilterFunc ¶ added in v0.19.141
ModelFilterFunc reports whether the given model may be used for the org. Provide this to enforce per-org / per-model quota: return false when the model must not be used for the org (e.g. quota exceeded), true otherwise. The orgID can be empty, in which case the check applies globally. The modelName can be in the format of <provider_name>/<model_name>.
type ModelOptions ¶ added in v0.19.141
type ModelOptions struct {
// ProviderType specifies the provider type to use.
// If not specified, a matching provider will be used.
ProviderType llms.ProviderType
// OrgID specifies the organization ID to use,
// if configuration provides Org overrides.
OrgID string
// AssistantName specifies the assistant name to use.
AssistantName string
// PreferredModels specifies the preferred models to use.
PreferredModels []string
// RequiredCapabilities specifies the required capabilities the model must support.
// When non-zero, only providers whose type supports ALL of the requested
// capabilities are considered.
RequiredCapabilities llms.Capability
}
type OpenAIConfig ¶
type OpenAIConfig struct {
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
APIVersion string `json:"api_version,omitempty" yaml:"api_version,omitempty"`
// APIType specifies the type of API to use:
// OPENAI|AZURE|AZURE_AD|CLOUDFLARE|ANTHROPIC|GOOGLEAI|BEDROCK|PERPLEXITY
APIType string `json:"api_type,omitempty" yaml:"api_type,omitempty"`
}
OpenAIConfig specifies API parameters for OpenAI‑style providers. APIType selects the provider family: OPENAI|AZURE|AZURE_AD|CLOUDFLARE|ANTHROPIC|GOOGLEAI|BEDROCK|PERPLEXITY.
type Option ¶ added in v0.16.114
type Option func(*Options)
Option configures Options.
func WithAWSConfigFactory ¶ added in v0.16.114
func WithHTTPClient ¶ added in v0.16.114
func WithHTTPClient(client HTTPClient) Option
WithHTTPClient allows setting a custom HTTP client. If not set, the default value is http.DefaultClient.
func WithModelFilter ¶ added in v0.19.141
func WithModelFilter(filter ModelFilterFunc) Option
WithModelFilter sets a predicate used to restrict which models an org may use, for example to enforce per-org / per-model quota.
type Options ¶ added in v0.16.114
type Options struct {
// HTTPClient is used to create a new HTTP client.
HTTPClient HTTPClient
// AwsConfigFactory is used to create a new AWS config.
AwsConfigFactory func() (*aws.Config, error)
// ModelFilter reports whether a model may be used for an org,
// e.g. to enforce per-org / per-model quota.
ModelFilter ModelFilterFunc
}
Options customize model construction for providers that need extra clients or environment hooks, and allow installing per‑org model filters.
func NewOptions ¶ added in v0.16.114
type OrgConfig ¶ added in v0.19.141
type OrgConfig struct {
// AssistantModels specifies the mapping of assistants to models.
// key is the assistant name, value is the model name.
// The model name can be in the format of <provider_name>/<model_name>.
// Use `default: <model_name>` as the default model for assistants.
AssistantModels map[string][]string `json:"assistant_models" yaml:"assistant_models"`
}
OrgConfig defines assistant→model mappings that override global mappings for a given organization.
type ProviderConfig ¶
type ProviderConfig struct {
Name string `json:"name" yaml:"name"`
Token string `json:"token,omitempty" yaml:"token,omitempty"`
DefaultModel string `json:"default_model,omitempty" yaml:"default_model,omitempty"`
AvailableModels []string `json:"available_models,omitempty" yaml:"available_models,omitempty"`
OpenAI OpenAIConfig `json:"open_ai" yaml:"open_ai"`
}
ProviderConfig defines a single provider instance and its available models. The OpenAI field conveys the API style for both OpenAI proper and OpenAI‑compatible APIs (Azure, Perplexity, Cloudflare, etc.).