本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Invoke API
調用 API 可讓您直接存取 Amazon Nova 模型,更能夠控制請求和回應格式。與抽象化模型特定詳細資訊的 Converse API 不同,調用 API 可讓您直接使用模型的原生請求和回應結構。
注意
調用 API 支援與 Converse API 相同的功能,但文件輸入模式除外,這是 Converse API 特有的。
請求結構
調用 API 請求需要模型 ID 和 JSON 請求內文:
import boto3 import json bedrock = boto3.client('bedrock-runtime', region_name='us-east-1') request_body = { 'messages': [ { 'role': 'user', 'content': [{'text': 'What is machine learning?'}] } ], 'inferenceConfig': { 'maxTokens': 512, 'temperature': 0.7 } } response = bedrock.invoke_model( modelId='us.amazon.nova-2-lite-v1:0', body=json.dumps(request_body) ) response_body = json.loads(response['body'].read()) content_list = response_body["output"]["message"]["content"] # Extract the first text block text_block = next((item for item in content_list if "text" in item), None) if text_block is not None: print(text_block["text"])
請求參數
調用 API 支援下列金鑰參數:
-
messages:具有角色和內容的對話訊息陣列 -
system:內容和指示的選用系統提示 -
inferenceConfig:控制模型輸出的參數 (溫度、maxTokens、 topP、 topK、 stopSequences、 reasoningConfig) -
toolConfig:函數呼叫的工具規格和工具選擇