proxy

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package proxy contains configuration, middleware, routing, and model interaction logic for the language model proxy server.

Index

Constants

View Source
const (
	// DefaultPort is the TCP port used by the HTTP server when no explicit port is provided.
	DefaultPort = 8080
	// DefaultWorkers is the number of worker goroutines that process upstream requests.
	DefaultWorkers = 4
	// DefaultQueueSize is the capacity of the internal request queue.
	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      = 180 // overall app-side request timeout
	DefaultUpstreamPollTimeoutSeconds = 60  // poll budget after "incomplete"
	// DefaultMaxPromptBytes limits JSON LLM request bodies accepted by POST /.
	DefaultMaxPromptBytes     = 4 * 1024 * 1024
	DefaultDictationModel     = "gpt-4o-mini-transcribe"
	DefaultMaxInputAudioBytes = 25 * 1024 * 1024
)
View Source
const (
	// LogLevelDebug indicates that the application should log debug information.
	LogLevelDebug = "debug"

	// LogLevelInfo indicates that the application should log informational messages.
	LogLevelInfo = "info"
)
View Source
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"
)
View Source
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"
	// ProviderNameMoonshot identifies Moonshot/Kimi routing.
	ProviderNameMoonshot = "moonshot"
	// ProviderNameSiliconFlow identifies SiliconFlow routing.
	ProviderNameSiliconFlow = "siliconflow"
	// ProviderNameZhipu identifies Zhipu/GLM routing.
	ProviderNameZhipu = "zhipu"
	// ProviderNameGemini identifies Google Gemini routing.
	ProviderNameGemini = "gemini"
)
View Source
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"
	// ModelNameMoonshotKimi identifies the Kimi K2 preview model.
	ModelNameMoonshotKimi = "kimi-k2-0905-preview"
	// 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"
)

Variables

View Source
var (
	// SchemaGPT4oMini defines allowed payload fields for the GPT-4o-mini model.
	SchemaGPT4oMini = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyTemperature}}
	// SchemaGPT4o defines allowed payload fields for the GPT-4o model.
	SchemaGPT4o = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyTemperature, keyTools, keyToolChoice}}
	// SchemaGPT41 defines allowed payload fields for the GPT-4.1 model.
	SchemaGPT41 = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyTemperature, keyTools, keyToolChoice}}
	// SchemaGPT5Mini defines allowed payload fields for the GPT-5-mini model.
	SchemaGPT5Mini = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens}}
	// SchemaGPT5 defines allowed payload fields for the GPT-5 model.
	SchemaGPT5 = ModelPayloadSchema{AllowedRequestFields: []string{keyModel, keyInput, keyMaxOutputTokens, keyTools, keyToolChoice, keyReasoning}}
	// SchemaGPT55 defines allowed payload fields for GPT-5.5 family models.
	SchemaGPT55 = SchemaGPT5
)
View Source
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)
)
View Source
var ErrUnknownModel = errors.New(errorUnknownModel)

ErrUnknownModel is returned when a model identifier is not recognized.

View Source
var ErrUpstreamIncomplete = errors.New(errorUpstreamIncomplete)

ErrUpstreamIncomplete indicates that the upstream provider returned an incomplete response before the poll deadline.

Functions

func BuildRequestPayload

func BuildRequestPayload(modelIdentifier string, combinedPrompt string, webSearchEnabled bool, maxTokens *int) any

BuildRequestPayload selects the correct struct for the given model and returns it.

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 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 {
	ServiceSecret                string
	OpenAIKey                    string
	DeepSeekKey                  string
	DashScopeKey                 string
	MoonshotKey                  string
	SiliconFlowKey               string
	ZhipuKey                     string
	GeminiKey                    string
	DefaultProvider              string
	DefaultModel                 string
	DefaultDictationProvider     string
	DeepSeekBaseURL              string
	DashScopeBaseURL             string
	MoonshotBaseURL              string
	SiliconFlowBaseURL           string
	SiliconFlowTranscriptionsURL string
	ZhipuBaseURL                 string
	GeminiBaseURL                string
	Port                         int
	LogLevel                     string
	SystemPrompt                 string
	WorkerCount                  int
	QueueSize                    int
	RequestTimeoutSeconds        int
	UpstreamPollTimeoutSeconds   int
	MaxPromptBytes               int64
	DictationModel               string
	MaxInputAudioBytes           int64
	Endpoints                    *Endpoints
	// 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 (*Endpoints) GetModelsURL

func (endpointConfiguration *Endpoints) GetModelsURL() string

GetModelsURL returns the URL used for the OpenAI models endpoint.

func (*Endpoints) GetResponsesURL

func (endpointConfiguration *Endpoints) GetResponsesURL() string

GetResponsesURL returns the URL used for the OpenAI responses endpoint.

func (*Endpoints) GetTranscriptionsURL

func (endpointConfiguration *Endpoints) GetTranscriptionsURL() string

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

func (endpointConfiguration *Endpoints) SetModelsURL(newURL string)

SetModelsURL sets the URL for the OpenAI models endpoint.

func (*Endpoints) SetResponsesURL

func (endpointConfiguration *Endpoints) SetResponsesURL(newURL string)

SetResponsesURL sets the URL for the OpenAI responses endpoint.

func (*Endpoints) SetTranscriptionsURL

func (endpointConfiguration *Endpoints) SetTranscriptionsURL(newURL string)

SetTranscriptionsURL sets the URL for the OpenAI audio transcriptions endpoint.

type HTTPDoer

type HTTPDoer interface {
	Do(httpRequest *http.Request) (*http.Response, error)
}

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 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(modelIdentifier string) ModelPayloadSchema

ResolveModelPayloadSchema returns the schema for a model 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, pollTimeout time.Duration) *OpenAIClient

NewOpenAIClient constructs an OpenAIClient initialized with the supplied components.

type Reasoning

type Reasoning struct {
	Effort string `json:"effort"`
}

Reasoning specifies configuration options for reasoning-capable models. Effort indicates the desired reasoning intensity and uses constants such as reasoningEffortMinimal or reasoningEffortMedium.

type Tool

type Tool struct {
	Type string `json:"type"`
}

Tool represents a tool available to the model.

Jump to

Keyboard shortcuts

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