Documentation
¶
Overview ¶
Package aigateway provides a high-performance, zero-dependency AI gateway for routing requests to large language model (LLM) providers.
The Gateway type is the main entry point: create one with New, register providers with RegisterProvider, load plugins from config with LoadPlugins, and route requests with Route or RouteStream.
Plugins and routing strategies (single, fallback, load-balance, conditional) are configured via Config which can be loaded from a YAML or JSON file using LoadConfig.
Index ¶
- Constants
- func ValidateConfig(cfg Config) error
- type Condition
- type Config
- type EventPublisher
- type Gateway
- func (g *Gateway) Close() error
- func (g *Gateway) GetConfig() Config
- func (g *Gateway) LoadPlugins() error
- func (g *Gateway) RegisterPlugin(stage plugin.Stage, p plugin.Plugin) error
- func (g *Gateway) RegisterProvider(p providers.Provider)
- func (g *Gateway) ReloadConfig(cfg Config) error
- func (g *Gateway) Route(ctx context.Context, req providers.Request) (*providers.Response, error)
- func (g *Gateway) RouteStream(ctx context.Context, req providers.Request) (<-chan providers.StreamChunk, error)
- func (g *Gateway) SetEventsPublisher(p EventPublisher)
- type PluginConfig
- type RetryConfig
- type StrategyConfig
- type StrategyMode
- type Target
Constants ¶
const ( SubjectRequestCompleted = "gateway.request.completed" SubjectRequestFailed = "gateway.request.failed" )
Event subject constants used when publishing gateway lifecycle events.
Variables ¶
This section is empty.
Functions ¶
func ValidateConfig ¶
ValidateConfig validates a Config for correctness.
Types ¶
type Condition ¶
type Condition struct {
Key string `json:"key" yaml:"key"`
Value string `json:"value" yaml:"value"`
TargetKey string `json:"target_key" yaml:"target_key"`
}
Condition represents a condition for conditional routing.
type Config ¶
type Config struct {
// Strategy defines how requests are routed (e.g., single, fallback, loadbalance).
Strategy StrategyConfig `json:"strategy" yaml:"strategy"`
// Targets is a list of provider targets to route requests to.
Targets []Target `json:"targets" yaml:"targets"`
// Plugins configuration (optional).
Plugins []PluginConfig `json:"plugins,omitempty" yaml:"plugins,omitempty"`
}
Config holds the configuration for the AI Gateway.
func LoadConfig ¶
LoadConfig reads and parses a config file from the given path. Supported formats: JSON (.json), YAML (.yaml, .yml).
type EventPublisher ¶
type EventPublisher interface {
Publish(ctx context.Context, subject string, payload interface{}) error
}
EventPublisher is satisfied by any event bus implementation. Any type with a Publish method can be used for event publishing.
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
Gateway is the main entry point for routing LLM requests.
func (*Gateway) LoadPlugins ¶
LoadPlugins initializes and registers plugins from the gateway configuration.
func (*Gateway) RegisterPlugin ¶
RegisterPlugin registers a plugin at the given lifecycle stage.
func (*Gateway) RegisterProvider ¶
RegisterProvider registers a provider with the gateway.
func (*Gateway) ReloadConfig ¶
ReloadConfig validates and applies a new configuration, forcing strategy rebuild on next request.
func (*Gateway) Route ¶
Route routes a request to the appropriate provider based on the configuration.
func (*Gateway) RouteStream ¶
func (g *Gateway) RouteStream(ctx context.Context, req providers.Request) (<-chan providers.StreamChunk, error)
RouteStream runs before-request plugins then returns a streaming response channel. Provider resolution follows the configured strategy mode, then falls back to any registered provider that supports the requested model and streaming.
func (*Gateway) SetEventsPublisher ¶
func (g *Gateway) SetEventsPublisher(p EventPublisher)
SetEventsPublisher sets the event publisher for the gateway.
type PluginConfig ¶
type PluginConfig struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"`
Stage string `json:"stage" yaml:"stage"`
Enabled bool `json:"enabled" yaml:"enabled"`
Config map[string]interface{} `json:"config" yaml:"config"`
}
PluginConfig holds plugin configuration.
type RetryConfig ¶
type RetryConfig struct {
Attempts int `json:"attempts" yaml:"attempts"`
}
RetryConfig defines retry behavior.
type StrategyConfig ¶
type StrategyConfig struct {
Mode StrategyMode `json:"mode" yaml:"mode"`
Conditions []Condition `json:"conditions,omitempty" yaml:"conditions,omitempty"` // For conditional routing
}
StrategyConfig defines the routing strategy.
type StrategyMode ¶
type StrategyMode string
StrategyMode represents the routing strategy mode.
const ( ModeSingle StrategyMode = "single" ModeFallback StrategyMode = "fallback" ModeLoadBalance StrategyMode = "loadbalance" ModeConditional StrategyMode = "conditional" )
StrategyMode constants define the supported routing strategies.
type Target ¶
type Target struct {
// VirtualKey is the unique identifier for the provider (or a virtual key in the vault).
VirtualKey string `json:"virtual_key" yaml:"virtual_key"`
// Weight is used for load balancing.
Weight float64 `json:"weight,omitempty" yaml:"weight,omitempty"`
// Retry configuration for this target.
Retry *RetryConfig `json:"retry,omitempty" yaml:"retry,omitempty"`
}
Target represents a specific provider target.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
ferrogw
command
Package main provides the HTTP handlers for legacy OpenAI completions endpoint.
|
Package main provides the HTTP handlers for legacy OpenAI completions endpoint. |
|
ferrogw-cli
command
Package main provides the ferrogw-cli command-line tool for managing the FerroGateway.
|
Package main provides the ferrogw-cli command-line tool for managing the FerroGateway. |
|
examples
|
|
|
basic
command
Package main demonstrates sending a request directly to any configured LLM provider.
|
Package main demonstrates sending a request directly to any configured LLM provider. |
|
custom-plugin
command
Package main demonstrates how to write and register a custom plugin with FerroGateway.
|
Package main demonstrates how to write and register a custom plugin with FerroGateway. |
|
embedded
command
Package main demonstrates embedding FerroGateway inside an existing Go HTTP server.
|
Package main demonstrates embedding FerroGateway inside an existing Go HTTP server. |
|
fallback
command
Package main demonstrates the fallback routing strategy.
|
Package main demonstrates the fallback routing strategy. |
|
loadbalance
command
Package main demonstrates weighted load balancing across multiple providers.
|
Package main demonstrates weighted load balancing across multiple providers. |
|
with-guardrails
command
Package main demonstrates using built-in guardrail plugins.
|
Package main demonstrates using built-in guardrail plugins. |
|
internal
|
|
|
admin
Package admin provides HTTP handlers for the gateway administration API.
|
Package admin provides HTTP handlers for the gateway administration API. |
|
cache
Package cache provides the CacheEntry and Cache interface used by the response-cache plugin.
|
Package cache provides the CacheEntry and Cache interface used by the response-cache plugin. |
|
plugins/cache
Package cache provides a response-cache plugin that stores LLM responses in memory and serves them on exact-match cache hits, reducing provider cost and latency for repeated requests.
|
Package cache provides a response-cache plugin that stores LLM responses in memory and serves them on exact-match cache hits, reducing provider cost and latency for repeated requests. |
|
plugins/logger
Package logger provides a request-logger plugin that records each LLM request and response to standard output.
|
Package logger provides a request-logger plugin that records each LLM request and response to standard output. |
|
plugins/maxtoken
Package maxtoken provides a max-token guardrail plugin that caps the max_tokens and message count on outgoing requests.
|
Package maxtoken provides a max-token guardrail plugin that caps the max_tokens and message count on outgoing requests. |
|
plugins/wordfilter
Package wordfilter provides a word-filter guardrail plugin that rejects requests containing blocked words.
|
Package wordfilter provides a word-filter guardrail plugin that rejects requests containing blocked words. |
|
strategies
Package strategies implements the routing strategies used by the gateway.
|
Package strategies implements the routing strategies used by the gateway. |
|
version
Package version holds build-time version information for FerroGateway binaries.
|
Package version holds build-time version information for FerroGateway binaries. |
|
Package plugin defines the Plugin interface and the lifecycle stages used to hook into the gateway request pipeline.
|
Package plugin defines the Plugin interface and the lifecycle stages used to hook into the gateway request pipeline. |
|
Package providers defines the Provider interface and shared data types used across all LLM provider implementations.
|
Package providers defines the Provider interface and shared data types used across all LLM provider implementations. |
Ferro AI Gateway