BedrockTextGateway
class BedrockTextGateway implements EmbeddingGateway, StepTextGateway
Traits
Constants
| protected STRUCTURED_OUTPUT_TOOL |
|
| protected MODELS_REJECTING_FORCED_TOOL_CHOICE |
|
Methods
Create an HTTP client with the provider's request options.
Select a boot-registered connection without retaining dynamic provider names.
Create a new Bedrock client instance.
Resolve the configured region, falling back to the default.
Resolve the provider authentication configuration.
Resolve the auth configuration for static, or automatically discovered, credentials.
Create a loader that assumes the configured role.
Create the STS client used to assume the configured role.
Build the parameters for the STS assume role request.
Decode a structured output payload, tolerating markdown code fences.
Execute a callback with failoverable error handling.
Get the short Retry-After delay of a rate limited or overloaded response, if it has one.
Determine if the response is a 402, or a 429 insufficient credits or quota error rather than a rate limit.
Get the status codes that indicate a provider is transiently unavailable and the request should fail over.
Get the patterns used to detect insufficient credits or quota errors.
Get the error codes or types that indicate insufficient credits or quota on a 429 response.
Join reasoning blocks, separating each block with a blank line.
Map the given Hypervel attachments to Bedrock content blocks.
Normalize the embeddings of a Cohere response into a list of vectors.
Create a Bedrock text gateway.
Generate text for a single Converse step.
Stream text for a single Converse step.
Build the Converse request parameters for the current text generation step.
Extract usage data from a Converse response.
Parse a single Converse response into a step response.
Extract the reasoning text from Converse content blocks.
Stream a single Converse step, returning the parsed step response.
Generate embeddings using AWS Bedrock.
Generate embeddings using a Cohere Bedrock model in a single batched call.
Determine if the given model identifier refers to a Cohere embeddings model.
Resolve the maximum number of steps for the given tools and options.
Extract and map the finish reason from the Bedrock Converse response.
Build the request parameters for the Bedrock Converse API.
Ensure longer-lived cache points precede shorter-lived cache points.
Build a Bedrock cache point for the requested TTL.
Build the inferenceConfig block for Bedrock's Converse API.
Build the assistant conversation message block combining text and tool calls.
Cast empty toolUse.input arrays to objects so the Converse API doesn't reject them.
Build the user conversation message block carrying tool results.
Determine if the model rejects a forced tool choice, so structured output must come from the text answer.
Determine whether the request enables manual extended thinking.
Build the system instruction that asks for the final answer as JSON matching the schema.
Build the synthetic structured-output tool plus any real tools.
Build Bedrock's toolConfig for the current step.
Format Hypervel AI messages for Bedrock's Converse API.
Format a ToolResultMessage for the Converse API.
Format a UserMessage and its attachments for the Converse API.
Format a generic Message (system/user/assistant) for the Converse API.
Format a raw array-shaped message for the Converse API.
Format tools for the Converse API.
Details
in
CreatesClient at line 21
protected PendingRequest
createClient(string $baseUrl, array $headers = [], array $configuredHeaders = [], int|null $timeout = 60, bool $throw = true)
Create an HTTP client with the provider's request options.
in
CreatesClient at line 43
protected string
httpConnection(Provider $provider)
Select a boot-registered connection without retaining dynamic provider names.
in
CreatesBedrockClient at line 34
protected BedrockRuntimeClient
createBedrockClient(Provider $provider, int|null $timeout = null)
Create a new Bedrock client instance.
in
CreatesBedrockClient at line 89
protected string
bedrockRegion(array $config)
Resolve the configured region, falling back to the default.
in
CreatesBedrockClient at line 101
protected array
resolveAuthConfig(array $credentials, array $config, int|null $timeout = null)
Resolve the provider authentication configuration.
in
CreatesBedrockClient at line 124
protected array
resolveSourceAuthConfig(array $credentials, array $config)
Resolve the auth configuration for static, or automatically discovered, credentials.
in
CreatesBedrockClient at line 153
protected Closure
assumeRoleCredentialProvider(array $credentials, array $config, int|null $timeout = null)
Create a loader that assumes the configured role.
in
CreatesBedrockClient at line 167
protected StsClient
createStsClient(array $credentials, array $config, int|null $timeout = null)
Create the STS client used to assume the configured role.
in
CreatesBedrockClient at line 189
protected array
assumeRoleParameters(array $assumeRole)
Build the parameters for the STS assume role request.
in
DecodesStructuredOutput at line 12
protected array
decodeStructuredOutput(string|null $text)
Decode a structured output payload, tolerating markdown code fences.
in
HandlesFailoverErrors at line 26
protected mixed
withErrorHandling(string $providerName, Closure $callback)
Execute a callback with failoverable error handling.
in
HandlesFailoverErrors at line 91
protected int|null
retryAfterSeconds(Throwable $exception)
Get the short Retry-After delay of a rate limited or overloaded response, if it has one.
in
HandlesFailoverErrors at line 115
protected bool
isInsufficientCredits(RequestException $exception)
Determine if the response is a 402, or a 429 insufficient credits or quota error rather than a rate limit.
in
HandlesFailoverErrors at line 138
protected array
overloadedStatusCodes()
Get the status codes that indicate a provider is transiently unavailable and the request should fail over.
in
HandlesFailoverErrors at line 148
protected array
insufficientCreditPatterns()
Get the patterns used to detect insufficient credits or quota errors.
in
HandlesFailoverErrors at line 158
protected array
insufficientCreditCodes()
Get the error codes or types that indicate insufficient credits or quota on a 429 response.
in
JoinsReasoning at line 16
static protected string
joinReasoning(iterable $blocks)
Join reasoning blocks, separating each block with a blank line.
in
MapsAttachments at line 33
protected array
mapAttachments(Collection $attachments)
Map the given Hypervel attachments to Bedrock content blocks.
in
MapsAttachments at line 81
protected array
buildDocumentBlock(Document $document)
Build a Bedrock document content block.
in
MapsAttachments at line 105
protected array
buildImageBlock(Image $image, string $bytes)
Build a Bedrock image content block.
in
MapsAttachments at line 120
protected string|null
getDocumentFormat(Document $document)
Map a Document's MIME type to a Bedrock document format.
in
MapsAttachments at line 148
protected string
getDocumentName(Document $document)
Build a Bedrock-compliant document name.
Bedrock limits names to 200 characters containing alphanumerics, whitespace, hyphens, parentheses, and square brackets. mapAttachments resolves duplicates.
in
MapsAttachments at line 164
protected string
getImageFormat(Image $image)
Map an Image's MIME type to a Bedrock image format.
Bedrock supports JPEG, PNG, GIF, and WebP images.
in
ParsesEmbeddings at line 18
protected array
parseCohereEmbeddings(mixed $embeddings)
Normalize the embeddings of a Cohere response into a list of vectors.
at line 84
__construct()
Create a Bedrock text gateway.
at line 91
StepResponse
generateTextStep(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)
Generate text for a single Converse step.
at line 124
Generator
generateStreamStep(string $invocationId, TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, int|null $timeout, StepContext $stepContext)
Stream text for a single Converse step.
at line 168
protected array
buildStepBody(TextProvider $provider, string $model, string|null $instructions, array $messages, array $tools, array|null $schema, TextGenerationOptions|null $options, StepContext $stepContext)
Build the Converse request parameters for the current text generation step.
at line 201
protected TextUsage
extractUsage(array $data)
Extract usage data from a Converse response.
at line 218
protected StepResponse
parseTextResponse(array $result, TextProvider $provider, string $model, bool $structured)
Parse a single Converse response into a step response.
at line 283
protected string
extractReasoning(array $content)
Extract the reasoning text from Converse content blocks.
at line 296
protected Generator
processTextStream(string $invocationId, TextProvider $provider, string $model, iterable $stream, bool $structured)
Stream a single Converse step, returning the parsed step response.
at line 600
EmbeddingsResponse
generateEmbeddings(EmbeddingProvider $provider, string $model, array $inputs, int $dimensions, int $timeout = 30, array $providerOptions = [])
Generate embeddings using AWS Bedrock.
at line 660
protected EmbeddingsResponse
generateCohereEmbeddings(EmbeddingProvider $provider, string $model, BedrockRuntimeClient $client, array $inputs, array $providerOptions = [])
Generate embeddings using a Cohere Bedrock model in a single batched call.
at line 701
protected bool
isCohereEmbeddingModel(string $model)
Determine if the given model identifier refers to a Cohere embeddings model.
at line 711
protected int
resolveMaxSteps(array $tools, TextGenerationOptions|null $options)
Resolve the maximum number of steps for the given tools and options.
at line 723
protected FinishReason
extractFinishReason(array $data)
Extract and map the finish reason from the Bedrock Converse response.
at line 742
protected array
buildConverseParameters(string $model, string|null $instructions, array $conversationMessages, array|null $schemaTools, array|null $formattedTools, bool $toolsEmpty, TextGenerationOptions|null $options, bool $isFinalStep, array|null $jsonSchema = null)
Build the request parameters for the Bedrock Converse API.
at line 813
protected void
ensureValidPromptCacheOrder(TextGenerationOptions|null $options)
Ensure longer-lived cache points precede shorter-lived cache points.
at line 825
protected array
cachePoint(string|null $ttl)
Build a Bedrock cache point for the requested TTL.
at line 833
protected array
buildInferenceConfig(TextGenerationOptions|null $options)
Build the inferenceConfig block for Bedrock's Converse API.
at line 852
protected array
buildAssistantConversationMessage(string $text, array $toolCalls, array $replayBlocks = [])
Build the assistant conversation message block combining text and tool calls.
at line 865
protected array
ensureToolInputIsObject(array $content)
Cast empty toolUse.input arrays to objects so the Converse API doesn't reject them.
at line 881
protected array
buildToolResultConversationMessage(array $toolResults)
Build the user conversation message block carrying tool results.
at line 899
protected bool
rejectsForcedToolChoice(string $model)
Determine if the model rejects a forced tool choice, so structured output must come from the text answer.
at line 907
protected bool
usesManualThinking(array $providerOptions)
Determine whether the request enables manual extended thinking.
at line 915
protected string
jsonInstruction(array $schema)
Build the system instruction that asks for the final answer as JSON matching the schema.
at line 924
protected array
buildSchemaTools(array $schema, array $tools)
Build the synthetic structured-output tool plus any real tools.
at line 947
protected array|null
buildToolConfig(array|null $schemaTools, array|null $formattedTools, bool $toolsEmpty, bool $isFinalStep)
Build Bedrock's toolConfig for the current step.
When a schema is present, toolChoice is only forced to the synthetic tool on the final step so real tools can be invoked on earlier iterations.
at line 968
protected array
formatMessages(array $messages)
Format Hypervel AI messages for Bedrock's Converse API.
at line 982
protected array
formatAssistantMessage(AssistantMessage $message)
Format an AssistantMessage for the Converse API.
at line 1013
protected array
formatToolResultMessage(ToolResultMessage $message)
Format a ToolResultMessage for the Converse API.
at line 1034
protected array
formatUserMessage(UserMessage $message)
Format a UserMessage and its attachments for the Converse API.
at line 1048
protected array
formatGenericMessage(Message $message)
Format a generic Message (system/user/assistant) for the Converse API.
at line 1061
protected array
formatArrayMessage(array $message)
Format a raw array-shaped message for the Converse API.
at line 1074
protected array
formatTools(array $tools)
Format tools for the Converse API.