Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Concerns/HasSuccessHandlers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Prism\Prism\Concerns;

use Prism\Prism\Contracts\PrismRequest;
use Prism\Prism\Structured\Response as StructuredResponse;
use Prism\Prism\Text\Response as TextResponse;

trait HasSuccessHandlers
{
/** @var array<int, callable> */
protected array $handlers = [];

public function onSuccess(callable $callable): self
{
$this->handlers[] = $callable;

return $this;
}

public function processSuccessHandlers(PrismRequest $request, TextResponse|StructuredResponse $response): void
{
foreach ($this->handlers as $handler) {
$handler($request, $response);
}
}
}
7 changes: 6 additions & 1 deletion src/Structured/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Prism\Prism\Concerns\HasPrompts;
use Prism\Prism\Concerns\HasProviderOptions;
use Prism\Prism\Concerns\HasSchema;
use Prism\Prism\Concerns\HasSuccessHandlers;
use Prism\Prism\Exceptions\PrismException;
use Prism\Prism\ValueObjects\Messages\UserMessage;

Expand All @@ -26,6 +27,7 @@ class PendingRequest
use HasPrompts;
use HasProviderOptions;
use HasSchema;
use HasSuccessHandlers;

/**
* @deprecated Use `asStructured` instead.
Expand All @@ -40,7 +42,10 @@ public function asStructured(): Response
$request = $this->toRequest();

try {
return $this->provider->structured($request);
$response = $this->provider->structured($request);
$this->processSuccessHandlers($request, $response);

return $response;
} catch (RequestException $e) {
$this->provider->handleRequestException($request->model(), $e);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Text/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Prism\Prism\Concerns\HasPrompts;
use Prism\Prism\Concerns\HasProviderOptions;
use Prism\Prism\Concerns\HasProviderTools;
use Prism\Prism\Concerns\HasSuccessHandlers;
use Prism\Prism\Concerns\HasTools;
use Prism\Prism\Exceptions\PrismException;
use Prism\Prism\Tool;
Expand All @@ -31,6 +32,7 @@ class PendingRequest
use HasPrompts;
use HasProviderOptions;
use HasProviderTools;
use HasSuccessHandlers;
use HasTools;

/**
Expand All @@ -46,7 +48,10 @@ public function asText(): Response
$request = $this->toRequest();

try {
return $this->provider->text($request);
$response = $this->provider->text($request);
$this->processSuccessHandlers($request, $response);

return $response;
} catch (RequestException $e) {
$this->provider->handleRequestException($request->model(), $e);
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Testing/AssertRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Prism\Prism\Prism;
use Prism\Prism\Testing\TextResponseFake;
use Prism\Prism\ValueObjects\Usage;
use Tests\Fixtures\FixtureResponse;

it('can generate text and assert provider', function (): void {
$fakeResponse = TextResponseFake::make()
Expand Down Expand Up @@ -52,3 +53,49 @@
expect($requests[0]->model())->toBe('gpt-4');
});
});

describe('Success Handlers', function (): void {
it('calls success handlers on successful response', function (): void {

$fakeResponse = TextResponseFake::make()
->withText('Hello, I am Claude!')
->withUsage(new Usage(10, 20));

// Set up the fake
$fake = Prism::fake([$fakeResponse]);

// Run your code
$response = Prism::text()
->using(Provider::Anthropic, 'claude-3-5-sonnet-latest')
->onSuccess(function ($request, $response): void {
expect($response->usage->promptTokens)->toBe(10);
expect($response->usage->completionTokens)->toBe(20);
expect($request->model())->toBe('claude-3-5-sonnet-latest');
expect($request->provider())->toBe('anthropic');
})
->withPrompt('Who are you?')
->asText();

// Make assertions
expect($response->text)->toBe('Hello, I am Claude!');

$fake->assertRequest(function (array $requests): void {
expect($requests[0]->provider())->toBe('anthropic');
expect($requests[0]->model())->toBe('claude-3-5-sonnet-latest');
});
});

it('does not call success handlers on unsuccessful response', function (): void {

FixtureResponse::fakeResponseSequence('v1/messages', 'anthropic/generate-text-with-a-prompt', [], 404);

$response = Prism::text()
->using(Provider::Anthropic, 'claude-3-5-sonnet-latest')
->onSuccess(function ($request, $response): void {
$this->fail('Reached success function, on failure');
})
->withPrompt('Who are you?')
->asText();

})->throws(\Prism\Prism\Exceptions\PrismException::class);
});