From be10dbfaf58c9877b1142e47c8abc80ff4139da5 Mon Sep 17 00:00:00 2001 From: xueyao-aws Date: Wed, 12 Mar 2025 10:18:59 +0800 Subject: [PATCH 1/2] add converse_stream_pdf.py under bedrock_runtime...anthropic_claude --- .../anthropic_claude/converse_stream_pdf.py | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py diff --git a/python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py b/python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py new file mode 100644 index 00000000000..f58cdf09c55 --- /dev/null +++ b/python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py @@ -0,0 +1,78 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +# Use the Conversation API to send a text message along with PDF as input to Anthropic Claude +# and print the response stream. + +import boto3 +from botocore.config import Config + +config = Config( + connect_timeout=1000, + read_timeout=1000, +) +# Create a Bedrock Runtime client in the AWS Region you want to use. +session = boto3.session.Session(region_name='us-west-2') +bedrock_runtime = session.client(service_name = 'bedrock-runtime', + config=config) +prompt = """ +Please analyze this PDF document and provide the following information: + +1. Document Title +2. Main topics covered +3. Key findings or conclusions +4. Important dates or numbers mentioned +5. Summary in 3-4 sentences + +Format your response in a clear, structured way. +""" + +# Set the model ID + +SONNET_V2_MODEL_ID = "anthropic.claude-3-5-sonnet-20241022-v2:0" + +def optimize_reel_prompt(user_prompt,ref_image): + # open PDF + with open(ref_image, "rb") as f: + image = f.read() + + system = [ + { + "text": "You are an expert in summarizing PDF docs." + } + ] + # payload of PDF as input + messages = [ + { + "role": "user", + "content": [ + { + "document": { + "format": "pdf", + "name": "DocumentPDFmessages", + "source": { + "bytes": image + } + } + }, + {"text": user_prompt} + ], + } + ] + # Configure the inference parameters. + inf_params = {"maxTokens": 800, "topP": 0.9, "temperature": 0.5} + model_response = bedrock_runtime.converse_stream( + modelId=SONNET_V2_MODEL_ID, messages=messages, system=system, inferenceConfig=inf_params + ) + text = "" + stream = model_response.get("stream") + if stream: + for event in stream: + if "contentBlockDelta" in event: + text += event["contentBlockDelta"]["delta"]["text"] + print(event["contentBlockDelta"]["delta"]["text"], end="") + return text + +if __name__ == "__main__": + txt = optimize_reel_prompt(prompt,"/Path/To/your.pdf") + print(txt) \ No newline at end of file From 8deb8c6babea49dedc4e9d78fe265841d16c6dc5 Mon Sep 17 00:00:00 2001 From: xueyao-aws Date: Tue, 18 Mar 2025 11:36:05 +0800 Subject: [PATCH 2/2] 1,fix corss-region inference endpoint issue; 2,change pdf path by asking user input; --- .../models/anthropic_claude/converse_stream_pdf.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py b/python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py index f58cdf09c55..8540e877ac2 100644 --- a/python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py +++ b/python/example_code/bedrock-runtime/models/anthropic_claude/converse_stream_pdf.py @@ -12,9 +12,10 @@ read_timeout=1000, ) # Create a Bedrock Runtime client in the AWS Region you want to use. -session = boto3.session.Session(region_name='us-west-2') +session = boto3.session.Session(region_name='us-east-1') bedrock_runtime = session.client(service_name = 'bedrock-runtime', config=config) +pdf_path = input("Enter the path to the PDF file: ") prompt = """ Please analyze this PDF document and provide the following information: @@ -29,8 +30,8 @@ # Set the model ID -SONNET_V2_MODEL_ID = "anthropic.claude-3-5-sonnet-20241022-v2:0" - +#SONNET_V2_MODEL_ID = "anthropic.claude-3-5-sonnet-20241022-v2:0" +SONNET_V2_MODEL_ID = "us.anthropic.claude-3-5-sonnet-20241022-v2:0" def optimize_reel_prompt(user_prompt,ref_image): # open PDF with open(ref_image, "rb") as f: @@ -74,5 +75,5 @@ def optimize_reel_prompt(user_prompt,ref_image): return text if __name__ == "__main__": - txt = optimize_reel_prompt(prompt,"/Path/To/your.pdf") + txt = optimize_reel_prompt(prompt,pdf_path) print(txt) \ No newline at end of file