This guide walks you through installing Polyglot and making your first LLM
inference request. By the end you will have a working PHP script that sends a
prompt to an LLM provider and prints the response.
Polyglot is already included in the Instructor for PHP package. If you have
Instructor installed, you do not need to install Polyglot separately.
Installation
Polyglot requires PHP 8.3 or later. Install it with Composer:
Polyglot ships with ready-made presets for over 25 providers including OpenAI,
Anthropic, Gemini, Mistral, Groq, Deepseek, and many more. Each preset reads
its API key from an environment variable so credentials never appear in code.
For this quickstart we will use the openai preset. Export your key before
running any PHP code:
Never hard-code API keys in source files. Use environment variables or a
.env file to keep credentials out of version control.
Your First Request
Create a file called test-polyglot.php in your project directory:
Run it from the terminal:
That is all it takes. The Inference class is the main entry point for every
LLM request in Polyglot.
Understanding the Flow
Every Polyglot request follows a three-step pattern:
- Select a provider — call
Inference::using('preset') to choose a
bundled or custom preset, or create an Inference instance directly.
- Build the request — chain fluent methods such as
withMessages(),
withModel(), withMaxTokens(), or withOptions() to describe what you
want.
- Execute — call a terminal method to send the request and retrieve the
result.
The available terminal methods are:
Switching Providers
Because every provider is just a preset name, switching from OpenAI to another
provider is a one-line change:
Set the corresponding environment variable for each provider you want to use
(ANTHROPIC_API_KEY, GEMINI_API_KEY, GROQ_API_KEY, and so on).
Overriding the Model
The preset defines a default model, but you can override it per-request:
Streaming Responses
For long responses or interactive UIs, you can stream the output token by token:
Next Steps
Now that you have a working setup, explore the rest of the documentation to
unlock the full power of Polyglot:
- Setup — define your own presets and customize provider
configuration.
- Essentials — learn the complete request and
response API.
- Streaming — handle streamed responses, events,
and partial updates.
- Embeddings — generate vector embeddings for
semantic search and RAG.