Cookbook
Cookbook \ Instructor \ Basics
- Basic use
- Basic use via mixin
- Fluent API
- 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 connection presets from config file
- Validation
- Custom validation using Symfony Validator
- Validation across multiple fields
- Validation with LLM
Cookbook \ Instructor \ Advanced
- Use custom configuration providers
- Context caching (structured output)
- Customize parameters of LLM driver
- Custom prompts
- Customize parameters via DSN
- Extracting arguments of function or method
- Logging monolog
- Logging psr
- Streaming partial updates during inference
- Providing example inputs and outputs
- Extracting scalar values
- Extracting sequences of objects
- Streaming
- Structures
Cookbook \ Instructor \ Troubleshooting
Cookbook \ Instructor \ LLM API Support
Cookbook \ Instructor \ Extras
- Extraction of complex objects
- Extraction of complex objects (Anthropic)
- Extraction of complex objects (Cohere)
- Extraction of complex objects (Gemini)
- Using structured data as an input
- Image processing - car damage detection
- Image to data (OpenAI)
- Image to data (Anthropic)
- Image to data (Gemini)
- Generating JSON Schema from PHP classes
- Generating JSON Schema from PHP classes
- Generating JSON Schema dynamically
- Create tasks from meeting transcription
- Translating UI text fields
- Web page to PHP objects
Cookbook \ Polyglot \ LLM Basics
- 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
- Working directly with LLMs and JSON - Tools mode
- Generating JSON Schema from PHP classes
- Generating JSON Schema from PHP classes
Cookbook \ Polyglot \ LLM Advanced
Cookbook \ Polyglot \ LLM Troubleshooting
Cookbook \ Polyglot \ LLM API Support
Cookbook \ Polyglot \ LLM Extras
Cookbook \ Prompting \ Zero-Shot Prompting
Cookbook \ Prompting \ Few-Shot Prompting
Cookbook \ Prompting \ Thought Generation
Cookbook \ Prompting \ Miscellaneous
- Arbitrary properties
- Consistent values of arbitrary properties
- Chain of Summaries
- Chain of Thought
- Single label classification
- Multiclass classification
- Entity relationship extraction
- Handling errors
- Limiting the length of lists
- Reflection Prompting
- Restating instructions
- Ask LLM to rewrite instructions
- Expanding search queries
- Summary with Keywords
- Reusing components
- Using CoT to improve interpretation of component data
Cookbook \ Instructor \ Advanced
Use custom configuration providers
Overview
You can inject your own configuration providers to StructuredOutput class. This is useful for integration with your preferred framework (e.g. Symfony, Laravel).
Example
Copy
<?php
require 'examples/boot.php';
use Adbar\Dot;
use Cognesy\Config\Contracts\CanProvideConfig;
use Cognesy\Config\Env;
use Cognesy\Events\Dispatchers\EventDispatcher;
use Cognesy\Http\HttpClientBuilder;
use Cognesy\Instructor\Extras\Structure\Structure;
use Cognesy\Instructor\StructuredOutput;
use Cognesy\Polyglot\Inference\Enums\OutputMode;
class CustomConfigProvider implements CanProvideConfig
{
private Dot $dot;
public function __construct(array $config = []) {
$this->dot = new Dot($config);
}
public function get(string $path, mixed $default = null): mixed {
return $this->dot->get($path, $default);
}
public function has(string $path): bool {
return $this->dot->has($path);
}
}
$configData = [
'http' => [
'defaultPreset' => 'symfony',
'presets' => [
'symfony' => [
'driver' => 'symfony',
'connectTimeout' => 10,
'requestTimeout' => 30,
'idleTimeout' => -1,
'maxConcurrent' => 5,
'poolTimeout' => 60,
'failOnError' => true,
],
// Add more HTTP presets as needed
],
],
'debug' => [
'defaultPreset' => 'off',
'presets' => [
'off' => [
'httpEnabled' => false,
],
'on' => [
'httpEnabled' => true,
'httpTrace' => true,
'httpRequestUrl' => true,
'httpRequestHeaders' => true,
'httpRequestBody' => true,
'httpResponseHeaders' => true,
'httpResponseBody' => true,
'httpResponseStream' => true,
'httpResponseStreamByLine' => true,
],
],
],
'llm' => [
'defaultPreset' => 'deepseek',
'presets' => [
'deepseek' => [
'apiUrl' => 'https://api.deepseek.com',
'apiKey' => Env::get('DEEPSEEK_API_KEY'),
'endpoint' => '/chat/completions',
'defaultModel' => 'deepseek-chat',
'defaultMaxTokens' => 128,
'driver' => 'deepseek',
'httpClientPreset' => 'symfony',
],
'openai' => [
'apiUrl' => 'https://api.openai.com',
'apiKey' => Env::get('OPENAI_API_KEY'),
'endpoint' => '/v1/chat/completions',
'defaultModel' => 'gpt-4',
'defaultMaxTokens' => 256,
'driver' => 'openai',
'httpClientPreset' => 'symfony',
],
],
],
'structured' => [
'defaultPreset' => 'tools',
'presets' => [
'tools' => [
'outputMode' => OutputMode::Tools,
'useObjectReferences' => true,
'maxRetries' => 3,
'retryPrompt' => 'Please try again ...',
'modePrompts' => [
OutputMode::MdJson->value => "Response must validate against this JSON Schema:\n<|json_schema|>\n. Respond correctly with strict JSON object within a ```json {} ``` codeblock.\n",
OutputMode::Json->value => "Response must follow JSON Schema:\n<|json_schema|>\n. Respond correctly with strict JSON object.\n",
OutputMode::JsonSchema->value => "Response must follow provided JSON Schema. Respond correctly with strict JSON object.\n",
OutputMode::Tools->value => "Extract correct and accurate data from the input using provided tools.\n",
],
'schemaName' => 'user_schema',
'toolName' => 'user_tool',
'toolDescription' => 'Tool to extract user information ...',
'chatStructure' => [
'system',
'pre-cached',
'pre-cached-prompt', 'cached-prompt', 'post-cached-prompt',
'pre-cached-examples', 'cached-examples', 'post-cached-examples',
'cached-messages',
'post-cached',
'pre-prompt', 'prompt', 'post-prompt',
'pre-examples', 'examples', 'post-examples',
'pre-messages', 'messages', 'post-messages',
'pre-retries', 'retries', 'post-retries'
],
// defaultOutputClass is not used in this example
'defaultOutputClass' => Structure::class,
]
]
]
];
$events = new EventDispatcher();
$configProvider = new CustomConfigProvider($configData);
$customClient = (new HttpClientBuilder(
events: $events,
configProvider: $configProvider,
))
->withConfigProvider($configProvider)
->withPreset('symfony')
->create();
$structuredOutput = (new StructuredOutput(
events: $events,
configProvider: $configProvider,
))
->withHttpClient($customClient);
// Call with custom model and execution mode
class User {
public int $age;
public string $name;
}
$user = $structuredOutput
->using('deepseek') // Use 'deepseek' preset defined in our config provider
->wiretap(fn($e) => $e->print())
->withMessages("Our user Jason is 25 years old.")
->withResponseClass(User::class)
->withOutputMode(OutputMode::Tools)
->withStreaming()
->get();
dump($user);
assert(isset($user->name));
assert(isset($user->age));
?>
Assistant
Responses are generated using AI and may contain mistakes.