Documentation
¶
Overview ¶
Package proxy contains configuration, middleware, routing, and model interaction logic for the language model proxy server.
Index ¶
- Constants
- Variables
- func BuildRequestPayload(modelIdentifier string, rawRequestProfile string, combinedPrompt string, ...) any
- func BuildRouter(configuration Configuration, structuredLogger *zap.SugaredLogger) (*gin.Engine, error)
- func RenderManagementConfigUI(configuration ManagementConfiguration) string
- func Serve(configuration Configuration, structuredLogger *zap.SugaredLogger) error
- type Configuration
- type Endpoints
- func (endpointConfiguration *Endpoints) GetModelsURL() string
- func (endpointConfiguration *Endpoints) GetResponsesURL() string
- func (endpointConfiguration *Endpoints) GetTranscriptionsURL() string
- func (endpointConfiguration *Endpoints) ResetModelsURL()
- func (endpointConfiguration *Endpoints) ResetResponsesURL()
- func (endpointConfiguration *Endpoints) ResetTranscriptionsURL()
- func (endpointConfiguration *Endpoints) SetModelsURL(newURL string)
- func (endpointConfiguration *Endpoints) SetResponsesURL(newURL string)
- func (endpointConfiguration *Endpoints) SetTranscriptionsURL(newURL string)
- type HTTPDoer
- type LegacyTokenMigrationConfiguration
- type ManagementConfiguration
- type ModelConfiguration
- type ModelEndpointCatalog
- type ModelPayloadSchema
- type OpenAIClient
- type ProviderModelCatalog
- type ProviderModelCatalogs
- type Reasoning
- type ReasoningEffortCapability
- type TenantConfiguration
- type TenantDefaults
- type Tool
- type UpstreamRateLimitConfiguration
Constants ¶
const ( // DefaultPort is the TCP port used by the HTTP server when no explicit port is provided. DefaultPort = 8080 // DefaultWorkers is the maximum number of concurrent upstream HTTP operations. DefaultWorkers = 4 // DefaultQueueSize is the number of upstream HTTP operations that may wait for a worker. DefaultQueueSize = 100 // DefaultModel is the model identifier used when the client does not supply one. DefaultModel = ModelNameGPT41 // DefaultProvider is the provider identifier used when the client does not supply one. DefaultProvider = ProviderNameOpenAI // DefaultDictationProvider is the provider used when /dictate does not supply one. DefaultDictationProvider = ProviderNameOpenAI // DefaultRequestTimeoutSeconds is the overall app-side request timeout. DefaultRequestTimeoutSeconds = 360 // DefaultMaxPromptBytes limits JSON LLM request bodies accepted by POST /. DefaultMaxPromptBytes = 4 * 1024 * 1024 DefaultDictationModel = "gpt-4o-mini-transcribe" DefaultMaxInputAudioBytes = 25 * 1024 * 1024 DefaultManagementJWTIssuer = "tauth" // ManagementDatabaseDialectPostgres selects the GORM Postgres dialector. ManagementDatabaseDialectPostgres = "postgres" // ManagementDatabaseDialectSQLite selects the GORM SQLite dialector. ManagementDatabaseDialectSQLite = "sqlite" )
const ( // LogLevelDebug indicates that the application should log debug information. LogLevelDebug = "debug" // LogLevelInfo indicates that the application should log informational messages. LogLevelInfo = "info" )
const ( ManagementConfigUIFileName = "config-ui.yaml" ManagementConfigUIPath = "/" + ManagementConfigUIFileName )
const ( // ModelNameGPT4oMini identifies the GPT-4o-mini model. ModelNameGPT4oMini = "gpt-4o-mini" // ModelNameGPT4o identifies the GPT-4o model. ModelNameGPT4o = "gpt-4o" // ModelNameGPT41 identifies the GPT-4.1 model. ModelNameGPT41 = "gpt-4.1" // ModelNameGPT5Mini identifies the GPT-5-mini model. ModelNameGPT5Mini = "gpt-5-mini" // ModelNameGPT5 identifies the GPT-5 model which does not accept the temperature field. ModelNameGPT5 = "gpt-5" // ModelNameGPT55 identifies the GPT-5.5 model which does not accept the temperature field. ModelNameGPT55 = "gpt-5.5" // ModelNameGPT55Pro identifies the GPT-5.5 pro model which does not accept the temperature field. ModelNameGPT55Pro = "gpt-5.5-pro" // ModelNameGPT56 identifies the GPT-5.6 model which does not accept the temperature field. ModelNameGPT56 = "gpt-5.6" )
const ( // ProviderNameOpenAI identifies the OpenAI provider. ProviderNameOpenAI = "openai" // ProviderNameDeepSeek identifies the DeepSeek provider. ProviderNameDeepSeek = "deepseek" // ProviderNameDashScope identifies Alibaba Cloud Model Studio DashScope-compatible routing. ProviderNameDashScope = "dashscope" // ProviderNameQwenCloud identifies Qwen Cloud Token Plan routing. ProviderNameQwenCloud = "qwencloud" // ProviderNameMoonshot identifies Moonshot/Kimi routing. ProviderNameMoonshot = "moonshot" // ProviderNameMiniMax identifies MiniMax routing. ProviderNameMiniMax = "minimax" // ProviderNameSiliconFlow identifies SiliconFlow routing. ProviderNameSiliconFlow = "siliconflow" // ProviderNameZhipu identifies Zhipu/GLM routing. ProviderNameZhipu = "zhipu" // ProviderNameGemini identifies Google Gemini routing. ProviderNameGemini = "gemini" // ProviderNameAnthropic identifies Anthropic Claude routing. ProviderNameAnthropic = "anthropic" // ProviderNameMeta identifies Meta Model API routing. ProviderNameMeta = "meta" // ProviderNameGrok identifies xAI Grok routing. ProviderNameGrok = "grok" )
const ( // ModelNameDeepSeekV4Flash identifies the low-cost DeepSeek V4 flash model. ModelNameDeepSeekV4Flash = "deepseek-v4-flash" // ModelNameDeepSeekV4Pro identifies the higher-capability DeepSeek V4 pro model. ModelNameDeepSeekV4Pro = "deepseek-v4-pro" // ModelNameDeepSeekChat identifies the legacy DeepSeek chat model name. ModelNameDeepSeekChat = "deepseek-chat" // ModelNameDeepSeekReasoner identifies the legacy DeepSeek reasoner model name. ModelNameDeepSeekReasoner = "deepseek-reasoner" // ModelNameDashScopeQwenPlus identifies DashScope Qwen Plus. ModelNameDashScopeQwenPlus = "qwen-plus" // ModelNameQwenCloudQwen38MaxPreview identifies Qwen Cloud Token Plan's Qwen 3.8 Max preview. ModelNameQwenCloudQwen38MaxPreview = "qwen3.8-max-preview" // ModelNameMoonshotKimi identifies the Kimi K2 preview model. ModelNameMoonshotKimi = "kimi-k2-0905-preview" // ModelNameMiniMaxM27 identifies MiniMax M2.7. ModelNameMiniMaxM27 = "MiniMax-M2.7" // ModelNameSiliconFlowDeepSeek identifies SiliconFlow-hosted DeepSeek R1. ModelNameSiliconFlowDeepSeek = "deepseek-ai/DeepSeek-R1" // ModelNameZhipuGLM identifies the GLM 5.1 model. ModelNameZhipuGLM = "glm-5.1" // ModelNameGemini35Flash identifies Gemini 3.5 Flash. ModelNameGemini35Flash = "gemini-3.5-flash" // ModelNameGemini31FlashLite identifies Gemini 3.1 Flash-Lite. ModelNameGemini31FlashLite = "gemini-3.1-flash-lite" // ModelNameGemini25Flash identifies Gemini 2.5 Flash. ModelNameGemini25Flash = "gemini-2.5-flash" // ModelNameGemini25FlashLite identifies Gemini 2.5 Flash-Lite. ModelNameGemini25FlashLite = "gemini-2.5-flash-lite" // ModelNameGemini25Pro identifies Gemini 2.5 Pro. ModelNameGemini25Pro = "gemini-2.5-pro" // ModelNameClaudeOpus48 identifies Claude Opus 4.8. ModelNameClaudeOpus48 = "claude-opus-4-8" // ModelNameClaudeSonnet46 identifies Claude Sonnet 4.6. ModelNameClaudeSonnet46 = "claude-sonnet-4-6" // ModelNameClaudeHaiku45 identifies Claude Haiku 4.5. ModelNameClaudeHaiku45 = "claude-haiku-4-5-20251001" // ModelNameClaudeHaiku45Alias identifies the Claude Haiku 4.5 convenience alias. ModelNameClaudeHaiku45Alias = "claude-haiku-4-5" // ModelNameClaudeSonnet45 identifies Claude Sonnet 4.5. ModelNameClaudeSonnet45 = "claude-sonnet-4-5-20250929" // ModelNameClaudeSonnet45Alias identifies the Claude Sonnet 4.5 convenience alias. ModelNameClaudeSonnet45Alias = "claude-sonnet-4-5" // ModelNameClaudeOpus41 identifies Claude Opus 4.1. ModelNameClaudeOpus41 = "claude-opus-4-1-20250805" // ModelNameClaudeOpus41Alias identifies the Claude Opus 4.1 convenience alias. ModelNameClaudeOpus41Alias = "claude-opus-4-1" // ModelNameMuseSpark11 identifies Meta Muse Spark 1.1. ModelNameMuseSpark11 = "muse-spark-1.1" // ModelNameGrok43 identifies the current Grok 4.3 model. ModelNameGrok43 = "grok-4.3" // ModelNameGrok43Latest identifies the Grok 4.3 latest alias. ModelNameGrok43Latest = "grok-4.3-latest" // ModelNameGrokLatest identifies the current Grok latest alias. ModelNameGrokLatest = "grok-latest" // ModelNameGrokBuild01 identifies the Grok Build coding model. ModelNameGrokBuild01 = "grok-build-0.1" // ModelNameGrokCodeFast identifies the Grok code fast alias. ModelNameGrokCodeFast = "grok-code-fast" // ModelNameGrokCodeFast1 identifies the Grok code fast 1 alias. ModelNameGrokCodeFast1 = "grok-code-fast-1" // ModelNameGrokCodeFast10825 identifies the dated Grok code fast 1 model. ModelNameGrokCodeFast10825 = "grok-code-fast-1-0825" )
Variables ¶
var ( // SchemaGPT4oMini defines allowed payload fields for the GPT-4o-mini model. SchemaGPT4oMini = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyBackground, keyStore, keyTemperature}} // SchemaGPT4o defines allowed payload fields for the GPT-4o model. SchemaGPT4o = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyBackground, keyStore, keyTemperature, keyTools, keyToolChoice}} // SchemaGPT41 defines allowed payload fields for the GPT-4.1 model. SchemaGPT41 = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyBackground, keyStore, keyTemperature, keyTools, keyToolChoice}} // SchemaGPT5 defines allowed payload fields for the GPT-5 model. SchemaGPT5 = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyBackground, keyStore, keyTools, keyToolChoice, keyReasoning}} // SchemaGPT55 defines allowed payload fields for GPT-5.5 family models. SchemaGPT55 = SchemaGPT5 )
var ( // ErrUnknownProvider is returned when a request names a provider that is not registered. ErrUnknownProvider = errors.New(errorUnknownProvider) // ErrProviderNotConfigured is returned when a registered provider lacks a required server-side credential. ErrProviderNotConfigured = errors.New(errorProviderNotConfigured) // ErrUnsupportedCapability is returned when a request asks a provider for an unsupported capability. ErrUnsupportedCapability = errors.New(errorUnsupportedCapability) // ErrUnsupportedEndpoint is returned when a provider does not support the requested endpoint. ErrUnsupportedEndpoint = errors.New(errorUnsupportedEndpoint) // ErrConflictingModelParameters is returned when query and JSON body model values disagree. ErrConflictingModelParameters = errors.New(errorConflictingModelParameters) // ErrProviderRateLimited is returned when an upstream provider reports rate limiting. ErrProviderRateLimited = errors.New(errorProviderRateLimited) // ErrProviderAPI is returned when an upstream provider returns an unsuccessful response. ErrProviderAPI = errors.New(errorProviderAPI) // ErrInvalidChatMessages is returned when a JSON request body contains invalid chat messages. ErrInvalidChatMessages = errors.New(errorInvalidChatMessages) // ErrInvalidModelCatalog is returned when configured provider model catalogs are incomplete or inconsistent. ErrInvalidModelCatalog = errors.New("invalid_model_catalog") )
var ( ErrMissingTenants = errors.New("tenants must include at least one tenant") ErrInvalidTenant = errors.New("invalid tenant") ErrInvalidManagementConfiguration = errors.New("invalid management configuration") )
var ErrInvalidUpstreamRateLimitConfiguration = errors.New("invalid_upstream_rate_limit_configuration")
ErrInvalidUpstreamRateLimitConfiguration identifies an invalid shared upstream rate-limit rule.
var ErrUnknownModel = errors.New(errorUnknownModel)
ErrUnknownModel is returned when a model identifier is not recognized.
var ErrUpstreamIncomplete = errors.New(errorUpstreamIncomplete)
ErrUpstreamIncomplete indicates that the upstream provider returned an incomplete response before the request deadline.
Functions ¶
func BuildRequestPayload ¶
func BuildRequestPayload(modelIdentifier string, rawRequestProfile string, combinedPrompt string, webSearchEnabled bool, maxTokens *int, reasoningEffort string) any
BuildRequestPayload selects the correct OpenAI Responses payload shape for the configured request profile.
func BuildRouter ¶
func BuildRouter(configuration Configuration, structuredLogger *zap.SugaredLogger) (*gin.Engine, error)
BuildRouter constructs the HTTP router used by the proxy. configuration supplies queue sizes, worker counts, timeout values, API credentials and other settings. structuredLogger records structured log messages during routing.
func RenderManagementConfigUI ¶ added in v0.2.19
func RenderManagementConfigUI(configuration ManagementConfiguration) string
RenderManagementConfigUI renders the browser-facing llm-proxy, MPR UI, and TAuth YAML config.
func Serve ¶
func Serve(configuration Configuration, structuredLogger *zap.SugaredLogger) error
Serve builds the router from the supplied configuration and structuredLogger and starts the HTTP server on the configured port.
Types ¶
type Configuration ¶
type Configuration struct {
Tenants []TenantConfiguration
Management ManagementConfiguration
OpenAIKey string
DeepSeekKey string
DashScopeKey string
QwenCloudKey string
MoonshotKey string
MiniMaxKey string
SiliconFlowKey string
ZhipuKey string
GeminiKey string
AnthropicKey string
MetaKey string
GrokKey string
OpenAIBaseURL string
OpenAITranscriptionsURL string
DeepSeekBaseURL string
DashScopeBaseURL string
QwenCloudBaseURL string
MoonshotBaseURL string
MiniMaxBaseURL string
SiliconFlowBaseURL string
SiliconFlowTranscriptionsURL string
ZhipuBaseURL string
ZhipuTranscriptionsURL string
GeminiBaseURL string
AnthropicBaseURL string
MetaBaseURL string
GrokBaseURL string
GrokTranscriptionsURL string
Port int
LogLevel string
WorkerCount int
QueueSize int
RequestTimeoutSeconds int
MaxPromptBytes int64
MaxInputAudioBytes int64
UpstreamRateLimits []UpstreamRateLimitConfiguration
Endpoints *Endpoints
ProviderModels ProviderModelCatalogs
// contains filtered or unexported fields
}
Configuration holds runtime settings.
func NewConfiguration ¶
func NewConfiguration(configuration Configuration) (Configuration, error)
NewConfiguration returns a normalized runtime configuration after validating startup invariants.
func (*Configuration) ApplyTunables ¶
func (configuration *Configuration) ApplyTunables()
ApplyTunables ensures tunable configuration values have sensible defaults.
type Endpoints ¶
type Endpoints struct {
// contains filtered or unexported fields
}
Endpoints provides concurrency-safe access to OpenAI endpoint URLs.
func NewEndpoints ¶
func NewEndpoints() *Endpoints
NewEndpoints creates an Endpoints instance initialized with default URLs.
func NewEndpointsForURLs ¶ added in v0.2.17
NewEndpointsForURLs creates an Endpoints instance from configured OpenAI URLs.
func (*Endpoints) GetModelsURL ¶
GetModelsURL returns the URL used for the OpenAI models endpoint.
func (*Endpoints) GetResponsesURL ¶
GetResponsesURL returns the URL used for the OpenAI responses endpoint.
func (*Endpoints) GetTranscriptionsURL ¶
GetTranscriptionsURL returns the URL used for the OpenAI audio transcriptions endpoint.
func (*Endpoints) ResetModelsURL ¶
func (endpointConfiguration *Endpoints) ResetModelsURL()
ResetModelsURL resets the models endpoint to the default.
func (*Endpoints) ResetResponsesURL ¶
func (endpointConfiguration *Endpoints) ResetResponsesURL()
ResetResponsesURL resets the responses endpoint to the default.
func (*Endpoints) ResetTranscriptionsURL ¶
func (endpointConfiguration *Endpoints) ResetTranscriptionsURL()
ResetTranscriptionsURL resets the transcriptions endpoint to the default.
func (*Endpoints) SetModelsURL ¶
SetModelsURL sets the URL for the OpenAI models endpoint.
func (*Endpoints) SetResponsesURL ¶
SetResponsesURL sets the URL for the OpenAI responses endpoint.
func (*Endpoints) SetTranscriptionsURL ¶
SetTranscriptionsURL sets the URL for the OpenAI audio transcriptions endpoint.
type HTTPDoer ¶
HTTPDoer executes HTTP requests, allowing the proxy to abstract the underlying HTTP client.
var ( // HTTPClient is the default HTTPDoer implementation that delegates to http.DefaultClient. HTTPClient HTTPDoer = http.DefaultClient )
type LegacyTokenMigrationConfiguration ¶ added in v0.2.28
LegacyTokenMigrationConfiguration identifies the one legacy tenant and verified account email allowed to claim it.
type ManagementConfiguration ¶ added in v0.2.19
type ManagementConfiguration struct {
Enabled bool
PublicOrigin string
UIDescription string
UIOrigins []string
AdminEmails []string
TAuthURL string
TAuthTenantID string
GoogleClientID string
LoginPath string
LogoutPath string
NoncePath string
JWTSigningKey string
JWTIssuer string
SessionCookieName string
DatabaseDialect string
DatabaseDSN string
ProviderKeyEncryptionKey string
ManagementAPIOrigin string
ProxyOrigin string
LegacyTokenMigration LegacyTokenMigrationConfiguration
DatabaseDialector gorm.Dialector
}
ManagementConfiguration holds authenticated browser UI and self-service tenant settings.
func (*ManagementConfiguration) ApplyTunables ¶ added in v0.2.19
func (configuration *ManagementConfiguration) ApplyTunables()
ApplyTunables normalizes optional management settings.
type ModelConfiguration ¶ added in v0.2.17
type ModelConfiguration struct {
ID string
RequestProfile string
WebSearch bool
OutputTokenLimit int
ReasoningEffort *ReasoningEffortCapability
}
ModelConfiguration declares runtime metadata for one configured model.
type ModelEndpointCatalog ¶ added in v0.2.17
type ModelEndpointCatalog struct {
DefaultModel string
Models []ModelConfiguration
}
ModelEndpointCatalog declares allowed models and the endpoint default model.
type ModelPayloadSchema ¶
type ModelPayloadSchema struct {
// AllowedRequestFields enumerates JSON fields permitted in the request payload.
AllowedRequestFields []string
}
ModelPayloadSchema lists request fields allowed by a model.
func ResolveModelPayloadSchema ¶
func ResolveModelPayloadSchema(requestProfile string) ModelPayloadSchema
ResolveModelPayloadSchema returns the schema for a request profile or an empty schema when unknown.
type OpenAIClient ¶
type OpenAIClient struct {
// contains filtered or unexported fields
}
OpenAIClient provides access to the OpenAI responses API with configurable endpoints and tunable parameters.
func NewOpenAIClient ¶
func NewOpenAIClient(httpClient HTTPDoer, endpoints *Endpoints, requestTimeout time.Duration) *OpenAIClient
NewOpenAIClient constructs an OpenAIClient initialized with the supplied components.
type ProviderModelCatalog ¶ added in v0.2.17
type ProviderModelCatalog struct {
Text ModelEndpointCatalog
Dictation ModelEndpointCatalog
}
ProviderModelCatalog declares text and dictation model support for one provider.
type ProviderModelCatalogs ¶ added in v0.2.17
type ProviderModelCatalogs map[string]ProviderModelCatalog
ProviderModelCatalogs maps canonical provider identifiers to configured model catalogs.
type Reasoning ¶
type Reasoning struct {
Effort string `json:"effort"`
}
Reasoning specifies configuration options for reasoning-capable models. Effort indicates the tenant-selected reasoning intensity supported by the configured upstream adapter.
type ReasoningEffortCapability ¶ added in v0.2.35
ReasoningEffortCapability declares the configured upstream mapping for one exact text provider/model route.
type TenantConfiguration ¶ added in v0.2.15
type TenantConfiguration struct {
ID string
Secret string
Defaults TenantDefaults
}
TenantConfiguration is the config-file shape for one authenticated tenant.
func DefaultTenantConfiguration ¶ added in v0.2.15
func DefaultTenantConfiguration(identifier string, secret string) TenantConfiguration
DefaultTenantConfiguration returns one tenant using the built-in request defaults.
func SingleTenantConfigurations ¶ added in v0.2.15
func SingleTenantConfigurations(identifier string, secret string) []TenantConfiguration
SingleTenantConfigurations returns a tenant slice for tests and small deployments.
func SingleTenantConfigurationsWithDefaults ¶ added in v0.2.15
func SingleTenantConfigurationsWithDefaults(identifier string, secret string, defaults TenantDefaults) []TenantConfiguration
SingleTenantConfigurationsWithDefaults returns one tenant with caller-specified request defaults.
type TenantDefaults ¶ added in v0.2.15
type TenantDefaults struct {
Provider string
Model string
DictationProvider string
DictationModel string
SystemPrompt string
ReasoningEffort string
}
TenantDefaults holds default request values selected by an authenticated tenant.
func DefaultTenantDefaults ¶ added in v0.2.15
func DefaultTenantDefaults() TenantDefaults
DefaultTenantDefaults returns the built-in request defaults for a single tenant.
type Tool ¶
type Tool struct {
Type string `json:"type"`
}
Tool represents a tool available to the model.
type UpstreamRateLimitConfiguration ¶ added in v0.2.28
UpstreamRateLimitConfiguration describes one exact-origin rolling-window limit.
Source Files
¶
- anthropic.go
- chat_messages.go
- config.go
- constants.go
- doc.go
- endpoints.go
- formats.go
- gemini.go
- limited_http.go
- management_api.go
- management_frontend_config.go
- management_routing_defaults.go
- management_session.go
- management_store.go
- management_usage.go
- middleware.go
- model_capabilities.go
- model_catalog.go
- model_validator.go
- openai.go
- openai_compatible_chat.go
- openai_dictation.go
- provider_errors.go
- provider_key_rejection.go
- provider_registry.go
- provider_router.go
- provider_types.go
- router.go
- tenants.go
- token_usage.go
- upstream_rate_limit.go