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 \ Polyglot \ LLM Advanced
Customize configuration providers of LLM driver
Overview
You can provide your own LLM configuration instance to Inference
object. This is useful
when you want to initialize LLM client with custom values.
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\Events\Event;
use Cognesy\Http\HttpClientBuilder;
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Utils\Str;
use Symfony\Component\HttpClient\HttpClient as SymfonyHttpClient;
$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',
],
],
],
];
class CustomConfigProvider implements CanProvideConfig
{
private Dot $dot;
public function __construct(array $data = []) {
$this->dot = new Dot($data);
}
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);
}
}
$configProvider = new CustomConfigProvider($configData);
$events = new EventDispatcher();
$customClient = (new HttpClientBuilder(
events: $events,
configProvider: $configProvider,
))
->withClientInstance(SymfonyHttpClient::create(['http_version' => '2.0']))
->create();
$inference = (new Inference(
events: $events,
configProvider: $configProvider,
))
->withHttpClient($customClient);
$answer = $inference
->using('deepseek') // Use 'deepseek' preset from CustomLLMConfigProvider
//->withDebugPreset('on')
->wiretap(fn(Event $e) => $e->print())
->withMessages([['role' => 'user', 'content' => 'What is the capital of France']])
->withMaxTokens(256)
->withStreaming()
->get();
echo "USER: What is capital of France\n";
echo "ASSISTANT: $answer\n";
assert(Str::contains($answer, 'Paris'));
?>
Assistant
Responses are generated using AI and may contain mistakes.