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-west-2' )
16+ bedrock_runtime = session .client (service_name = 'bedrock-runtime' ,
17+ config = config )
18+ prompt = """
19+ Please analyze this PDF document and provide the following information:
20+
21+ 1. Document Title
22+ 2. Main topics covered
23+ 3. Key findings or conclusions
24+ 4. Important dates or numbers mentioned
25+ 5. Summary in 3-4 sentences
26+
27+ Format your response in a clear, structured way.
28+ """
29+
30+ # Set the model ID
31+
32+ SONNET_V2_MODEL_ID = "anthropic.claude-3-5-sonnet-20241022-v2:0"
33+
34+ def optimize_reel_prompt (user_prompt ,ref_image ):
35+ # open PDF
36+ with open (ref_image , "rb" ) as f :
37+ image = f .read ()
38+
39+ system = [
40+ {
41+ "text" : "You are an expert in summarizing PDF docs."
42+ }
43+ ]
44+ # payload of PDF as input
45+ messages = [
46+ {
47+ "role" : "user" ,
48+ "content" : [
49+ {
50+ "document" : {
51+ "format" : "pdf" ,
52+ "name" : "DocumentPDFmessages" ,
53+ "source" : {
54+ "bytes" : image
55+ }
56+ }
57+ },
58+ {"text" : user_prompt }
59+ ],
60+ }
61+ ]
62+ # Configure the inference parameters.
63+ inf_params = {"maxTokens" : 800 , "topP" : 0.9 , "temperature" : 0.5 }
64+ model_response = bedrock_runtime .converse_stream (
65+ modelId = SONNET_V2_MODEL_ID , messages = messages , system = system , inferenceConfig = inf_params
66+ )
67+ text = ""
68+ stream = model_response .get ("stream" )
69+ if stream :
70+ for event in stream :
71+ if "contentBlockDelta" in event :
72+ text += event ["contentBlockDelta" ]["delta" ]["text" ]
73+ print (event ["contentBlockDelta" ]["delta" ]["text" ], end = "" )
74+ return text
75+
76+ if __name__ == "__main__" :
77+ txt = optimize_reel_prompt (prompt ,"/Path/To/your.pdf" )
78+ print (txt )
0 commit comments