Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Converse method to BedrockRuntime client #1861

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/clients/bedrock-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,28 @@ echo $response['content'][0]['text'];

```
more information [InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html)

### Converse

```php
use AsyncAws\BedrockRuntime\BedrockRuntimeClient;
use AsyncAws\BedrockRuntime\Input\ConverseRequest;
use AsyncAws\BedrockRuntime\ValueObject\ContentBlock;
use AsyncAws\BedrockRuntime\ValueObject\Message;

$bedrockRuntime = new BedrockRuntimeClient();

$result = $bedrockRuntime->converse(new ConverseRequest([
'modelId' => 'us.anthropic.claude-3-7-sonnet-20250219-v1:0',
'messages' => [new Message([
'role' => 'user',
'content' => [new ContentBlock([
'text' => 'Write me a love poem.'
])],
])]
]));

echo $result->getOutput()->getMessage()->getContent()[0]->getText();

```
more information [Converse](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html)
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"example": "https://raw.githubusercontent.com/aws/aws-sdk-php/${LATEST}/src/data/bedrock-runtime/2023-09-30/examples-1.json",
"api-reference": "https://docs.aws.amazon.com/bedrock/latest/APIReference",
"methods": [
"Converse",
"InvokeModel"
]
},
Expand Down
1 change: 1 addition & 0 deletions src/Service/BedrockRuntime/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"require": {
"php": "^7.2.5 || ^8.0",
"ext-json": "*",
"async-aws/core": "^1.9"
},
"autoload": {
Expand Down
95 changes: 95 additions & 0 deletions src/Service/BedrockRuntime/src/BedrockRuntimeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@
use AsyncAws\BedrockRuntime\Exception\ServiceUnavailableException;
use AsyncAws\BedrockRuntime\Exception\ThrottlingException;
use AsyncAws\BedrockRuntime\Exception\ValidationException;
use AsyncAws\BedrockRuntime\Input\ConverseRequest;
use AsyncAws\BedrockRuntime\Input\InvokeModelRequest;
use AsyncAws\BedrockRuntime\Result\ConverseResponse;
use AsyncAws\BedrockRuntime\Result\InvokeModelResponse;
use AsyncAws\BedrockRuntime\ValueObject\Document;
use AsyncAws\BedrockRuntime\ValueObject\GuardrailConfiguration;
use AsyncAws\BedrockRuntime\ValueObject\InferenceConfiguration;
use AsyncAws\BedrockRuntime\ValueObject\Message;
use AsyncAws\BedrockRuntime\ValueObject\PerformanceConfiguration;
use AsyncAws\BedrockRuntime\ValueObject\PromptVariableValues;
use AsyncAws\BedrockRuntime\ValueObject\SystemContentBlock;
use AsyncAws\BedrockRuntime\ValueObject\ToolConfiguration;
use AsyncAws\Core\AbstractApi;
use AsyncAws\Core\AwsError\AwsErrorFactoryInterface;
use AsyncAws\Core\AwsError\JsonRestAwsErrorFactory;
Expand All @@ -24,6 +34,91 @@

class BedrockRuntimeClient extends AbstractApi
{
/**
* Sends messages to the specified Amazon Bedrock model. `Converse` provides a consistent interface that works with all
* models that support messages. This allows you to write code once and use it with different models. If a model has
* unique inference parameters, you can also pass those unique parameters to the model.
*
* Amazon Bedrock doesn't store any text, images, or documents that you provide as content. The data is only used to
* generate the response.
*
* You can submit a prompt by including it in the `messages` field, specifying the `modelId` of a foundation model or
* inference profile to run inference on it, and including any other fields that are relevant to your use case.
*
* You can also submit a prompt from Prompt management by specifying the ARN of the prompt version and including a map
* of variables to values in the `promptVariables` field. You can append more messages to the prompt by using the
* `messages` field. If you use a prompt from Prompt management, you can't include the following fields in the request:
* `additionalModelRequestFields`, `inferenceConfig`, `system`, or `toolConfig`. Instead, these fields must be defined
* through Prompt management. For more information, see Use a prompt from Prompt management [^1].
*
* For information about the Converse API, see *Use the Converse API* in the *Amazon Bedrock User Guide*. To use a
* guardrail, see *Use a guardrail with the Converse API* in the *Amazon Bedrock User Guide*. To use a tool with a
* model, see *Tool use (Function calling)* in the *Amazon Bedrock User Guide*
*
* For example code, see *Converse API examples* in the *Amazon Bedrock User Guide*.
*
* This operation requires permission for the `bedrock:InvokeModel` action.
*
* ! To deny all inference access to resources that you specify in the modelId field, you need to deny access to the
* ! `bedrock:InvokeModel` and `bedrock:InvokeModelWithResponseStream` actions. Doing this also denies access to the
* ! resource through the base inference actions (InvokeModel [^2] and InvokeModelWithResponseStream [^3]). For more
* ! information see Deny access for inference on specific models [^4].
*
* For troubleshooting some of the common errors you might encounter when using the `Converse` API, see Troubleshooting
* Amazon Bedrock API Error Codes [^5] in the Amazon Bedrock User Guide
*
* [^1]: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-management-use.html
* [^2]: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
* [^3]: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html
* [^4]: https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-deny-inference
* [^5]: https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html
*
* @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Converse.html
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-bedrock-runtime-2023-09-30.html#converse
*
* @param array{
* modelId: string,
* messages?: null|array<Message|array>,
* system?: null|array<SystemContentBlock|array>,
* inferenceConfig?: null|InferenceConfiguration|array,
* toolConfig?: null|ToolConfiguration|array,
* guardrailConfig?: null|GuardrailConfiguration|array,
* additionalModelRequestFields?: null|Document|array,
* promptVariables?: null|array<string, PromptVariableValues|array>,
* additionalModelResponseFieldPaths?: null|string[],
* requestMetadata?: null|array<string, string>,
* performanceConfig?: null|PerformanceConfiguration|array,
* '@region'?: string|null,
* }|ConverseRequest $input
*
* @throws AccessDeniedException
* @throws ResourceNotFoundException
* @throws ThrottlingException
* @throws ModelTimeoutException
* @throws InternalServerException
* @throws ServiceUnavailableException
* @throws ValidationException
* @throws ModelNotReadyException
* @throws ModelErrorException
*/
public function converse($input): ConverseResponse
{
$input = ConverseRequest::create($input);
$response = $this->getResponse($input->request(), new RequestContext(['operation' => 'Converse', 'region' => $input->getRegion(), 'exceptionMapping' => [
'AccessDeniedException' => AccessDeniedException::class,
'ResourceNotFoundException' => ResourceNotFoundException::class,
'ThrottlingException' => ThrottlingException::class,
'ModelTimeoutException' => ModelTimeoutException::class,
'InternalServerException' => InternalServerException::class,
'ServiceUnavailableException' => ServiceUnavailableException::class,
'ValidationException' => ValidationException::class,
'ModelNotReadyException' => ModelNotReadyException::class,
'ModelErrorException' => ModelErrorException::class,
]]));

return new ConverseResponse($response);
}

/**
* Invokes the specified Amazon Bedrock model to run inference using the prompt and inference parameters provided in the
* request body. You use model inference to generate text, images, and embeddings.
Expand Down
17 changes: 17 additions & 0 deletions src/Service/BedrockRuntime/src/Enum/ConversationRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class ConversationRole
{
public const ASSISTANT = 'assistant';
public const USER = 'user';

public static function exists(string $value): bool
{
return isset([
self::ASSISTANT => true,
self::USER => true,
][$value]);
}
}
31 changes: 31 additions & 0 deletions src/Service/BedrockRuntime/src/Enum/DocumentFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class DocumentFormat
{
public const CSV = 'csv';
public const DOC = 'doc';
public const DOCX = 'docx';
public const HTML = 'html';
public const MD = 'md';
public const PDF = 'pdf';
public const TXT = 'txt';
public const XLS = 'xls';
public const XLSX = 'xlsx';

public static function exists(string $value): bool
{
return isset([
self::CSV => true,
self::DOC => true,
self::DOCX => true,
self::HTML => true,
self::MD => true,
self::PDF => true,
self::TXT => true,
self::XLS => true,
self::XLSX => true,
][$value]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailContentFilterConfidence
{
public const HIGH = 'HIGH';
public const LOW = 'LOW';
public const MEDIUM = 'MEDIUM';
public const NONE = 'NONE';

public static function exists(string $value): bool
{
return isset([
self::HIGH => true,
self::LOW => true,
self::MEDIUM => true,
self::NONE => true,
][$value]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailContentFilterStrength
{
public const HIGH = 'HIGH';
public const LOW = 'LOW';
public const MEDIUM = 'MEDIUM';
public const NONE = 'NONE';

public static function exists(string $value): bool
{
return isset([
self::HIGH => true,
self::LOW => true,
self::MEDIUM => true,
self::NONE => true,
][$value]);
}
}
25 changes: 25 additions & 0 deletions src/Service/BedrockRuntime/src/Enum/GuardrailContentFilterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailContentFilterType
{
public const HATE = 'HATE';
public const INSULTS = 'INSULTS';
public const MISCONDUCT = 'MISCONDUCT';
public const PROMPT_ATTACK = 'PROMPT_ATTACK';
public const SEXUAL = 'SEXUAL';
public const VIOLENCE = 'VIOLENCE';

public static function exists(string $value): bool
{
return isset([
self::HATE => true,
self::INSULTS => true,
self::MISCONDUCT => true,
self::PROMPT_ATTACK => true,
self::SEXUAL => true,
self::VIOLENCE => true,
][$value]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailContentPolicyAction
{
public const BLOCKED = 'BLOCKED';

public static function exists(string $value): bool
{
return isset([
self::BLOCKED => true,
][$value]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailContextualGroundingFilterType
{
public const GROUNDING = 'GROUNDING';
public const RELEVANCE = 'RELEVANCE';

public static function exists(string $value): bool
{
return isset([
self::GROUNDING => true,
self::RELEVANCE => true,
][$value]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailContextualGroundingPolicyAction
{
public const BLOCKED = 'BLOCKED';
public const NONE = 'NONE';

public static function exists(string $value): bool
{
return isset([
self::BLOCKED => true,
self::NONE => true,
][$value]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailConverseContentQualifier
{
public const GROUNDING_SOURCE = 'grounding_source';
public const GUARD_CONTENT = 'guard_content';
public const QUERY = 'query';

public static function exists(string $value): bool
{
return isset([
self::GROUNDING_SOURCE => true,
self::GUARD_CONTENT => true,
self::QUERY => true,
][$value]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailConverseImageFormat
{
public const JPEG = 'jpeg';
public const PNG = 'png';

public static function exists(string $value): bool
{
return isset([
self::JPEG => true,
self::PNG => true,
][$value]);
}
}
15 changes: 15 additions & 0 deletions src/Service/BedrockRuntime/src/Enum/GuardrailManagedWordType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AsyncAws\BedrockRuntime\Enum;

final class GuardrailManagedWordType
{
public const PROFANITY = 'PROFANITY';

public static function exists(string $value): bool
{
return isset([
self::PROFANITY => true,
][$value]);
}
}
Loading
Loading