

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

# 需要結構化輸出
<a name="prompting-structured-output"></a>

為了確保一致且結構化的輸出格式，您可以使用*結構化輸出*，包括 XML、JSON 或 Markdown 等格式。此方法可讓下游使用案例更有效地使用和處理模型產生的輸出。透過提供明確指示給模型，模型會以遵循預先定義之結構描述的方式產生回應。建議您提供 `output schema` 供模型遵循。

例如，如果下游解析器預期 JSON 物件中的鍵采用特定命名慣例，則您應該在查詢的**輸出結構描述**欄位中指定此點。此外，如果您偏好回應為 JSON 格式，而且沒有任何前序文字，請相應地指示模型。也就是說，明確陳述「**請只產生 JSON 輸出。請勿提供任何前序。**」。

**使用預先填入協助模型啟動**

有效的替代方案是預先填入 `assistant` 內容，藉此*引導*模型的回應。此技術可讓您引導模型的動作、略過前序，以及強制執行特定輸出格式，例如 JSON 和 XML。例如，如果您使用 `"{"` 或 `"```json"` 預先填入助理內容，則該輸入可以引導模型產生 JSON 物件，而無需提供其他資訊。

**提示**  
如果您明確想要擷取 JSON，一種常見的觀察到的模式是使用 ````json` 預先填充 JSON，並在 ````` 上新增停止序列。這可確保模型輸出的 JSON 物件可透過程式設計方式進行解析。

以下是一些常見格式結構描述的範例。

------
#### [ JSON ]

```
JSON_schema = """Make sure your final response is a valid JSON schema follow the below Response Schema:

##Response Schema:
```json
{ 
"key1": "value1",
"key2": "value2",
key3: [{
"key3_1": "value_3_1,
"key3_2": "value_3_2,
...}```
"""
```

------
#### [ XML ]

```
XML_format = """Make sure your final response is a valid XML schema follow the below Response Schema:
##Response Schema:
<thinking>
( your thoughts go hee )
</thinking>
<output>
    <task>"task1"</task>
    <subtask>
    <task1_result> ( task 1 result )</task1_result>
    <task2_result> ( task 2 result )</task2_result>
    <task3_result> ( task 3 result )</task3_result>
    </subtask>
    <task>"task2"</task>
    <subtask>
    <task1_result> ( task 1 result )</task1_result>
    <task2_result> ( task 2 result )</task2_result>
    <task3_result> ( task 3 result )</task3_result>
    </subtask>
</output>

"""
```

------
#### [ Markdown ]

```
markdown_schema = """Make sure your final response is a valid Markdown schema follow the below Response Schema:

##Response Schema:
## Introduction
( 2-3 line intro)

## Design Guidance 
(Bulleted list of design guidance)

## Step by Step Instructions on Execution
( Bulleted list of instructions with each with bold title.

## Conclusion
( conclusion )


"""
```

------

以下程式碼示範如何使用 API 進行預先填充：

```
import boto3
import json

# Create a Bedrock Runtime client.
client = boto3.client(
    "bedrock-runtime",
    region_name="us-east-1"
)

request_body = {
    "system": [
        {"text": "You write JSON objects based on the given instructions"}
    ],
    "messages": [
        {
            "role": "user",
            "content": [{"text": "Provide details about the best selling full-frame cameras in past three years.
Answer in JSON format with keys like name, brand, price and a summary."}]
        },
        {
            "role": "assistant",
            "content": [{"text": " Here is the JSON response: ```json"}]
        },
    ],
    "inferenceConfig": {
        "maxTokens": 300,
        "temperature": 0,
    }
}

