Inference
class offers access to LLM APIs and convenient methods to execute
model inference, incl. chat completions, tool calling or JSON output
generation.
LLM providers access details can be found and modified via /config/llm.php
.
Simple Text Generation
The simplest way to use Polyglot is to generate text using staticInference::text()
method.
Simplified inference API uses the default connection for convenient ad-hoc calls.
Creating an Inference Object
For more control, you can create an instance of theInference
class:
Specifying a Connection
You can specify which connection preset to use:Creating Chat Conversations
For multi-turn conversations, provide an array of messages:Customizing Request Parameters
You can customize various parameters for your requests:Fluent API
Regular inference API allows you to customize inference options, letting you set values specific for a given LLM provider. Most of the provider options are compatible with OpenAI API. This example shows how to create an inference object, specify a connection and generate text using thecreate()
method.
The toText()
method returns text completion from the LLM response.
Streaming inference results
Inference API allows streaming responses, which is useful for building more responsive UX as you can display partial responses from LLM as soon as they arrive, without waiting until the whole response is ready.Connecting to a specific LLM API provider
Instructor allows you to define multiple API connections inllm.php
file.
This is useful when you want to use different LLMs or API providers in your application.
Default configuration is located in /config/llm.php
in the root directory
of Instructor codebase. It contains a set of predefined connections to all LLM APIs
supported out-of-the-box by Instructor.
Config file defines connections to LLM APIs and their parameters. It also specifies
the default connection to be used when calling Instructor without specifying
the client connection.
withPreset
method with the connection preset name.
INSTRUCTOR_CONFIG_PATHS
environment variable. You can use copies of the default
configuration files as a starting point.