Skip to content

Commit

Permalink
Cleanup (consistent naming) + PartialJsonParser bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Oct 3, 2024
1 parent 4b73881 commit 5411bf8
Show file tree
Hide file tree
Showing 45 changed files with 296 additions and 205 deletions.
4 changes: 2 additions & 2 deletions docs/cookbook/examples/advanced/context_cache_llm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ $inference = (new Inference)->withConnection('anthropic')->withCachedContext(
$response = $inference->create(
messages: [['role' => 'user', 'content' => 'CTO of lead gen software vendor']],
options: ['max_tokens' => 256],
)->toApiResponse();
)->toLLMResponse();

print("----------------------------------------\n");
print("\n# Summary for CTO of lead gen vendor\n");
Expand All @@ -60,7 +60,7 @@ assert(Str::contains($response->content, 'lead', false));
$response2 = $inference->create(
messages: [['role' => 'user', 'content' => 'CIO of insurance company']],
options: ['max_tokens' => 256],
)->toApiResponse();
)->toLLMResponse();

print("----------------------------------------\n");
print("\n# Summary for CIO of insurance company\n");
Expand Down
6 changes: 5 additions & 1 deletion docs/cookbook/examples/extras/web_to_objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ $companyGen = Webpage::withScraper('none')
->get('https://themanifest.com/pl/software-development/laravel/companies?page=1')
->cleanup()
->select('.directory-providers__list')
->selectMany(selector: '.provider-card', callback: fn($item) => $item->asMarkdown(), limit: 3);
->selectMany(
selector: '.provider-card',
callback: fn($item) => $item->asMarkdown(),
limit: 3
);

