-
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.
Example of token consumption counting using events
- Loading branch information
1 parent
45a0458
commit 091dcf2
Showing
38 changed files
with
403 additions
and
203 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: 'Image to data' | ||
title: 'Image to data (OpenAI)' | ||
docname: 'image_to_data' | ||
--- | ||
|
||
|
74 changes: 74 additions & 0 deletions
74
docs/cookbook/examples/techniques/image_to_data_anthropic.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,74 @@ | ||
--- | ||
title: 'Image to data (Anthropic)' | ||
docname: 'image_to_data_anthropic' | ||
--- | ||
|
||
## Overview | ||
|
||
This is an example of how to extract structured data from an image using | ||
Instructor. The image is loaded from a file and converted to base64 format | ||
before sending it to OpenAI API. | ||
|
||
The response model is a PHP class that represents the structured receipt | ||
information with data of vendor, items, subtotal, tax, tip, and total. | ||
|
||
|
||
## Scanned image | ||
|
||
Here's the image we're going to extract data from. | ||
|
||
![Receipt](images/receipt.png) | ||
|
||
|
||
## Example | ||
|
||
```php | ||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/'); | ||
|
||
use Cognesy\Instructor\Clients\Anthropic\AnthropicClient; | ||
use Cognesy\Instructor\Enums\Mode; | ||
use Cognesy\Instructor\Extras\Image\Image; | ||
use Cognesy\Instructor\Instructor; | ||
use Cognesy\Instructor\Utils\Env; | ||
|
||
class Vendor { | ||
public ?string $name = ''; | ||
public ?string $address = ''; | ||
public ?string $phone = ''; | ||
} | ||
|
||
class ReceiptItem { | ||
public string $name; | ||
public ?int $quantity = 1; | ||
public float $price; | ||
} | ||
|
||
class Receipt { | ||
public Vendor $vendor; | ||
/** @var ReceiptItem[] */ | ||
public array $items = []; | ||
public ?float $subtotal; | ||
public ?float $tax; | ||
public ?float $tip; | ||
public float $total; | ||
} | ||
|
||
$client = new AnthropicClient( | ||
apiKey: Env::get('ANTHROPIC_API_KEY'), | ||
); | ||
|
||
$receipt = (new Instructor)->withClient($client)->respond( | ||
input: Image::fromFile(__DIR__ . '/receipt.png'), | ||
responseModel: Receipt::class, | ||
prompt: 'Extract structured data from the receipt. Return result as JSON following this schema: <|json_schema|>', | ||
mode: Mode::Json, | ||
options: ['max_tokens' => 4096] | ||
); | ||
|
||
dump($receipt); | ||
|
||
assert($receipt->total === 169.82); | ||
?> | ||
``` |
94 changes: 94 additions & 0 deletions
94
docs/cookbook/examples/troubleshooting/token_usage_events.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,94 @@ | ||
--- | ||
title: 'Tracking token usage via events' | ||
docname: 'token_usage_events' | ||
--- | ||
|
||
## Overview | ||
|
||
Some use cases require tracking the token usage of the API responses. | ||
Currently, this can be done by listening to the `ApiResponseReceived` | ||
and `PartialApiResponseReceived` events and summing the token usage | ||
of the responses. | ||
|
||
Code below demonstrates how it can be implemented using Instructor | ||
event listeners. | ||
|
||
> Note: OpenAI API requires `stream_options` to be set to | ||
> `['include_usage' => true]` to include token usage in the streamed | ||
> responses. | ||
## Example | ||
|
||
```php | ||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/'); | ||
|
||
use Cognesy\Instructor\ApiClient\Responses\ApiResponse; | ||
use Cognesy\Instructor\ApiClient\Responses\PartialApiResponse; | ||
use Cognesy\Instructor\ApiClient\Traits\PartialApiResponseReceived; | ||
use Cognesy\Instructor\Events\ApiClient\ApiResponseReceived; | ||
use Cognesy\Instructor\Instructor; | ||
|
||
class User { | ||
public int $age; | ||
public string $name; | ||
} | ||
|
||
class TokenCounter { | ||
private int $input = 0; | ||
private int $output = 0; | ||
private int $cacheCreation = 0; | ||
private int $cacheRead = 0; | ||
|
||
public function add(ApiResponse|PartialApiResponse $response) { | ||
$this->input += $response->inputTokens; | ||
$this->output += $response->outputTokens; | ||
$this->cacheCreation += $response->cacheCreationTokens; | ||
$this->cacheRead += $response->cacheReadTokens; | ||
} | ||
|
||
public function reset() { | ||
$this->input = 0; | ||
$this->output = 0; | ||
$this->cacheCreation = 0; | ||
$this->cacheRead = 0; | ||
} | ||
|
||
public function print() { | ||
echo "Input tokens: $this->input\n"; | ||
echo "Output tokens: $this->output\n"; | ||
echo "Cache creation tokens: $this->cacheCreation\n"; | ||
echo "Cache read tokens: $this->cacheRead\n"; | ||
} | ||
} | ||
|
||
$counter = new TokenCounter(); | ||
|
||
echo "COUNTING TOKENS FOR SYNC RESPONSE\n"; | ||
$text = "Jason is 25 years old and works as an engineer."; | ||
$instructor = (new Instructor) | ||
->onEvent(ApiResponseReceived::class, fn($e) => $counter->add($e->apiResponse)) | ||
->respond( | ||
messages: $text, | ||
responseModel: User::class, | ||
); | ||
echo "\nTEXT: $text\n"; | ||
$counter->print(); | ||
$counter->reset(); | ||
|
||
echo "\n\nCOUNTING TOKENS FOR STREAMED RESPONSE\n"; | ||
$text = "Anna is 19 years old."; | ||
$instructor = (new Instructor) | ||
->onEvent(PartialApiResponseReceived::class, fn($e) => $counter->add($e->partialApiResponse)) | ||
->respond( | ||
messages: $text, | ||
responseModel: User::class, | ||
options: ['stream' => true, 'stream_options' => ['include_usage' => true]], | ||
); | ||
echo "\nTEXT: $text\n"; | ||
$counter->print(); | ||
$counter->reset(); | ||
|
||
?> | ||
``` |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: 'Image to data' | ||
title: 'Image to data (OpenAI)' | ||
docname: 'image_to_data' | ||
--- | ||
|
||
|
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
Oops, something went wrong.