Overview of Inference
How to use LLM inference API
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 static Inference::text()
method.
Simplified inference API uses the default connection for convenient ad-hoc calls.
This static method uses the default connection specified in your configuration. Default LLM connection can be configured via config/llm.php.
Creating an Inference Object
For more control, you can create an instance of the Inference
class:
Specifying a Connection
You can specify which connection 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 the create()
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 in llm.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.
To customize the available connections you can either modify existing entries or add your own.
Connecting to LLM API via predefined connection is as simple as calling withClient
method with the connection name.
You can change the location of the configuration files for Instructor to use via
INSTRUCTOR_CONFIG_PATH
environment variable. You can use copies of the default
configuration files as a starting point.
Switching Between Providers
Polyglot makes it easy to switch between different LLM providers at runtime.
Using Different Providers for LLM Requests
Selecting Different Models
Each provider offers multiple models with different capabilities, context lengths, and pricing. Polyglot lets you override the default model for each request.