
Using Document Chat with the Amazon Bedrock Converse API
Learn the basics of using Amazon Bedrock Converse API document chat to directly pass PDF, text, HTML, and other file formats directly to Amazon Bedrock.
- PDF
- HTML
- Word (.docx, .doc)
- Markdown (.md)
- Text (.txt)
- Set up the Boto3 AWS SDK and Python: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html
- Select an AWS region that supports the Anthropic Claude 3 Sonnet model. I'm using us-west-2 (Oregon). You can check the documentation for model support by region.
- Configure Amazon Bedrock model access for your account and region. Example here: https://catalog.workshops.aws/building-with-amazon-bedrock/en-US/prerequisites/bedrock-setup
- If you don’t have your own local integrated development environment, you can try AWS Cloud9. Setup instructions here: https://catalog.workshops.aws/building-with-amazon-bedrock/en-US/prerequisites/cloud9-setup
- Large language models are non-deterministic. You should expect different results than those shown in this article.
- If you run this code from your own AWS account, you will be charged for the tokens consumed.
- I generally subscribe to a “Minimum Viable Prompt” philosophy. You may need to write more detailed prompts for your use case.
- Not every model supports all of the capabilities of the Converse API, so it’s important to review the supported model features in the official documentation.
1
2
3
4
import boto3, json
session = boto3.Session()
bedrock = session.client(service_name='bedrock-runtime')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
with open("amazon-leadership-principles-070621-us.pdf", "rb") as doc_file:
doc_bytes = doc_file.read()
doc_message = {
"role": "user",
"content": [
{
"document": {
"name": "Document 1",
"format": "pdf",
"source": {
"bytes": doc_bytes #Look Ma, no base64 encoding!
}
}
},
{ "text": "Based on the document, which is the single most important leadership principle for an intern to adopt at the start of their career?" }
]
}
1
2
3
4
5
6
7
8
response = bedrock.converse(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[doc_message],
inferenceConfig={
"maxTokens": 2000,
"temperature": 0
},
)
1
2
response_text = response['output']['message']['content'][0]['text']
print(response_text)
- As an intern or someone new to the workforce, there is a vast amount to learn - about the company, the industry, professional skills, and more. Having a mindset of continuous learning and curiosity will allow the intern to soak up as much knowledge and experience as possible during this formative stage.
- The principle states "Leaders are never done learning and always seek to improve themselves." This growth mindset is invaluable early in one's career when the potential for development is immense.
- Being curious about "new possibilities and acting to explore them" as mentioned in the principle can open doors to new opportunities, projects, and career paths that the intern may not have initially considered.
- Many of the other principles like "Customer Obsession", "Invent and Simplify", and "Think Big" require a foundation of learning, curiosity, and always seeking to improve oneself.
- Not every model supports every capability of the Converse API, so it’s important to review the supported model features in the official documentation.
- You can find more generative AI hands-on activities at the Building with Amazon Bedrock workshop guide.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.