Skip to main content
Presets are the recommended way to configure Polyglot. They live in YAML files, keep secrets in environment variables, and let you switch providers without touching application code. However, there are situations where presets are not sufficient — when configuration values are dynamic, generated at runtime, or sourced from your own application’s settings. In these cases, you can build 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
A typical preset file looks like this:
Environment variable references like ${OPENAI_API_KEY} are resolved automatically at load time. Polyglot searches for preset files in the following directories, in order:
  1. config/llm/presets/ (your project root)
  2. packages/polyglot/resources/config/llm/presets/ (monorepo layout)
  3. vendor/cognesy/instructor-php/packages/polyglot/resources/config/llm/presets/
  4. vendor/cognesy/instructor-polyglot/resources/config/llm/presets/
The first directory that exists wins. You can override the search path by passing a $basePath argument to Inference::using().

Configuration Parameters

Each LLM configuration includes these parameters: Embeddings configurations use a similar structure with dimensions and maxInputs instead of the token-related fields.

Runtime Configuration with LLMConfig

When you need to build a configuration programmatically, use the LLMConfig class:
Note: 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 an LLMConfig 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, use withOverrides() to create a new instance with specific values changed:

Embeddings Configuration

The embeddings equivalent follows the same pattern:
The embeddings configuration parameters are:

DSN Input

Both LLMConfig and EmbeddingsConfig support a lightweight DSN (Data Source Name) format for quick inline configuration:
The DSN format encodes the driver as the scheme, the host and path as the API URL, and query parameters for the remaining configuration values.

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:
Load them with a package like vlucas/phpdotenv, or rely on your framework’s built-in environment handling (Laravel loads .env automatically).
Security Tip: The apiKey parameter on LLMConfig is 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 the options parameter on each request:
For Anthropic, the available options differ:
Polyglot passes these options through to the provider’s API without modification, so consult each provider’s documentation for the full list of supported parameters. You can also set default options in the preset YAML file so they apply to every request made with that preset:

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:
Then select the preset based on your application’s environment:
This pattern keeps your application code completely environment-agnostic. The only thing that changes between environments is which preset name is selected.

Creating Custom Preset Files

To add a new provider or a custom configuration, create a new YAML file in your project’s config/llm/presets/ directory:
Then reference it by name:
Polyglot will find your custom preset file before falling back to the bundled presets, so you can override any built-in preset by creating a file with the same name in your project’s configuration directory.