在 JupyterLab 或 Studio Classic 中使用 Jupyter AI - Amazon SageMaker AI

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

在 JupyterLab 或 Studio Classic 中使用 Jupyter AI

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

從聊天 UI 使用語言模型

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

注意

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

從筆記本儲存格使用語言模型

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

%load_ext jupyter_ai_magics
  • 對於 託管的模型AWS:

    • 若要調用 SageMaker AI 中部署的模型,請使用下列必要參數將字串 sagemaker-endpoint:endpoint-name 傳遞至 %%ai 魔術命令,然後在下列幾行中新增您的提示。

      下表列出調用 SageMaker AI 或 Amazon Bedrock 託管的模型時的必要和選用參數。

      參數名稱 Parameter (參數) 簡短版本 Description
      請求結構描述 --request-schema -q 必要:端點預期的 JSON 物件,其中提示會替換為符合字串常值 <prompt> 的任何值。
      區域名稱 --region-name -n 必要:部署模型AWS 區域的 。
      回應路徑 --response-path -p 必要:用來從端點的 JSON 回應擷取語言模型輸出的 JSONPath 字串。
      額外模型參數 --model-parameters -m 選用:指定要傳遞至模型之其他參數的 JSON 值。接受的值會剖析為字典、解壓縮,並直接傳遞至提供者類別。當端點或模型需要自訂參數時,這很有用。例如,在 Llama 2 模型中,當需要接受最終使用者授權合約 (EULA) 時,您可以使用 -m {"endpoint_kwargs":{"CustomAttributes":"accept_eula=true"}} 將 EULA 認可傳遞至端點。或者,您可以使用 -m 參數傳遞額外的模型參數,例如為模型產生的回應設定字符數量上限。例如,使用 AI21 Labs Jurassic 模型時:-m {"model_kwargs":{"maxTokens":256}}
      輸出格式 --format -f 選用:用來轉譯輸出的 IPython 顯示內容。它可以是下列任何值 [code|html|image|json|markdown|math|md|text],前提是調用模型支援指定的格式。

      下列命令會調用 SageMaker AI 託管的 Llama2-7b 模型。

      %%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 託管模型的參數清單中定義的任何選用參數,將字串 bedrock:model-name 傳遞至 %%ai 魔術命令,然後在下列幾行中新增您的提示。

      下列範例會調用 Amazon Bedrock 託管的 AI21 Labs Jurassic-2 模型

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

    若要調用第三方供應商託管的模型,請使用選用的 Output format 將字串 provider-id:model-name 傳遞至 %%ai 魔術命令,然後在下列幾行中新增您的提示。您可以在 Jupyter AI 模型提供者清單中尋找每個提供者的詳細資訊,包括其 ID。

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

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