Basics
Basic use via mixin
Basics
- Basic use
- Basic use via mixin
- Handling errors with `Maybe` helper class
- Modes
- Making some fields optional
- Private vs public object field
- Automatic correction based on validation results
- Using attributes
- Using LLM API connections from config file
- Validation
- Custom validation using Symfony Validator
- Validation across multiple fields
Advanced
- Context caching
- Context caching (Anthropic)
- Customize parameters of OpenAI client
- Custom prompts
- Using structured data as an input
- Extracting arguments of function or method
- Streaming partial updates during inference
- Providing example inputs and outputs
- Extracting scalar values
- Extracting sequences of objects
- Streaming
- Structures
Troubleshooting
LLM API Support
Extras
- Extraction of complex objects
- Extraction of complex objects (Anthropic)
- Extraction of complex objects (Cohere)
- Extraction of complex objects (Gemini)
- Embeddings
- Image processing - car damage detection
- Image to data (OpenAI)
- Image to data (Anthropic)
- Image to data (Gemini)
- Working directly with LLMs
- Working directly with LLMs and JSON - JSON mode
- Working directly with LLMs and JSON - JSON Schema mode
- Working directly with LLMs and JSON - MdJSON mode
- Inference and tool use
- Working directly with LLMs and JSON - Tools mode
- Prompts
- Generating JSON Schema from PHP classes
- Generating JSON Schema dynamically
- Simple content summary
- Create tasks from meeting transcription
- Translating UI text fields
- Web page to PHP objects
Basics
Basic use via mixin
Overview
Instructor provides HandlesSelfInference
trait that you can use to enable
extraction capabilities directly on class via static infer()
method.
infer()
method returns an instance of the class with the data extracted
using the Instructor.
infer()
method has following signature (you can also find it in the
CanSelfInfer
interface):
static public function infer(
string|array $messages, // (required) The message(s) to infer data from
string $prompt = '', // (optional) The prompt to use for inference
array $examples = [], // (optional) Examples to include in the prompt
string $model = '', // (optional) The model to use for inference (otherwise - use default)
int $maxRetries = 2, // (optional) The number of retries in case of validation failure
array $options = [], // (optional) Additional data to pass to the Instructor or LLM API
Mode $mode = Mode::Tools, // (optional) The mode to use for inference
LLM $llm = null // (optional) LLM instance to use for inference
) : static;
Example
<?php
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');
use Cognesy\Instructor\Extras\Mixin\HandlesSelfInference;
class User {
use HandlesSelfInference;
public int $age;
public string $name;
}
$user = User::infer("Jason is 25 years old and works as an engineer.");
dump($user);
assert(isset($user->name));
assert(isset($user->age));
assert($user->name === 'Jason');
assert($user->age === 25);
?>