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
2 changes: 1 addition & 1 deletion src/Bedrock.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function schema(PrismRequest $request): BedrockSchema
{
$override = $request->providerOptions();

$override = data_get($override, 'apiSchema', null);
$override = data_get($override, 'apiSchema');

return $override ?? BedrockSchema::fromModelString($request->model());
}
Expand Down
2 changes: 1 addition & 1 deletion src/BedrockServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function getCredentials(array $config): Credentials
protected function registerWithPrism(): void
{
$this->app->extend(PrismManager::class, function (PrismManager $prismManager): \Prism\Prism\PrismManager {
$prismManager->extend(Bedrock::KEY, fn ($app, $config): Bedrock => new Bedrock(
$prismManager->extend(Bedrock::KEY, fn ($app, array $config): Bedrock => new Bedrock(
credentials: BedrockServiceProvider::getCredentials($config),
region: $config['region']
));
Expand Down
4 changes: 2 additions & 2 deletions src/Schemas/Anthropic/AnthropicStructuredHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ protected function prepareTempResponse(): void
usage: new Usage(
promptTokens: data_get($data, 'usage.input_tokens'),
completionTokens: data_get($data, 'usage.output_tokens'),
cacheWriteInputTokens: data_get($data, 'usage.cache_creation_input_tokens', null),
cacheReadInputTokens: data_get($data, 'usage.cache_read_input_tokens', null)
cacheWriteInputTokens: data_get($data, 'usage.cache_creation_input_tokens'),
cacheReadInputTokens: data_get($data, 'usage.cache_read_input_tokens')
),
meta: new Meta(
id: data_get($data, 'id'),
Expand Down
1 change: 1 addition & 0 deletions src/Schemas/Anthropic/AnthropicTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ protected function addStep(Request $request, array $toolResults = []): void
finishReason: $this->tempResponse->finishReason,
toolCalls: $this->tempResponse->toolCalls,
toolResults: $toolResults,
providerToolCalls: [],
usage: $this->tempResponse->usage,
meta: $this->tempResponse->meta,
messages: $request->messages(),
Expand Down
4 changes: 3 additions & 1 deletion src/Schemas/Anthropic/Concerns/ExtractsToolCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ protected function extractToolCalls(array $data): array
{
$toolCalls = array_map(function ($content): ?ToolCall {
if (data_get($content, 'type') === 'tool_use') {
$input = data_get($content, 'input');

return new ToolCall(
id: data_get($content, 'id'),
name: data_get($content, 'name'),
arguments: data_get($content, 'input')
arguments: is_string($input) ? (json_decode($input, true) ?? []) : ($input ?? [])
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Schemas/Anthropic/Maps/MessageMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function map(array $messages): array
}

return array_map(
fn (Message $message): array => self::mapMessage($message),
self::mapMessage(...),
$messages
);
}
Expand All @@ -40,7 +40,7 @@ public static function map(array $messages): array
public static function mapSystemMessages(array $messages): array
{
return array_map(
fn (Message $message): array => self::mapSystemMessage($message),
self::mapSystemMessage(...),
$messages
);
}
Expand All @@ -65,7 +65,7 @@ protected static function mapSystemMessage(SystemMessage $systemMessage): array
{
$providerOptions = $systemMessage->providerOptions();

$cacheType = data_get($providerOptions, 'cacheType', null);
$cacheType = data_get($providerOptions, 'cacheType');

return array_filter([
'type' => 'text',
Expand Down Expand Up @@ -96,7 +96,7 @@ protected static function mapUserMessage(UserMessage $message): array
{
$providerOptions = $message->providerOptions();

$cacheType = data_get($providerOptions, 'cacheType', null);
$cacheType = data_get($providerOptions, 'cacheType');
$cache_control = $cacheType ? ['type' => $cacheType instanceof BackedEnum ? $cacheType->value : $cacheType] : null;

if ($message->documents() !== []) {
Expand All @@ -123,7 +123,7 @@ protected static function mapAssistantMessage(AssistantMessage $message): array
{
$providerOptions = $message->providerOptions();

$cacheType = data_get($providerOptions, 'cacheType', null);
$cacheType = data_get($providerOptions, 'cacheType');

$content = [];

Expand All @@ -150,7 +150,7 @@ protected static function mapAssistantMessage(AssistantMessage $message): array
'type' => 'tool_use',
'id' => $toolCall->id,
'name' => $toolCall->name,
'input' => $toolCall->arguments(),
'input' => $toolCall->arguments() === [] ? new \stdClass : $toolCall->arguments(),
], $message->toolCalls)
: [];

Expand Down
4 changes: 2 additions & 2 deletions src/Schemas/Anthropic/Maps/ToolMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class ToolMap
public static function map(array $tools): array
{
return array_map(function (PrismTool $tool): array {
$cacheType = data_get($tool->providerOptions(), 'cacheType', null);
$cacheType = data_get($tool->providerOptions(), 'cacheType');

return array_filter([
'name' => $tool->name(),
'description' => $tool->description(),
'input_schema' => [
'type' => 'object',
'properties' => $tool->parametersAsArray(),
'properties' => $tool->parametersAsArray() ?: (object) [],
'required' => $tool->requiredParameters(),
],
'cache_control' => $cacheType
Expand Down
2 changes: 1 addition & 1 deletion src/Schemas/Cohere/CohereEmbeddingsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function buildResponse(): EmbeddingsResponse
$body = $this->httpResponse->json();

return new EmbeddingsResponse(
embeddings: array_map(fn (array $item): Embedding => Embedding::fromArray($item), data_get($body, 'embeddings', [])),
embeddings: array_map(Embedding::fromArray(...), data_get($body, 'embeddings', [])),
usage: new EmbeddingsUsage(
tokens: (int) $this->httpResponse->header('X-Amzn-Bedrock-Input-Token-Count')
),
Expand Down
4 changes: 3 additions & 1 deletion src/Schemas/Converse/Concerns/ExtractsToolCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ protected function extractToolCalls(array $data): array
return;
}

$input = data_get($use, 'input');

return new ToolCall(
id: data_get($use, 'toolUseId'),
name: data_get($use, 'name'),
arguments: data_get($use, 'input')
arguments: is_string($input) ? (json_decode($input, true) ?? []) : ($input ?? [])
);

}, data_get($data, 'output.message.content', []));
Expand Down
1 change: 1 addition & 0 deletions src/Schemas/Converse/ConverseTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ protected function addStep(Request $request, array $toolResults = []): void
finishReason: $this->tempResponse->finishReason,
toolCalls: $this->tempResponse->toolCalls,
toolResults: $toolResults,
providerToolCalls: [],
usage: $this->tempResponse->usage,
meta: $this->tempResponse->meta,
messages: $request->messages(),
Expand Down
10 changes: 5 additions & 5 deletions src/Schemas/Converse/Maps/MessageMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function map(array $messages): array
}

return array_map(
fn (Message $message): array => self::mapMessage($message),
self::mapMessage(...),
$messages
);
}
Expand All @@ -44,7 +44,7 @@ public static function mapSystemMessages(array $systemPrompts): array
foreach ($systemPrompts as $prompt) {
$output[] = self::mapSystemMessage($prompt);

$cacheType = data_get($prompt->providerOptions(), 'cacheType', null);
$cacheType = data_get($prompt->providerOptions(), 'cacheType');

if ($cacheType) {
$output[] = ['cachePoint' => ['type' => $cacheType]];
Expand Down Expand Up @@ -102,7 +102,7 @@ protected static function mapToolResultMessage(ToolResultMessage $message): arra
*/
protected static function mapUserMessage(UserMessage $message): array
{
$cacheType = data_get($message->providerOptions(), 'cacheType', null);
$cacheType = data_get($message->providerOptions(), 'cacheType');

return [
'role' => 'user',
Expand All @@ -120,7 +120,7 @@ protected static function mapUserMessage(UserMessage $message): array
*/
protected static function mapAssistantMessage(AssistantMessage $message): array
{
$cacheType = data_get($message->providerOptions(), 'cacheType', null);
$cacheType = data_get($message->providerOptions(), 'cacheType');

return [
'role' => 'assistant',
Expand All @@ -142,7 +142,7 @@ protected static function mapToolCalls(array $parts): array
'toolUse' => [
'toolUseId' => $toolCall->id,
'name' => $toolCall->name,
'input' => $toolCall->arguments(),
'input' => $toolCall->arguments() === [] ? new \stdClass : $toolCall->arguments(),
],
], $parts);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/BedrockServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Prism\Prism\Prism;

it('registers itself as a provider with prism', function (): void {
$pendingRequest = Prism::text()->using('bedrock', 'test-model');
$pendingRequest = (new Prism())->text()->using('bedrock', 'test-model');

expect($pendingRequest->provider())->toBeInstanceOf(Bedrock::class);
});
Expand All @@ -16,7 +16,7 @@
Http::fake();
Http::preventStrayRequests();

Prism::embeddings()
(new Prism())->embeddings()
->using('bedrock', 'test-model')
->withProviderOptions(['apiSchema' => BedrockSchema::Anthropic])
->fromInput('Hello world')
Expand All @@ -27,7 +27,7 @@
Http::fake();
Http::preventStrayRequests();

Prism::embeddings()
(new Prism())->embeddings()
->using('bedrock', 'test-model')
->fromInput('Hello world')
->asEmbeddings();
Expand Down
8 changes: 4 additions & 4 deletions tests/Schemas/Anthropic/AnthropicStructuredHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
['weather', 'game_time', 'coat_required']
);

$response = Prism::structured()
$response = (new Prism())->structured()
->withSchema($schema)
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withSystemPrompt('The tigers game is at 3pm and the temperature will be 70º')
Expand Down Expand Up @@ -60,7 +60,7 @@

$customMessage = 'Please return a JSON response using this custom format instruction';

Prism::structured()
(new Prism())->structured()
->withSchema($schema)
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withProviderOptions([
Expand Down Expand Up @@ -95,7 +95,7 @@

$defaultMessage = 'Respond with ONLY JSON (i.e. not in backticks or a code block, with NO CONTENT outside the JSON) that matches the following schema:';

Prism::structured()
(new Prism())->structured()
->withSchema($schema)
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withSystemPrompt('The tigers game is at 3pm and the temperature will be 70º')
Expand Down Expand Up @@ -125,7 +125,7 @@
['weather', 'game_time', 'coat_required']
);

Prism::structured()
(new Prism())->structured()
->withSchema($schema)
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withProviderOptions([
Expand Down
18 changes: 9 additions & 9 deletions tests/Schemas/Anthropic/AnthropicTextHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
it('can generate text with a prompt', function (): void {
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/generate-text-with-a-prompt');

$response = Prism::text()
$response = (new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withPrompt('Who are you?')
->asText();
Expand All @@ -33,7 +33,7 @@
it('can generate text with a system prompt', function (): void {
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/generate-text-with-system-prompt');

$response = Prism::text()
$response = (new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withSystemPrompt('MODEL ADOPTS ROLE of [PERSONA: Nyx the Cthulhu]!')
->withPrompt('Who are you?')
Expand Down Expand Up @@ -62,7 +62,7 @@
->using(fn (string $query): string => 'The tigers game is at 3pm in detroit'),
];

$response = Prism::text()
$response = (new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withTools($tools)
->withMaxSteps(3)
Expand Down Expand Up @@ -102,7 +102,7 @@
it('can send images from file', function (): void {
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/generate-text-with-image');

Prism::text()
(new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withMessages([
new UserMessage(
Expand Down Expand Up @@ -146,7 +146,7 @@
->using(fn (string $query): string => 'The tigers game is at 3pm in detroit'),
];

$response = Prism::text()
$response = (new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withPrompt('Do something')
->withTools($tools)
Expand All @@ -159,7 +159,7 @@
it('can calculate cache usage correctly', function (): void {
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/calculate-cache-usage');

$response = Prism::text()
$response = (new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withSystemPrompt(new SystemMessage('Old context'))->withProviderOptions(['cacheType' => 'ephemeral'])
->withMessages([
Expand All @@ -174,7 +174,7 @@
it('does not enable prompt caching if the enableCaching provider meta is not set on the request', function (): void {
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/generate-text-with-a-prompt');

Prism::text()
(new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withPrompt('Who are you?')
->asText();
Expand All @@ -185,7 +185,7 @@
it('enables prompt caching if the enableCaching provider meta is set on the request', function (): void {
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/generate-text-with-a-prompt');

Prism::text()
(new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withProviderOptions(['enableCaching' => true])
->withPrompt('Who are you?')
Expand All @@ -197,7 +197,7 @@
it('does not remove 0 values from payloads', function (): void {
FixtureResponse::fakeResponseSequence('invoke', 'anthropic/generate-text-with-a-prompt');

Prism::text()
(new Prism())->text()
->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
->withPrompt('Who are you?')
->usingTemperature(0)
Expand Down
Loading
Loading