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]
0 commit comments