Skip to content

Commit

Permalink
Fixed PartialsGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Oct 2, 2024
1 parent adede3c commit c75edf5
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 106 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,10 @@ $yourApiKey = Env::get('OPENAI_API_KEY'); // use your own API key
$driver = new OpenAIDriver(new LLMConfig(
apiUrl: 'https://api.openai.com/v1', // you can change base URI
apiKey: $yourApiKey,
endpoint: '/chat/completions',
metadata: ['organization' => ''],
model: 'gpt-4o-mini',
maxTokens: 128,
));

/// Get Instructor with the default client component overridden with your own
Expand Down
3 changes: 3 additions & 0 deletions docs/advanced/model_options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use Cognesy\Instructor\Extras\LLM\Drivers\OpenAIDriver;
$driver = new OpenAIDriver(new LLMConfig(
apiUrl: 'https://api.openai.com/v1', // you can change base URI
apiKey: $yourApiKey,
endpoint: '/chat/completions',
metadata: ['organization' => ''],
model: 'gpt-4o-mini',
maxTokens: 128,
));

/// Get Instructor with the default client component overridden with your own
Expand Down
4 changes: 2 additions & 2 deletions docs/cookbook/examples/advanced/context_cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Project {
public array $applications;
#[Description('Explain the purpose of the project and the domain specific problems it solves')]
public string $description;
#[Description('Example code in Markdown demonstrating domain specific application of the library')]
#[Description('Example code as Markdown fragment, demonstrating domain specific application of the library')]
public string $code;
}
?>
Expand Down Expand Up @@ -93,7 +93,7 @@ which results in faster processing and lower costs.
```php
<?php
$project = $cached->respond(
messages: "Describe the project in a way compelling to my audience: boutique CMS consulting company owner.",
messages: "Describe the project in a way compelling to my audience: lead gen software vendor.",
responseModel: Project::class,
options: ['max_tokens' => 4096],
mode: Mode::Json,
Expand Down
3 changes: 3 additions & 0 deletions docs/cookbook/examples/advanced/custom_client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class User {
$driver = new OpenAIDriver(new LLMConfig(
apiUrl: 'https://api.openai.com/v1',
apiKey: Env::get('OPENAI_API_KEY'),
endpoint: '/chat/completions',
metadata: ['organization' => ''],
model: 'gpt-4o-mini',
maxTokens: 128,
)
);

Expand Down
1 change: 1 addition & 0 deletions docs/cookbook/examples/advanced/partials.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ response is received.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Events\Event;
use Cognesy\Instructor\Instructor;

class UserRole
Expand Down
4 changes: 2 additions & 2 deletions examples/A02_Advanced/ContextCaching/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Project {
public array $applications;
#[Description('Explain the purpose of the project and the domain specific problems it solves')]
public string $description;
#[Description('Example code in Markdown demonstrating domain specific application of the library')]
#[Description('Example code as Markdown fragment, demonstrating domain specific application of the library')]
public string $code;
}
?>
Expand Down Expand Up @@ -93,7 +93,7 @@ class Project {
```php
<?php
$project = $cached->respond(
messages: "Describe the project in a way compelling to my audience: boutique CMS consulting company owner.",
messages: "Describe the project in a way compelling to my audience: lead gen software vendor.",
responseModel: Project::class,
options: ['max_tokens' => 4096],
mode: Mode::Json,
Expand Down
3 changes: 3 additions & 0 deletions examples/A02_Advanced/CustomClientParameters/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class User {
$driver = new OpenAIDriver(new LLMConfig(
apiUrl: 'https://api.openai.com/v1',
apiKey: Env::get('OPENAI_API_KEY'),
endpoint: '/chat/completions',
metadata: ['organization' => ''],
model: 'gpt-3.5-turbo',
maxTokens: 128,
)
);

Expand Down
1 change: 1 addition & 0 deletions examples/A02_Advanced/PartialUpdates/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Events\Event;
use Cognesy\Instructor\Instructor;

class UserRole
Expand Down
8 changes: 7 additions & 1 deletion src/Core/StreamResponse/PartialsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ protected function tryGetPartialObject(
string $partialJsonData,
ResponseModel $responseModel,
) : Result {
return Chain::from(fn() => Json::fix(Json::find($partialJsonData)))
// dump('raw:', $partialJsonData);
// $found = Json::findPartial($partialJsonData);
// dump('found:', $found);
// $json = Json::fix($found);
// dump('fixed:', $json);

return Chain::from(fn() => Json::fix(Json::findPartial($partialJsonData)))
->through(fn($jsonData) => $this->responseDeserializer->deserialize($jsonData, $responseModel, $this?->toolCalls->last()->name))
->through(fn($object) => $this->responseTransformer->transform($object))
->result();
Expand Down
Loading

0 comments on commit c75edf5

Please sign in to comment.