

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

# 配置模型提供程序
<a name="sagemaker-jupyterai-model-configuration"></a>

**注意**  
在本节中，我们假设您计划使用的语言和嵌入模型已经部署。对于提供的模型 AWS，您应该已经拥有 SageMaker 人工智能终端节点的 ARN 或者可以访问 Amazon Bedrock。对于其他模型提供程序，您应该使用 API 密钥进行身份验证以及授权向您的模型提出的请求。  
Jupyter AI 支持多种模型提供程序和语言模型，请参阅其[支持的模型](https://jupyter-ai.readthedocs.io/en/latest/users/index.html#model-providers)列表，了解最新的可用模型。有关如何部署提供的模型的信息 JumpStart，请参阅 JumpStart 文档[中的部署模型](https://docs.aws.amazon.com/sagemaker/latest/dg/jumpstart-deploy.html)。您需要申请访问 [Amazon Bedrock](https://aws.amazon.com/bedrock/) 才能将其用作您的模型提供程序。

Jupyter AI 的配置因使用聊天用户界面还是神奇命令而异。

## 在聊天用户界面中配置您的模型提供程序
<a name="sagemaker-jupyterai-model-configuration-chatui"></a>

**注意**  
您可以按照相同的说明配置多个模型 LLMs 和嵌入模型。但是，您必须配置至少一种**语言模型**。

**要配置聊天用户界面**

1. 在中 JupyterLab，通过选择左侧导航面板中的聊天图标 (![\[Icon of a rectangular shape with a curved arrow pointing to the upper right corner.\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/studio/icons/jupyterai/jupyterai-chat-ui.png)) 来访问聊天界面。

1. 选择左侧窗格右上角的配置图标 (![\[Gear or cog icon representing settings or configuration options.\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/studio/icons/jupyterai/jupyterai-configure-models.png))。这将打开 Jupyter AI 配置面板。

1. 填写与服务提供程序相关的字段。
   + **适用于 JumpStart 或 Amazon Bedrock 提供的型号**
     + 在**语言模型**下拉列表中，选择`sagemaker-endpoint`使用 Amazon Bedrock 部署的模型 JumpStart 或`bedrock`由 Amazon Bedrock 管理的模型。
     + 根据您的模型部署在 SageMaker AI 上还是 Amazon Bedrock 上，参数会有所不同。
       + 对于使用以下设备部署的模型 JumpStart：
         + 在终端节点名称中输入您的**终端节点的名称**，然后 AWS 区域 在[**区域名称中输入部署模型的名称**](sagemaker-jupyterai-use.md#sagemaker-jupyterai-region-name)。要检索 A SageMaker I 终端节点的 ARN，请导航到左侧菜单中的 “**推理[https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)**和**终端节点**”，然后选择该菜单。
         + 粘贴为您的模型量身定制的[**请求架构**](sagemaker-jupyterai-use.md#sagemaker-jupyterai-request-schema)的 JSON，以及解析模型输出的相应[**响应路径**](sagemaker-jupyterai-use.md#sagemaker-jupyterai-response-path)。
**注意**  
您可以在以下[示例笔记本](https://github.com/aws/amazon-sagemaker-examples/tree/main/introduction_to_amazon_algorithms/jumpstart-foundation-models)中找到各种 JumpStart 基础模型的请求和响应格式。每个笔记本都以其演示的模型命名。
       + 对于由 Amazon Bedrock 管理的模型：在系统上添加存储您的 AWS 凭证的 AWS 配置文件（可选），然后 AWS 区域 在[**区域名称**](sagemaker-jupyterai-use.md#sagemaker-jupyterai-region-name)中添加部署模型的配置文件。
     + （可选）选择您有权访问的[嵌入模型](sagemaker-jupyterai-overview.md#sagemaker-jupyterai-embedding-model)。嵌入模型用于从本地文档中捕获更多信息，从而使文本生成模型能够在这些文档的上下文中回答问题。
     + 选择**保存更改**，然后导航到左侧窗格左上角的向左箭头图标 (![\[Left-pointing arrow icon, typically used for navigation or returning to a previous page.\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/studio/icons/jupyterai/jupyterai-return-to-chat.png))。这将打开 Jupyter AI 聊天用户界面。您可以开始与模型交互了。
   + **对于由第三方提供商托管的模型**
     + 在**语言模型**下拉列表中，选择您的提供商 ID。您可以在 Jupyter AI [模型提供商列表](https://jupyter-ai.readthedocs.io/en/latest/users/index.html#model-providers)中找到每个提供商的详细信息，包括其 ID。
     + （可选）选择您有权访问的[嵌入模型](sagemaker-jupyterai-overview.md#sagemaker-jupyterai-embedding-model)。嵌入模型用于从本地文档中捕获更多信息，从而使文本生成模型能够在这些文档的上下文中回答问题。
     + 插入模型的 API 密钥。
     + 选择**保存更改**，然后导航到左侧窗格左上角的向左箭头图标 (![\[Left-pointing arrow icon, typically used for navigation or returning to a previous page.\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/studio/icons/jupyterai/jupyterai-return-to-chat.png))。这将打开 Jupyter AI 聊天用户界面。您可以开始与模型交互了。

以下快照说明了聊天界面配置面板，该面板设置为调用由 AI 提供 JumpStart 并部署在 AI 中的 Flan-t5-Small 模型。 SageMaker 

![\[聊天界面配置面板设置为调用由提供的 Flan-t5-Small 模型。 JumpStart\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/studio/jupyterai/jupyterai-chatui-configuration.png)


### 将额外的模型参数和自定义参数传递给您的请求
<a name="sagemaker-jupyterai-configuration-model-parameters"></a>

您的模型可能需要额外的参数，例如用于用户协议批准的自定义属性，或调整其他模型参数（如温度或响应长度）。我们建议使用生命周期配置将这些设置配置为 JupyterLab应用程序的启动选项。有关如何创建生命周期配置并将其附加到您的域或从 [SageMaker AI 控制台](https://console.aws.amazon.com/sagemaker/)附加到用户配置文件的信息，请参阅[创建和关联生命周期配置](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-lcc.html)。在为 JupyterLab 应用程序创建空间时，您可以选择 LCC 脚本。

使用以下 JSON 架构配置[额外参数](sagemaker-jupyterai-use.md#sagemaker-jupyterai-extra-model-params)：

```
{
  "AiExtension": {
    "model_parameters": {
      "<provider_id>:<model_id>": { Dictionary of model parameters which is unpacked and passed as-is to the provider.}
      }
    }
  }
}
```

以下脚本是 JSON 配置文件的示例，您可以在创建 JupyterLab 应用程序 LCC 时使用该文件来设置[部署在 Amazon Bedrock 上的 L AI21 abs Jurassic-2 模型](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html)的最大长度。增加模型生成响应的长度可以防止模型响应的系统截断。

```
#!/bin/bash
set -eux

mkdir -p /home/sagemaker-user/.jupyter

json='{"AiExtension": {"model_parameters": {"bedrock:ai21.j2-mid-v1": {"model_kwargs": {"maxTokens": 200}}}}}'
# equivalent to %%ai bedrock:ai21.j2-mid-v1 -m {"model_kwargs":{"maxTokens":200}}

# File path
file_path="/home/sagemaker-user/.jupyter/jupyter_jupyter_ai_config.json"

#jupyter --paths

# Write JSON to file
echo "$json" > "$file_path"

# Confirmation message
echo "JSON written to $file_path"

restart-jupyter-server

# Waiting for 30 seconds to make sure the Jupyter Server is up and running
sleep 30
```

以下脚本是一个用于创建 JupyterLab 应用程序 LCC 的 JSON 配置文件示例，该文件用于为部署在 Amazon Bed [rock 上的 Anthropic Claude 模型](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html)设置其他模型参数。

```
#!/bin/bash
set -eux

mkdir -p /home/sagemaker-user/.jupyter

json='{"AiExtension": {"model_parameters": {"bedrock:anthropic.claude-v2":{"model_kwargs":{"temperature":0.1,"top_p":0.5,"top_k":25
0,"max_tokens_to_sample":2}}}}}'
# equivalent to %%ai bedrock:anthropic.claude-v2 -m {"model_kwargs":{"temperature":0.1,"top_p":0.5,"top_k":250,"max_tokens_to_sample":2000}}

# File path
file_path="/home/sagemaker-user/.jupyter/jupyter_jupyter_ai_config.json"

#jupyter --paths

# Write JSON to file
echo "$json" > "$file_path"

# Confirmation message
echo "JSON written to $file_path"

restart-jupyter-server

# Waiting for 30 seconds to make sure the Jupyter Server is up and running
sleep 30
```

将 LCC 附加到域名或用户个人资料后，请在启动应用程序时将您的 LCC 添加到您的 JupyterLab 空间。要确保 LCC 更新您的配置文件，请在终端中运行 `more ~/.jupyter/jupyter_jupyter_ai_config.json`。文件内容应与传递给 LCC 的 JSON 文件内容一致。

## 在笔记本中配置您的模型提供程序
<a name="sagemaker-jupyterai-model-configuration-magic-commands"></a>

**使用和魔法命令通过 Studio Classic 笔记本电脑 JupyterLab 中的 Jupyter AI 调用模型 `%%ai` `%ai`**

1. 在您的笔记本环境中安装特定于您的模型提供程序的客户端库。例如，在使用 OpenAI 模型时，您需要安装 `openai` 客户端库。您可以在 Jupyter AI *模型提供程序列表*的 [Python 软件包](https://jupyter-ai.readthedocs.io/en/latest/users/index.html#model-providers)列中找到每个提供程序所需的客户端库列表。
**注意**  
对于托管的模型 AWS，已安装在使用`boto3`的 SageMaker AI 分发映像中 JupyterLab，或者安装在 Studio Classic 中使用的任何数据科学映像中。

1. 
   + **对于托管的模特 AWS**

     确保您的执行角色有权为由 Amazon Bedrock 提供的模型调用您的 SageMaker AI 终端节点， JumpStart 或者您有权访问 Amazon Bedrock。
   + **对于由第三方提供商托管的模型**

     使用环境变量在笔记本环境中导出提供程序的 API 密钥。您可以使用以下神奇命令。将命令中的 `provider_API_key` 替换为在 Jupyter AI *模型提供商列表*的环境变量栏中为您的提供程序找到的[环境变量](https://jupyter-ai.readthedocs.io/en/latest/users/index.html#model-providers)。

     ```
     %env provider_API_key=your_API_key
     ```