Skip to content

Python: Bedrock Runtime document understanding examples #7446

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

Merged
merged 8 commits into from
May 21, 2025
Merged
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
103 changes: 103 additions & 0 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,109 @@ bedrock-runtime_InvokeModelWithResponseStream_TitanTextEmbeddings:
services:
bedrock-runtime: {InvokeModel}

# Document understanding
bedrock-runtime_DocumentUnderstanding_AmazonNova:
title: Send and process a document with Amazon Nova on &BR;
title_abbrev: "Document understanding"
synopsis: send and process a document with Amazon Nova on &BR;.
category: Amazon Nova
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Send and process a document with Amazon Nova on &BR;.
snippet_tags:
- python.example_code.bedrock-runtime.DocumentUnderstanding_AmazonNovaText
services:
bedrock-runtime: {Converse}

bedrock-runtime_DocumentUnderstanding_AnthropicClaude:
title: Send and process a document with Anthropic Claude on &BR;
title_abbrev: "Document understanding"
synopsis: send and process a document with Anthropic Claude on &BR;.
category: Anthropic Claude
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Send and process a document with Anthropic Claude on &BR;.
snippet_tags:
- python.example_code.bedrock-runtime.DocumentUnderstanding_AnthropicClaude
services:
bedrock-runtime: {Converse}

bedrock-runtime_DocumentUnderstanding_CohereCommand:
title: Send and process a document with Cohere Command models on &BR;
title_abbrev: "Document understanding"
synopsis: send and process a document with Cohere Command models on &BR;.
category: Cohere Command
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Send and process a document with Cohere Command models on &BR;.
snippet_tags:
- python.example_code.bedrock-runtime.DocumentUnderstanding_CohereCommand
services:
bedrock-runtime: {Converse}

bedrock-runtime_DocumentUnderstanding_DeepSeek:
title: Send and process a document with DeepSeek on &BR;
title_abbrev: "Document understanding"
synopsis: send and process a document with DeepSeek on &BR;.
category: DeepSeek
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Send and process a document with DeepSeek on &BR;.
snippet_tags:
- python.example_code.bedrock-runtime.DocumentUnderstanding_DeepSeek
services:
bedrock-runtime: {Converse}

bedrock-runtime_DocumentUnderstanding_MetaLlama:
title: Send and process a document with Llama on &BR;
title_abbrev: "Document understanding"
synopsis: send and process a document with Llama on &BR;.
category: Meta Llama
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Send and process a document with Llama on &BR;.
snippet_tags:
- python.example_code.bedrock-runtime.DocumentUnderstanding_MetaLlama
services:
bedrock-runtime: {Converse}

bedrock-runtime_DocumentUnderstanding_Mistral:
title: Send and process a document with Mistral models on &BR;
title_abbrev: "Document understanding"
synopsis: send and process a document with Mistral models on &BR;.
category: Mistral AI
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-runtime
excerpts:
- description: Send and process a document with Mistral models on &BR;.
snippet_tags:
- python.example_code.bedrock-runtime.DocumentUnderstanding_Mistral
services:
bedrock-runtime: {Converse}

# Tool use scenarios
bedrock-runtime_Scenario_ToolUseDemo_AmazonNova:
title: "A tool use demo illustrating how to connect AI models on &BR; with a custom tool or API"
Expand Down
9 changes: 9 additions & 0 deletions python/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ functions within the same service.

