Understanding Provider Configuration
Polyglot organizes provider settings through connection presets - named configurations that include the details needed to communicate with a specific LLM provider. These connection presets are defined in the configuration files and can be selected at runtime.The Configuration Files
The primary configuration files for Polyglot are:config/llm.php: Contains configurations for LLM providers (chat/completion)config/embed.php: Contains configurations for embedding providers
LLM Configuration Structure
Thellm.php configuration file has the following structure:
Embedding Configuration Structure
Theembed.php configuration file follows a similar pattern:
Connection Parameters
Each connection includes several parameters:providerType: The type of provider (OpenAI, Anthropic, etc.)apiUrl: The base URL for the provider’s APIapiKey: The API key for authenticationendpoint: The specific API endpoint for chat completions or embeddingsmetadata: Additional provider-specific settingsmodel: The default model to usemaxTokens: Default maximum tokens for responsescontextLength: Maximum context length supported by the modelmaxOutputLength: Maximum output length supported by the modelhttpClient: (Optional) Custom HTTP client to use
providerType: The type of provider (OpenAI, Anthropic, etc.)apiUrl: The base URL for the provider’s APIapiKey: The API key for authenticationendpoint: The specific API endpoint for chat completions or embeddingsmetadata: Additional provider-specific settingsmodel: The default model to usedefaultDimensions: The default dimensions of embedding vectorsmaxInputs: Maximum number of inputs that can be processed in a single request
Connection preset name vs provider type
Configuration filellm.php contains a list of connection presets with the default names that might resemble
provider type names, but those are separate entities.
Provider type name refers to one of the supported LLM API providers and its underlying driver implementation,
either specific to this provider or a generic one - for example compatible with OpenAI (‘openai-compatible’).
Connection preset name refers to LLM API provider endpoint configuration with specific provider type, but also URL,
credentials, default model name, and default model parameter values.
Managing API Keys
API keys should be stored securely and never committed to your codebase. Polyglot uses environment variables for API keys.Setting Up Environment Variables
Create a.env file in your project root:
vlucas/phpdotenv:
Rotating API Keys
For better security, consider rotating your API keys regularly. You can update the environment variables without changing your code.Provider-Specific Parameters
Different providers may support unique parameters and features. You can pass these as options to thecreate() method.
OpenAI-Specific Parameters
Anthropic-Specific Parameters
Creating Custom Provider Configurations
You can create custom configurations for providers that aren’t included in the default settings or to modify existing ones.Modifying Configuration Files
You can edit theconfig/llm.php and config/embed.php files directly:
Runtime Configuration
You can also create custom configurations at runtime using theLLMConfig class:
Environment-Based Configuration
You might want to use different providers in different environments:Creating Custom Inference Drivers
In this example we will use an existing driver bundled with Polyglot (OpenAIDriver) as a base class for our custom driver. The driver can be any class that implementsCanHandleInference interface.