

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 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 模型的更多信息，请参阅 [https://docs.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_cn/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_cn/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_cn/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** – 模型请求您运行一个工具。

------