使用 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 } }