1+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ # SPDX-License-Identifier: Apache-2.0
3+
4+ # Use the Conversation API to send a text message along with PDF as input to Anthropic Claude
5+ # and print the response stream.
6+
7+ import boto3
8+ from botocore .config import Config
9+
10+ config = Config (
11+ connect_timeout = 1000 ,
12+ read_timeout = 1000 ,
13+ )
14+ # Create a Bedrock Runtime client in the AWS Region you want to use.
15+ session = boto3 .session .Session (region_name = 'us-east-1' )
16+ bedrock_runtime = session .client (service_name = 'bedrock-runtime' ,
17+ config = config )
18+ pdf_path = input ("Enter the path to the PDF file: " )
19+ prompt = """
20+ Please analyze this PDF document and provide the following information:
21+
22+ 1. Document Title
23+ 2. Main topics covered
24+ 3. Key findings or conclusions
25+ 4. Important dates or numbers mentioned
26+ 5. Summary in 3-4 sentences
27+
28+ Format your response in a clear, structured way.
29+ """
30+
31+ # Set the model ID
32+
33+ #SONNET_V2_MODEL_ID = "anthropic.claude-3-5-sonnet-20241022-v2:0"
34+ SONNET_V2_MODEL_ID = "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
35+ def optimize_reel_prompt (user_prompt ,ref_image ):
36+ # open PDF
37+ with open (ref_image , "rb" ) as f :
38+ image = f .read ()
39+
40+ system = [
41+ {
42+ "text" : "You are an expert in summarizing PDF docs."
43+ }
44+ ]
45+ # payload of PDF as input
46+ messages = [
47+ {
48+ "role" : "user" ,
49+ "content" : [
50+ {
51+ "document" : {
52+ "format" : "pdf" ,
53+ "name" : "DocumentPDFmessages" ,
54+ "source" : {
55+ "bytes" : image
56+ }
57+ }
58+ },
59+ {"text" : user_prompt }
60+ ],
61+ }
62+ ]
63+ # Configure the inference parameters.
64+ inf_params = {"maxTokens" : 800 , "topP" : 0.9 , "temperature" : 0.5 }
65+ model_response = bedrock_runtime .converse_stream (
66+ modelId = SONNET_V2_MODEL_ID , messages = messages , system = system , inferenceConfig = inf_params
67+ )
68+ text = ""
69+ stream = model_response .get ("stream" )
70+ if stream :
71+ for event in stream :
72+ if "contentBlockDelta" in event :
73+ text += event ["contentBlockDelta" ]["delta" ]["text" ]
74+ print (event ["contentBlockDelta" ]["delta" ]["text" ], end = "" )
75+ return text
76+
77+ if __name__ == "__main__" :
78+ txt = optimize_reel_prompt (prompt ,pdf_path )
79+ print (txt )
0 commit comments