11<?php
2- # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3- # SPDX-License-Identifier: Apache-2.0
4-
5- #snippet-start:[php.example_code.bedrock-runtime.service]
2+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ // SPDX-License-Identifier: Apache-2.0
64
5+ // snippet-start:[php.example_code.bedrock-runtime.service]
76namespace BedrockRuntime ;
87
98use Aws \BedrockRuntime \BedrockRuntimeClient ;
9+ use AwsUtilities \AWSServiceClass ;
1010use Exception ;
1111
12- class BedrockRuntimeService extends \ AwsUtilities \ AWSServiceClass
12+ class BedrockRuntimeService extends AWSServiceClass
1313{
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) {
14+ public function __construct (BedrockRuntimeClient $ client = null )
15+ {
16+ if ($ client ) {
2317 $ this ->bedrockRuntimeClient = $ client ;
24- return ;
18+ } else {
19+ $ this ->bedrockRuntimeClient = new BedrockRuntimeClient ([
20+ 'region ' => 'us-east-1 ' ,
21+ 'profile ' => 'default '
22+ ]);
2523 }
26- $ this ->bedrockRuntimeClient = new BedrockRuntimeClient ([
27- 'region ' => $ region ,
28- 'version ' => $ version ,
29- 'profile ' => $ profile ,
30- ]);
3124 }
3225
33- #snippet-start:[php.example_code.bedrock-runtime.service.invokeClaude]
26+ public function getClient (): BedrockRuntimeClient
27+ {
28+ return $ this ->bedrockRuntimeClient ;
29+ }
30+
31+ // snippet-start:[php.example_code.bedrock-runtime.service.invokeClaude]
3432 public function invokeClaude ($ prompt )
3533 {
36- # The different model providers have individual request and response formats.
37- # For the format, ranges, and default values for Anthropic Claude, refer to:
38- # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
34+ // The different model providers have individual request and response formats.
35+ // For the format, ranges, and default values for Anthropic Claude, refer to:
36+ // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
3937
4038 $ completion = "" ;
41-
4239 try {
4340 $ modelId = 'anthropic.claude-v2 ' ;
44-
45- # Claude requires you to enclose the prompt as follows:
41+ // Claude requires you to enclose the prompt as follows:
4642 $ prompt = "\n\nHuman: {$ prompt }\n\nAssistant: " ;
47-
4843 $ body = [
4944 'prompt ' => $ prompt ,
5045 'max_tokens_to_sample ' => 200 ,
5146 'temperature ' => 0.5 ,
5247 'stop_sequences ' => ["\n\nHuman: " ],
5348 ];
54-
5549 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
5650 'contentType ' => 'application/json ' ,
5751 'body ' => json_encode ($ body ),
5852 'modelId ' => $ modelId ,
5953 ]);
60-
6154 $ response_body = json_decode ($ result ['body ' ]);
62-
6355 $ completion = $ response_body ->completion ;
6456 } catch (Exception $ e ) {
6557 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
6658 }
6759
6860 return $ completion ;
6961 }
70- # snippet-end:[php.example_code.bedrock-runtime.service.invokeClaude]
62+ // snippet-end:[php.example_code.bedrock-runtime.service.invokeClaude]
7163
72- # snippet-start:[php.example_code.bedrock-runtime.service.invokeJurassic2]
64+ // snippet-start:[php.example_code.bedrock-runtime.service.invokeJurassic2]
7365 public function invokeJurassic2 ($ prompt )
7466 {
7567 # The different model providers have individual request and response formats.
7668 # For the format, ranges, and default values for AI21 Labs Jurassic-2, refer to:
7769 # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html
7870
7971 $ completion = "" ;
80-
8172 try {
8273 $ modelId = 'ai21.j2-mid-v1 ' ;
83-
8474 $ body = [
8575 'prompt ' => $ prompt ,
8676 'temperature ' => 0.5 ,
8777 'maxTokens ' => 200 ,
8878 ];
89-
9079 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
9180 'contentType ' => 'application/json ' ,
9281 'body ' => json_encode ($ body ),
9382 'modelId ' => $ modelId ,
9483 ]);
95-
9684 $ response_body = json_decode ($ result ['body ' ]);
97-
9885 $ completion = $ response_body ->completions [0 ]->data ->text ;
9986 } catch (Exception $ e ) {
10087 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
10188 }
10289
10390 return $ completion ;
10491 }
105- # snippet-end:[php.example_code.bedrock-runtime.service.invokeJurassic2]
92+ // snippet-end:[php.example_code.bedrock-runtime.service.invokeJurassic2]
10693
107- # snippet-start:[php.example_code.bedrock-runtime.service.invokeLlama2]
94+ // snippet-start:[php.example_code.bedrock-runtime.service.invokeLlama2]
10895 public function invokeLlama2 ($ prompt )
10996 {
110- # The different model providers have individual request and response formats.
111- # For the format, ranges, and default values for Meta Llama 2 Chat, refer to:
112- # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html
97+ // The different model providers have individual request and response formats.
98+ // For the format, ranges, and default values for Meta Llama 2 Chat, refer to:
99+ // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html
113100
114101 $ completion = "" ;
115-
116102 try {
117103 $ modelId = 'meta.llama2-13b-chat-v1 ' ;
118-
119104 $ body = [
120105 'prompt ' => $ prompt ,
121106 'temperature ' => 0.5 ,
122107 'max_gen_len ' => 512 ,
123108 ];
124-
125109 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
126110 'contentType ' => 'application/json ' ,
127111 'body ' => json_encode ($ body ),
128112 'modelId ' => $ modelId ,
129113 ]);
130-
131114 $ response_body = json_decode ($ result ['body ' ]);
132-
133115 $ completion = $ response_body ->generation ;
134116 } catch (Exception $ e ) {
135117 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
136118 }
137119
138120 return $ completion ;
139121 }
140- # snippet-end:[php.example_code.bedrock-runtime.service.invokeLlama2]
122+ // snippet-end:[php.example_code.bedrock-runtime.service.invokeLlama2]
141123
142- # snippet-start:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
124+ // snippet-start:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
143125 public function invokeStableDiffusion (string $ prompt , int $ seed , string $ style_preset )
144126 {
145- # The different model providers have individual request and response formats.
146- # For the format, ranges, and available style_presets of Stable Diffusion models refer to:
147- # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html
127+ // The different model providers have individual request and response formats.
128+ // For the format, ranges, and available style_presets of Stable Diffusion models refer to:
129+ // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html
148130
149131 $ base64_image_data = "" ;
150-
151132 try {
152- $ modelId = 'stability.stable-diffusion-xl ' ;
153-
133+ $ modelId = 'stability.stable-diffusion-xl-v1 ' ;
154134 $ body = [
155135 'text_prompts ' => [
156136 ['text ' => $ prompt ]
@@ -159,7 +139,6 @@ public function invokeStableDiffusion(string $prompt, int $seed, string $style_p
159139 'cfg_scale ' => 10 ,
160140 'steps ' => 30
161141 ];
162-
163142 if ($ style_preset ) {
164143 $ body ['style_preset ' ] = $ style_preset ;
165144 }
@@ -169,30 +148,26 @@ public function invokeStableDiffusion(string $prompt, int $seed, string $style_p
169148 'body ' => json_encode ($ body ),
170149 'modelId ' => $ modelId ,
171150 ]);
172-
173151 $ response_body = json_decode ($ result ['body ' ]);
174-
175152 $ base64_image_data = $ response_body ->artifacts [0 ]->base64 ;
176153 } catch (Exception $ e ) {
177154 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
178155 }
179156
180157 return $ base64_image_data ;
181158 }
182- # snippet-end:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
159+ // snippet-end:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
183160
184- # snippet-start:[php.example_code.bedrock-runtime.service.invokeTitanImage]
161+ // snippet-start:[php.example_code.bedrock-runtime.service.invokeTitanImage]
185162 public function invokeTitanImage (string $ prompt , int $ seed )
186163 {
187- # The different model providers have individual request and response formats.
188- # For the format, ranges, and default values for Titan Image models refer to:
189- # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-image.html
164+ // The different model providers have individual request and response formats.
165+ // For the format, ranges, and default values for Titan Image models refer to:
166+ // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-image.html
190167
191168 $ base64_image_data = "" ;
192-
193169 try {
194170 $ modelId = 'amazon.titan-image-generator-v1 ' ;
195-
196171 $ request = json_encode ([
197172 'taskType ' => 'TEXT_IMAGE ' ,
198173 'textToImageParams ' => [
@@ -207,23 +182,19 @@ public function invokeTitanImage(string $prompt, int $seed)
207182 'seed ' => $ seed
208183 ]
209184 ]);
210-
211185 $ result = $ this ->bedrockRuntimeClient ->invokeModel ([
212186 'contentType ' => 'application/json ' ,
213187 'body ' => $ request ,
214188 'modelId ' => $ modelId ,
215189 ]);
216-
217190 $ response_body = json_decode ($ result ['body ' ]);
218-
219191 $ base64_image_data = $ response_body ->images [0 ];
220192 } catch (Exception $ e ) {
221193 echo "Error: ( {$ e ->getCode ()}) - {$ e ->getMessage ()}\n" ;
222194 }
223195
224196 return $ base64_image_data ;
225197 }
226- # snippet-end:[php.example_code.bedrock-runtime.service.invokeTitanImage]
198+ // snippet-end:[php.example_code.bedrock-runtime.service.invokeTitanImage]
227199}
228-
229- #snippet-end:[php.example_code.bedrock-runtime.service]
200+ // snippet-end:[php.example_code.bedrock-runtime.service]
0 commit comments