AWS Bedrock
This guide shows how to use the AWS Bedrock client with Adastra LLMGW. It allows you to access models like Claude from Anthropic.
Currently only InvokeModel and InvokeModelWithResponseStream Bedrock APIs are supported.
Setup
We need to have boto3 package installed which provides AWS SDK functionality for Python. Install it via:
Set your endpoint and API key to use the AWS Bedrock client:
Next, we need to create a client for the AWS Bedrock service.
Unfortunately currently there is no simple solution to set custom headers on
every boto3 Bedrock operation.
In order to include, for example, Authentication, user and project
headers, you should register custom boto3 client events:
Making Requests
Now, let’s make a request using the client to generate a completion with Claude.
In this example:
- For list of
modelIdparameters seeconfig.yamlwhere you look fordeployment_name. It refers to model group configured in LLMGW. - The request body format follows Anthropic’s Claude API specification for Bedrock.
- The event handler registration is required before making any requests to ensure proper authentication and metadata.
Accessing Response Metadata
For more detailed information, such as request cost and model information, you can inspect the response metadata in the headers. LLMGW includes custom headers prefixed with x-llmgw.
The output may look like this:
x-llmgw-cost- The cost of the request in cents.x-llmgw-request-id- The request id used for the request.x-llmgw-model-id- The model id used for the request.x-llmgw-attempts- The number of attempts made to get the response.
Streaming Responses
The standard approach blocks until the entire response is ready, which may take time for longer responses. An alternative way would be to access the completion in a streaming mode, rendering pieces of the response as soon as they get generated, as we can see for example in the ChatGPT user interface.
For streaming functionality, we recommend using the OpenAI or Azure OpenAI clients which have full streaming support with LLMGW.
Please see the AWS Bedrock documentation for more information on the streaming mode and different model formats.