- [Converse](models/amazon_nova/amazon_nova_text/converse.py#L4)
- [ConverseStream](models/amazon_nova/amazon_nova_text/converse_stream.py#L4)
- [Document understanding](models/amazon_nova/amazon_nova_text/document_understanding.py#L4)

### Amazon Nova Canvas

Expand Down Expand Up @@ -88,6 +89,7 @@ functions within the same service.

- [Converse](models/anthropic_claude/converse.py#L4)
- [ConverseStream](models/anthropic_claude/converse_stream.py#L4)
- [Document understanding](models/anthropic_claude/document_understanding.py#L4)
- [InvokeModel](models/anthropic_claude/invoke_model.py#L4)
- [InvokeModelWithResponseStream](models/anthropic_claude/invoke_model_with_response_stream.py#L4)
- [Scenario: Tool use with the Converse API](cross-model-scenarios/tool_use_demo/tool_use_demo.py)
Expand All @@ -96,23 +98,30 @@ functions within the same service.

- [Converse](models/cohere_command/converse.py#L4)
- [ConverseStream](models/cohere_command/converse_stream.py#L4)
- [Document understanding](models/cohere_command/document_understanding.py#L4)
- [InvokeModel: Command R and R+](models/cohere_command/command_r_invoke_model.py#L4)
- [InvokeModel: Command and Command Light](models/cohere_command/command_invoke_model.py#L4)
- [InvokeModelWithResponseStream: Command R and R+](models/cohere_command/command_r_invoke_model_with_response_stream.py#L4)
- [InvokeModelWithResponseStream: Command and Command Light](models/cohere_command/command_invoke_model_with_response_stream.py#L4)
- [Scenario: Tool use with the Converse API](cross-model-scenarios/tool_use_demo/tool_use_demo.py)

### DeepSeek

- [Document understanding](models/deepseek/document_understanding.py#L4)

### Meta Llama

- [Converse](models/meta_llama/converse.py#L4)
- [ConverseStream](models/meta_llama/converse_stream.py#L4)
- [Document understanding](models/meta_llama/document_understanding.py#L4)
- [InvokeModel](models/meta_llama/llama3_invoke_model.py#L4)
- [InvokeModelWithResponseStream](models/meta_llama/llama3_invoke_model_with_response_stream.py#L4)

### Mistral AI

- [Converse](models/mistral_ai/converse.py#L4)
- [ConverseStream](models/mistral_ai/converse_stream.py#L4)
- [Document understanding](models/mistral_ai/document_understanding.py#L4)
- [InvokeModel](models/mistral_ai/invoke_model.py#L4)
- [InvokeModelWithResponseStream](models/mistral_ai/invoke_model_with_response_stream.py#L4)

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# snippet-start:[python.example_code.bedrock-runtime.DocumentUnderstanding_AmazonNovaText]
# Send and process a document with Amazon Nova on Amazon Bedrock.

import boto3
from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS Region you want to use.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g. Amazon Nova Lite.
model_id = "amazon.nova-lite-v1:0"

# Load the document
with open("example-data/amazon-nova-service-cards.pdf", "rb") as file:
document_bytes = file.read()

# Start a conversation with a user message and the document
conversation = [
{
"role": "user",
"content": [
{"text": "Briefly compare the models described in this document"},
{
"document": {
# Available formats: html, md, pdf, doc/docx, xls/xlsx, csv, and txt
"format": "pdf",
"name": "Amazon Nova Service Cards",
"source": {"bytes": document_bytes},
}
},
],
}
]

try:
# Send the message to the model, using a basic inference configuration.
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 500, "temperature": 0.3},
)

# Extract and print the response text.
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)

except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)

# snippet-end:[python.example_code.bedrock-runtime.DocumentUnderstanding_AmazonNovaText]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# snippet-start:[python.example_code.bedrock-runtime.DocumentUnderstanding_AnthropicClaude]
# Send and process a document with Anthropic Claude on Amazon Bedrock.

import boto3
from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS Region you want to use.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g. Claude 3 Haiku.
model_id = "anthropic.claude-3-haiku-20240307-v1:0"

# Load the document
with open("example-data/amazon-nova-service-cards.pdf", "rb") as file:
document_bytes = file.read()

# Start a conversation with a user message and the document
conversation = [
{
"role": "user",
"content": [
{"text": "Briefly compare the models described in this document"},
{
"document": {
# Available formats: html, md, pdf, doc/docx, xls/xlsx, csv, and txt
"format": "pdf",
"name": "Amazon Nova Service Cards",
"source": {"bytes": document_bytes},
}
},
],
}
]

try:
# Send the message to the model, using a basic inference configuration.
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 500, "temperature": 0.3},
)

# Extract and print the response text.
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)

except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)

# snippet-end:[python.example_code.bedrock-runtime.DocumentUnderstanding_AnthropicClaude]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# snippet-start:[python.example_code.bedrock-runtime.DocumentUnderstanding_CohereCommand]
# Send and process a document with Cohere Command models on Amazon Bedrock.

import boto3
from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS Region you want to use.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g. Command R+.
model_id = "cohere.command-r-plus-v1:0"

# Load the document
with open("example-data/amazon-nova-service-cards.pdf", "rb") as file:
document_bytes = file.read()

# Start a conversation with a user message and the document
conversation = [
{
"role": "user",
"content": [
{"text": "Briefly compare the models described in this document"},
{
"document": {
# Available formats: html, md, pdf, doc/docx, xls/xlsx, csv, and txt
"format": "pdf",
"name": "Amazon Nova Service Cards",
"source": {"bytes": document_bytes},
}
},
],
}
]

try:
# Send the message to the model, using a basic inference configuration.
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 500, "temperature": 0.3},
)

# Extract and print the response text.
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)

except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)

# snippet-end:[python.example_code.bedrock-runtime.DocumentUnderstanding_CohereCommand]
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# snippet-start:[python.example_code.bedrock-runtime.DocumentUnderstanding_DeepSeek]
# Send and process a document with DeepSeek on Amazon Bedrock.

import boto3
from botocore.exceptions import ClientError

# Create a Bedrock Runtime client in the AWS Region you want to use.
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# Set the model ID, e.g. DeepSeek-R1
model_id = "us.deepseek.r1-v1:0"

# Load the document
with open("example-data/amazon-nova-service-cards.pdf", "rb") as file:
document_bytes = file.read()

# Start a conversation with a user message and the document
conversation = [
{
"role": "user",
"content": [
{"text": "Briefly compare the models described in this document"},
{
"document": {
# Available formats: html, md, pdf, doc/docx, xls/xlsx, csv, and txt
"format": "pdf",
"name": "Amazon Nova Service Cards",
"source": {"bytes": document_bytes},
}
},
],
}
]

try:
# Send the message to the model, using a basic inference configuration.
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 2000, "temperature": 0.3},
)

# Extract and print the reasoning and response text.
reasoning, response_text = "", ""
for item in response["output"]["message"]["content"]:
for key, value in item.items():
if key == "reasoningContent":
reasoning = value["reasoningText"]["text"]
elif key == "text":
response_text = value

print(f"\nReasoning:\n{reasoning}")
print(f"\nResponse:\n{response_text}")

except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)

# snippet-end:[python.example_code.bedrock-runtime.DocumentUnderstanding_DeepSeek]
Loading
Loading