$companies = [];
foreach($companyGen as $companyDiv) {
Expand Down
18 changes: 9 additions & 9 deletions docs/cookbook/examples/troubleshooting/token_usage_events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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
Currently, this can be done by listening to the `LLMResponseReceived`
and `PartialLLMResponseReceived` events and summing the token usage
of the responses.

Code below demonstrates how it can be implemented using Instructor
Expand All @@ -24,10 +24,10 @@ event listeners.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Events\ApiClient\ApiResponseReceived;
use Cognesy\Instructor\Events\ApiClient\PartialApiResponseReceived;
use Cognesy\Instructor\Extras\LLM\Data\LLMApiResponse;
use Cognesy\Instructor\Extras\LLM\Data\PartialLLMApiResponse;
use Cognesy\Instructor\Events\ApiClient\LLMResponseReceived;
use Cognesy\Instructor\Events\ApiClient\PartialLLMResponseReceived;
use Cognesy\Instructor\Extras\LLM\Data\LLMResponse;
use Cognesy\Instructor\Extras\LLM\Data\PartialLLMResponse;
use Cognesy\Instructor\Instructor;

class User {
Expand All @@ -41,7 +41,7 @@ class TokenCounter {
public int $cacheCreation = 0;
public int $cacheRead = 0;

public function add(LLMApiResponse|PartialLLMApiResponse $response) {
public function add(LLMResponse|PartialLLMResponse $response) {
$this->input += $response->inputTokens;
$this->output += $response->outputTokens;
$this->cacheCreation += $response->cacheCreationTokens;
Expand All @@ -68,7 +68,7 @@ $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))
->onEvent(LLMResponseReceived::class, fn(LLMResponseReceived $e) => $counter->add($e->llmResponse))
->respond(
messages: $text,
responseModel: User::class,
Expand All @@ -84,7 +84,7 @@ $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))
->onEvent(PartialLLMResponseReceived::class, fn(PartialLLMResponseReceived $e) => $counter->add($e->partialLLMResponse))
->respond(
messages: $text,
responseModel: User::class,
Expand Down
10 changes: 5 additions & 5 deletions evals/LLMModes/CompareModes.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ private function execute(string $connection, Mode $mode, bool $isStreamed) : Eva
$key = $this->makeKey($connection, $mode, $isStreamed);
try {
$time = microtime(true);
$apiResponse = $this->modes->callInferenceFor($this->query, $mode, $connection, $this->modes->schema(), $isStreamed);
$answer = $apiResponse->content;
$llmResponse = $this->modes->callInferenceFor($this->query, $mode, $connection, $this->modes->schema(), $isStreamed);
$answer = $llmResponse->content;
$timeElapsed = microtime(true) - $time;
$evalRequest = new EvalRequest(
answer: $answer,
Expand All @@ -58,16 +58,16 @@ private function execute(string $connection, Mode $mode, bool $isStreamed) : Eva
mode: $mode,
connection: $connection,
isStreamed: $isStreamed,
response: $apiResponse,
response: $llmResponse,
);
$isCorrect = ($this->evalFn)($evalRequest);
$evalResponse = new EvalResponse(
id: $key,
answer: $answer,
isCorrect: $isCorrect,
timeElapsed: $timeElapsed,
inputTokens: $apiResponse->inputTokens,
outputTokens: $apiResponse->outputTokens,
inputTokens: $llmResponse->inputTokens,
outputTokens: $llmResponse->outputTokens,
);
} catch(Exception $e) {
$timeElapsed = microtime(true) - $time;
Expand Down
16 changes: 8 additions & 8 deletions evals/LLMModes/EvalRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace Cognesy\Evals\LLMModes;

use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Extras\LLM\Data\LLMApiResponse;
use Cognesy\Instructor\Extras\LLM\Data\LLMResponse;

class EvalRequest
{
public function __construct(
public string $answer = '',
public string|array $query = '',
public array $schema = [],
public Mode $mode = Mode::Text,
public string $connection = '',
public bool $isStreamed = false,
public ?LLMApiResponse $response = null,
public string $answer = '',
public string|array $query = '',
public array $schema = [],
public Mode $mode = Mode::Text,
public string $connection = '',
public bool $isStreamed = false,
public ?LLMResponse $response = null,
) {}
}
6 changes: 3 additions & 3 deletions evals/LLMModes/Modes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Cognesy\Evals\LLMModes;

use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Extras\LLM\Data\LLMApiResponse;
use Cognesy\Instructor\Extras\LLM\Data\LLMResponse;
use Cognesy\Instructor\Extras\LLM\Inference;
use Cognesy\Instructor\Extras\LLM\InferenceResponse;

Expand All @@ -25,7 +25,7 @@ public function schema() : array {
return $this->model->schema();
}

public function callInferenceFor(string|array $query, Mode $mode, string $connection, array $schema, bool $isStreamed) : LLMApiResponse {
public function callInferenceFor(string|array $query, Mode $mode, string $connection, array $schema, bool $isStreamed) : LLMResponse {
$query = is_array($query) ? $query : [['role' => 'user', 'content' => $query]];
$inferenceResponse = match($mode) {
Mode::Tools => $this->forModeTools($query, $connection, $schema, $isStreamed),
Expand All @@ -34,7 +34,7 @@ public function callInferenceFor(string|array $query, Mode $mode, string $connec
Mode::MdJson => $this->forModeMdJson($query, $connection, $schema, $isStreamed),
Mode::Text => $this->forModeText($query, $connection, $isStreamed),
};
return $inferenceResponse->toApiResponse();
return $inferenceResponse->toLLMResponse();
}

public function forModeTools(string|array $query, string $connection, array $schema, bool $isStreamed) : InferenceResponse {
Expand Down
5 changes: 2 additions & 3 deletions evals/LLMModes/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Cognesy\Evals\LLMModes\CompareModes;
use Cognesy\Evals\LLMModes\EvalRequest;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Extras\Debug\Debug;
use Cognesy\Instructor\Utils\Json\Json;
use Cognesy\Instructor\Utils\Str;

Expand All @@ -24,8 +23,8 @@
];

$streamingModes = [
false,
true,
false,
];

$modes = [
Expand Down Expand Up @@ -59,7 +58,7 @@ function evalFn(EvalRequest $er) {
function validateToolsData(array $data) : bool {
return 'store_company' === ($data[0]['name'] ?? '')
&& 'ACME' === ($data[0]['arguments']['name'] ?? '')
&& 2020 === ($data[0]['arguments']['year'] ?? 0);
&& 2020 === (int) ($data[0]['arguments']['year'] ?? 0);
}

(new CompareModes(
Expand Down
4 changes: 2 additions & 2 deletions examples/A02_Advanced/ContextCacheLLM/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
$response = $inference->create(
messages: [['role' => 'user', 'content' => 'CTO of lead gen software vendor']],
options: ['max_tokens' => 256],
)->toApiResponse();
)->toLLMResponse();

print("----------------------------------------\n");
print("\n# Summary for CTO of lead gen vendor\n");
Expand All @@ -60,7 +60,7 @@
$response2 = $inference->create(
messages: [['role' => 'user', 'content' => 'CIO of insurance company']],
options: ['max_tokens' => 256],
)->toApiResponse();
)->toLLMResponse();

print("----------------------------------------\n");
print("\n# Summary for CIO of insurance company\n");
Expand Down
20 changes: 10 additions & 10 deletions examples/A03_Troubleshooting/TokenUsage/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
## 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
Currently, this can be done by listening to the `LLMResponseReceived`
and `PartialLLMResponseReceived` events and summing the token usage
of the responses.

Code below demonstrates how it can be implemented using Instructor
Expand All @@ -24,10 +24,10 @@
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Events\ApiClient\ApiResponseReceived;
use Cognesy\Instructor\Events\ApiClient\PartialApiResponseReceived;
use Cognesy\Instructor\Extras\LLM\Data\LLMApiResponse;
use Cognesy\Instructor\Extras\LLM\Data\PartialLLMApiResponse;
use Cognesy\Instructor\Events\ApiClient\LLMResponseReceived;
use Cognesy\Instructor\Events\ApiClient\PartialLLMResponseReceived;
use Cognesy\Instructor\Extras\LLM\Data\LLMResponse;
use Cognesy\Instructor\Extras\LLM\Data\PartialLLMResponse;
use Cognesy\Instructor\Instructor;

class User {
Expand All @@ -41,7 +41,7 @@ class TokenCounter {
public int $cacheCreation = 0;
public int $cacheRead = 0;

public function add(LLMApiResponse|PartialLLMApiResponse $response) {
public function add(LLMResponse|PartialLLMResponse $response) {
$this->input += $response->inputTokens;
$this->output += $response->outputTokens;
$this->cacheCreation += $response->cacheCreationTokens;
Expand All @@ -68,11 +68,11 @@ public function print() {
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))
->onEvent(LLMResponseReceived::class, fn(LLMResponseReceived $e) => $counter->add($e->llmResponse))
->respond(
messages: $text,
responseModel: User::class,
);
);
echo "\nTEXT: $text\n";
assert($counter->input > 0);
assert($counter->output > 0);
Expand All @@ -84,7 +84,7 @@ public function print() {
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))
->onEvent(PartialLLMResponseReceived::class, fn(PartialLLMResponseReceived $e) => $counter->add($e->partialLLMResponse))
->respond(
messages: $text,
responseModel: User::class,
Expand Down
2 changes: 1 addition & 1 deletion examples/A05_Extras/ComplexExtractionClaude/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ enum StakeholderRole: string {
->request(
messages: $report,
responseModel: Sequence::of(ProjectEvent::class),
model: 'claude-3-haiku-20240307', //'claude-3-sonnet-20240229',
model: 'claude-3-5-sonnet-20240620', // 'claude-3-haiku-20240307'
prompt: 'Extract a list of project events with all the details from the provided input in JSON format using schema: <|json_schema|>',
mode: Mode::Json,
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",],],]]]]],
Expand Down
1 change: 1 addition & 0 deletions examples/A05_Extras/ImageToDataAnthropic/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Receipt {
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|>',
model: 'claude-3-5-sonnet-20240620',
mode: Mode::Json,
options: ['max_tokens' => 4096]
);
Expand Down
6 changes: 5 additions & 1 deletion examples/A05_Extras/WebToObjects/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ class Company {
->get('https://themanifest.com/pl/software-development/laravel/companies?page=1')
->cleanup()
->select('.directory-providers__list')
->selectMany(selector: '.provider-card', callback: fn($item) => $item->asMarkdown(), limit: 3);
->selectMany(
selector: '.provider-card',
callback: fn($item) => $item->asMarkdown(),
limit: 3
);

$companies = [];
foreach($companyGen as $companyDiv) {
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/CanGeneratePartials.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Cognesy\Instructor\Contracts;

use Cognesy\Instructor\Data\ResponseModel;
use Cognesy\Instructor\Extras\LLM\Data\PartialLLMApiResponse;
use Cognesy\Instructor\Extras\LLM\Data\PartialLLMResponse;
use Generator;

interface CanGeneratePartials
{
/**
* @param Generator<PartialLLMApiResponse> $stream
* @param Generator<PartialLLMResponse> $stream
* @param ResponseModel $responseModel
* @return Generator<mixed>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/CanGenerateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Cognesy\Instructor\Contracts;

use Cognesy\Instructor\Data\ResponseModel;
use Cognesy\Instructor\Extras\LLM\Data\LLMApiResponse;
use Cognesy\Instructor\Extras\LLM\Data\LLMResponse;
use Cognesy\Instructor\Utils\Result\Result;

interface CanGenerateResponse
{
public function makeResponse(LLMApiResponse $response, ResponseModel $responseModel) : Result;
public function makeResponse(LLMResponse $response, ResponseModel $responseModel) : Result;
}
Loading

0 comments on commit 5411bf8

Please sign in to comment.