From b281aef6d3425fb7333d0ace04117b92b95dc772 Mon Sep 17 00:00:00 2001 From: ddebowczyk Date: Sat, 21 Dec 2024 11:12:17 +0100 Subject: [PATCH] Fix: sequence handling when streaming large chunks (rather than per token) --- .../examples/extras/complex_extraction_gemini.mdx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/cookbook/examples/extras/complex_extraction_gemini.mdx b/docs/cookbook/examples/extras/complex_extraction_gemini.mdx index 29c433e1..e5398229 100644 --- a/docs/cookbook/examples/extras/complex_extraction_gemini.mdx +++ b/docs/cookbook/examples/extras/complex_extraction_gemini.mdx @@ -16,6 +16,7 @@ $loader = require 'vendor/autoload.php'; $loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/'); use Cognesy\Instructor\Enums\Mode; +use Cognesy\Instructor\Events\Inference\PartialLLMResponseReceived; use Cognesy\Instructor\Extras\Sequence\Sequence; use Cognesy\Instructor\Instructor; use Cognesy\Instructor\Utils\Debug\Debug; @@ -87,7 +88,7 @@ class Stakeholder { /** Role of the stakeholder, if specified */ public StakeholderRole $role = StakeholderRole::Other; /** Any details on the stakeholder, if specified - any mentions of company, organization, structure, group, team, function */ - public string $details = ''; + public ?string $details = ''; } enum StakeholderRole: string { @@ -101,19 +102,21 @@ $instructor = (new Instructor)->withConnection('gemini'); echo "PROJECT EVENTS:\n\n"; -//Debug::enable(); - $events = $instructor ->onSequenceUpdate(fn($sequence) => displayEvent($sequence->last())) + //->onEvent(PartialLLMResponseReceived::class, fn(PartialLLMResponseReceived $e) => print "---\n".$e->partialLLMResponse->content()."---\n") ->request( messages: $report, responseModel: Sequence::of(ProjectEvent::class), examples: [['input' => 'Acme Insurance project to implement SalesTech CRM solution is currently in RED status due to delayed delivery of document production system, led by 3rd party vendor - Alfatech. Customer (Acme) is discussing the resolution with the vendor. Production deployment plan has been finalized on Aug 15th and awaiting customer approval.', 'output' => [["type" => "object", "title" => "sequenceOfProjectEvent", "description" => "A sequence of ProjectEvent", "properties" => ["list" => [["title" => "Absorbing delay by deploying extra resources", "description" => "System integrator (SysCorp) are working to absorb some of the delay by deploying extra resources to speed up development when the doc production is done.", "type" => "action", "status" => "open", "stakeholders" => [["name" => "SysCorp", "role" => "system integrator", "details" => "System integrator",],], "date" => "2021-09-01",], ["title" => "Finalization of production deployment plan", "description" => "Production deployment plan has been finalized on Aug 15th and awaiting customer approval.", "type" => "progress", "status" => "open", "stakeholders" => [["name" => "Acme", "role" => "customer", "details" => "Customer",],], "date" => "2021-08-15",],],]]]]], + model: 'gemini-1.5-flash', + //model: 'gemini-2.0-flash-exp', + //model: 'gemini-2.0-flash-thinking-exp', options: [ 'max_tokens' => 2048, 'stream' => true, ], - mode: Mode::Tools, + mode: Mode::Json, )->get(); echo "TOTAL EVENTS: " . count($events) . "\n";