-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b93a519
commit e4450e4
Showing
9 changed files
with
294 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: 'Caching' | ||
title: 'Request caching' | ||
docname: 'caching' | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
docs/cookbook/examples/basics/complex_extraction_cohere.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
--- | ||
title: 'Extraction of complex objects (Cohere)' | ||
docname: 'complex_extraction_cohere' | ||
--- | ||
|
||
## Overview | ||
|
||
This is an example of extraction of a very complex structure from | ||
the provided text with Cohere R models. | ||
|
||
## Example | ||
|
||
```php | ||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/'); | ||
|
||
use Cognesy\Instructor\Clients\Cohere\CohereClient; | ||
use Cognesy\Instructor\Enums\Mode; | ||
use Cognesy\Instructor\Extras\Sequence\Sequence; | ||
use Cognesy\Instructor\Instructor; | ||
use Cognesy\Instructor\Utils\Env; | ||
|
||
$report = <<<'EOT' | ||
[2021-09-01] | ||
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. Due to dependencies it will result in delay of the | ||
ecommerce track by 2 sprints. 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. Another issue is that the | ||
customer is not able to provide the test data for the ecommerce track. | ||
SysCorp notified it will impact stabilization schedule unless resolved by | ||
the end of the month. Steerco has been informed last week about the | ||
potential impact of the issues, but insists on maintaining release schedule | ||
due to marketing campaign already ongoing. Customer executives are asking | ||
us - SalesTech team - to confirm SysCorp's assessment of the situation. | ||
We're struggling with that due to communication issues - SysCorp team has | ||
not shown up on 2 recent calls. Lack of insight has been escalated to | ||
SysCorp's leadership team yesterday, but we've got no response yet. The | ||
previously reported Integration Proxy connectivity issue which was blocking | ||
policy track has been resolved on 2021-08-30 - the track is now GREEN. | ||
Production deployment plan has been finalized on Aug 15th and awaiting | ||
customer approval. | ||
EOT; | ||
|
||
echo "Extracting project events from the report:\n\n"; | ||
echo $report . "\n\n"; | ||
|
||
/** Represents a project event */ | ||
class ProjectEvent { | ||
/** Title of the event - this should be a short, descriptive title of the event */ | ||
public string $title = ''; | ||
/** Concise, informative description of the event */ | ||
public string $description = ''; | ||
/** Type of the event */ | ||
public ProjectEventType $type = ProjectEventType::Other; | ||
/** Status of the event */ | ||
public ProjectEventStatus $status = ProjectEventStatus::Unknown; | ||
/** Stakeholders involved in the event */ | ||
/** @var Stakeholder[] */ | ||
public array $stakeholders = []; | ||
/** Date of the event if reported in the text */ | ||
public ?string $date = ''; | ||
} | ||
|
||
/** Represents status of project event */ | ||
enum ProjectEventStatus: string { | ||
case Open = 'open'; | ||
case Closed = 'closed'; | ||
case Unknown = 'unknown'; | ||
} | ||
|
||
/** Represents type of project event */ | ||
enum ProjectEventType: string { | ||
case Risk = 'risk'; | ||
case Issue = 'issue'; | ||
case Action = 'action'; | ||
case Progress = 'progress'; | ||
case Other = 'other'; | ||
} | ||
|
||
/** Represents a project stakeholder */ | ||
class Stakeholder { | ||
/** Name of the stakeholder */ | ||
public string $name = ''; | ||
/** 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 = ''; | ||
} | ||
|
||
enum StakeholderRole: string { | ||
case Customer = 'customer'; | ||
case Vendor = 'vendor'; | ||
case SystemIntegrator = 'system integrator'; | ||
case Other = 'other'; | ||
} | ||
|
||
$client = new CohereClient( | ||
apiKey: Env::get('COHERE_API_KEY'), | ||
); | ||
|
||
$instructor = (new Instructor)->withClient($client)->withDebug(); | ||
|
||
echo "PROJECT EVENTS:\n\n"; | ||
|
||
$events = $instructor | ||
->onSequenceUpdate(fn($sequence) => displayEvent($sequence->last())) | ||
->request( | ||
messages: $report, | ||
responseModel: Sequence::of(ProjectEvent::class), | ||
model: 'command-r-plus-08-2024', | ||
mode: Mode::JsonSchema, | ||
options: [ | ||
'max_tokens' => 2048, | ||
'stream' => true, | ||
]) | ||
->get(); | ||
|
||
echo "TOTAL EVENTS: " . count($events) . "\n"; | ||
|
||
function displayEvent(ProjectEvent $event) : void { | ||
echo "Event: {$event->title}\n"; | ||
echo " - Descriptions: {$event->description}\n"; | ||
echo " - Type: {$event->type->value}\n"; | ||
echo " - Status: {$event->status->value}\n"; | ||
echo " - Date: {$event->date}\n"; | ||
if (empty($event->stakeholders)) { | ||
echo " - Stakeholders: none\n"; | ||
} else { | ||
echo " - Stakeholders:\n"; | ||
foreach($event->stakeholders as $stakeholder) { | ||
echo " - {$stakeholder->name} ({$stakeholder->role->value})\n"; | ||
} | ||
} | ||
echo "\n"; | ||
} | ||
?> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
--- | ||
title: 'Extraction of complex objects (Cohere)' | ||
docname: 'complex_extraction_cohere' | ||
--- | ||
|
||
## Overview | ||
|
||
This is an example of extraction of a very complex structure from | ||
the provided text with Cohere R models. | ||
|
||
## Example | ||
|
||
```php | ||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/'); | ||
|
||
use Cognesy\Instructor\Clients\Cohere\CohereClient; | ||
use Cognesy\Instructor\Enums\Mode; | ||
use Cognesy\Instructor\Extras\Sequence\Sequence; | ||
use Cognesy\Instructor\Instructor; | ||
use Cognesy\Instructor\Utils\Env; | ||
|
||
$report = <<<'EOT' | ||
[2021-09-01] | ||
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. Due to dependencies it will result in delay of the | ||
ecommerce track by 2 sprints. 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. Another issue is that the | ||
customer is not able to provide the test data for the ecommerce track. | ||
SysCorp notified it will impact stabilization schedule unless resolved by | ||
the end of the month. Steerco has been informed last week about the | ||
potential impact of the issues, but insists on maintaining release schedule | ||
due to marketing campaign already ongoing. Customer executives are asking | ||
us - SalesTech team - to confirm SysCorp's assessment of the situation. | ||
We're struggling with that due to communication issues - SysCorp team has | ||
not shown up on 2 recent calls. Lack of insight has been escalated to | ||
SysCorp's leadership team yesterday, but we've got no response yet. The | ||
previously reported Integration Proxy connectivity issue which was blocking | ||
policy track has been resolved on 2021-08-30 - the track is now GREEN. | ||
Production deployment plan has been finalized on Aug 15th and awaiting | ||
customer approval. | ||
EOT; | ||
|
||
echo "Extracting project events from the report:\n\n"; | ||
echo $report . "\n\n"; | ||
|
||
/** Represents a project event */ | ||
class ProjectEvent { | ||
/** Title of the event - this should be a short, descriptive title of the event */ | ||
public string $title = ''; | ||
/** Concise, informative description of the event */ | ||
public string $description = ''; | ||
/** Type of the event */ | ||
public ProjectEventType $type = ProjectEventType::Other; | ||
/** Status of the event */ | ||
public ProjectEventStatus $status = ProjectEventStatus::Unknown; | ||
/** Stakeholders involved in the event */ | ||
/** @var Stakeholder[] */ | ||
public array $stakeholders = []; | ||
/** Date of the event if reported in the text */ | ||
public ?string $date = ''; | ||
} | ||
|
||
/** Represents status of project event */ | ||
enum ProjectEventStatus: string { | ||
case Open = 'open'; | ||
case Closed = 'closed'; | ||
case Unknown = 'unknown'; | ||
} | ||
|
||
/** Represents type of project event */ | ||
enum ProjectEventType: string { | ||
case Risk = 'risk'; | ||
case Issue = 'issue'; | ||
case Action = 'action'; | ||
case Progress = 'progress'; | ||
case Other = 'other'; | ||
} | ||
|
||
/** Represents a project stakeholder */ | ||
class Stakeholder { | ||
/** Name of the stakeholder */ | ||
public string $name = ''; | ||
/** 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 = ''; | ||
} | ||
|
||
enum StakeholderRole: string { | ||
case Customer = 'customer'; | ||
case Vendor = 'vendor'; | ||
case SystemIntegrator = 'system integrator'; | ||
case Other = 'other'; | ||
} | ||
|
||
$client = new CohereClient( | ||
apiKey: Env::get('COHERE_API_KEY'), | ||
); | ||
|
||
$instructor = (new Instructor)->withClient($client)->withDebug(); | ||
|
||
echo "PROJECT EVENTS:\n\n"; | ||
|
||
$events = $instructor | ||
->onSequenceUpdate(fn($sequence) => displayEvent($sequence->last())) | ||
->request( | ||
messages: $report, | ||
responseModel: Sequence::of(ProjectEvent::class), | ||
model: 'command-r-plus-08-2024', | ||
mode: Mode::JsonSchema, | ||
options: [ | ||
'max_tokens' => 2048, | ||
'stream' => true, | ||
]) | ||
->get(); | ||
|
||
echo "TOTAL EVENTS: " . count($events) . "\n"; | ||
|
||
function displayEvent(ProjectEvent $event) : void { | ||
echo "Event: {$event->title}\n"; | ||
echo " - Descriptions: {$event->description}\n"; | ||
echo " - Type: {$event->type->value}\n"; | ||
echo " - Status: {$event->status->value}\n"; | ||
echo " - Date: {$event->date}\n"; | ||
if (empty($event->stakeholders)) { | ||
echo " - Stakeholders: none\n"; | ||
} else { | ||
echo " - Stakeholders:\n"; | ||
foreach($event->stakeholders as $stakeholder) { | ||
echo " - {$stakeholder->name} ({$stakeholder->role->value})\n"; | ||
} | ||
} | ||
echo "\n"; | ||
} | ||
?> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: 'Caching' | ||
title: 'Request caching' | ||
docname: 'caching' | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters