11<?php
2+
23# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
34# SPDX-License-Identifier: Apache-2.0
45
78namespace BedrockRuntime ;
89
910use Aws \BedrockRuntime \BedrockRuntimeClient ;
11+ use AwsUtilities \AWSServiceClass ;
1012use Exception ;
1113
12- class BedrockRuntimeService extends \ AwsUtilities \ AWSServiceClass
14+ class BedrockRuntimeService extends AWSServiceClass
1315{
14- protected BedrockRuntimeClient $ bedrockRuntimeClient ;
15-
16- public function __construct (
17- $ client = null ,
18- $ region = 'us-east-1 ' ,
19- $ version = 'latest ' ,
20- $ profile = 'default '
21- ) {
22- if (gettype ($ client ) == BedrockRuntimeClient::class) {
23- $ this ->bedrockRuntimeClient = $ client ;
24- return ;
25- }
16+ public function __construct ()
17+ {
2618 $ this ->bedrockRuntimeClient = new BedrockRuntimeClient ([
27- 'region ' => $ region ,
28- 'version ' => $ version ,
29- 'profile ' => $ profile ,
19+ 'region ' => 'us-east-1 ' ,
20+ 'profile ' => 'default '
3021 ]);
3122 }
3223
@@ -38,28 +29,22 @@ public function invokeClaude($prompt)
3829 # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
3930
4031 $ completion = "" ;
41-
4232 try {
4333 $ modelId = 'anthropic.claude-v2 ' ;
44-
4534 # Claude requires you to enclose the prompt as follows:
4635 $ prompt = "\n\nHuman: {$ prompt }\n\nAssistant: " ;
47-
4836 $ body = [
4937 'prompt ' => $ prompt ,
5038 'max_tokens_to_sample ' => 200 ,
5139 'temperature ' => 0.5 ,
5240 'stop_sequences ' => ["\n\nHuman: " ],
5341 ];
54-
5542 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
5643 'contentType ' => 'application/json ' ,
5744 'body ' => json_encode ($ body ),
5845 'modelId ' => $ modelId ,
5946 ]);
60-
6147 $ response_body = json_decode ($ result ['body ' ]);
62-
6348 $ completion = $ response_body ->completion ;
6449 } catch (Exception $ e ) {
6550 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
@@ -77,24 +62,19 @@ public function invokeJurassic2($prompt)
7762 # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html
7863
7964 $ completion = "" ;
80-
8165 try {
8266 $ modelId = 'ai21.j2-mid-v1 ' ;
83-
8467 $ body = [
8568 'prompt ' => $ prompt ,
8669 'temperature ' => 0.5 ,
8770 'maxTokens ' => 200 ,
8871 ];
89-
9072 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
9173 'contentType ' => 'application/json ' ,
9274 'body ' => json_encode ($ body ),
9375 'modelId ' => $ modelId ,
9476 ]);
95-
9677 $ response_body = json_decode ($ result ['body ' ]);
97-
9878 $ completion = $ response_body ->completions [0 ]->data ->text ;
9979 } catch (Exception $ e ) {
10080 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
@@ -112,24 +92,19 @@ public function invokeLlama2($prompt)
11292 # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html
11393
11494 $ completion = "" ;
115-
11695 try {
11796 $ modelId = 'meta.llama2-13b-chat-v1 ' ;
118-
11997 $ body = [
12098 'prompt ' => $ prompt ,
12199 'temperature ' => 0.5 ,
122100 'max_gen_len ' => 512 ,
123101 ];
124-
125102 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
126103 'contentType ' => 'application/json ' ,
127104 'body ' => json_encode ($ body ),
128105 'modelId ' => $ modelId ,
129106 ]);
130-
131107 $ response_body = json_decode ($ result ['body ' ]);
132-
133108 $ completion = $ response_body ->generation ;
134109 } catch (Exception $ e ) {
135110 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
@@ -147,10 +122,8 @@ public function invokeStableDiffusion(string $prompt, int $seed, string $style_p
147122 # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html
148123
149124 $ base64_image_data = "" ;
150-
151125 try {
152126 $ modelId = 'stability.stable-diffusion-xl-v1 ' ;
153-
154127 $ body = [
155128 'text_prompts ' => [
156129 ['text ' => $ prompt ]
@@ -159,7 +132,6 @@ public function invokeStableDiffusion(string $prompt, int $seed, string $style_p
159132 'cfg_scale ' => 10 ,
160133 'steps ' => 30
161134 ];
162-
163135 if ($ style_preset ) {
164136 $ body ['style_preset ' ] = $ style_preset ;
165137 }
@@ -169,9 +141,7 @@ public function invokeStableDiffusion(string $prompt, int $seed, string $style_p
169141 'body ' => json_encode ($ body ),
170142 'modelId ' => $ modelId ,
171143 ]);
172-
173144 $ response_body = json_decode ($ result ['body ' ]);
174-
175145 $ base64_image_data = $ response_body ->artifacts [0 ]->base64 ;
176146 } catch (Exception $ e ) {
177147 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
@@ -189,10 +159,8 @@ public function invokeTitanImage(string $prompt, int $seed)
189159 # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-image.html
190160
191161 $ base64_image_data = "" ;
192-
193162 try {
194163 $ modelId = 'amazon.titan-image-generator-v1 ' ;
195-
196164 $ request = json_encode ([
197165 'taskType ' => 'TEXT_IMAGE ' ,
198166 'textToImageParams ' => [
@@ -207,15 +175,12 @@ public function invokeTitanImage(string $prompt, int $seed)
207175 'seed ' => $ seed
208176 ]
209177 ]);
210-
211178 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
212179 'contentType ' => 'application/json ' ,
213180 'body ' => $ request ,
214181 'modelId ' => $ modelId ,
215182 ]);
216-
217183 $ response_body = json_decode ($ result ['body ' ]);
218-
219184 $ base64_image_data = $ response_body ->images [0 ];
220185 } catch (Exception $ e ) {
221186 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
0 commit comments