LLMConfig or EmbeddingsConfig objects directly.
The Configuration Files
Polyglot ships with YAML preset files organized in two directories:config/llm/presets/— one file per inference provider (e.g.openai.yaml,anthropic.yaml)config/embed/presets/— one file per embeddings provider
${OPENAI_API_KEY} are resolved automatically at load
time. Polyglot searches for preset files in the following directories, in order:
config/llm/presets/(your project root)packages/polyglot/resources/config/llm/presets/(monorepo layout)vendor/cognesy/instructor-php/packages/polyglot/resources/config/llm/presets/vendor/cognesy/instructor-polyglot/resources/config/llm/presets/
$basePath
argument to Inference::using().
Configuration Parameters
Each LLM configuration includes these parameters:| Parameter | Type | Description |
|---|---|---|
driver | string | The protocol driver to use (e.g. openai, anthropic, openai-compatible) |
apiUrl | string | Base URL for the provider’s API |
apiKey | string | API key for authentication |
endpoint | string | The API endpoint path (e.g. /chat/completions) |
queryParams | array | Optional query string parameters appended to the URL |
metadata | array | Provider-specific settings (organization, API version, etc.) |
model | string | Default model name |
maxTokens | int | Default maximum tokens for responses |
contextLength | int | Maximum context window supported by the model |
maxOutputLength | int | Maximum output length supported by the model |
options | array | Default request options passed to every call |
pricing | array | Optional token pricing information for cost tracking |
dimensions and maxInputs instead of
the token-related fields.
Runtime Configuration with LLMConfig
When you need to build a configuration programmatically, use theLLMConfig class:
Messages is imported from Cognesy\Messages\Messages. You can create messages from a
string with Messages::fromString() or from an array of role/content pairs with
Messages::fromArray().
This is useful when your application stores provider credentials in a database, rotates API
keys at runtime, or needs to construct configurations for providers not included in the
bundled presets.
Creating Configurations from Arrays
You can also create anLLMConfig from an associative array, which is convenient when loading
configuration from a database or external source:
Overriding Configuration Values
If you need to modify an existing configuration, usewithOverrides() to create a new
instance with specific values changed:
Embeddings Configuration
The embeddings equivalent follows the same pattern:| Parameter | Type | Description |
|---|---|---|
driver | string | The driver to use (e.g. openai, cohere, gemini, jina) |
apiUrl | string | Base URL for the provider’s API |
apiKey | string | API key for authentication |
endpoint | string | The API endpoint path (e.g. /embeddings) |
model | string | The embedding model name |
dimensions | int | The dimensionality of the embedding vectors |
maxInputs | int | Maximum number of inputs per batch request |
metadata | array | Provider-specific settings |
DSN Input
BothLLMConfig and EmbeddingsConfig support a lightweight DSN (Data Source Name) format
for quick inline configuration:
Managing API Keys
API keys should never be committed to your codebase. Polyglot preset files use environment variable references (${OPENAI_API_KEY}) that are resolved at load time. Store your keys in
a .env file:
vlucas/phpdotenv, or rely on your framework’s built-in
environment handling (Laravel loads .env automatically).
Security Tip: TheapiKeyparameter onLLMConfigis marked with PHP’s#[SensitiveParameter]attribute, which prevents it from appearing in stack traces. Polyglot also redacts sensitive values from event payloads and debug output.
Provider-Specific Options
Different providers support unique request parameters. You can pass these through theoptions parameter on each request:
Environment-Based Configuration
You can create custom presets for different deployment environments. For example, use a local Ollama instance in development and a cloud provider in production:Creating Custom Preset Files
To add a new provider or a custom configuration, create a new YAML file in your project’sconfig/llm/presets/ directory: