Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP: Bedrock constructor argument corrections. #6824

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 43 additions & 72 deletions php/example_code/bedrock-runtime/BedrockRuntimeService.php
Original file line number Diff line number Diff line change
@@ -1,156 +1,136 @@
<?php
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

#snippet-start:[php.example_code.bedrock-runtime.service]
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[php.example_code.bedrock-runtime.service]
namespace BedrockRuntime;

use Aws\BedrockRuntime\BedrockRuntimeClient;
use AwsUtilities\AWSServiceClass;
use Exception;

class BedrockRuntimeService extends \AwsUtilities\AWSServiceClass
class BedrockRuntimeService extends AWSServiceClass
{
protected BedrockRuntimeClient $bedrockRuntimeClient;

public function __construct(
$client = null,
$region = 'us-east-1',
$version = 'latest',
$profile = 'default'
) {
if (gettype($client) == BedrockRuntimeClient::class) {
public function __construct(BedrockRuntimeClient $client = null)
{
if ($client) {
$this->bedrockRuntimeClient = $client;
return;
} else {
$this->bedrockRuntimeClient = new BedrockRuntimeClient([
'region' => 'us-east-1',
'profile' => 'default'
]);
}
$this->bedrockRuntimeClient = new BedrockRuntimeClient([
'region' => $region,
'version' => $version,
'profile' => $profile,
]);
}

#snippet-start:[php.example_code.bedrock-runtime.service.invokeClaude]
public function getClient(): BedrockRuntimeClient
{
return $this->bedrockRuntimeClient;
}

// snippet-start:[php.example_code.bedrock-runtime.service.invokeClaude]
public function invokeClaude($prompt)
{
# The different model providers have individual request and response formats.
# For the format, ranges, and default values for Anthropic Claude, refer to:
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
// The different model providers have individual request and response formats.
// For the format, ranges, and default values for Anthropic Claude, refer to:
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html

$completion = "";

try {
$modelId = 'anthropic.claude-v2';

# Claude requires you to enclose the prompt as follows:
// Claude requires you to enclose the prompt as follows:
$prompt = "\n\nHuman: {$prompt}\n\nAssistant:";

$body = [
'prompt' => $prompt,
'max_tokens_to_sample' => 200,
'temperature' => 0.5,
'stop_sequences' => ["\n\nHuman:"],
];

$result = $this->bedrockRuntimeClient->invokeModel([
'contentType' => 'application/json',
'body' => json_encode($body),
'modelId' => $modelId,
]);

$response_body = json_decode($result['body']);

$completion = $response_body->completion;
} catch (Exception $e) {
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
}

return $completion;
}
#snippet-end:[php.example_code.bedrock-runtime.service.invokeClaude]
// snippet-end:[php.example_code.bedrock-runtime.service.invokeClaude]

#snippet-start:[php.example_code.bedrock-runtime.service.invokeJurassic2]
// snippet-start:[php.example_code.bedrock-runtime.service.invokeJurassic2]
public function invokeJurassic2($prompt)
{
# The different model providers have individual request and response formats.
# For the format, ranges, and default values for AI21 Labs Jurassic-2, refer to:
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html

$completion = "";

try {
$modelId = 'ai21.j2-mid-v1';

$body = [
'prompt' => $prompt,
'temperature' => 0.5,
'maxTokens' => 200,
];

$result = $this->bedrockRuntimeClient->invokeModel([
'contentType' => 'application/json',
'body' => json_encode($body),
'modelId' => $modelId,
]);

$response_body = json_decode($result['body']);

$completion = $response_body->completions[0]->data->text;
} catch (Exception $e) {
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
}

return $completion;
}
#snippet-end:[php.example_code.bedrock-runtime.service.invokeJurassic2]
// snippet-end:[php.example_code.bedrock-runtime.service.invokeJurassic2]

#snippet-start:[php.example_code.bedrock-runtime.service.invokeLlama2]
// snippet-start:[php.example_code.bedrock-runtime.service.invokeLlama2]
public function invokeLlama2($prompt)
{
# The different model providers have individual request and response formats.
# For the format, ranges, and default values for Meta Llama 2 Chat, refer to:
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html
// The different model providers have individual request and response formats.
// For the format, ranges, and default values for Meta Llama 2 Chat, refer to:
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html

$completion = "";

try {
$modelId = 'meta.llama2-13b-chat-v1';

$body = [
'prompt' => $prompt,
'temperature' => 0.5,
'max_gen_len' => 512,
];

$result = $this->bedrockRuntimeClient->invokeModel([
'contentType' => 'application/json',
'body' => json_encode($body),
'modelId' => $modelId,
]);

$response_body = json_decode($result['body']);

$completion = $response_body->generation;
} catch (Exception $e) {
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
}

return $completion;
}
#snippet-end:[php.example_code.bedrock-runtime.service.invokeLlama2]
// snippet-end:[php.example_code.bedrock-runtime.service.invokeLlama2]

#snippet-start:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
// snippet-start:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
public function invokeStableDiffusion(string $prompt, int $seed, string $style_preset)
{
# The different model providers have individual request and response formats.
# For the format, ranges, and available style_presets of Stable Diffusion models refer to:
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html
// The different model providers have individual request and response formats.
// For the format, ranges, and available style_presets of Stable Diffusion models refer to:
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html

$base64_image_data = "";

try {
$modelId = 'stability.stable-diffusion-xl';

$modelId = 'stability.stable-diffusion-xl-v1';
$body = [
'text_prompts' => [
['text' => $prompt]
Expand All @@ -159,7 +139,6 @@ public function invokeStableDiffusion(string $prompt, int $seed, string $style_p
'cfg_scale' => 10,
'steps' => 30
];

if ($style_preset) {
$body['style_preset'] = $style_preset;
}
Expand All @@ -169,30 +148,26 @@ public function invokeStableDiffusion(string $prompt, int $seed, string $style_p
'body' => json_encode($body),
'modelId' => $modelId,
]);

$response_body = json_decode($result['body']);

$base64_image_data = $response_body->artifacts[0]->base64;
} catch (Exception $e) {
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
}

return $base64_image_data;
}
#snippet-end:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]
// snippet-end:[php.example_code.bedrock-runtime.service.invokeStableDiffusion]

#snippet-start:[php.example_code.bedrock-runtime.service.invokeTitanImage]
// snippet-start:[php.example_code.bedrock-runtime.service.invokeTitanImage]
public function invokeTitanImage(string $prompt, int $seed)
{
# The different model providers have individual request and response formats.
# For the format, ranges, and default values for Titan Image models refer to:
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-image.html
// The different model providers have individual request and response formats.
// For the format, ranges, and default values for Titan Image models refer to:
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-image.html

$base64_image_data = "";

try {
$modelId = 'amazon.titan-image-generator-v1';

$request = json_encode([
'taskType' => 'TEXT_IMAGE',
'textToImageParams' => [
Expand All @@ -207,23 +182,19 @@ public function invokeTitanImage(string $prompt, int $seed)
'seed' => $seed
]
]);

$result = $this->bedrockRuntimeClient->invokeModel([
'contentType' => 'application/json',
'body' => $request,
'modelId' => $modelId,
]);

$response_body = json_decode($result['body']);

$base64_image_data = $response_body->images[0];
} catch (Exception $e) {
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
}

return $base64_image_data;
}
#snippet-end:[php.example_code.bedrock-runtime.service.invokeTitanImage]
// snippet-end:[php.example_code.bedrock-runtime.service.invokeTitanImage]
}

#snippet-end:[php.example_code.bedrock-runtime.service]
// snippet-end:[php.example_code.bedrock-runtime.service]
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
<?php
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/**
* Purpose
* Shows how to use the AWS SDK for PHP with the Amazon Bedrock Runtime to:
* - ...
**/

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

class GettingStartedWithBedrockRuntime
{
protected BedrockRuntimeService $bedrockRuntimeService;

public function runExample()
{
echo "\n";
echo "---------------------------------------------------------------------\n";
echo "Welcome to the Amazon Bedrock Runtime getting started demo using PHP!\n";
echo "---------------------------------------------------------------------\n";

$clientArgs = [
'region' => 'us-east-1',
'version' => 'latest',
'profile' => 'default',
];

$bedrockRuntimeService = new BedrockRuntimeService($clientArgs);

$bedrockRuntimeService = new BedrockRuntimeService();
$prompt = 'In one paragraph, who are you?';

echo "\nPrompt: " . $prompt;

echo "\n\nAnthropic Claude:";
echo $bedrockRuntimeService->invokeClaude($prompt);

echo "\n\nAI21 Labs Jurassic-2: ";
echo $bedrockRuntimeService->invokeJurassic2($prompt);

echo "\n\nMeta Llama 2 Chat: ";
echo $bedrockRuntimeService->invokeLlama2($prompt);

echo "\n---------------------------------------------------------------------\n";

$image_prompt = 'stylized picture of a cute old steampunk robot';

echo "\nImage prompt: " . $image_prompt;

echo "\n\nStability.ai Stable Diffusion XL:\n";
$diffusionSeed = rand(0, 4294967295);
$style_preset = 'photographic';
$base64 = $bedrockRuntimeService->invokeStableDiffusion($image_prompt, $diffusionSeed, $style_preset);
$image_path = $this->saveImage($base64, 'stability.stable-diffusion-xl');
echo "The generated images have been saved to $image_path";

echo "\n\nAmazon Titan Image Generation:\n";
$titanSeed = rand(0, 2147483647);
$base64 = $bedrockRuntimeService->invokeTitanImage($image_prompt, $titanSeed);
Expand All @@ -66,7 +48,6 @@ public function runExample()
private function saveImage($base64_image_data, $model_id): string
{
$output_dir = "output";

if (!file_exists($output_dir)) {
mkdir($output_dir);
}
Expand All @@ -77,15 +58,11 @@ private function saveImage($base64_image_data, $model_id): string
}

$image_data = base64_decode($base64_image_data);

$file_path = "$output_dir/$model_id" . '_' . "$i.png";

$file = fopen($file_path, 'wb');
fwrite($file, $image_data);
fclose($file);

return $file_path;
}
}

# snippet-end:[php.example_code.bedrock-runtime.basics.scenario]
// snippet-end:[php.example_code.bedrock-runtime.basics.scenario]
10 changes: 5 additions & 5 deletions php/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@ functions within the same service.

### AI21 Labs Jurassic-2

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

### Amazon Titan Image Generator

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

### Anthropic Claude

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

### Meta Llama

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

### Stable Diffusion

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


<!--custom.examples.start-->
Expand Down
2 changes: 0 additions & 2 deletions php/example_code/bedrock-runtime/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use BedrockRuntime\GettingStartedWithBedrockRuntime;

include __DIR__ . '/vendor/autoload.php';

include 'GettingStartedWithBedrockRuntime.php';

try {
$runner = new GettingStartedWithBedrockRuntime();
$runner->runExample();
Expand Down
Loading
Loading