

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

# 設定您的模型提供者
<a name="sagemaker-jupyterai-model-configuration"></a>

**注意**  
在本節中，我們假設已部署您計劃使用的語言和嵌入模型。對於 提供的模型 AWS，您應該已經擁有 SageMaker AI 端點的 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 的組態會有所不同，取決於您使用的是聊天 UI 還是魔術命令。

## 在聊天 UI 中設定模型提供者
<a name="sagemaker-jupyterai-model-configuration-chatui"></a>

**注意**  
您可以遵循相同指示來設定數個 LLM 和嵌入模型。不過，您必須設定至少一個**語言模型**。

**設定聊天 UI**

1. 在 JupyterLab 中，選擇左側導覽面板中的聊天圖示 (![Chat icon.](http://docs.aws.amazon.com/zh_tw/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_tw/sagemaker/latest/dg/images/studio/icons/jupyterai/jupyterai-configure-models.png))。這會開啟 Jupyter AI 組態面板。

1. 填寫與服務提供者相關的欄位。
   + **對於 JumpStart 或 Amazon Bedrock 提供的模型**
     + 在**語言模型**下拉式清單中，針對使用 JumpStart 部署的模型選取 `sagemaker-endpoint`，或針對 Amazon Bedrock 管理的模型選取 `bedrock`。
     + 這些參數會根據您的模型部署在 SageMaker AI 還是 Amazon Bedrock 上而有所不同。
       + 對於使用 JumpStart 部署的模型：
         + 在端點名稱中輸入**端點的名稱**，然後在[**區域名稱**](sagemaker-jupyterai-use.md#sagemaker-jupyterai-region-name)中部署模型 AWS 區域 的 。若要擷取 SageMaker AI 端點的 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_tw/sagemaker/latest/dg/images/studio/icons/jupyterai/jupyterai-return-to-chat.png))。這會開啟 Jupyter AI 聊天 UI。您可以開始與模型互動。
   + **針對第三方提供者託管的模型**
     + 在**語言模型**下拉式清單中，選取您的提供者 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 金鑰。
     + 選擇**儲存變更**，然後導覽至左窗格左上角的向左箭頭圖示 (![](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/studio/icons/jupyterai/jupyterai-return-to-chat.png))。這會開啟 Jupyter AI 聊天 UI。您可以開始與模型互動。

下列快照是聊天 UI 組態面板的圖例，此面板設定為調用 JumpStart 提供並部署在 SageMaker AI 中的 Flan-t5-small 模型。

![聊天 UI 組態面板設定為調用 JumpStart 提供的 Flan-t5-small 模型。](http://docs.aws.amazon.com/zh_tw/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 上的 [AI21 Labs 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 Bedrock 上的 [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 連接至您的網域或使用者設定檔，請在啟動您的 JupyterLab 應用程式時將您的 LCC 新增至您的空間。若要確保 LCC 已更新您的組態檔案，請在終端機中執行 `more ~/.jupyter/jupyter_jupyter_ai_config.json`。檔案的內容應該對應至傳遞至 LCC 的 JSON 檔案內容。

## 在筆記本中設定您的模型提供者
<a name="sagemaker-jupyterai-model-configuration-magic-commands"></a>

**使用 `%%ai` 和 `%ai` 魔術命令，在 JupyterLab 或 Studio Classic 筆記本內透過 Jupyter AI 調用模型**

1. 在筆記本環境中安裝模型提供者特有的用戶端程式庫。例如，使用 OpenAI 模型時，您需要安裝 `openai` 用戶端程式庫。您可以在 Jupyter AI [模型提供者清單](https://jupyter-ai.readthedocs.io/en/latest/users/index.html#model-providers)的 *Python 套件*欄中尋找每個提供者所需的用戶端程式庫清單。
**注意**  
對於 託管的模型 AWS， `boto3` 已安裝在 JupyterLab 所使用的 SageMaker AI 分佈映像中，或與 Studio Classic 搭配使用的任何資料科學映像中。

1. 
   + **對於 託管的模型 AWS**

     確定您的執行角色具有為 JumpStart 提供的模型調用 SageMaker AI 端點的許可，或者您可以存取 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
     ```