File tree Expand file tree Collapse file tree 5 files changed +59
-32
lines changed
php/example_code/bedrock-runtime Expand file tree Collapse file tree 5 files changed +59
-32
lines changed Original file line number Diff line number Diff line change 1111
1212class 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 ,
Original file line number Diff line number Diff 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-->
Original file line number Diff line number Diff line change 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" : {
Original file line number Diff line number Diff line change 88
99namespace bedrockruntime \tests ;
1010
11+ use Aws \BedrockRuntime \BedrockRuntimeClient ;
1112use BedrockRuntime \BedrockRuntimeService ;
1213use 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 );
You can’t perform that action at this time.
0 commit comments