Skip to content

Commit

Permalink
Examples update
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Aug 26, 2024
1 parent 091dcf2 commit 071a211
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 10 additions & 6 deletions docs/cookbook/examples/troubleshooting/token_usage_events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class User {
}

class TokenCounter {
private int $input = 0;
private int $output = 0;
private int $cacheCreation = 0;
private int $cacheRead = 0;
public int $input = 0;
public int $output = 0;
public int $cacheCreation = 0;
public int $cacheRead = 0;

public function add(ApiResponse|PartialApiResponse $response) {
$this->input += $response->inputTokens;
Expand Down Expand Up @@ -74,7 +74,11 @@ $instructor = (new Instructor)
responseModel: User::class,
);
echo "\nTEXT: $text\n";
assert($counter->input > 0);
assert($counter->output > 0);
$counter->print();

// Reset the counter
$counter->reset();

echo "\n\nCOUNTING TOKENS FOR STREAMED RESPONSE\n";
Expand All @@ -87,8 +91,8 @@ $instructor = (new Instructor)
options: ['stream' => true, 'stream_options' => ['include_usage' => true]],
);
echo "\nTEXT: $text\n";
assert($counter->input > 0);
assert($counter->output > 0);
$counter->print();
$counter->reset();

?>
```
16 changes: 10 additions & 6 deletions examples/04_Troubleshooting/TokenUsage/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class User {
}

class TokenCounter {
private int $input = 0;
private int $output = 0;
private int $cacheCreation = 0;
private int $cacheRead = 0;
public int $input = 0;
public int $output = 0;
public int $cacheCreation = 0;
public int $cacheRead = 0;

public function add(ApiResponse|PartialApiResponse $response) {
$this->input += $response->inputTokens;
Expand Down Expand Up @@ -74,7 +74,11 @@ public function print() {
responseModel: User::class,
);
echo "\nTEXT: $text\n";
assert($counter->input > 0);
assert($counter->output > 0);
$counter->print();

// Reset the counter
$counter->reset();

echo "\n\nCOUNTING TOKENS FOR STREAMED RESPONSE\n";
Expand All @@ -87,8 +91,8 @@ public function print() {
options: ['stream' => true, 'stream_options' => ['include_usage' => true]],
);
echo "\nTEXT: $text\n";
assert($counter->input > 0);
assert($counter->output > 0);
$counter->print();
$counter->reset();

?>
```

0 comments on commit 071a211

Please sign in to comment.