Skip to content

Commit 7bf1872

Browse files
authored
PHP: Bedrock constructor argument corrections. (#6824)
1 parent a975bb9 commit 7bf1872

File tree

13 files changed

+257
-335
lines changed

13 files changed

+257
-335
lines changed
Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,136 @@
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]
76
namespace BedrockRuntime;
87

98
use Aws\BedrockRuntime\BedrockRuntimeClient;
9+
use AwsUtilities\AWSServiceClass;
1010
use 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]
Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,43 @@
11
<?php
2-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
# SPDX-License-Identifier: Apache-2.0
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
44

55
/**
66
* Purpose
77
* Shows how to use the AWS SDK for PHP with the Amazon Bedrock Runtime to:
88
* - ...
99
**/
1010

11-
# snippet-start:[php.example_code.bedrock-runtime.basics.scenario]
11+
// snippet-start:[php.example_code.bedrock-runtime.basics.scenario]
1212
namespace BedrockRuntime;
1313

1414
class GettingStartedWithBedrockRuntime
1515
{
1616
protected BedrockRuntimeService $bedrockRuntimeService;
17-
1817
public function runExample()
1918
{
2019
echo "\n";
2120
echo "---------------------------------------------------------------------\n";
2221
echo "Welcome to the Amazon Bedrock Runtime getting started demo using PHP!\n";
2322
echo "---------------------------------------------------------------------\n";
24-
25-
$clientArgs = [
26-
'region' => 'us-east-1',
27-
'version' => 'latest',
28-
'profile' => 'default',
29-
];
30-
31-
$bedrockRuntimeService = new BedrockRuntimeService($clientArgs);
32-
23+
$bedrockRuntimeService = new BedrockRuntimeService();
3324
$prompt = 'In one paragraph, who are you?';
34-
3525
echo "\nPrompt: " . $prompt;
36-
3726
echo "\n\nAnthropic Claude:";
3827
echo $bedrockRuntimeService->invokeClaude($prompt);
39-
4028
echo "\n\nAI21 Labs Jurassic-2: ";
4129
echo $bedrockRuntimeService->invokeJurassic2($prompt);
42-
4330
echo "\n\nMeta Llama 2 Chat: ";
4431
echo $bedrockRuntimeService->invokeLlama2($prompt);
45-
4632
echo "\n---------------------------------------------------------------------\n";
47-
4833
$image_prompt = 'stylized picture of a cute old steampunk robot';
49-
5034
echo "\nImage prompt: " . $image_prompt;
51-
5235
echo "\n\nStability.ai Stable Diffusion XL:\n";
5336
$diffusionSeed = rand(0, 4294967295);
5437
$style_preset = 'photographic';
5538
$base64 = $bedrockRuntimeService->invokeStableDiffusion($image_prompt, $diffusionSeed, $style_preset);
5639
$image_path = $this->saveImage($base64, 'stability.stable-diffusion-xl');
5740
echo "The generated images have been saved to $image_path";
58-
5941
echo "\n\nAmazon Titan Image Generation:\n";
6042
$titanSeed = rand(0, 2147483647);
6143
$base64 = $bedrockRuntimeService->invokeTitanImage($image_prompt, $titanSeed);
@@ -66,7 +48,6 @@ public function runExample()
6648
private function saveImage($base64_image_data, $model_id): string
6749
{
6850
$output_dir = "output";
69-
7051
if (!file_exists($output_dir)) {
7152
mkdir($output_dir);
7253
}
@@ -77,15 +58,11 @@ private function saveImage($base64_image_data, $model_id): string
7758
}
7859

7960
$image_data = base64_decode($base64_image_data);
80-
8161
$file_path = "$output_dir/$model_id" . '_' . "$i.png";
82-
8362
$file = fopen($file_path, 'wb');
8463
fwrite($file, $image_data);
8564
fclose($file);
86-
8765
return $file_path;
8866
}
8967
}
90-
91-
# snippet-end:[php.example_code.bedrock-runtime.basics.scenario]
68+
// snippet-end:[php.example_code.bedrock-runtime.basics.scenario]

php/example_code/bedrock-runtime/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ functions within the same service.
4444

4545
### AI21 Labs Jurassic-2
4646

47-
- [InvokeModel](BedrockRuntimeService.php#L72)
47+
- [InvokeModel](BedrockRuntimeService.php#L64)
4848

4949
### Amazon Titan Image Generator
5050

51-
- [InvokeModel](BedrockRuntimeService.php#L184)
51+
- [InvokeModel](BedrockRuntimeService.php#L161)
5252

5353
### Anthropic Claude
5454

55-
- [InvokeModel](BedrockRuntimeService.php#L33)
55+
- [InvokeModel](BedrockRuntimeService.php#L31)
5656

5757
### Meta Llama
5858

59-
- [InvokeModel: Llama 2](BedrockRuntimeService.php#L107)
59+
- [InvokeModel: Llama 2](BedrockRuntimeService.php#L94)
6060

6161
### Stable Diffusion
6262

63-
- [InvokeModel](BedrockRuntimeService.php#L142)
63+
- [InvokeModel](BedrockRuntimeService.php#L124)
6464

6565

6666
<!--custom.examples.start-->

php/example_code/bedrock-runtime/Runner.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use BedrockRuntime\GettingStartedWithBedrockRuntime;
66

77
include __DIR__ . '/vendor/autoload.php';
8-
98
include 'GettingStartedWithBedrockRuntime.php';
10-
119
try {
1210
$runner = new GettingStartedWithBedrockRuntime();
1311
$runner->runExample();

0 commit comments

Comments
 (0)