Skip to content

Commit 518cd3b

Browse files
victor-upmeetVictor Thuillier
and
Victor Thuillier
authored
Create BedrockRuntime client with InvokeModel operation (#1849)
* Create BedrockRuntime client with InvokeModel operation * Fix documentation URL * Fix BedrockRuntime InvokeModel documentation example * Regenrate BedrockRuntime InvokeModel * Input and Result unit tests for BedrockRuntime InvokeModel operation * Fix BedrockRuntime tests * Fix BedrockRuntime tests * Fix SymfonyBundle Changelog * Upgraded Symfony/Bundle branch-alias dev-master from 1.13-dev to 1.14-dev * Removed .idea files * Remove BedrockRuntime service endpoint- and smoke properties --------- Co-authored-by: Victor Thuillier <[email protected]>
0 parents  commit 518cd3b

32 files changed

+1040
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.github export-ignore
2+
/tests export-ignore
3+
/.gitignore export-ignore
4+
/Makefile export-ignore
5+
/phpunit.xml.dist export-ignore

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: [nyholm, jderusse]

.github/workflows/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.yml]
2+
indent_size = 2

.github/workflows/checks.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: BC Check
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
roave-bc-check:
10+
name: Roave BC Check
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Modify composer.json
20+
run: |
21+
sed -i -re 's/"require": \{/"minimum-stability": "dev","prefer-stable": true,"require": \{/' composer.json
22+
cat composer.json
23+
24+
git config --local user.email "[email protected]"
25+
git config --local user.name "AsyncAws Bot"
26+
git commit -am "Allow unstable dependencies"
27+
28+
- name: Install PHP with extensions
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: 8.3
32+
tools: composer:v2
33+
34+
- name: Install roave/backward-compatibility-check
35+
run: composer require --dev roave/backward-compatibility-check
36+
37+
- name: Roave BC Check
38+
run: vendor/bin/roave-backward-compatibility-check

.github/workflows/ci.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
10+
build:
11+
name: Build
12+
runs-on: ubuntu-latest
13+
strategy:
14+
max-parallel: 10
15+
matrix:
16+
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
17+
18+
steps:
19+
- name: Set up PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php }}
23+
coverage: none
24+
25+
- name: Checkout code
26+
uses: actions/checkout@v4
27+
28+
- name: Initialize tests
29+
run: make initialize
30+
31+
- name: Download dependencies
32+
run: |
33+
composer config minimum-stability dev
34+
composer req symfony/phpunit-bridge --no-update
35+
composer update --no-interaction --prefer-dist --optimize-autoloader --prefer-stable
36+
37+
- name: Run tests
38+
run: ./vendor/bin/simple-phpunit

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
*.cache
3+
composer.lock

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
## NOT RELEASED
4+
5+
## 1.0.0
6+
7+
First version

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Jérémy Derussé, Tobias Nyholm
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.EXPORT_ALL_VARIABLES:
2+
3+
initialize: start-docker
4+
start-docker:
5+
echo "Noop"
6+
7+
test: initialize
8+
./vendor/bin/simple-phpunit
9+
10+
clean: stop-docker
11+
stop-docker:
12+
echo "Noop"

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# AsyncAws BedrockRuntime Client
2+
3+
![](https://github.com/async-aws/bedrock-runtime/workflows/Tests/badge.svg?branch=master)
4+
![](https://github.com/async-aws/bedrock-runtime/workflows/BC%20Check/badge.svg?branch=master)
5+
6+
An API client for BedrockRuntime.
7+
8+
## Install
9+
10+
```cli
11+
composer require async-aws/bedrock-runtime
12+
```
13+
14+
## Documentation
15+
16+
See https://async-aws.com/clients/bedrock-runtime.html for documentation.
17+
18+
## Contribute
19+
20+
Contributions are welcome and appreciated. Please read https://async-aws.com/contribute/

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "async-aws/bedrock-runtime",
3+
"description": "BedrockRuntime client, part of the AWS SDK provided by AsyncAws.",
4+
"license": "MIT",
5+
"type": "library",
6+
"keywords": [
7+
"aws",
8+
"amazon",
9+
"sdk",
10+
"async-aws",
11+
"bedrock-runtime"
12+
],
13+
"require": {
14+
"php": "^7.2.5 || ^8.0",
15+
"async-aws/core": "^1.9"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"AsyncAws\\BedrockRuntime\\": "src"
20+
}
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"AsyncAws\\BedrockRuntime\\Tests\\": "tests/"
25+
}
26+
},
27+
"extra": {
28+
"branch-alias": {
29+
"dev-master": "1.0-dev"
30+
}
31+
}
32+
}

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
backupGlobals="false"
5+
colors="true"
6+
bootstrap="vendor/autoload.php"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
>
10+
<coverage>
11+
<include>
12+
<directory>./src</directory>
13+
</include>
14+
</coverage>
15+
<php>
16+
<ini name="error_reporting" value="-1"/>
17+
</php>
18+
<testsuites>
19+
<testsuite name="Test Suite">
20+
<directory>./tests/</directory>
21+
</testsuite>
22+
</testsuites>
23+
</phpunit>

