Skip to content

Commit

Permalink
Docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Sep 2, 2024
1 parent b93a519 commit e4450e4
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/cookbook/examples/advanced/caching.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: 'Caching'
title: 'Request caching'
docname: 'caching'
---

Expand Down
4 changes: 2 additions & 2 deletions docs/cookbook/examples/basics/complex_extraction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ $events = $instructor
->request(
messages: $report,
responseModel: Sequence::of(ProjectEvent::class),
model: 'gpt-4o-mini',
mode: Mode::Json,
model: 'gpt-4o',
mode: Mode::Tools,
options: [
'max_tokens' => 2048,
'stream' => true,
Expand Down
141 changes: 141 additions & 0 deletions docs/cookbook/examples/basics/complex_extraction_cohere.mdx
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";
}
?>
```
6 changes: 5 additions & 1 deletion docs/essentials/prompts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Instructor to deserialize it correctly.

```php
<?php
// NOTE: You don't have to create JSON Schema manually, you can use
// schema automatically generated by Instructor for your response model.
// See the next example.

$jsonSchema = json_encode([
"type" => "object",
"properties" => [
Expand All @@ -88,7 +92,7 @@ $user = $instructor
```

The example above demonstrates how to manually create JSON Schema, but
with Instructor you do not have to build the schema manually - you can use prompt
**with Instructor you do not have to build the schema manually** - you can use prompt
template placeholder syntax to use Instructor-generated JSON Schema.


Expand Down
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"cookbook/examples/basics/basic_use_mixin",
"cookbook/examples/basics/complex_extraction",
"cookbook/examples/basics/complex_extraction_claude",
"cookbook/examples/basics/complex_extraction_cohere",
"cookbook/examples/basics/complex_extraction_gemini",
"cookbook/examples/basics/maybe",
"cookbook/examples/basics/modes",
Expand Down
4 changes: 2 additions & 2 deletions examples/01_Basics/ComplexExtraction/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ enum StakeholderRole: string {
->request(
messages: $report,
responseModel: Sequence::of(ProjectEvent::class),
model: 'gpt-4o-mini',
mode: Mode::Json,
model: 'gpt-4o',
mode: Mode::Tools,
options: [
'max_tokens' => 2048,
'stream' => true,
Expand Down
141 changes: 141 additions & 0 deletions examples/01_Basics/ComplexExtractionCohere/run.php
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";
}
?>
```
2 changes: 1 addition & 1 deletion examples/02_Advanced/Caching/run.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: 'Caching'
title: 'Request caching'
docname: 'caching'
---

Expand Down
1 change: 0 additions & 1 deletion src/Traits/Instructor/HandlesInvocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function respond(
return $this->get();
}

// TODO: to be implemented
public function cacheContext(
string|array $messages = '',
string|array|object $input = '',
Expand Down

0 comments on commit e4450e4

Please sign in to comment.