Skip to content

Commit 95044b9

Browse files
Dennis Traubcpyle0819
authored andcommitted
Add optional client injection to BedrockRuntimeService
1 parent 9518b64 commit 95044b9

File tree

5 files changed

+59
-32
lines changed

5 files changed

+59
-32
lines changed

php/example_code/bedrock-runtime/BedrockRuntimeService.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@
1111

1212
class BedrockRuntimeService extends AWSServiceClass
1313
{
14-
public function __construct()
14+
public function __construct(BedrockRuntimeClient $client = null)
1515
{
16-
$this->bedrockRuntimeClient = new BedrockRuntimeClient([
17-
'region' => 'us-east-1',
18-
'profile' => 'default'
19-
]);
16+
if ($client) {
17+
$this->bedrockRuntimeClient = $client;
18+
} else {
19+
$this->bedrockRuntimeClient = new BedrockRuntimeClient([
20+
'region' => 'us-east-1',
21+
'profile' => 'default'
22+
]);
23+
}
24+
}
25+
26+
public function getClient(): BedrockRuntimeClient
27+
{
28+
return $this->bedrockRuntimeClient;
2029
}
2130

2231
// snippet-start:[php.example_code.bedrock-runtime.service.invokeClaude]
@@ -29,7 +38,7 @@ public function invokeClaude($prompt)
2938
$completion = "";
3039
try {
3140
$modelId = 'anthropic.claude-v2';
32-
// Claude requires you to enclose the prompt as follows:
41+
// Claude requires you to enclose the prompt as follows:
3342
$prompt = "\n\nHuman: {$prompt}\n\nAssistant:";
3443
$body = [
3544
'prompt' => $prompt,

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#L55)
47+
- [InvokeModel](BedrockRuntimeService.php#L64)
4848

4949
### Amazon Titan Image Generator
5050

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

5353
### Anthropic Claude
5454

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

5757
### Meta Llama
5858

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

6161
### Stable Diffusion
6262

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

6565

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

php/example_code/bedrock-runtime/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"require": {
3-
"aws/aws-sdk-php": "^3.321",
4-
"guzzlehttp/guzzle": "^7.8"
3+
"aws/aws-sdk-php": "^3.324.10",
4+
"guzzlehttp/guzzle": "^7.9.2"
55
},
66
"autoload": {
77
"psr-0": {

php/example_code/bedrock-runtime/composer.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

php/example_code/bedrock-runtime/tests/BedrockRuntimeTests.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace bedrockruntime\tests;
1010

11+
use Aws\BedrockRuntime\BedrockRuntimeClient;
1112
use BedrockRuntime\BedrockRuntimeService;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -23,6 +24,23 @@ public function setup(): void
2324
$this->bedrockRuntimeService = new BedrockRuntimeService();
2425
}
2526

27+
public function test_default_constructor_creates_client()
28+
{
29+
$service = new BedrockRuntimeService();
30+
self::assertNotNull($service->getClient());
31+
self::assertEquals('us-east-1', $service->getClient()->getRegion());
32+
}
33+
34+
public function test_constructor_uses_injected_client()
35+
{
36+
$client = new BedrockRuntimeClient([
37+
'region' => 'us-west-2'
38+
]);
39+
$service = new BedrockRuntimeService($client);
40+
self::assertNotNull($service->getClient());
41+
self::assertEquals($client, $service->getClient());
42+
}
43+
2644
public function test_claude_can_be_invoked()
2745
{
2846
$completion = $this->bedrockRuntimeService->invokeClaude($this->prompt);

0 commit comments

Comments
 (0)