

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

# 使用工具
<a name="speech-tools-use"></a>

**注意**  
本文件適用於 Amazon Nova 第 1 版。如需 Amazon Nova 2 Sonic 指南，請造訪[工具組態](https://docs.aws.amazon.com/nova/latest/nova2-userguide/sonic-tool-configuration.html)。

若要使用工具，必須在工作階段組態中將其定義為 `promptStart` 事件的一部分。下列程式碼中示範了這一操作：

```
{
  "event": {
    "promptStart": {
      "promptName": "string",
      "textOutputConfiguration": {
        "mediaType": "text/plain"
      },
      "audioOutputConfiguration": {
        "mediaType": "audio/lpcm",
        "sampleRateHertz": 8000 | 16000 | 24000,
        "sampleSizeBits": 16,
        "channelCount": 1,
        "voiceId": "matthew" | "tiffany" | "amy",
        "encoding": "base64",
        "audioType": "SPEECH"
      },
      "toolUseOutputConfiguration": {
        "mediaType": "application/json"
      },
      "toolConfiguration": {
        "tools": [
          {
            "toolSpec": {
              "name": "string",
              "description": "string",
              "inputSchema": {
                "json": "{}"
              }
            }
          }
        ]
      }
    }
  }
}
```

## 工具定義元件
<a name="speech-tools-definition"></a>

每個工具規格都需要下列元素：
+ **名稱** - 工具的唯一識別符。
+ **描述** - 說明工具的功能和使用時機。
+ **輸入結構描述** - 定義必填參數的 JSON 結構描述。

## 基本工具範例
<a name="speech-tools-example"></a>

以下是擷取目前日期相關資訊的簡易工具範例。如需如何定義工具的詳細資訊，請參閱[定義工具](https://docs.aws.amazon.com/nova/latest/userguide/tool-use-definition.html)。

```
// A simple tool with no required parameters
const dateTool = {
  toolSpec: {
    name: "getDateTool",
    description: "Get information about the current date",
    inputSchema: {
      json: JSON.stringify({
        type: "object",
        properties: {},
        required: []
      })
    }
  }
};
```

以下是 `promptStart` 事件會呈現的樣子：

```
{
  event: {
    promptStart: {
      promptName: "string",
      textOutputConfiguration: {
        mediaType: "text/plain"
      },
      audioOutputConfiguration: {
        mediaType: "audio/lpcm",
        sampleRateHertz: 24000,
        sampleSizeBits: 16,
        channelCount: 1,
        voiceId: "tiffany",
        encoding: "base64",
        audioType: "SPEECH"
      },
      toolUseOutputConfiguration: {
        mediaType: "application/json"
      },
      toolConfiguration: {
        tools: [
          {
            toolSpec: {
              name: "getDateTool",
              description: "get information about the current date",
              inputSchema: {
                json: JSON.stringify({
                  type: "object",
                  properties: {},
                  required: []
                })
              }
            }
          }
        ]
      }
    }
  }
}
```