

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

# 在 JupyterLab 或 Studio Classic 中使用 Jupyter AI
<a name="sagemaker-jupyterai-use"></a>

您可以在 JupyterLab 或 Studio Classic 中使用 Jupyter AI，方法是從聊天 UI 或筆記本儲存格調用語言模型。下列各節提供完成此作業所需步驟的相關資訊。

## 從聊天 UI 使用語言模型
<a name="sagemaker-jupyterai-use-chatui"></a>

在聊天 UI 文字方塊中編寫您的訊息，以開始與您的模型互動。若要清除訊息歷程記錄，請使用 `/clear` 命令。

**注意**  
清除訊息歷程記錄不會消除與模型提供者的聊天內容。

## 從筆記本儲存格使用語言模型
<a name="sagemaker-jupyterai-use-magic-commands"></a>

使用 `%%ai` 和 `%ai` 命令調用語言模型之前，請在 JupyterLab 或 Studio Classic 筆記本儲存格中執行下列命令來載入 IPython 延伸模組。

```
%load_ext jupyter_ai_magics
```
+ **對於 託管的模型 AWS：**
  + 若要調用 SageMaker AI 中部署的模型，請使用下列必要參數將字串 `sagemaker-endpoint:{{endpoint-name}}` 傳遞至 `%%ai` 魔術命令，然後在下列幾行中新增您的提示。

    下表列出調用 SageMaker AI 或 Amazon Bedrock 託管的模型時的必要和選用參數。<a name="sagemaker-jupyterai-jumpstart-inference-params"></a>    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/sagemaker-jupyterai-use.html)

    下列命令會調用 SageMaker AI 託管的 [Llama2-7b](https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html) 模型。

    ```
    %%ai sagemaker-endpoint:jumpstart-dft-meta-textgeneration-llama-2-7b -q {"inputs":"<prompt>","parameters":{"max_new_tokens":64,"top_p":0.9,"temperature":0.6,"return_full_text":false}} -n us-east-2 -p [0].generation -m {"endpoint_kwargs":{"CustomAttributes":"accept_eula=true"}} -f text
    Translate English to French:
    sea otter => loutre de mer
    peppermint => menthe poivrée
    plush girafe => girafe peluche
    cheese =>
    ```

    下列範例會調用 SageMaker AI 託管的 Flan-t5-small 模型。

    ```
    %%ai sagemaker-endpoint:hf-text2text-flan-t5-small --request-schema={"inputs":"<prompt>","parameters":{"num_return_sequences":4}} --region-name=us-west-2 --response-path=[0]["generated_text"] -f text
    What is the atomic number of Hydrogen?
    ```
  + 若要調用 Amazon Bedrock 中部署的模型，請透過[用於調用 JumpStart 或 Amazon Bedrock 託管模型的參數](#sagemaker-jupyterai-jumpstart-inference-params)清單中定義的任何選用參數，將字串 `bedrock:{{model-name}}` 傳遞至 `%%ai` 魔術命令，然後在下列幾行中新增您的提示。

    下列範例會調用 Amazon Bedrock 託管的 [AI21 Labs Jurassic-2 模型](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html)。

    ```
    %%ai bedrock:ai21.j2-mid-v1 -m {"model_kwargs":{"maxTokens":256}} -f code
    Write a function in python implementing a bubbble sort.
    ```
+ **針對第三方提供者託管的模型**

  若要調用第三方供應商託管的模型，請使用選用的 [`Output format`](#sagemaker-jupyterai-output-format-params) 將字串 `{{provider-id}}:{{model-name}}` 傳遞至 `%%ai` 魔術命令，然後在下列幾行中新增您的提示。您可以在 Jupyter AI [模型提供者清單](https://jupyter-ai.readthedocs.io/en/latest/users/index.html#model-providers)中尋找每個提供者的詳細資訊，包括其 ID。

  下列命令會要求 Anthropic Claude 模型輸出 HTML 檔案，其中包含帶有黑色邊框的白色方塊影像。

  ```
  %%ai anthropic:claude-v1.2 -f html
  Create a square using SVG with a black border and white fill.
  ```