Skip to content

Commit 7fc628c

Browse files
authoredDec 20, 2024··
PHP: Adds example code for Amazon Bedrock Agent (#7158)
Adds example code for bedrock agent
1 parent 865f766 commit 7fc628c

File tree

8 files changed

+1232
-0
lines changed

8 files changed

+1232
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
output/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?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-agent-runtime.service]
6+
namespace BedrockAgentRuntime;
7+
8+
use Aws\BedrockAgentRuntime\BedrockAgentRuntimeClient;
9+
use AwsUtilities\AWSServiceClass;
10+
use Exception;
11+
12+
class BedrockAgentRuntimeService extends AWSServiceClass
13+
{
14+
public function __construct(
15+
string $agentId,
16+
string $agentAliasId,
17+
BedrockAgentRuntimeClient $client = null
18+
) {
19+
if ($client) {
20+
$this->bedrockAgentRuntimeClient = $client;
21+
} else {
22+
$this->bedrockAgentRuntimeClient = new BedrockAgentRuntimeClient([
23+
'region' => 'us-east-1',
24+
'profile' => 'default'
25+
]);
26+
}
27+
$this->agentId = $agentId;
28+
$this->agentAliasId = $agentAliasId;
29+
}
30+
31+
public function getClient(): BedrockAgentRuntimeClient
32+
{
33+
return $this->bedrockAgentRuntimeClient;
34+
}
35+
36+
// snippet-start:[php.example_code.bedrock-agent-runtime.service.invokeClaude]
37+
public function invokeAgent(string $prompt, string $sessionId): string {
38+
39+
// The Amazon Bedrock Agents Runtime API provides more parameters.
40+
// For example to filter knowledge base; for the full list of parameters refer to:
41+
// https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-bedrock-agent-runtime-2023-07-26.html#invokeagent
42+
43+
$payload = [
44+
'agentId' => $this->agentId,
45+
'agentAliasId' => $this->agentAliasId,
46+
'sessionId' => $sessionId,
47+
'enableTrace' => false,
48+
'inputText' => $prompt
49+
];
50+
51+
$completion = "";
52+
53+
try {
54+
$response = $this->bedrockAgentRuntimeClient->invokeAgent($payload);
55+
56+
foreach ($response['completion'] as $chunkEvent) {
57+
$chunk = $chunkEvent['chunk'];
58+
$decodedResponse = utf8_decode($chunk['bytes']);
59+
$completion .= $decodedResponse;
60+
}
61+
62+
return $completion;
63+
64+
} catch (AwsException $e) {
65+
throw new Exception("Bedrock Agent invocation failed: " . $e->getMessage());
66+
}
67+
}
68+
// snippet-end:[php.example_code.bedrock-agent-runtime.service.invokeClaude]
69+
70+
}
71+
// snippet-end:[php.example_code.bedrock-agent-runtime.service]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
/**
6+
* Purpose
7+
* Shows how to use the AWS SDK for PHP with the Amazon Bedrock Agent Runtime to:
8+
* - chat with an agent
9+
**/
10+
11+
// snippet-start:[php.example_code.bedrock-agent-runtime.basics.scenario]
12+
namespace BedrockAgentRuntime;
13+
14+
class GettingStartedWithBedrockAgentRuntime
15+
{
16+
protected BedrockAgentRuntimeService $bedrockAgentRuntimeService;
17+
public function runExample()
18+
{
19+
echo "\n";
20+
echo "---------------------------------------------------------------------\n";
21+
echo "Welcome to the Amazon Bedrock Agent Runtime getting started demo using PHP!\n";
22+
echo "---------------------------------------------------------------------\n";
23+
$agent_id = "YOUR_AGENT_ID"
24+
$agent_alias_id = "YOUR_AGENT_ALIAS_ID"
25+
$sessionId = uniqid()
26+
$bedrockAgentRuntimeService = new BedrockAgentRuntimeService($agent_id,$agent_alias_id);
27+
$prompt = "Hello, I am Mateo. Who are you?"
28+
echo "\nSession ID: " . $sessionId;
29+
echo "\nInput Text: " . $prompt;
30+
echo "\n\nAgent:";
31+
echo $bedrockAgentRuntimeService->invokeAgent($prompt, $sessionId);
32+
$prompt = "Do you remember my name?"
33+
echo "\nSession ID: " . $sessionId;
34+
echo "\nInput Text: " . $prompt;
35+
echo "\n\nAgent:";
36+
echo $bedrockAgentRuntimeService->invokeAgent($prompt, $sessionId);
37+
}
38+
}
39+
// snippet-end:[php.example_code.bedrock-agent-runtime.basics.scenario]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Amazon Bedrock Runtime code examples for the SDK for PHP
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for PHP to work with Amazon Bedrock Runtime.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_Amazon Bedrock Runtime is a fully managed service that makes it easy to use foundation models from third-party providers and Amazon._
11+
12+
## ⚠ Important
13+
14+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
15+
* Running the tests might result in charges to your AWS account.
16+
* 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).
17+
* 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).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `php` folder.
27+
28+
29+
<!--custom.prerequisites.start-->
30+
- [PHP](https://www.php.net/) version 8.2 or higher
31+
- [Composer](https://getcomposer.org), for dependency management
32+
- You must have an AWS account, and have your default credentials and AWS Region
33+
configured as described in the
34+
[AWS Tools and SDKs Shared Configuration and Credentials Reference Guide](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html).
35+
36+
> ⚠ 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).
37+
<!--custom.prerequisites.end-->
38+
### Scenarios
39+
40+
Code examples that show you how to accomplish a specific task by calling multiple
41+
functions within the same service.
42+
43+
- [Invoke multiple foundation models on Amazon Bedrock](GettingStartedWithBedrockRuntime.php)
44+
45+
### AI21 Labs Jurassic-2
46+
47+
- [InvokeModel](BedrockRuntimeService.php#L64)
48+
49+
### Amazon Titan Image Generator
50+
51+
- [InvokeModel](BedrockRuntimeService.php#L131)
52+
53+
### Anthropic Claude
54+
55+
- [InvokeModel](BedrockRuntimeService.php#L31)
56+
57+
### Stable Diffusion
58+
59+
- [InvokeModel](BedrockRuntimeService.php#L94)
60+
61+
62+
<!--custom.examples.start-->
63+
<!--custom.examples.end-->
64+
65+
## Run the examples
66+
67+
### Instructions
68+
69+
70+
<!--custom.instructions.start-->
71+
<!--custom.instructions.end-->
72+
73+
74+
75+
#### Invoke multiple foundation models on Amazon Bedrock
76+
77+
This example shows you how to prepare and send a prompt to a variety of large-language models (LLMs) on Amazon Bedrock
78+
79+
80+
<!--custom.scenario_prereqs.bedrock-runtime_Scenario_InvokeModels.start-->
81+
<!--custom.scenario_prereqs.bedrock-runtime_Scenario_InvokeModels.end-->
82+
83+
84+
<!--custom.scenarios.bedrock-runtime_Scenario_InvokeModels.start-->
85+
<!--custom.scenarios.bedrock-runtime_Scenario_InvokeModels.end-->
86+
87+
### Tests
88+
89+
⚠ Running tests might result in charges to your AWS account.
90+
91+
92+
To find instructions for running these tests, see the [README](../../README.md#Tests)
93+
in the `php` folder.
94+
95+
96+
97+
<!--custom.tests.start-->
98+
From the `aws-doc-sdk-examples/php/example_code/bedrock-runtime` directory:
99+
100+
Install the reequired dependencies using Composer:
101+
102+
```
103+
composer install
104+
```
105+
Run the tests with the following command:
106+
```
107+
../vendor/bin/phpunit tests/BedrockRuntimeTests.php
108+
```
109+
110+
<!--custom.tests.end-->
111+
112+
## Additional resources
113+
114+
- [Amazon Bedrock Runtime User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html)
115+
- [Amazon Bedrock Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
116+
- [SDK for PHP Amazon Bedrock Runtime reference](https://docs.aws.amazon.com/aws-sdk-php/v3/api/namespace-Aws.Bedrock-runtime.html)
117+
118+
<!--custom.resources.start-->
119+
<!--custom.resources.end-->
120+
121+
---
122+
123+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
124+
125+
SPDX-License-Identifier: Apache-2.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
use BedrockAgentRuntime\GettingStartedWithBedrockAgentRuntime;
6+
7+
include __DIR__ . '/vendor/autoload.php';
8+
include 'GettingStartedWithBedrockAgentRuntime.php';
9+
try {
10+
$runner = new GettingStartedWithBedrockAgentRuntime();
11+
$runner->runExample();
12+
} catch (Exception $e) {
13+
echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n";
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"require": {
3+
"aws/aws-sdk-php": "^3.324.10",
4+
"guzzlehttp/guzzle": "^7.9.2"
5+
},
6+
"autoload": {
7+
"psr-0": {
8+
"": "*"
9+
},
10+
"psr-4": {
11+
"BedrockAgentRuntime\\": "../bedrock-agent-runtime/",
12+
"AwsUtilities\\": "../aws_utilities/"
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.