Skip to content

Commit

Permalink
Add optional client injection to BedrockRuntimeService
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Traub authored and cpyle0819 committed Oct 30, 2024
1 parent 9518b64 commit 95044b9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
21 changes: 15 additions & 6 deletions php/example_code/bedrock-runtime/BedrockRuntimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@

class BedrockRuntimeService extends AWSServiceClass
{
public function __construct()
public function __construct(BedrockRuntimeClient $client = null)
{
$this->bedrockRuntimeClient = new BedrockRuntimeClient([
'region' => 'us-east-1',
'profile' => 'default'
]);
if ($client) {
$this->bedrockRuntimeClient = $client;
} else {
$this->bedrockRuntimeClient = new BedrockRuntimeClient([
'region' => 'us-east-1',
'profile' => 'default'
]);
}
}

public function getClient(): BedrockRuntimeClient
{
return $this->bedrockRuntimeClient;
}

// snippet-start:[php.example_code.bedrock-runtime.service.invokeClaude]
Expand All @@ -29,7 +38,7 @@ public function invokeClaude($prompt)
$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,
Expand Down
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#L55)
- [InvokeModel](BedrockRuntimeService.php#L64)

### Amazon Titan Image Generator

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

### Anthropic Claude

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

### Meta Llama

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

### Stable Diffusion

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


<!--custom.examples.start-->
Expand Down
4 changes: 2 additions & 2 deletions php/example_code/bedrock-runtime/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require": {
"aws/aws-sdk-php": "^3.321",
"guzzlehttp/guzzle": "^7.8"
"aws/aws-sdk-php": "^3.324.10",
"guzzlehttp/guzzle": "^7.9.2"
},
"autoload": {
"psr-0": {
Expand Down
38 changes: 19 additions & 19 deletions php/example_code/bedrock-runtime/composer.lock

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

18 changes: 18 additions & 0 deletions php/example_code/bedrock-runtime/tests/BedrockRuntimeTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace bedrockruntime\tests;

use Aws\BedrockRuntime\BedrockRuntimeClient;
use BedrockRuntime\BedrockRuntimeService;
use PHPUnit\Framework\TestCase;

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

public function test_default_constructor_creates_client()
{
$service = new BedrockRuntimeService();
self::assertNotNull($service->getClient());
self::assertEquals('us-east-1', $service->getClient()->getRegion());
}

public function test_constructor_uses_injected_client()
{
$client = new BedrockRuntimeClient([
'region' => 'us-west-2'
]);
$service = new BedrockRuntimeService($client);
self::assertNotNull($service->getClient());
self::assertEquals($client, $service->getClient());
}

public function test_claude_can_be_invoked()
{
$completion = $this->bedrockRuntimeService->invokeClaude($this->prompt);
Expand Down

0 comments on commit 95044b9

Please sign in to comment.