src/BedrockRuntimeClient.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace AsyncAws\BedrockRuntime;
4+
5+
use AsyncAws\BedrockRuntime\Enum\PerformanceConfigLatency;
6+
use AsyncAws\BedrockRuntime\Enum\Trace;
7+
use AsyncAws\BedrockRuntime\Exception\AccessDeniedException;
8+
use AsyncAws\BedrockRuntime\Exception\InternalServerException;
9+
use AsyncAws\BedrockRuntime\Exception\ModelErrorException;
10+
use AsyncAws\BedrockRuntime\Exception\ModelNotReadyException;
11+
use AsyncAws\BedrockRuntime\Exception\ModelTimeoutException;
12+
use AsyncAws\BedrockRuntime\Exception\ResourceNotFoundException;
13+
use AsyncAws\BedrockRuntime\Exception\ServiceQuotaExceededException;
14+
use AsyncAws\BedrockRuntime\Exception\ServiceUnavailableException;
15+
use AsyncAws\BedrockRuntime\Exception\ThrottlingException;
16+
use AsyncAws\BedrockRuntime\Exception\ValidationException;
17+
use AsyncAws\BedrockRuntime\Input\InvokeModelRequest;
18+
use AsyncAws\BedrockRuntime\Result\InvokeModelResponse;
19+
use AsyncAws\Core\AbstractApi;
20+
use AsyncAws\Core\AwsError\AwsErrorFactoryInterface;
21+
use AsyncAws\Core\AwsError\JsonRestAwsErrorFactory;
22+
use AsyncAws\Core\Configuration;
23+
use AsyncAws\Core\RequestContext;
24+
25+
class BedrockRuntimeClient extends AbstractApi
26+
{
27+
/**
28+
* Invokes the specified Amazon Bedrock model to run inference using the prompt and inference parameters provided in the
29+
* request body. You use model inference to generate text, images, and embeddings.
30+
*
31+
* For example code, see *Invoke model code examples* in the *Amazon Bedrock User Guide*.
32+
*
33+
* This operation requires permission for the `bedrock:InvokeModel` action.
34+
*
35+
* ! To deny all inference access to resources that you specify in the modelId field, you need to deny access to the
36+
* ! `bedrock:InvokeModel` and `bedrock:InvokeModelWithResponseStream` actions. Doing this also denies access to the
37+
* ! resource through the Converse API actions (Converse [^1] and ConverseStream [^2]). For more information see Deny
38+
* ! access for inference on specific models [^3].
39+
*
40+
* For troubleshooting some of the common errors you might encounter when using the `InvokeModel` API, see
41+
* Troubleshooting Amazon Bedrock API Error Codes [^4] in the Amazon Bedrock User Guide
42+
*
43+
* [^1]: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html
44+
* [^2]: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html
45+
* [^3]: https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-deny-inference
46+
* [^4]: https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html
47+
*
48+
* @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_InvokeModel.html
49+
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-bedrock-runtime-2023-09-30.html#invokemodel
50+
*
51+
* @param array{
52+
* body?: null|string,
53+
* contentType?: null|string,
54+
* accept?: null|string,
55+
* modelId: string,
56+
* trace?: null|Trace::*,
57+
* guardrailIdentifier?: null|string,
58+
* guardrailVersion?: null|string,
59+
* performanceConfigLatency?: null|PerformanceConfigLatency::*,
60+
* '@region'?: string|null,
61+
* }|InvokeModelRequest $input
62+
*
63+
* @throws AccessDeniedException
64+
* @throws ResourceNotFoundException
65+
* @throws ThrottlingException
66+
* @throws ModelTimeoutException
67+
* @throws InternalServerException
68+
* @throws ServiceUnavailableException
69+
* @throws ValidationException
70+
* @throws ModelNotReadyException
71+
* @throws ServiceQuotaExceededException
72+
* @throws ModelErrorException
73+
*/
74+
public function invokeModel($input): InvokeModelResponse
75+
{
76+
$input = InvokeModelRequest::create($input);
77+
$response = $this->getResponse($input->request(), new RequestContext(['operation' => 'InvokeModel', 'region' => $input->getRegion(), 'exceptionMapping' => [
78+
'AccessDeniedException' => AccessDeniedException::class,
79+
'ResourceNotFoundException' => ResourceNotFoundException::class,
80+
'ThrottlingException' => ThrottlingException::class,
81+
'ModelTimeoutException' => ModelTimeoutException::class,
82+
'InternalServerException' => InternalServerException::class,
83+
'ServiceUnavailableException' => ServiceUnavailableException::class,
84+
'ValidationException' => ValidationException::class,
85+
'ModelNotReadyException' => ModelNotReadyException::class,
86+
'ServiceQuotaExceededException' => ServiceQuotaExceededException::class,
87+
'ModelErrorException' => ModelErrorException::class,
88+
]]));
89+
90+
return new InvokeModelResponse($response);
91+
}
92+
93+
protected function getAwsErrorFactory(): AwsErrorFactoryInterface
94+
{
95+
return new JsonRestAwsErrorFactory();
96+
}
97+
98+
protected function getEndpointMetadata(?string $region): array
99+
{
100+
if (null === $region) {
101+
$region = Configuration::DEFAULT_REGION;
102+
}
103+
104+
return [
105+
'endpoint' => "https://bedrock-runtime.$region.amazonaws.com",
106+
'signRegion' => $region,
107+
'signService' => 'bedrock',
108+
'signVersions' => ['v4'],
109+
];
110+
}
111+
}

