在本系列的上篇中,我們介紹了如何通過Amazon Bedrock部署并測試使用了DeepSeek模型。在接下來的下篇中小李哥將繼續介紹,如何利用亞馬遜的AI模型訓練平臺SageMaker AI中的,Amazon Sagemaker JumpStart通過腳本輕松一鍵式部署DeepSeek預訓練模型。
使用SageMaker JumpStart部署DeepSeek-R1
SageMaker JumpStart是一個包含基礎模型(FM)、內置算法和預構建機器學習模型解決方案的便捷功能,我們只需點擊幾下即可完成我們想要模型的部署。通過SageMaker JumpStart,我們還可以使用自己的數據自定義預訓練模型,并通過UI或SDK將其部署到生產環境中。
在亞馬遜云科技上,有兩種便捷的方法通過SageMaker JumpStart部署DeepSeek-R1模型:分別是使用直觀的SageMaker JumpStart UI或通過SageMaker Python SDK進行編程部署。在本篇中我們就將介紹這兩種方法,幫助大家選擇最適合自己需求的部署方式。
通過SageMaker JumpStart UI部署DeepSeek-R1
大家需要按照以下步驟使用SageMaker JumpStart UI部署DeepSeek-R1:
1. 在SageMaker控制臺中,選擇左側導航欄的 Studio。
2. 注意首次使用SageMaker的用戶,需要先創建一個域環境才能開始正常使用這個功能。
3. 在SageMaker Studio控制臺中,選擇左側導航欄的JumpStart。
進入JumpStart后,主頁將顯示所有可用的模型,并提供供應商名稱和模型功能等詳細信息。
4. 接下來大家搜索DeepSeek-R1,進入后查看DeepSeek-R1模型卡。其中每個模型卡片都會顯示模型關鍵信息,包括:
模型名稱:DeepSeek-R1
供應商名稱: DeepSeek
任務類別(例如文本生成)
Bedrock Ready標識,表示該模型可在Amazon Bedrock中加載使用,并可使用Amazon Bedrock API進行調用
5. 接下來,我們選擇模型卡片進入模型詳情頁面。
模型卡片詳情頁面包括以下信息:
- 模型名稱和供應商信息
- "Deploy" 按鈕,點擊部署模型
- "About" 和 "Notebooks" 選項卡,點擊進入可以看到詳細信息
"About" 選項卡中包括重要信息:
- 模型描述
- 許可信息
- 技術規格
- 使用指南
在部署模型之前,各位開發者一定要先閱讀模型詳情和許可條款,以確保其與大家的使用場景兼容,同時保證大家擁有使用的授權。
6. 接下來選擇Deploy繼續部署。
7. 配置部署選項:選擇合適的實例類型和數量對于優化成本和提升模型的推理性能至關重要。我們可以在部署后實時監控模型運行情況,并根據我們的業務需求調整這些算力設置。
8. 填入Endpoint name,我們可以使用自動生成的名稱或創建自定義名稱。
9. 選擇Instance type,選擇實例類型(默認:ml.p5e.48xlarge
)。
10. 選擇Initial instance count:我們在這里輸入實例數量(默認:1)。
11. 選擇大模型的推理模式Inference type,SageMaker默認選擇實時推理(Real-time inference),該模式對實時流量流和延遲都進行了優化。
12. 最后我們仔細檢查所有配置是否正確。對于DeepSeek模型,小李哥建議遵循SageMaker JumpStart的默認設置,并確保網絡隔離(network isolation) 保持啟用狀態,保證大模型環境不能被外界公網訪問,保證數據的安全。
13. 點擊Deploy就完成部署模型了,部署過程通常長袖幾分鐘。
當部署完成后,我們的模型端點狀態將更改為InService。此時模型端點已準備好接收api推理請求調用。我們可以在SageMaker控制臺的Endpoints頁面中監控實時API調用情況,該頁面會顯示相關指標和狀態信息。完成部署后,我們可以使用SageMaker SDK提供的API,通過代碼調用模型,并將其集成到大家的應用程序中。
使用 SageMaker Python SDK 部署 DeepSeek-R1
要通過SageMaker Python SDK,以代碼形式使用DeepSeek-R1,我們需要先安裝SageMaker Python SDK - Boto3,并確保我們具備必要的AWS權限和環境變量設置。以下是一個通過API調用DeepSeek的代碼示例,展示了如何以編程方式部署DeepSeek-R1并進行推理。
部署模型的代碼已在亞馬遜云科技GitHub倉庫上線。我們可以克隆該Notebook并在SageMaker Studio中運行。
!pip install --force-reinstall --no-cache-dir sagemaker==2.235.2from sagemaker.serve.builder.model_builder import ModelBuilder
from sagemaker.serve.builder.schema_builder import SchemaBuilder
from sagemaker.jumpstart.model import ModelAccessConfig
from sagemaker.session import Session
import logging sagemaker_session = Session()artifacts_bucket_name = sagemaker_session.default_bucket()
execution_role_arn = sagemaker_session.get_caller_identity_arn()js_model_id = "deepseek-llm-r1"gpu_instance_type = "ml.p5e.48xlarge"response = "Hello, I'm a language model, and I'm here to help you with your English."sample_input = {"inputs": "Hello, I'm a language model,","parameters": {"max_new_tokens": 128, "top_p": 0.9, "temperature": 0.6},}sample_output = [{"generated_text": response}]schema_builder = SchemaBuilder(sample_input, sample_output)model_builder = ModelBuilder( model=js_model_id, schema_builder=schema_builder, sagemaker_session=sagemaker_session, role_arn=execution_role_arn, log_level=logging.ERROR ) model= model_builder.build() predictor = model.deploy(model_access_configs={js_model_id:ModelAccessConfig(accept_eula=True)}, accept_eula=True) predictor.predict(sample_input)
接下來的代碼是通過代碼形式調用該端點生成推理的代碼段
new_input = {"inputs": "What is Amazon doing in Generative AI?","parameters": {"max_new_tokens": 64, "top_p": 0.8, "temperature": 0.7},
}prediction = predictor.predict(new_input)
print(prediction)
?
加載安全過濾器Guardrails并利用DeekSeek運行推理
與Amazon Bedrock相同,我們也可以使用ApplyGuardrail API保護我們的SageMaker JumpStart中模型的推理過程。可以通過Amazon Bedrock控制臺或API創建 Guardrail,并按照以下代碼示例保護推理過程:
# Get the response from the modelmodel_response = json.loads(response['Body'].read().decode())# Apply guardrail to outputoutput_guardrail_response = bedrock_runtime.apply_guardrail(guardrailIdentifier=guardrail_id,guardrailVersion=guardrail_version,source='OUTPUT',content=[{ "text": { "text": model_response['generated_text'] }}])# Check if output passes guardrailsif output_guardrail_response['action'] != 'GUARDRAIL_INTERVENED':print(model_response['generated_text'])else:print("Output blocked: ", output_guardrail_response['outputs'][0]['text'])
else:print("Input blocked: ", input_guardrail_response['outputs'][0]['text'])
以上就是全部的在亞馬遜云科技上部署、測試、安全地通過API調用DeekSeek-R1模型的全部步驟。歡迎大家持續關注小李哥分享的國際前沿的云平臺AI解決方案,關注小李哥不要錯過未來更多精彩內容。