Skip to content

Commit

Permalink
Cleanup of request handlers; fixes in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Sep 7, 2024
1 parent 491eb1a commit 682f553
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 415 deletions.
2 changes: 0 additions & 2 deletions docs/cookbook/examples/advanced/caching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ $user2 = $instructor->request(
responseModel: User::class,
)->withRequestCache()->get();

eval(\Psy\sh());

$delta = Profiler::mark('cache 1st call')->mili();
echo "Time elapsed (cache on, 1st call): $delta msec\n\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ $client = new AnthropicClient(
);

/// Get Instructor with the default client component overridden with your own
$instructor = (new Instructor)->withClient($client);
$instructor = (new Instructor)->withClient('anthropic');

echo "PROJECT EVENTS:\n\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ $client = new CohereClient(
apiKey: Env::get('COHERE_API_KEY'),
);

$instructor = (new Instructor)->withClient($client)->withDebug();
$instructor = (new Instructor)->withClient('cohere');

echo "PROJECT EVENTS:\n\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ $client = new GeminiClient(
apiKey: Env::get('GEMINI_API_KEY'),
);

$instructor = (new Instructor)->withClient($client);
$instructor = (new Instructor)->withClient('gemini');

echo "PROJECT EVENTS:\n\n";

Expand Down
2 changes: 1 addition & 1 deletion docs/cookbook/examples/basics/self_correction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ $user = (new Instructor)
->respond(
messages: $text,
responseModel: UserDetails::class,
maxRetries: 3
maxRetries: 3,
);

print("\nOUTPUT:\n");
Expand Down
6 changes: 4 additions & 2 deletions docs/cookbook/examples/techniques/chain_of_thought.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ $loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/');

use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Schema\Attributes\Instructions;

class Employee {
/** Think step by step to determine the correct year of employment. */
public string $chainOfThought;
#[Instructions('Think step by step to determine the correct year of employment.')]
public string $reasoning;
public int $yearOfEmployment;
// ... other data fields of your employee class
}

$text = 'He was working here for 5 years. Now, in 2019, he is a manager.';
Expand Down
2 changes: 1 addition & 1 deletion examples/01_Basics/ComplexExtractionClaude/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ enum StakeholderRole: string {
);

/// Get Instructor with the default client component overridden with your own
$instructor = (new Instructor)->withClient($client);
$instructor = (new Instructor)->withClient('anthropic');

echo "PROJECT EVENTS:\n\n";

Expand Down
2 changes: 1 addition & 1 deletion examples/01_Basics/ComplexExtractionCohere/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ enum StakeholderRole: string {
apiKey: Env::get('COHERE_API_KEY'),
);

$instructor = (new Instructor)->withClient($client)->withDebug();
$instructor = (new Instructor)->withClient('cohere');

echo "PROJECT EVENTS:\n\n";

Expand Down
2 changes: 1 addition & 1 deletion examples/01_Basics/ComplexExtractionGemini/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ enum StakeholderRole: string {
apiKey: Env::get('GEMINI_API_KEY'),
);

$instructor = (new Instructor)->withClient($client);
$instructor = (new Instructor)->withClient('gemini');

echo "PROJECT EVENTS:\n\n";

Expand Down
2 changes: 1 addition & 1 deletion examples/01_Basics/SelfCorrection/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class UserDetails
->respond(
messages: $text,
responseModel: UserDetails::class,
maxRetries: 3
maxRetries: 3,
);

print("\nOUTPUT:\n");
Expand Down
2 changes: 0 additions & 2 deletions examples/02_Advanced/Caching/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class User {
responseModel: User::class,
)->withRequestCache()->get();

eval(\Psy\sh());

$delta = Profiler::mark('cache 1st call')->mili();
echo "Time elapsed (cache on, 1st call): $delta msec\n\n";

Expand Down
6 changes: 4 additions & 2 deletions examples/03_Techniques/ChainOfThought/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/');

use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Schema\Attributes\Instructions;

class Employee {
/** Think step by step to determine the correct year of employment. */
public string $chainOfThought;
#[Instructions('Think step by step to determine the correct year of employment.')]
public string $reasoning;
public int $yearOfEmployment;
// ... other data fields of your employee class
}

$text = 'He was working here for 5 years. Now, in 2019, he is a manager.';
Expand Down
4 changes: 2 additions & 2 deletions src/ApiClient/LLMConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function resolveBaseUrl(): string {
}

protected function defaultHeaders(): array {
return [
return array_merge([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];
]);
}

public function defaultConfig(): array {
Expand Down
24 changes: 2 additions & 22 deletions src/Configs/ResponseHandlingConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
use Cognesy\Instructor\Container\Contracts\CanAddConfiguration;
use Cognesy\Instructor\Contracts\CanGeneratePartials;
use Cognesy\Instructor\Contracts\CanGenerateResponse;
use Cognesy\Instructor\Contracts\CanHandleRequest;
use Cognesy\Instructor\Contracts\CanHandleSyncRequest;
use Cognesy\Instructor\Contracts\CanHandleStreamRequest;
use Cognesy\Instructor\Core\NewerRequestHandler;
use Cognesy\Instructor\Core\RawRequestHandler;
use Cognesy\Instructor\Core\RawStreamRequestHandler;
use Cognesy\Instructor\Core\RequestHandler;
use Cognesy\Instructor\Core\Response\ResponseGenerator;
use Cognesy\Instructor\Core\StreamRequestHandler;
use Cognesy\Instructor\Core\StreamResponse\PartialsGenerator;
use Cognesy\Instructor\Deserialization\ResponseDeserializer;
use Cognesy\Instructor\Deserialization\Deserializers\SymfonyDeserializer;
Expand All @@ -33,19 +30,12 @@ class: RawRequestHandler::class,
]
);

$config->object(
class: RawStreamRequestHandler::class,
context: [
'events' => $config->reference(EventDispatcher::class),
]
);

$config->object(
class: RequestHandler::class,
name: CanHandleRequest::class,
context: [
'events' => $config->reference(EventDispatcher::class),
'responseGenerator' => $config->reference(CanGenerateResponse::class),
'partialsGenerator' => $config->reference(CanGeneratePartials::class),
]
);

Expand All @@ -60,16 +50,6 @@ class: ResponseGenerator::class,
]
);

$config->object(
class: StreamRequestHandler::class,
name: CanHandleStreamRequest::class,
context: [
'events' => $config->reference(EventDispatcher::class),
'responseGenerator' => $config->reference(CanGenerateResponse::class),
'partialsGenerator' => $config->reference(CanGeneratePartials::class),
]
);

$config->object(
class: PartialsGenerator::class,
name: CanGeneratePartials::class,
Expand Down
10 changes: 0 additions & 10 deletions src/Contracts/CanHandleRequest.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Contracts/CanHandleStreamRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

interface CanHandleStreamRequest
{
public function respondTo(Request $request) : mixed;
public function streamResponseFor(Request $request) : Generator;
}
10 changes: 10 additions & 0 deletions src/Contracts/CanHandleSyncRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cognesy\Instructor\Contracts;

use Cognesy\Instructor\Data\Request;

interface CanHandleSyncRequest
{
public function responseFor(Request $request) : mixed;
}
150 changes: 0 additions & 150 deletions src/Core/NewerRequestHandler.php

This file was deleted.

Loading

0 comments on commit 682f553

Please sign in to comment.