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.Documentation Index
Fetch the complete documentation index at: https://docs.instructorphp.com/llms.txt
Use this file to discover all available pages before exploring further.
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:Configure Your API Key
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 theopenai preset. Export your key before
running any PHP code:
Your First Request
Create a file calledtest-polyglot.php in your project directory:
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 anInferenceinstance directly. - Build the request — chain fluent methods such as
withMessages(),withModel(),withMaxTokens(), orwithOptions()to describe what you want. - Execute — call a terminal method to send the request and retrieve the result.
| Method | Returns | Description |
|---|---|---|
get() | string | The plain-text content of the response. |
response() | InferenceResponse | The full response object with content, usage, tool calls, and metadata. |
asJson() | string | The response content parsed as a JSON string. |
asJsonData() | array | The response content parsed into a PHP array. |
stream() | InferenceStream | A streamed response you can iterate over in real time. |
Switching Providers
Because every provider is just a preset name, switching from OpenAI to another provider is a one-line change: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.