Skip to content

Commit 913ea63

Browse files
DennisTraubbrmur
authored andcommitted
Add Nova Text PHP example
1 parent 3debe59 commit 913ea63

File tree

4 files changed

+1896
-62
lines changed

4 files changed

+1896
-62
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
namespace BedrockRuntime\Models\AmazonNova\Text;
6+
require_once __DIR__ . '/../../../vendor/autoload.php';
7+
8+
// snippet-start:[php.example_code.bedrock-runtime.service.Converse_AmazonNovaText]
9+
// Use the Conversation API to send a text message to Amazon Nova.
10+
11+
use Aws\BedrockRuntime\BedrockRuntimeClient;
12+
use Aws\Exception\AwsException;
13+
use RuntimeException;
14+
15+
class Converse
16+
{
17+
public function converse(): string
18+
{
19+
// Create a Bedrock Runtime client in the AWS Region you want to use.
20+
$client = new BedrockRuntimeClient([
21+
'region' => 'us-east-1',
22+
'profile' => 'default'
23+
]);
24+
25+
// Set the model ID, e.g., Amazon Nova Lite.
26+
$modelId = 'amazon.nova-lite-v1:0';
27+
28+
// Start a conversation with the user message.
29+
$userMessage = "Describe the purpose of a 'hello world' program in one line.";
30+
$conversation = [
31+
[
32+
"role" => "user",
33+
"content" => [["text" => $userMessage]]
34+
]
35+
];
36+
37+
try {
38+
// Send the message to the model, using a basic inference configuration.
39+
$response = $client->converse([
40+
'modelId' => $modelId,
41+
'messages' => $conversation,
42+
'inferenceConfig' => [
43+
'maxTokens' => 512,
44+
'temperature' => 0.5
45+
]
46+
]);
47+
48+
// Extract and return the response text.
49+
$responseText = $response['output']['message']['content'][0]['text'];
50+
return $responseText;
51+
52+
} catch (AwsException $e) {
53+
echo "ERROR: Can't invoke {$modelId}. Reason: {$e->getAwsErrorMessage()}";
54+
throw new RuntimeException("Failed to invoke model: " . $e->getAwsErrorMessage(), 0, $e);
55+
}
56+
}
57+
}
58+
59+
$demo = new Converse();
60+
echo $demo->converse();
61+
62+
// snippet-end:[php.example_code.bedrock-runtime.service.Converse_AmazonNovaText]

php/example_code/bedrock-runtime/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"aws/aws-sdk-php": "^3.336",
3+
"aws/aws-sdk-php": "^3.343",
44
"guzzlehttp/guzzle": "^7.9.2"
55
},
66
"autoload": {
@@ -11,5 +11,8 @@
1111
"BedrockRuntime\\": "../bedrock-runtime/",
1212
"AwsUtilities\\": "../aws_utilities/"
1313
}
14+
},
15+
"scripts": {
16+
"Models/AmazonNova/Text/Converse": "php Models/AmazonNova/Text/Converse.php"
1417
}
1518
}

0 commit comments

Comments
 (0)