API를 통한 Nova의 문서 이해 사용
문서 QA(질문 답변) 또는 분석에 Amazon Nova를 사용하는 방법을 설명하기 위해 Python의 간단한 예제를 소개합니다. AWS Bedrock API(Boto3 SDK를 통해)를 사용해 PDF 문서를 모델이 답변할 질문과 함께 보냅니다.
import base64 import base64 import json import boto3 # Initialize Bedrock runtime client (adjust region as needed) client = boto3.client("bedrock-runtime", region_name="us-east-1") MODEL_ID = "us.amazon.nova-lite-v1:5" # using Nova Lite model in this example # Read the document file (PDF) in binary mode with open("my_document.pdf", "rb") as file: doc_bytes = file.read() # Construct the conversation messages with document + question messages = [ { "role": "user", "content": [ { "document": { "format": "pdf", "name": "Document1", # neutral name for the document "source": { "bytes": doc_bytes # embedding the PDF content directly } } }, { "text": "Here is a question about the document: ... (your question) ... ?" } ] } ] # Set inference parameters (optional) inf_params = {"maxTokens": 4000, "topP": 0.1, "temperature": 0.3} # Invoke the model response = client.converse(modelId=MODEL_ID, messages=messages, inferenceConfig=inf_params) # Extract and print the answer answer_text = response["output"]["message"]["content"][0]["text"] print(answer_text)
입력 파일이 크거나(직접 업로드 한도인 25MB 초과) 파일이 많은 경우 Amazon S3에 저장하고 참조할 수 있습니다. 이렇게 하면 요청을 통해 원시 바이트가 전송되지 않습니다. S3를 사용하는 경우 Bedrock 서비스에 버킷/객체에 액세스할 수 있는 권한이 있는지 확인합니다. 예를 들어 S3의 PDF를 참조하기 위해 문서 소스는 다음과 같이 ‘바이트’ 대신 ‘s3Location’을 사용합니다.
messages = [ { "role": "user", "content": [ { "document": { "format": "pdf", "name": "Report2023", "source": { "s3Location": { "uri": "s3://your-bucket/path/to/document1.pdf", "bucketOwner": "123456789012" } } } }, { "text": "Summarize the key findings from the Q3 2023 report." } ] } ]
참고
문서 이름에는 영숫자, 하이픈, 괄호, 대괄호만 포함할 수 있습니다.
name 필드는 모델이 지침으로 잘못 해석할 여지가 있어 프롬프트 인젝션에 취약합니다. 따라서 중립 이름을 지정하는 것이 좋습니다.