Skip to main content
This guide walks you through installing Polyglot, configuring API keys, and choosing the right configuration strategy for your project.

Installation

Install Polyglot via Composer:
Note: Polyglot ships as part of the Instructor PHP monorepo. If you already have cognesy/instructor-php installed, Polyglot is included automatically — there is no need to install it separately.

Requirements

  • PHP 8.3 or higher
  • Composer
  • A valid API key for at least one supported LLM provider

Setting Up API Keys

Polyglot authenticates with LLM providers through API keys. The simplest approach is to export them as environment variables:
If your project uses a .env file, add the keys there instead and load them with a library such as vlucas/phpdotenv. Polyglot’s bundled preset files reference these variables using ${VAR_NAME} syntax, so the names above are the expected defaults.

Quick Start

Once an API key is available, a single call is all you need:
If you see a friendly greeting, your installation is working correctly. The using() method loads a preset — a small YAML file that tells Polyglot which driver, API URL, endpoint, and model to use. Polyglot ships with presets for every supported provider, so you can swap 'openai' for 'anthropic', 'gemini', 'mistral', or any other supported name and it will just work (provided the matching API key is set).

Configuration Strategies

Polyglot offers three ways to configure connections, from simplest to most flexible.

1. Bundled Presets (Zero Configuration)

Polyglot ships with ready-made presets for all supported providers. Set the appropriate environment variable and call using():
Bundled presets live inside the package at resources/config/llm/presets/ and resources/config/embed/presets/. You never need to edit these files — override them with your own presets instead (see below).

2. Custom Presets (App-Owned YAML Files)

When you need to change a model, adjust token limits, or add metadata, create your own preset files. Polyglot checks the following directories in order and uses the first match: All paths are relative to your project root. A file placed in config/llm/presets/ takes precedence over the bundled default.

LLM Preset Example

Create config/llm/presets/openai.yaml:
The driver field determines which Polyglot driver handles the request. The apiKey value supports ${ENV_VAR} interpolation so secrets never appear in plain text.

Embeddings Preset Example

Create config/embed/presets/openai.yaml:
Once the file exists, Inference::using('openai') or Embeddings::using('openai') will resolve it automatically. You can also create entirely new presets for custom deployments. For example, to add a preset for a local vLLM server, create config/llm/presets/local-vllm.yaml:
Then use it like any other preset:

EmbeddingsConfig Reference

3. Runtime Configuration (Programmatic)

When connection details come from a database, user input, or any other dynamic source, build the config object directly in PHP:
The same approach works for embeddings:

LLMConfig Reference

Overriding a Preset at Runtime

You can start from a preset and selectively override specific values using withOverrides():
This is useful when you want to keep all the defaults from a preset but need to swap the model or adjust limits for a specific use case.

DSN Strings

For compact, inline configuration you can use a DSN (Data Source Name) string. This is useful when storing connection info in a single environment variable or database column:
DSN strings are comma-separated key=value pairs. Nested keys use dot notation (e.g., metadata.apiVersion=2023-06-01).

Supported Providers

Polyglot includes built-in drivers for the following providers: Any provider that exposes an OpenAI-compatible chat completions endpoint can be used with the openai-compatible driver by pointing apiUrl to the provider’s base URL.

Troubleshooting

Composer dependency errors. Verify that you are running PHP 8.3 or higher (php -v) and that Composer is up to date (composer self-update). “No preset directory found” exception. This means Polyglot could not locate any preset YAML file for the name you passed to using(). Double-check the preset name and ensure your custom config directory (if any) follows the expected structure: config/llm/presets/<name>.yaml. API key not found. Make sure the environment variable is exported in the same shell session that runs your PHP script. You can verify with echo $OPENAI_API_KEY before launching PHP. Wrong model or endpoint. Create a custom preset (see above) to override the bundled defaults with the model and endpoint you need.