Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode:...
🚨 错误信息
InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: 5a5b163d-d0f6-4ff0-b5dd-82e2e629a9ab, ValidationException: `temperature` and `top_p` cannot both be specified for this model. Please use only one. (request id: 2026011117082858992711X5tlPETx) (request id: 20260111170816734925640f1837sHB) (request id: 20260111090732767921363TQXl0AIp)
🤖 AI解决方案
代码报错分析报告
错误翻译
``
原文:temperature and top_p cannot both be specified for this model. Please use only one.
译文:temperature 和 top_p 不能同时为此模型指定。请仅使用其中一个。
`原因分析
这是一个AWS Bedrock API调用时发生的参数验证错误。根本原因是您在使用Bedrock的流式响应功能时,同时指定了temperature和top_p这两个互斥的采样参数。某些Bedrock模型(特别是Claude系列模型)不支持同时使用这两个参数,因为它们控制文本生成随机性的机制不同,同时使用会产生冲突。错误发生在调用
InvokeModelWithResponseStream`方法时,Bedrock服务端在验证请求参数时检测到了这个冲突。解决方案
您需要修改Bedrock API调用代码,只保留其中一个参数:
python
错误示例 - 同时指定了两个参数
response = bedrock_runtime.invoke_model_with_response_stream(
modelId='anthropic.claude-3-sonnet-20240229-v1:0',
body={
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"temperature": 0.7, # ❌ 与top_p冲突
"top_p": 0.9, # ❌ 与temperature冲突
"messages": [...]
}
)解决方案1:只使用temperature
response = bedrock_runtime.invoke_model_with_response_stream(
modelId='anthropic.claude-3-sonnet-20240229-v1:0',
body={
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"temperature": 0.7, # ✅ 仅使用temperature
"messages": [...]
}
)解决方案2:只使用top_p
response = bedrock_runtime.invoke_model_with_response_stream(
modelId='anthropic.claude-3-sonnet-20240229-v1:0',
body={
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"top_p": 0.9, # ✅ 仅使用top_p
"messages": [...]
}
)
##️ 预防措施
python
def validate_bedrock_params(temperature, top_p):
if temperature is not None and top_p is not None:
raise ValueError("temperature和top_p不能同时指定,请选择其中一个")
return True
参数选择建议:
根据您的具体需求选择合适的参数即可!