Polyglot - LLM Extras
Chat with summary
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
Instructor - Advanced
- Context caching (structured output)
- Customize parameters of LLM driver
- 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
Instructor - Troubleshooting
Instructor - LLM API Support
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 dynamically
- Create tasks from meeting transcription
- Translating UI text fields
- Web page to PHP objects
Polyglot - LLM Basics
Polyglot - LLM Advanced
Polyglot - LLM Troubleshooting
Polyglot - LLM API Support
Polyglot - LLM Extras
Prompting - Zero-Shot Prompting
Prompting - Few-Shot Prompting
Prompting - Thought Generation
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
Polyglot - LLM Extras
Chat with summary
Overview
Example
<?php
die(); // TO BE FIXED
require 'examples/boot.php';
use Cognesy\Addons\Chat\Pipelines\ChatWithSummary;
use Cognesy\Addons\Chat\Utils\SummarizeMessages;
use Cognesy\Polyglot\LLM\Inference;
use Cognesy\Polyglot\LLM\LLM;
use Cognesy\Utils\Messages\Message;
use Cognesy\Utils\Messages\Messages;
$maxSteps = 5;
$sys = [
'You are helpful assistant explaining Challenger Sale method, you answer questions. Provide very brief answers, not more than one sentence. Simplify things, don\'t go into details, but be very pragmatic and focused on practical bizdev problems.',
'You are curious novice growth expert working to promote Instructor library, you keep asking questions. Use your knowledge of Instructor library and marketing of tech products for developers. Ask short, simple questions. Always ask a single question.',
];
$startMessage = new Message('assistant', 'Help me get better sales results. Be brief and concise.');
$context = "# CONTEXT\n\n" . file_get_contents(__DIR__ . '/summary.md');
$summarizer = new SummarizeMessages(
//prompt: 'Summarize the messages.',
llm: LLM::connection('deepseek'),
//model: 'gpt-4o-mini',
tokenLimit: 1024,
);
//$chat = new ChatWithSummary(
// null,
// 256,
// 256,
// 1024,
// true,
// true,
// $summarizer,
//);
$chat = ChatWithSummary::create(
256,
256,
1024,
$summarizer,
);
$chat->script()->section('main')->appendMessage($startMessage);
//Debug::setEnabled();
for($i = 0; $i < $maxSteps; $i++) {
$chat->script()
->section('system')
->withMessages(Messages::fromString($sys[$i % 2], 'system'));
$chat->script()
->section('context')
->withMessages(Messages::fromString($context, 'system'));
$messages = $chat->script()
->select(['system', 'context', 'summary', 'buffer', 'main'])
->toMessages()
->remapRoles(['assistant' => 'user', 'user' => 'assistant', 'system' => 'system']);
dump($messages->toRoleString());
$response = Inference::text(
messages: $messages->toArray(),
connection: 'deepseek',
options: ['max_tokens' => 256],
);
echo "\n";
dump('>>> '.$response);
echo "\n";
$chat->appendMessage(new Message(role: 'assistant', content: $response), 'main');
}
//dump($chat->script());