调用AWS Bedrock Claude Sonnet 3.5时图片JSON payload报错求助
问题描述
调用AWS Bedrock Claude 3.5 Sonnet模型时,传入系统提示词、本地JPEG图片、文本上下文及查询语句后出现JSON payload错误,错误信息如下:
A client error occured: messages.0.content.1.image.source: Field required
相关代码如下:
def generate_conversation_bedrock(bedrock_client, model_id, input_text, input_image, max_tokens): response = client.invoke_model( modelId=model_id, body=json.dumps( { "anthropic_version": "bedrock-2023-05-31", "max_tokens": max_tokens, "system": sys_msg, "messages": [ { "role": "user", "content": [ { "type": "text", "text": input_text }, { "type": "image", "image": { "format": "jpeg", "source": { "media_type": "image/jpeg", "data": input_image_base64 # Contiene la imagen en formato base64 } } } ] } ], } ), ) response_body = json.loads(response.get("body").read()) return response_body def main(): model_id = "anthropic.claude-3-5-sonnet-20240620-v1:0" input_text = "describe la imagen" input_image = "./combined_image.jpg" image = resize_and_compress_image(input_image) input_image_base64 = base64.b64encode(image).decode('utf-8') try: bedrock_client = boto3.client(service_name="bedrock-runtime", region_name="us-east-1") response = generate_conversation_bedrock(bedrock_client, model_id, input_text, input_image_base64, 1024) for output in response.get("content", []): print(output["text"]) except ClientError as err: message = err.response['Error']['Message'] print(f"A client error occured: {message}") if __name__ == "__main__": main()
注:resize_and_compress_image函数用于将图片调整至模型输入限制尺寸
问题分析与修复步骤
- JSON payload结构错误:Claude 3系列模型的图片内容格式不符合要求,错误地将
source嵌套在image对象中。正确格式是直接在type: "image"的内容项中包含source字段,且source内必须指定type: "base64"。 - 函数参数不匹配:
generate_conversation_bedrock函数定义的input_image参数未被使用,实际需要传入base64编码后的图片数据,需修改参数名为input_image_base64。 - 客户端对象误用:函数内使用了未定义的
client变量,应替换为传入的bedrock_client参数。 - 系统提示词未定义:代码中引用的
sys_msg变量未初始化,需补充定义系统提示词内容。
修正后的完整代码
import json import base64 import boto3 from botocore.exceptions import ClientError # 假设resize_and_compress_image函数已正确实现 def resize_and_compress_image(image_path): # 此处为示例实现,需替换为你的实际逻辑 with open(image_path, "rb") as f: return f.read() def generate_conversation_bedrock(bedrock_client, model_id, input_text, input_image_base64, max_tokens, sys_msg): response = bedrock_client.invoke_model( modelId=model_id, body=json.dumps( { "anthropic_version": "bedrock-2023-05-31", "max_tokens": max_tokens, "system": sys_msg, "messages": [ { "role": "user", "content": [ { "type": "text", "text": input_text }, { "type": "image", "source": { "type": "base64", "media_type": "image/jpeg", "data": input_image_base64 } } ] } ], } ), ) response_body = json.loads(response.get("body").read()) return response_body def main(): model_id = "anthropic.claude-3-5-sonnet-20240620-v1:0" input_text = "describe la imagen" input_image = "./combined_image.jpg" # 定义系统提示词 sys_msg = "你是一个专业的图片描述助手,需要根据用户提供的图片和文本指令输出准确的描述。" image = resize_and_compress_image(input_image) input_image_base64 = base64.b64encode(image).decode('utf-8') try: bedrock_client = boto3.client(service_name="bedrock-runtime", region_name="us-east-1") response = generate_conversation_bedrock(bedrock_client, model_id, input_text, input_image_base64, 1024, sys_msg) for output in response.get("content", []): print(output["text"]) except ClientError as err: message = err.response['Error']['Message'] print(f"A client error occured: {message}") if __name__ == "__main__": main()
内容的提问来源于stack exchange,提问作者Diego
相关产品推荐
相关产品推荐

