使用 Nova 產生結構化輸出 - Amazon Nova

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 Nova 產生結構化輸出

結構化輸出對於machine-to-machine的通訊使用案例至關重要,因為這可讓下游使用案例更有效地使用和處理產生的輸出。無論是從文件擷取資訊、建立從 APIs,還是開發採取動作的客服人員,這些任務都需要基礎模型來產生特定結構化格式的輸出。

Nova Models 利用限制解碼來確保所產生輸出中的高模型可靠性,並允許模型輕鬆處理複雜的結構描述。限制解碼倚賴文法「限制」模型在每個步驟中可以輸出的可能字符。這與過去使用的提示技術不同,因為這會變更模型在產生輸出時可以選擇的實際字符。例如,關閉 JSON 物件時,模型將限制為僅選取 } 個字符。每次傳遞工具組態時,都會利用限制解碼。由於工具使用已提供我們特定的結構描述,因此我們能夠使用它,根據開發人員所需的結構描述動態產生文法。限制解碼可防止模型產生無效的金鑰,並根據定義的結構描述強制執行正確的資料類型。

若要利用工具搭配結構化輸出使用,主要步驟是定義輸出所需的 JSON 結構描述。以下是tool_config定義中定義的 JSON 結構描述範例:

tool_config = { "tools": [ { "toolSpec": { "name": "ProductAnalysis", "description": "Analyze product information from text.", "inputSchema": { "json": { "type": "object", "properties": { "name": { "type": "string", "description": "Product name" }, "rating": { "maximum": 5, "description": "Customer rating 1-5", "type": [ "number", "null" ], "minimum": 1 }, "features": { "description": "Key product features", "type": "array", "items": { "type": "string" } }, "category": { "type": "string", "description": "Product category" }, "price": { "type": "number", "description": "Price in USD" } }, "required": [ "name", "category", "price", "features" ] } } } } ], "toolChoice": { "tool": { "name": "ProductAnalysis" } } }

當您稍後使用模型呼叫工具時,您將會收到以結構描述格式回應的輸出。例如,以下是使用 Python 呼叫模型的範例:

import boto3 client = boto3.client("bedrock-runtime") model_id = "amazon.nova-lite-1-5-v1:0" user_query = """The Amazon Kindle Scribe is a state-of-the-art e-reader designed for both reading and writing, featuring a 10.2-inch paper-like display and a premium stylus. This versatile device allows users to enjoy books, take notes, annotate PDFs, and even sketch, making it ideal for readers, students, and professionals. Priced at $339.99, it falls under the electronics category and boasts features like a front light, adjustable warm light settings, and up to 12 weeks of battery life on a single charge. Customer ratings for the Kindle Scribe average around 4.5 stars, reflecting its high user satisfaction.""" messages = [{ "role": "user", "content": [{ "text": user_query }] }] system = [{"text": "Leverage the ProductAnalysis tool to extract product information"}] inference_params = {"temperature": 0} response = client.converse(modelId=model_id, system=system, messages=messages, toolConfig=tool_config, inferenceConfig=inference_params) print(next( block["toolUse"] for block in response["output"]["message"]["content"] if "toolUse" in block ))

然後,輸出會顯示如下:

{ "toolUseId": "tooluse_hke1FUeuRbKXK8DPqIptVg", "name": "ProductAnalysis", "input": { "name": "Amazon Kindle Scribe", "rating": 4.5, "features": [ "10.2-inch paper-like display", "premium stylus", "front light", "adjustable warm light settings", "up to 12 weeks of battery life" ], "category": "electronics", "price": 339.99 } }