src/Enum/PerformanceConfigLatency.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace AsyncAws\BedrockRuntime\Enum;
4+
5+
final class PerformanceConfigLatency
6+
{
7+
public const OPTIMIZED = 'optimized';
8+
public const STANDARD = 'standard';
9+
10+
public static function exists(string $value): bool
11+
{
12+
return isset([
13+
self::OPTIMIZED => true,
14+
self::STANDARD => true,
15+
][$value]);
16+
}
17+
}

src/Enum/Trace.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace AsyncAws\BedrockRuntime\Enum;
4+
5+
final class Trace
6+
{
7+
public const DISABLED = 'DISABLED';
8+
public const ENABLED = 'ENABLED';
9+
10+
public static function exists(string $value): bool
11+
{
12+
return isset([
13+
self::DISABLED => true,
14+
self::ENABLED => true,
15+
][$value]);
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace AsyncAws\BedrockRuntime\Exception;
4+
5+
use AsyncAws\Core\Exception\Http\ClientException;
6+
7+
/**
8+
* The request is denied because you do not have sufficient permissions to perform the requested action. For
9+
* troubleshooting this error, see AccessDeniedException [^1] in the Amazon Bedrock User Guide.
10+
*
11+
* [^1]: https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-access-denied
12+
*/
13+
final class AccessDeniedException extends ClientException
14+
{
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace AsyncAws\BedrockRuntime\Exception;
4+
5+
use AsyncAws\Core\Exception\Http\ServerException;
6+
7+
/**
8+
* An internal server error occurred. For troubleshooting this error, see InternalFailure [^1] in the Amazon Bedrock
9+
* User Guide.
10+
*
11+
* [^1]: https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html#ts-internal-failure
12+
*/
13+
final class InternalServerException extends ServerException
14+
{
15+
}

0 commit comments

Comments
 (0)