

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

# Mistral AI 聊天完成
<a name="model-parameters-mistral-chat-completion"></a>

Mistral AI 聊天完成 API 可建立對話式應用程式。

**提示**  
您可以使用 Mistral AI 聊天完成 API 搭配基本推論操作 ([InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 或 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html))。不過，我們建議您使用 Converse API 在應用程式中實作訊息。Converse API 提供一組統一的參數，可用於支援訊息的所有模型。如需詳細資訊，請參閱[使用 Converse API 進行推論](conversation-inference.md)。

Mistral AI 模型可在 [Apache 2.0 授權](https://www.apache.org/licenses/LICENSE-2.0.txt)下使用。如需使用 Mistral AI 模型的詳細資訊，請參閱 [Mistral AI 文件](https://docs.mistral.ai/)。

**Topics**
+ [支援的模型](#mistral-supported-models-chat-completion)
+ [請求與回應](#model-parameters-mistral-chat-completion-request-response)

## 支援的模型
<a name="mistral-supported-models-chat-completion"></a>

您可以使用下列 Mistral AI 模型。
+ Mistral Large

您需要您想要使用的模型的模型 ID。若要取得模型 ID，請參閱 [Amazon Bedrock 中支援的基礎模型](models-supported.md)。

## 請求與回應
<a name="model-parameters-mistral-chat-completion-request-response"></a>

------
#### [ Request ]

這些 Mistral AI 模型具有下列推論參數。

```
{
    "messages": [
        {
            "role": "system"|"user"|"assistant",
            "content": str
        },
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "id": str,
                    "function": {
                        "name": str,
                        "arguments": str
                    }
                }
            ]
        },
        {
            "role": "tool",
            "tool_call_id": str,
            "content": str
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": str,
                "description": str,
                "parameters": dict
            }
        }
    ],
    "tool_choice": "auto"|"any"|"none",
    "max_tokens": int,
    "top_p": float,
    "temperature": float
}
```

下列是必要的參數。
+  **messages** – (必要) 您要傳遞給模型的訊息。
  + **role** – 訊息的角色。有效的值如下：
    + **system** – 在對話中設定模型的行為和內容。
    + **user** – 要傳送給模型的訊息。
    + **assistant** – 來自模型的回應。
  + **content** – 訊息的內容。

  ```
  [
      {
          "role": "user",
          "content": "What is the most popular song on WZPZ?"
      }
  ]
  ```

  若要傳遞工具結果，請將 JSON 搭配下列欄位使用。
  + **role** – 訊息的角色。值必須為 `tool`。
  + **tool\_call\_id** – 工具請求的 ID。您可以從上一個請求其回應中的 `tool_calls` 欄位取得 ID。
  + **content** – 工具的結果。

  下列範例是可從廣播電台取得熱門歌曲的工具提供的結果。

  ```
  {
      "role": "tool",
      "tool_call_id": "v6RMMiRlT7ygYkT4uULjtg",
      "content": "{\"song\": \"Elemental Hotel\", \"artist\": \"8 Storey Hike\"}"
  }
  ```

以下是選用參數。
+  **tools** – 模型可使用之工具的定義。

  如果您在請求中包含 `tools`，模型可能會在訊息中傳回 `tool_calls` 欄位，代表模型使用這些工具。然後，您可以使用模型產生的工具輸入來執行這些工具，然後使用 `tool_result` 內容區塊選擇性地將結果傳回至模型。

  下列範例適用於在廣播電台上取得最熱門歌曲的工具。

  ```
  [
      {
          "type": "function",
          "function": {
              "name": "top_song",
              "description": "Get the most popular song played on a radio station.",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "sign": {
                          "type": "string",
                          "description": "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP."
                      }
                  },
                  "required": [
                      "sign"
                  ]
              }
          }
      }
  ]
  ```
+  **tool\_choice** – 指定函數的呼叫方式。如果設定為 `none`，則模型不會呼叫函數，而是產生訊息。如果設定為 `auto`，則模型可以選擇產生訊息或呼叫函數。如果設定為 `any`，則會強制模型呼叫函數。
+ **max\_tokens** – 指定產生的回應中使用的字符數目上限。一旦產生的文字超過 `max_tokens`，模型就會截斷回應。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-mistral-chat-completion.html)
+ **temperature** – 控制模型所做預測的隨機性。如需詳細資訊，請參閱[使用推論參數影響回應生成](inference-parameters.md)。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-mistral-chat-completion.html)
+ **top\_p** – 透過設定模型認為最有可能成為下一個字符的百分比，來控制模型產生的文字多樣性。如需詳細資訊，請參閱[使用推論參數影響回應生成](inference-parameters.md)。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-mistral-chat-completion.html)

------
#### [ Response ]

來自對 `InvokeModel` 的呼叫的 `body` 回應如下：

```
{
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": str,
                "tool_calls": [...]
            },
            "stop_reason": "stop"|"length"|"tool_calls"
        }
    ]
}
```

`body` 回應具有以下欄位：
+ **choices** – 來自模型欄位的輸出。
  + **index** – 訊息的索引。
  + **message** – 來自模型的訊息。
    + **role** – 訊息的角色。
    + **content** – 訊息的內容。
    + **tool\_calls** – 如果 `stop_reason` 的值為 `tool_calls`，則此欄位包含模型希望您執行之工具請求的清單。
      + **id** – 工具請求的 ID。
      + **function** – 模型正在請求的函數。
        + **name** – 函數的名稱。
        + **arguments** – 傳遞至工具的引數。

      下列範例請求適用於取得廣播電台最高排名歌曲的工具。

      ```
      [
                          {
                              "id": "v6RMMiRlT7ygYkT4uULjtg",
                              "function": {
                                  "name": "top_song",
                                  "arguments": "{\"sign\": \"WZPZ\"}"
                              }
                          }
                      ]
      ```
  + **stop\_reason** – 回應停止產生文字的原因。可能值為：
    + **stop** — 模型已完成產生輸入提示的文字。模型會停止，因為它不再有要產生的內容，或者如果模型產生您在 `stop` 請求參數中定義的其中一個停止序列。
    + **length** – 所產生文字的字符長度超過 `max_tokens` 的值。回應會截斷為 `max_tokens` 記號。
    + **tool\_calls** – 模型正在請求您執行工具。

------