Skip to content

Commit f27c330

Browse files
Dennis Traubcpyle0819
authored andcommitted
Build clients locally
1 parent 222927c commit f27c330

File tree

12 files changed

+170
-3546
lines changed

12 files changed

+170
-3546
lines changed

php/example_code/bedrock-runtime/BedrockRuntimeService.php

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
34
# SPDX-License-Identifier: Apache-2.0
45

@@ -7,26 +8,16 @@
78
namespace BedrockRuntime;
89

910
use Aws\BedrockRuntime\BedrockRuntimeClient;
11+
use AwsUtilities\AWSServiceClass;
1012
use 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";

php/example_code/bedrock-runtime/GettingStartedWithBedrockRuntime.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
34
# SPDX-License-Identifier: Apache-2.0
45

@@ -11,55 +12,33 @@
1112
# snippet-start:[php.example_code.bedrock-runtime.basics.scenario]
1213
namespace BedrockRuntime;
1314

14-
use Aws\BedrockRuntime\BedrockRuntimeClient;
15-
use BedrockRuntime\BedrockRuntimeService;
16-
17-
1815
class GettingStartedWithBedrockRuntime
1916
{
2017
protected BedrockRuntimeService $bedrockRuntimeService;
21-
2218
public function runExample()
2319
{
2420
echo "\n";
2521
echo "---------------------------------------------------------------------\n";
2622
echo "Welcome to the Amazon Bedrock Runtime getting started demo using PHP!\n";
2723
echo "---------------------------------------------------------------------\n";
28-
29-
$clientArgs = [
30-
'region' => 'us-east-1',
31-
'version' => 'latest',
32-
'profile' => 'default',
33-
];
34-
35-
$bedrockRuntimeService = new BedrockRuntimeService(null, $clientArgs['region'], $clientArgs['version'], $clientArgs['profile']);
36-
24+
$bedrockRuntimeService = new BedrockRuntimeService();
3725
$prompt = 'In one paragraph, who are you?';
38-
3926
echo "\nPrompt: " . $prompt;
40-
4127
echo "\n\nAnthropic Claude:";
4228
echo $bedrockRuntimeService->invokeClaude($prompt);
43-
4429
echo "\n\nAI21 Labs Jurassic-2: ";
4530
echo $bedrockRuntimeService->invokeJurassic2($prompt);
46-
4731
echo "\n\nMeta Llama 2 Chat: ";
4832
echo $bedrockRuntimeService->invokeLlama2($prompt);
49-
5033
echo "\n---------------------------------------------------------------------\n";
51-
5234
$image_prompt = 'stylized picture of a cute old steampunk robot';
53-
5435
echo "\nImage prompt: " . $image_prompt;
55-
5636
echo "\n\nStability.ai Stable Diffusion XL:\n";
5737
$diffusionSeed = rand(0, 4294967295);
5838
$style_preset = 'photographic';
5939
$base64 = $bedrockRuntimeService->invokeStableDiffusion($image_prompt, $diffusionSeed, $style_preset);
6040
$image_path = $this->saveImage($base64, 'stability.stable-diffusion-xl');
6141
echo "The generated images have been saved to $image_path";
62-
6342
echo "\n\nAmazon Titan Image Generation:\n";
6443
$titanSeed = rand(0, 2147483647);
6544
$base64 = $bedrockRuntimeService->invokeTitanImage($image_prompt, $titanSeed);
@@ -70,7 +49,6 @@ public function runExample()
7049
private function saveImage($base64_image_data, $model_id): string
7150
{
7251
$output_dir = "output";
73-
7452
if (!file_exists($output_dir)) {
7553
mkdir($output_dir);
7654
}
@@ -81,13 +59,10 @@ private function saveImage($base64_image_data, $model_id): string
8159
}
8260

8361
$image_data = base64_decode($base64_image_data);
84-
8562
$file_path = "$output_dir/$model_id" . '_' . "$i.png";
86-
8763
$file = fopen($file_path, 'wb');
8864
fwrite($file, $image_data);
8965
fclose($file);
90-
9166
return $file_path;
9267
}
9368
}

php/example_code/bedrock-runtime/Runner.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
2+
23
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
34
// SPDX-License-Identifier: Apache-2.0
45

56
use BedrockRuntime\GettingStartedWithBedrockRuntime;
67

78
include __DIR__ . '/vendor/autoload.php';
8-
99
include 'GettingStartedWithBedrockRuntime.php';
10-
1110
try {
1211
$runner = new GettingStartedWithBedrockRuntime();
1312
$runner->runExample();

php/example_code/bedrock-runtime/composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@
1111
"BedrockRuntime\\": "../bedrock-runtime/",
1212
"AwsUtilities\\": "../aws_utilities/"
1313
}
14-
},
15-
"require-dev": {
16-
"phpunit/phpunit": "^11.3"
1714
}
1815
}

0 commit comments

Comments
 (0)