Configuration Deep Dive
Learn how to configure different LLM providers and models in Polyglot.
One of Polyglot’s core strengths is its ability to work with multiple LLM providers through a unified API. This chapter covers how to configure, manage, and switch between different providers and models to get the most out of the library.
Understanding Provider Configuration
Polyglot organizes provider settings through “connections” - named configurations that include all the details needed to communicate with a specific LLM provider. These connections 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
Let’s focus on the structure of these configuration files.
LLM Configuration Structure
The llm.php
configuration file has the following structure:
Embedding Configuration Structure
The embed.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 settingsdefaultModel
: The default model to usedefaultMaxTokens
: 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
For embedding connections, there are additional parameters:
defaultDimensions
: The default dimensions of embedding vectorsmaxInputs
: Maximum number of inputs that can be processed in a single request
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:
Then load these environment variables using a package like vlucas/phpdotenv
:
Or in frameworks like Laravel, environment variables are automatically loaded.
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 the create()
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 the config/llm.php
and config/embed.php
files directly:
Runtime Configuration
You can also create custom configurations at runtime using the LLMConfig
class:
Environment-Based Configuration
You might want to use different providers in different environments: