Skip to content

Commit

Permalink
PHP: Add Example for Listing Amazon Bedrock Foundation Models (#5683)
Browse files Browse the repository at this point in the history
* Add getting started with bedrock
  • Loading branch information
DennisTraub authored Nov 21, 2023
1 parent 89a2017 commit 2ca9793
Show file tree
Hide file tree
Showing 10 changed files with 1,198 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .doc_gen/metadata/bedrock_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ bedrock_ListFoundationModels:
- description: List the available &BRlong; foundation models.
snippet_tags:
- bedrock.java2.list_foundation_models.main
PHP:
versions:
- sdk_version: 3
github: php/example_code/bedrock
excerpts:
- description: List the available &BRlong; foundation models.
snippet_tags:
- php.example_code.bedrock.service.listFoundationModels
Python:
versions:
- sdk_version: 3
Expand Down
14 changes: 7 additions & 7 deletions php/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions php/example_code/bedrock/BedrockService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

#snippet-start:[php.example_code.bedrock.service]
namespace Bedrock;

use Aws\Bedrock\BedrockClient;

class BedrockService extends \AwsUtilities\AWSServiceClass
{
protected BedrockClient $bedrockClient;

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

#snippet-start:[php.example_code.bedrock.service.listFoundationModels]
public function listFoundationModels()
{
$result = $this->bedrockClient->listFoundationModels();
return $result;
}
#snippet-end:[php.example_code.bedrock.service.listFoundationModels]
}
#snippet-end:[php.example_code.bedrock.service]
51 changes: 51 additions & 0 deletions php/example_code/bedrock/GettingStartedWithBedrock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

# 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 Amazon Bedrock to:
* List information about the available foundation models.
**/

# snippet-start:[php.example_code.bedrock.basics.scenario]
namespace Bedrock;

class GettingStartedWithBedrock
{
protected BedrockService $bedrockService;

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

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

$bedrockService = new BedrockService($clientArgs);

echo "Let's retrieve the available foundation models (FMs).\n";

$result = $bedrockService->listFoundationModels();
foreach ($result['modelSummaries'] as $model) {
echo "\n==========================================\n";
echo " Model: {$model['modelId']}\n";
echo "------------------------------------------\n";
echo " Name: {$model['modelName']}\n";
echo " Provider: {$model['providerName']}\n";
echo " Input modalities: " . json_encode($model['inputModalities']) . "\n";
echo " Output modalities: " . json_encode($model['outputModalities']) . "\n";
echo " Supported customaizations: " . json_encode($model['customizationsSupported']) . "\n";
echo " Supported inference types: " . json_encode($model['inferenceTypesSupported']) . "\n";
echo "==========================================\n";
}
}
}
# snippet-end:[php.example_code.bedrock.basics.scenario]
109 changes: 109 additions & 0 deletions php/example_code/bedrock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!--Generated by WRITEME on 2023-11-17 14:22:11.546035 (UTC)-->
# Amazon Bedrock code examples for the SDK for PHP

## Overview

Shows how to use the AWS SDK for PHP to work with Amazon Bedrock.

<!--custom.overview.start-->
<!--custom.overview.end-->

*Amazon Bedrock enables you to build and scale generative AI applications with foundation models.*

## ⚠ Important

* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all).
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../../README.md#Prerequisites) in the `php` folder.


<!--custom.prerequisites.start-->
- [PHP](https://www.php.net/) version 8.2 or higher
- [Composer](https://getcomposer.org), for dependency management
- You must have an AWS account, and have your default credentials and AWS Region
configured as described in the
[AWS Tools and SDKs Shared Configuration and Credentials Reference Guide](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html).

> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).
<!--custom.prerequisites.end-->
### Single actions

Code excerpts that show you how to call individual service functions.

* [List available Amazon Bedrock foundation models](BedrockService.php#L32) (`ListFoundationModels`)

## Run the examples

### Instructions



<!--custom.instructions.start-->
From the `aws-doc-sdk-examples/php/example_code/bedrock` directory:

Install the required dependencies using Composer:

```
composer install
```

Once all dependencies have been installed, you can run the example by executing the
following command:

```
php Runner.php
```
<!--custom.instructions.end-->



### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../../README.md#Tests)
in the `php` folder.



<!--custom.tests.start-->
From the `aws-doc-sdk-examples/php/example_code/bedrock-runtime` directory:

Install the reequired dependencies using Composer:

```
composer install
```
Run the tests with the following command:
```
../vendor/bin/phpunit tests/BedrockBasicsTests.php
```

<!--custom.tests.end-->

## Additional resources

* [Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
* [Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
* [SDK for PHP Amazon Bedrock reference](https://docs.aws.amazon.com/aws-sdk-php/v3/api/namespace-Aws.Bedrock.html)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
17 changes: 17 additions & 0 deletions php/example_code/bedrock/Runner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use Bedrock\GettingStartedWithBedrock;

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

include 'GettingStartedWithBedrock.php';

try {
$runner = new GettingStartedWithBedrock();
$runner->runExample();
} catch (Exception $e) {
echo "Error: ({$e->getCode()} - {$e->getMessage()}\n";
}
15 changes: 15 additions & 0 deletions php/example_code/bedrock/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"require": {
"aws/aws-sdk-php": "^3.286",
"guzzlehttp/guzzle": "^7.8"
},
"autoload": {
"psr-0": {
"": "*"
},
"psr-4": {
"Bedrock\\": "../bedrock/",
"AwsUtilities\\": "../aws_utilities/"
}
}
}
Loading

0 comments on commit 2ca9793

Please sign in to comment.