使用AWS Bedrock Runtime调用Nova Premier模型处理PDF文档报错求助
AWS Bedrock Runtime 文档理解参数错误排查
问题场景
使用amazon.nova-premier-v1:0模型调用AWS Bedrock Runtime的converse接口处理PDF文档时,触发参数验证错误。
原代码
def process(): with open("file.pdf", "rb") as f: document_bytes = f.read() bedrock_client = boto3.client("bedrock-runtime", "us-east-1") response = bedrock_client.converse( modelId=MODEL_ID, messages=[ { "role": "user", "content": [ { "text": "explain this document", "document": { "format": "pdf", "name": "file", "source": { "bytes": document_bytes, }, }, }, ], }, ], ) print(response) return response
错误信息
botocore.exceptions.ParamValidationError: Parameter validation failed: Invalid number of parameters set for tagged union structure messages[0].content[0]. Can only set one of the following keys: text, image, document, video, toolUse, toolResult, guardContent, cachePoint, reasoningContent.
问题原因
messages[0].content是一个数组,其中每个元素属于标记联合结构,即每个元素只能包含text/document/image等其中一个键,不能在同一个元素内同时设置多个键。原代码将text和document放在了同一个content元素中,违反了参数规则。
修正后的代码
def process(): with open("file.pdf", "rb") as f: document_bytes = f.read() bedrock_client = boto3.client("bedrock-runtime", "us-east-1") response = bedrock_client.converse( modelId=MODEL_ID, messages=[ { "role": "user", "content": [ {"text": "explain this document"}, { "document": { "format": "pdf", "name": "file", "source": {"bytes": document_bytes}, }, }, ], }, ], ) print(response) return response
说明
将文本提示和文档内容拆分为content数组中的两个独立元素,每个元素仅包含一种类型的内容,即可通过参数验证并正常调用接口。官方文档中的示例也是遵循这一规则,将不同类型的内容作为独立的content元素传递。
内容的提问来源于stack exchange,提问作者Vivek K. Singh
相关产品推荐
相关产品推荐

