Skip to content

Instantly share code, notes, and snippets.

@m0n0x41d
Created November 6, 2025 17:32
Show Gist options
  • Select an option

  • Save m0n0x41d/78d8a696c65953e6d37e5b70508e1094 to your computer and use it in GitHub Desktop.

Select an option

Save m0n0x41d/78d8a696c65953e6d37e5b70508e1094 to your computer and use it in GitHub Desktop.
instructorphp SGR minimal example
<?php
namespace App\Services\AI;
use Cognesy\Instructor\StructuredOutput;
use Cognesy\Polyglot\Inference\Enums\OutputMode;
// =================
// RESPONSE SCHEMA
// =================
enum ProductCategory : string {
case Electronics = 'electronics';
case Clothing = 'clothing';
case Food = 'food';
case Books = 'books';
case Other = 'other';
}
enum SentimentType : string {
case Positive = 'positive';
case Neutral = 'neutral';
case Negative = 'negative';
}
enum UrgencyLevel : string {
case High = 'high';
case Medium = 'medium';
case Low = 'low';
}
enum ResponseTone : string {
case Formal = 'formal';
case Friendly = 'friendly';
case Apologetic = 'apologetic';
}
class ReviewAnalysis
{
public string $summary;
public string $keyPoints;
public SentimentType $sentiment;
public string $specificIssues;
}
class ProductClassification
{
public ProductCategory $category;
public UrgencyLevel $urgency;
public string $reasoning;
public bool $requiresManagerReview;
}
class GeneratedReply
{
public string $subject;
public string $htmlBody;
public ResponseTone $tone;
public string $addressedPoints;
}
class ReviewResponse
{
public ReviewAnalysis $analysis;
public ProductClassification $classification;
public GeneratedReply $reply;
}
// ==================
// MINIMAL PROCESSOR
// ==================
class ProductReviewProcessor
{
private StructuredOutput $instructor;
public function __construct()
{
$this->instructor = new StructuredOutput();
}
public function processReview(string $reviewText): ReviewResponse
{
$systemPrompt = 'You are an AI assistant for product review classification.
Analyze customer reviews and classify them into categories:
1. electronics - Tech products, gadgets, devices
2. clothing - Apparel, shoes, accessories
3. food - Food items, beverages
4. books - Books, magazines, publications
5. other - Everything else
Determine urgency and generate appropriate response.';
$userPrompt = "Process this product review:\n\n{$reviewText}";
$response = $this->instructor->with(
messages: [
['role' => 'system', 'content' => $systemPrompt],
['role' => 'user', 'content' => $userPrompt]
],
responseModel: ReviewResponse::class,
mode: OutputMode::JsonSchema,
model: 'gpt-4o-mini',
options: [
'temperature' => 0.3,
'max_tokens' => 1000,
]
)->get();
return $response;
}
}
// ==============
// USAGE EXAMPLE
// ==============
$processor = new ProductReviewProcessor();
$result = $processor->processReview("Bought these headphones last week, sound quality is terrible and they broke after 2 days!");
// Access nested structure:
echo $result->analysis->summary; // "Customer dissatisfied with headphone quality"
echo $result->analysis->keyPoints; // "Sound quality poor, product broke quickly"
echo $result->analysis->sentiment->value; // "negative"
echo $result->analysis->specificIssues; // "Durability issues, audio quality"
echo $result->classification->category->value; // "electronics"
echo $result->classification->urgency->value; // "high"
echo $result->classification->reasoning; // "Product defect reported, customer dissatisfaction"
echo $result->classification->requiresManagerReview; // true
echo $result->reply->subject; // "Re: Your Headphone Experience"
echo $result->reply->htmlBody; // "<p>We sincerely apologize for the experience...</p>"
echo $result->reply->tone->value; // "apologetic"
echo $result->reply->addressedPoints; // "Product quality, replacement process"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment