Skip to content

PHP: Add example for Amazon Nova text models, remove example for Jurassic-2 #7445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ bedrock-runtime_Converse_AmazonNovaText:
genai: some
snippet_tags:
- Bedrock.ConverseTool.dotnetv3.SendConverseRequest
PHP:
versions:
- sdk_version: 3
github: php/example_code/bedrock-runtime
excerpts:
- description: Send a text message to Amazon Nova, using Bedrock's Converse API.
snippet_tags:
- php.example_code.bedrock-runtime.service.Converse_AmazonNovaText
Python:
versions:
- sdk_version: 3
Expand Down Expand Up @@ -792,14 +800,6 @@ bedrock-runtime_InvokeModel_Ai21LabsJurassic2:
snippet_tags:
- gov2.bedrock-runtime.InvokeModelWrapper.struct
- gov2.bedrock-runtime.InvokeJurassic2
PHP:
versions:
- sdk_version: 3
github: php/example_code/bedrock-runtime
excerpts:
- description: Use the Invoke Model API to send a text message.
snippet_tags:
- php.example_code.bedrock-runtime.service.invokeJurassic2
services:
bedrock-runtime: {InvokeModel}

Expand Down
30 changes: 0 additions & 30 deletions php/example_code/bedrock-runtime/BedrockRuntimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,6 @@ public function invokeClaude($prompt)
}
// snippet-end:[php.example_code.bedrock-runtime.service.invokeClaude]

// snippet-start:[php.example_code.bedrock-runtime.service.invokeJurassic2]
public function invokeJurassic2($prompt)
{
# The different model providers have individual request and response formats.
# For the format, ranges, and default values for AI21 Labs Jurassic-2, refer to:
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html

$completion = "";
try {
$modelId = 'ai21.j2-mid-v1';
$body = [
'prompt' => $prompt,
'temperature' => 0.5,
'maxTokens' => 200,
];
$result = $this->bedrockRuntimeClient->invokeModel([
'contentType' => 'application/json',
'body' => json_encode($body),
'modelId' => $modelId,
]);
$response_body = json_decode($result['body']);
$completion = $response_body->completions[0]->data->text;
} catch (Exception $e) {
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
}

return $completion;
}
// snippet-end:[php.example_code.bedrock-runtime.service.invokeJurassic2]

// snippet-start:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
public function invokeStableDiffusion(string $prompt, int $seed, string $style_preset)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public function runExample()
echo "\nPrompt: " . $prompt;
echo "\n\nAnthropic Claude:\n";
echo $bedrockRuntimeService->invokeClaude($prompt);
echo "\n\nAI21 Labs Jurassic-2:\n";
echo $bedrockRuntimeService->invokeJurassic2($prompt);
echo "\n---------------------------------------------------------------------\n";
$image_prompt = 'stylized picture of a cute old steampunk robot';
echo "\nImage prompt: " . $image_prompt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace BedrockRuntime\Models\AmazonNova\Text;

require_once __DIR__ . '/../../../vendor/autoload.php';

// snippet-start:[php.example_code.bedrock-runtime.service.Converse_AmazonNovaText]
// Use the Conversation API to send a text message to Amazon Nova.

use Aws\BedrockRuntime\BedrockRuntimeClient;
use Aws\Exception\AwsException;
use RuntimeException;

class Converse
{
public function converse(): string
{
// Create a Bedrock Runtime client in the AWS Region you want to use.
$client = new BedrockRuntimeClient([
'region' => 'us-east-1',
'profile' => 'default'
]);

// Set the model ID, e.g., Amazon Nova Lite.
$modelId = 'amazon.nova-lite-v1:0';

// Start a conversation with the user message.
$userMessage = "Describe the purpose of a 'hello world' program in one line.";
$conversation = [
[
"role" => "user",
"content" => [["text" => $userMessage]]
]
];

try {
// Send the message to the model, using a basic inference configuration.
$response = $client->converse([
'modelId' => $modelId,
'messages' => $conversation,
'inferenceConfig' => [
'maxTokens' => 512,
'temperature' => 0.5
]
]);

// Extract and return the response text.
$responseText = $response['output']['message']['content'][0]['text'];
return $responseText;
} catch (AwsException $e) {
echo "ERROR: Can't invoke {$modelId}. Reason: {$e->getAwsErrorMessage()}";
throw new RuntimeException("Failed to invoke model: " . $e->getAwsErrorMessage(), 0, $e);
}
}
}

$demo = new Converse();
echo $demo->converse();

// snippet-end:[php.example_code.bedrock-runtime.service.Converse_AmazonNovaText]
8 changes: 4 additions & 4 deletions php/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ functions within the same service.

- [Invoke multiple foundation models on Amazon Bedrock](GettingStartedWithBedrockRuntime.php)

### AI21 Labs Jurassic-2
### Amazon Nova

- [InvokeModel](BedrockRuntimeService.php#L66)
- [Converse](Models/AmazonNova/Text/Converse.php#L9)

### Amazon Titan Image Generator

- [InvokeModel](BedrockRuntimeService.php#L133)
- [InvokeModel](BedrockRuntimeService.php#L103)

### Anthropic Claude

- [InvokeModel](BedrockRuntimeService.php#L31)

### Stable Diffusion

- [InvokeModel](BedrockRuntimeService.php#L96)
- [InvokeModel](BedrockRuntimeService.php#L66)


<!--custom.examples.start-->
Expand Down
5 changes: 4 additions & 1 deletion php/example_code/bedrock-runtime/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"aws/aws-sdk-php": "^3.336",
"aws/aws-sdk-php": "^3.343",
"guzzlehttp/guzzle": "^7.9.2"
},
"autoload": {
Expand All @@ -11,5 +11,8 @@
"BedrockRuntime\\": "../bedrock-runtime/",
"AwsUtilities\\": "../aws_utilities/"
}
},
"scripts": {
"Models/AmazonNova/Text/Converse": "php Models/AmazonNova/Text/Converse.php"
}
}
Loading
Loading