# Invoke the model and extract the response body.
response = client.invoke_model(
    modelId="amazon.nova-micro-v1:0",
    body=json.dumps(request_body)
)
model_response = json.loads(response["body"].read())
```

**注意**  
對於結構化輸出，無論是否利用了工具的使用，我們都建議使用貪婪解碼參數。也就是 `temperature=0`。

## 範例 1：新增輸出結構描述
<a name="structure-collapsible1"></a>

在輸出結構描述區段新增適當的結構描述，可讓模型偏好符合該結構描述。不過，模型輸出並非確定性的，並且可能與輸出結構描述不同。


| Role |  | 提示詞 | 
| --- |--- |--- |
| 使用者 | 提供過去三年中最暢銷的全片幅相機的詳細資訊。使用名稱、品牌、價格和摘要等鍵，以 JSON 格式回答。 | 提供過去三年中最暢銷的全片幅相機的詳細資訊。您必須僅以 JSON 格式回答。請遵循以下輸出結構描述。<br />**輸出結構描述：**<pre>[{<br />    "name": "name goes here",<br />    "brand": "brand goes here",<br />    "price": "price goes here",<br />    "summary": "summary goes here"<br />}]</pre> | 
| 助理 |  <pre>[<br /> {<br /> "name": "Sony Alpha A7R IV",<br /> "brand": "Sony",<br /> "price": "Approximately $3,500",<br /> "summary": "The Sony Alpha A7R IV is renowned for its<br /> exceptional image quality, thanks to a 61-megapixel<br /> full-frame sensor. It also features 4K video recording<br /> and a robust build, making it a favorite among professional<br /> photographers and videographers."<br /> },<br /> {<br /> "name": "Canon EOS R5",<br /> "brand": "Canon",<br /> "price": "Approximately $3,900",<br /> "summary": "The Canon EOS R5 combines high-resolution<br /> imaging with advanced video capabilities. It houses a<br /> 45-megapixel sensor, 8K video recording, and impressive<br /> autofocus performance, appealing to both photography<br /> and filmmaking professionals."<br /> },<br /> {<br /> "name": "Nikon Z7 II",<br /> "brand": "Nikon",<br /> "price": "Approximately $2,800",<br /> "summary": "The Nikon Z7 II offers a 45.7-megapixel<br /> full-frame sensor and is praised for its high resolution<br /> and excellent dynamic range. It also supports 4K video<br /> recording and features a versatile Z-mount lens system,<br /> making it a strong contender in the full-frame market."<br /> },<br /> {<br /> "name": "Fujifilm GFX 100S",<br /> "brand": "Fujifilm",<br /> "price": "Approximately $7,000",<br /> "summary": "While not a traditional full-frame camera,<br /> the Fujifilm GFX 100S stands out with its 102-megapixel<br /> medium format sensor. It is lauded for its stunning detail<br /> and color reproduction, though it comes at a higher price<br /> point and is aimed at specialized users."<br /> }<br /> ]</pre>  |  <pre>[<br /> {<br /> "name": "Nikon D850",<br /> "brand": "Nikon",<br /> "price": "$3,296.95",<br /> "summary": "The Nikon D850 is a professional full-frame<br /> DSLR camera known for its exceptional image quality,<br /> 45.7-megapixel resolution, 7 fps continuous shooting,<br /> and 4K video capabilities."<br /> },<br /> {<br /> "name": "Canon EOS 5D Mark IV",<br /> "brand": "Canon",<br /> "price": "$3,499.00",<br /> "summary": "The Canon EOS 5D Mark IV is a popular<br /> full-frame DSLR appreciated for its 30.4-megapixel sensor,<br /> 4K video recording, robust build quality, and reliable<br /> autofocus system."<br /> },<br /> {<br /> "name": "Sony A7R III",<br /> "brand": "Sony",<br /> "price": "$3,198.00",<br /> "summary": "The Sony A7R III is a high-resolution<br /> full-frame mirrorless camera renowned for its 42.4-megapixel<br /> sensor, excellent low-light performance, and versatile<br /> lens compatibility."<br /> },<br /> {<br /> "name": "Fujifilm GFX 100S",<br /> "brand": "Fujifilm",<br /> "price": "$5,999.99",<br /> "summary": "The Fujifilm GFX 100S is a medium format<br /> mirrorless camera that stands out for its 102-megapixel<br /> sensor, high dynamic range, and exceptional color reproduction."<br /> },<br /> {<br /> "name": "Sony A7 III",<br /> "brand": "Sony",<br /> "price": "$1,998.00",<br /> "summary": "The Sony A7 III is a full-frame mirrorless<br /> camera popular among enthusiasts and professionals for<br /> its 24.2-megapixel sensor, fast autofocus, and 4K video<br /> capabilities."<br /> }<br /> ]</pre>  | 

## 範例 2：預先填入模型回應
<a name="structure-collapsible2"></a>

另一種方法是透過在助理的訊息中包含所需的初始文字，來預先填入模型的回應。在此情況下，Amazon Nova 模型的回應將從助理訊息結束的位置繼續。


| Role | 提示詞 | 
| --- |--- |
| 使用者 | 提供過去三年中最暢銷的全片幅相機的詳細資訊。您的回應應該採用 JSON 格式，並具有下列鍵：名稱、品牌、年份、價格、摘要。 | 
| 助理*(預先填入)* | ```json | 
| 助理 |  <pre>{<br /> "cameras": [<br /> {<br /> "name": "Sony Alpha A7 III",<br /> "brand": "Sony",<br /> "year": 2018,<br /> "price": "$1999",<br /> "summary": "The Sony Alpha A7 III is a highly acclaimed<br /> full-frame mirrorless camera known for its excellent<br /> image quality, advanced autofocus system, and 4K video<br /> capabilities."<br /> },<br /> {<br /> "name": "Canon EOS R5",<br /> "brand": "Canon",<br /> "year": 2020,<br /> "price": "$3899",<br /> "summary": "The Canon EOS R5 is a top-tier full-frame<br /> mirrorless camera that offers stunning 45MP images, 8K<br /> video recording, and exceptional performance in various<br /> lighting conditions."<br /> },<br /> {<br /> "name": "Nikon Z7 II",<br /> "brand": "Nikon",<br /> "year": 2020,<br /> "price": "$2999",<br /> "summary": "The Nikon Z7 II is a flagship full-frame<br /> mirrorless camera with a 45.7MP sensor, superb image<br /> quality, and advanced video recording features."<br /> },<br /> {<br /> "name": "Fujifilm GFX 100S",<br /> "brand": "Fujifilm",<br /> "year": 2020,<br /> "price": "$7999",<br /> "summary": "The Fujifilm GFX 100S is a medium format<br /> mirrorless camera known for its impressive 102MP sensor,<br /> beautiful film simulations, and versatile lens options."<br /> },<br /> {<br /> "name": "Sony Alpha A7R IV",<br /> "brand": "Sony",<br /> "year": 2019,<br /> "price": "$3499",<br /> "summary": "The Sony Alpha A7R IV features a 61MP<br /> sensor, excellent low-light performance, and advanced<br /> video capabilities, making it a favorite among<br /> professional photographers."<br /> }<br /> ]<br /> }<br /> ```</pre>  | 

## 範例 3：指定要使用的工具
<a name="structure-collapsible3"></a>

第三種方法是透過在工具使用結構描述區段中包含*傳統 JSON 結構描述*，使用工具來強制模型回應的特定結構描述。您可以為提供的結構描述指定*工具選擇*，Amazon Nova 的回應將根據選取的工具進行結構化。若要進一步了解如何利用工具的使用，請參閱 [搭配 Amazon Nova 使用工具 (函數呼叫)](tool-use.md)。


|  |  | 
| --- |--- |
| 使用者 | 從下列提供的查詢中，擷取相關實體<pre>Query: John works in BUILDING-0987<br />and has been in charge of product id 23564#.<br />His performance has been excellent in past<br />year and he is up for a raise.<br /><br />Use the print_entities tool.</pre> | 
| ToolConfig |  <pre>tool_config = {<br />    "tools": [<br />        {<br />            "toolSpec": {<br />                    "name": "print_entities",<br />                    "description": "Extract the named entity based on provided input",<br />                    "inputSchema": {<br />                        "type": "object",<br />                        "properties": {<br />                            "name": {<br />                               "type": "string",<br />                               "description": "The extracted entity name. This should be a name of a person, place, animal or thing"<br />                            },<br />                            "location": {<br />                               "type": "string",<br />                                "description": "The extracted location name. This is a site name or a building name like SITE-001 or BUILDING-003"<br />                            },<br />                            "product": {<br />                              "type": "string",<br />                              "description": "The extracted product code, this is generally a 6 digit alphanumeric code such as 45623#, 234567"<br />                            }<br />                        },<br />                        "required": ["name", "location", "product"]<br />                 }<br />            }<br />        }<br />    ],<br />    "toolChoice": {<br />        "tool": {<br />            "name": "print_entities"<br />        }<br />    }<br />}</pre>  | 