How do I prevent read timeouts in Python when I use large models in Amazon Bedrock?

2 分的閱讀內容
1

I want to avoid getting read timeout errors when I use Amazon Bedrock to generate text.

Short description

Sometimes when you use Amazon Bedrock's large language models to generate text you get read timeout errors. These errors occur when the Python boto3 client queries the large language model but doesn't receive a response within botocore's default read timeout period. To resolve read timeout errors, increase the read timeout

Resolution

Example code to increase the read timeout value for the client:

from boto3 import client
from botocore.config import Config

config = Config(read_timeout=1000)

client = client(service_name='bedrock-runtime',
                      config=config)

If you use third-party libraries such as LangChain, first instantiate a boto3 client with a botocore configuration. Then, pass this as a client parameter to a callable model class.

Example code to increase the read timeout value when passing a boto3 client to third-party libraries.

from boto3 import client
from botocore.config import Config
from langchain_community.llms import Bedrock

config = Config(read_timeout=1000)

client = client(service_name='bedrock-runtime',
                      config=config)

llm = Bedrock(model_id="anthropic.claude-v2:1",
              client=client)

The preceding examples show that the read timeout is set to 1000 seconds. The read timeout period specifies how long botocore waits for a response from the server before it throws a read timeout exception.
Note: Large models such as Anthropic Claude Opus can take more than sixty seconds to return a response.

Best practices

Set the read timeout to be long enough to allow your queries to complete. Start with a large value such as 1000 seconds, and then adjust this duration until you no longer get a timeout error.

AWS 官方
AWS 官方已更新 9 個月前
2 評論

Simply extending the timeout is not acceptable for user-interactive applications. Even the default of 60 seconds is too long. Extending the timeout is masking an underlying problem, especially when the same request usually takes a small fraction of a minute. We would like to see more guidance on how to diagnose inconsistencies in response times from Bedrock for identical requests. Thank you.

回答 1 個月前

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
管理員
回答 1 個月前