在 Amazon Bedrock 上使用 Hugging Face 模型

我們激動地宣佈,Hugging Face 的熱門開放模型現已在 Amazon Bedrock 新推出的 Bedrock Marketplace 上架!AWS 客戶現在可以透過 Bedrock Marketplace 部署 83 款開放模型,以構建他們的生成式 AI 應用程式。
在底層,Bedrock Marketplace 的模型端點由 Amazon SageMaker Jumpstart 管理。透過 Bedrock Marketplace,您現在可以將 SageMaker JumpStart 的易用性與 Amazon Bedrock 的完全託管基礎設施相結合,包括與 Agents、Knowledge Bases、Guardrails 和 Model Evaluations 等高階 API 的相容性。
在 Amazon Bedrock 中註冊您的 SageMaker Jumpstart 端點時,您只需支付 SageMaker 的計算資源費用,並適用常規的 Amazon Bedrock API 價格。
在這篇博文中,我們將向您展示如何部署 Gemma 2 27B Instruct 模型,並透過 Amazon Bedrock API 使用該模型。您將學習如何:
- 部署 Google Gemma 2 27B Instruct
- 使用 Amazon Bedrock API 傳送請求
- 清理
部署 Google Gemma 2 27B Instruct
有兩種方法可以部署用於 Amazon Bedrock 的開放模型:
- 您可以從 Bedrock 模型目錄中部署您的開放模型。
- 您可以使用 Amazon Jumpstart 部署您的開放模型,並將其註冊到 Bedrock。
兩種方法類似,因此我們將引導您透過 Bedrock 模型目錄進行操作。
首先,在 Amazon Bedrock 控制檯中,請確保您位於 Bedrock Marketplace 可用的 14 個區域之一。然後,在導航窗格的“基礎模型”部分選擇 “模型目錄”。在這裡,您可以搜尋無伺服器模型和 Amazon Bedrock Marketplace 中可用的模型。按“Hugging Face”提供商篩選結果,您可以瀏覽 83 款可用的開放模型。
例如,讓我們搜尋並選擇 Google Gemma 2 27B Instruct。
選擇模型後會開啟模型詳情頁面,您可以在其中看到更多來自模型提供商的資訊,例如模型亮點和使用方法,包括示例 API 呼叫。
在右上角,讓我們點選“部署”。
這會帶您到部署頁面,您可以在此選擇端點名稱、例項配置以及與網路配置和用於在 SageMaker 中執行部署的服務角色相關的高階設定。讓我們使用預設的高階設定和推薦的例項型別。
您還需要接受模型提供商的終端使用者許可協議。
在右下角,讓我們點選“部署”。
我們剛剛在您的 Amazon SageMaker 租戶中,在一臺 ml.g5.48xlarge 例項上啟動了 Google Gemma 2 27B Instruct 模型的部署,該模型與 Amazon Bedrock API 相容!
端點部署可能需要幾分鐘。它將出現在“Marketplace 部署”頁面上,您可以在導航窗格的“基礎模型”部分找到該頁面。
透過 Amazon Bedrock API 使用模型
您可以透過 UI 在 Playground 中快速測試模型。但是,要以程式設計方式使用任何 Amazon Bedrock API 呼叫已部署的模型,您需要獲取端點 ARN。
從託管部署列表中,選擇您的模型部署以複製其端點 ARN。
您可以使用您偏好的語言中的 AWS SDK 或使用 AWS CLI 來查詢您的端點。
以下是使用 AWS SDK for Python (boto3) 透過 Bedrock Converse API 的示例:
import boto3
bedrock_runtime = boto3.client("bedrock-runtime")
# Add your bedrock endpoint arn here.
endpoint_arn = "arn:aws:sagemaker:<AWS::REGION>:<AWS::AccountId>:endpoint/<Endpoint_Name>"
# Base inference parameters to use.
inference_config = {
"maxTokens": 256,
"temperature": 0.1,
"topP": 0.999,
}
# Additional inference parameters to use.
additional_model_fields = {"parameters": {"repetition_penalty": 0.9, "top_k": 250, "do_sample": True}}
response = bedrock_runtime.converse(
modelId=endpoint_arn,
messages=[
{
"role": "user",
"content": [
{
"text": "What is Amazon doing in the field of generative AI?",
},
]
},
],
inferenceConfig=inference_config,
additionalModelRequestFields=additional_model_fields,
)
print(response["output"]["message"]["content"][0]["text"])
"Amazon is making significant strides in the field of generative AI, applying it across various products and services. Here's a breakdown of their key initiatives:\n\n**1. Amazon Bedrock:**\n\n* This is their **fully managed service** that allows developers to build and scale generative AI applications using models from Amazon and other leading AI companies. \n* It offers access to foundational models like **Amazon Titan**, a family of large language models (LLMs) for text generation, and models from Cohere"
就是這樣!如果您想更進一步,請查閱 Bedrock 文件。
清理
實驗結束後,別忘了刪除您的端點,以停止產生費用!在您獲取端點 ARN 的頁面右上角,點選“刪除”即可刪除您的端點。