一、前言
在大模型技术浪潮席卷全球的当下,AI 推理能力已成为企业数字化转型与开发者创新的核心驱动力。华为云顺势推出的 DeepSeek-V3/R1 商用大模型服务,凭借卓越性能脱颖而出。本文将手把手教你在华为云 ModelArts Studio 平台上开通服务,并深入分享真实使用体验,助你快速解锁高效 AI 应用开发新场景。
二、登录注册华为云账号
首先登录注册华为云账号并实名认证,之后进入 ModelArts Studio 平台,签署免责声明并完成服务授权。
三、开通DeepSeek-V3/R1商用服务
3.1 首先,点击https://www.huaweicloud.com/product/modelarts/studio.html,进入ModelArts Studio大模型即服务平台页面,如下图所示。
3.2 热门DeepSeek高效智能体模型任君选择
3.3 可以根据自身需要开发 DeepSeek-R1-32K 、DeepSeek-V3-32K 等相关服务
3.4 ✅ 开通成功后即可开始使用DeepSeek-V3/R1进行推理任务
四、 调用API接口说明教程
4.1 调用 API 接口
https://api.modelarts-maas.com/v1/chat/completions
DeepSeek-V3 模型
4.2 创建您自己的 API Key
https://console.huaweicloud.com/modelarts/?region=cn-southwest-2#/model-studio/authmanage
在项目中调用MaaS的模型服务时,需要填写您的API Key用于接口的鉴权认证
4.3 复制调用示例并替换接口信息、API Key
复制下方调用示例代码
替换其中的接口信息(API地址、模型名称)为上方接口信息
curl -X POST "https://api.modelarts-maas.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "DeepSeek-V3",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好"}
],
"stream": true,
"stream_options": { "include_usage": true },
"temperature": 0.6
}'
4.4 JAVA代码案例参考
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
public class ApiClient {
public static void main(String[] args) {
try {
// API配置
String apiUrl = "https://api.modelarts-maas.com/v1/chat/completions";
String apiKey = "yourApiKey"; // 替换为实际的API Key
// 禁用SSL证书验证
disableSslVerification();
// 创建URL对象
URL url = new URL(apiUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
// 设置请求方法和头信息
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
connection.setDoOutput(true);
// 构建请求体
String requestBody = "{" +
"\"model\":\"DeepSeek-V3\"," +
"\"messages\": [" +
"{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}," +
"{\"role\": \"user\", \"content\": \"你好\"}" +
"]," +
"\"stream\": true," +
"\"temperature\": 0.6" +
"}";
// 发送请求
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
// 获取响应状态
int statusCode = connection.getResponseCode();
System.out.println("Status Code: " + statusCode);
// 读取响应内容
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "utf-8"))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
// 处理流式响应,每次接收到一行数据就处理
if (!responseLine.trim().isEmpty()) {
System.out.println("Received: " + responseLine.trim());
}
}
System.out.println("Full Response: " + response.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 禁用SSL证书验证(仅用于开发测试环境)
private static void disableSslVerification() {
try {
// 创建信任所有证书的信任管理器
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// 安装信任管理器
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// 创建允许所有主机名的主机名验证器
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.5 Python代码案例参考
# coding=utf-8
import requests
import json
if __name__ == '__main__':
url = "https://api.modelarts-maas.com/v1/chat/completions" # API地址
api_key = "yourApiKey" # 把yourApiKey替换成已获取的API Key
# Send request.
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
"model":"DeepSeek-V3", # 模型名称
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好"}
],
# 是否开启流式推理, 默认为False, 表示不开启流式推理
"stream": True,
# 在流式输出时是否展示使用的token数目。只有当stream为True时改参数才会生效。
# "stream_options": { "include_usage": True },
# 控制采样随机性的浮点数,值较低时模型更具确定性,值较高时模型更具创造性。"0"表示贪婪取样。默认为0.6。
"temperature": 0.6
}
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
# Print result.
print(response.status_code)
print(response.text)
4.6 OpenAI SDK调用案例参考
# 安装环境命令。
pip install --upgrade "openai>=1.0"
4.7 OpenAI SDK调用示例。
from openai import OpenAI
if __name__ == '__main__':
base_url = "https://example.com/v1/infers/937cabe5-d673-47f1-9e7c-2b4de06******/v1"
api_key = "<your_apiKey>" # 把<your_apiKey>替换成已获取的API Key。
client = OpenAI(api_key=api_key, base_url=base_url)
response = client.chat.completions.create(
model="******",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
max_tokens=1024,
temperature=0.6,
stream=False
)
# Print result.
print(response.choices[0].message.content)
4.8 普通requests包、OpenAI SDK、curl命令的返回示例如下所示:
{
"id": "cmpl-29f7a172056541449eb1f9d31c*****",
"object": "chat.completion",
"created": 17231*****,
"model": "******",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!很高兴能为你提供帮助。有什么问题我可以回答或帮你解决吗?"
},
"logprobs": null,
"finish_reason": "stop",
"stop_reason": null
}
],
"usage": {
"prompt_tokens": 20,
"total_tokens": 38,
"completion_tokens": 18
}
}
模型需要通过在线推理提供服务,用于模型体验和调用,可通过监控面板查看运行状况
五、在线体验DeepSeek模型的快感
开通和配置完成后在线体验对应的模型,例如:DeepSeek-V3,在线体验方式如下图所示
六、REST API 与 OpenAI SDK 特性对比说明
补充说明
兼容性
REST API:基于 HTTP 协议,不依赖特定开发环境,适合需要跨平台调用(如移动端、嵌入式设备)的场景。
OpenAI SDK:针对 Python 优化,与 OpenAI 官方工具(如 Fine-tuning、Embeddings)深度集成,减少适配成本。
流式响应处理
REST API:需开发者自行处理数据分片(如 JSON 解析、断连重连),适合对流式逻辑有定制需求的场景。
OpenAI SDK:通过参数配置直接返回流式迭代器(如async for chunk in response),简化开发流程。
个人选型建议
七、最后总结
实际使用中,华为云与 DeepSeek 的结合表现出色。Flexus 系列云服务器凭借柔性计算技术,实现资源实时弹性分配,契合 DeepSeek-V3/R1 推理时的负载波动需求。DeepSeek-V3 以 6710 亿参数覆盖多模态任务,支持 128K 长上下文窗口;R1 在数学推理、代码生成等场景性能卓越,推理成本低。在 ModelArts Studio 平台上,昇腾 AI 处理器与 DeepSeek 模型深度适配,提升了推理速度与训练效率。通过 “模型即服务” 模式,用户可便捷调用预训练模型并享有全生命周期服务。此外,与 Dify -LLM 低代码开发平台协同,能快速构建智能应用。整体而言,服务开通流程顺畅,模型推理能力强大,在智能问答、代码生成等场景适配性佳,为开发者与企业提供了高效、强大的 AI 解决方案