Cookbook
Cookbook \ Instructor \ 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
- Validation with LLM
Cookbook \ Instructor \ Advanced
- Context caching (structured output)
- Customize parameters of LLM driver
- Custom prompts
- Customize parameters via DSN
- 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
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)
- 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 \ Extras
Create tasks from meeting transcription
Overview
This example demonstrates how you can create task assignments based on a transcription of meeting recording.
Example
<?php
require 'examples/boot.php';
use Cognesy\Instructor\Instructor;
use Cognesy\Polyglot\LLM\Enums\OutputMode;
// Step 1: Define a class that represents the structure and semantics
// of the data you want to extract
enum TaskStatus : string {
case Pending = 'pending';
case Completed = 'completed';
}
enum Role : string {
case PM = 'pm';
case Dev = 'dev';
}
class Task {
public string $title;
public string $description;
public DateTimeImmutable $dueDate;
public Role $owner;
public TaskStatus $status;
}
class Tasks {
public DateTime $meetingDate;
/** @var Task[] */
public array $tasks;
}
// Step 2: Get the text (or chat messages) you want to extract data from
$text = <<<TEXT
Transcription of meeting from 2024-01-15, 16:00
---
PM: Hey, how's progress on the video transcription engine?
Dev: I've got basic functionality working, but accuracy isn't great yet. Might need to switch to a different API.
PM: So the plan is to research alternatives and provide a comparison? Is it possible by Jan 20th?
Dev: Sure, I'll make it available before the meeting.
PM: The one at 12?
Dev: Yes, at 12. By the way, are we still planning to support real-time transcription?
PM: Yes, it's a key feature. Speaking of which, I need to update the product roadmap. I'll have that ready by Jan 18th.
Dev: Got it. I'll keep that in mind while evaluating APIs. Oh, and the UI for the summary view is ready for review.
PM: Great, I'll take a look tomorrow by 10.
TEXT;
print("Input text:\n");
print($text . "\n\n");
// Step 3: Extract structured data using default language model API (OpenAI)
print("Extracting structured data using LLM...\n\n");
$tasks = (new Instructor)->respond(
messages: $text,
responseModel: Tasks::class,
model: 'gpt-4o',
mode: OutputMode::Json,
);
// Step 4: Now you can use the extracted data in your application
print("Extracted data:\n");
dump($tasks);
assert($tasks->meetingDate->format('Y-m-d') === '2024-01-15');
assert(count($tasks->tasks) == 3);
assert($tasks->tasks[0]->dueDate->format('Y-m-d') === '2024-01-20');
assert($tasks->tasks[0]->status === TaskStatus::Pending);
assert($tasks->tasks[0]->owner === Role::Dev);
assert($tasks->tasks[1]->dueDate->format('Y-m-d') === '2024-01-18');
assert($tasks->tasks[1]->status === TaskStatus::Pending);
assert($tasks->tasks[1]->owner === Role::PM);
assert($tasks->tasks[2]->dueDate->format('Y-m-d') === '2024-01-16');
assert($tasks->tasks[2]->status === TaskStatus::Pending);
assert($tasks->tasks[2]->owner === Role::PM);
?>
Assistant
Responses are generated using AI and may contain mistakes.