Nova による構造化出力の生成 - Amazon Nova

Nova による構造化出力の生成

マシンツーマシン通信のユースケースでは構造化された出力が重要です。これにより、ダウンストリームのユースケースで生成された出力をより効果的に消費して処理できるためです。ドキュメントから情報を抽出し、API からデータを取得するアシスタントを作成する場合も、アクションを実行するエージェントを開発する場合も、これらのタスクには、特定の構造化形式で出力を生成する基盤モデルが必要です。

Nova モデルは制約付きデコードを活用して、生成された出力で高いモデル信頼性を確保し、モデルが複雑なスキーマを簡単に処理できるようにします。制約付きデコードは、文法に依存して、モデルが各ステップで出力できるトークンを「制約」します。これは、モデルが出力を生成するときに選択できる実際のトークンを変更するため、これまで使用されていたプロンプト手法とは異なります。たとえば、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 } }