作者:陈子康,Apache Dubbo Contributor
2025年1月20日,国产大模型公司深度求索(DeepSeek)正式发布了大语言模型 DeepSeek-R1,并同步开源其模型权重。通过大规模强化学习技术,DeepSeek-R1 显著提升了推理能力,性能媲美顶尖闭源产品,迅速引发全球关注。用户量以惊人的速度飙升,DeepSeek App 在苹果应用商店中美、英等 157 个国家登顶下载榜,日活量快速突破 2000 万,成为全球增长最快的 APP。
Apache Dubbo 作为一款易用且高性能的分布式服务框架,被广泛应用于构建企业级微服务架构,拥有庞大的用户群体。Dubbo 的多语言生态也十分丰富,支持 Java、Golang、Python、Rust、Node.js 等多种编程语言,为开发者提供了极大的灵活性。
本文将深入探讨如何利用 Dubbo 的多语言 SDK,从大模型的原生部署到将其无缝接入现有业务系统,全面释放 DeepSeek-R1 的潜力,助力 AI 开发的高效落地。
为什么选择本地部署 DeepSeek R1?
- 目前 DeepSeek 日活量巨大,时常出现“服务器繁忙”的情况。本地部署可以有效避免因服务器负载过高导致的响应延迟或服务中断,确保业务连续性。
- 本地离线部署能够避免敏感数据上传至云端,降低数据泄露风险,特别适合对数据安全要求较高的个人开发者和企业使用。
- 本地部署允许用户根据自身需求对 DeepSeek R1 模型进行微调或二次开发,满足特定业务场景的需求。
- ……
为什么要原生部署 DeepSeek R1?
这里的原生部署指的是使用 DeepSeek 官方仓库 [ 1] 推荐的部署方式进行部署。
在目前关于 DeepSeek 部署的热门文章中,ollama 被广泛推荐为一种快速、轻量的部署工具。对于个人开发者或尝鲜者而言,ollama 的便捷性确实足够。然而,对于大模型开发人员和企业用户来说,ollama 的灵活性和性能存在明显不足。原生部署的核心优势如下:
- 灵活性与可扩展性: 原生部署支持对 DeepSeek R1 模型进行任意二次开发,用户可以根据业务需求深度定制模型功能。相比之下,ollama 仅支持有限的微调操作,无法满足复杂的开发需求。
- 全精度模型支持: ollama 目前尚不支持全精度的 DeepSeek R1 模型,这可能导致模型性能的损失。而原生部署无此限制,能够充分发挥模型的全部潜力,确保推理精度和效果。
通过原生部署,开发者可以更好地掌控模型的运行环境,优化性能,并为后续的模型迭代和业务集成奠定坚实基础。
原生部署 DeepSeek R1
说明: 由于硬件条件限制,本文将使用 DeepSeek R1 的蒸馏版本进行演示,但整体流程适用于其他模型和推理框架。如果需要采用 Docker 等容器化方式部署,请参考 NVIDIA 容器工具包 [ 3] 文档进行相关配置。
基础环境
----------
操作系统:Ubuntu 22.04.5
Python 版本:3.11.10
PyTorch 版本:2.5.1
----------
模型下载
使用 modelscope 提供的 snapshot_download 函数下载模型。其中,第一个参数为模型名称,cache_dir 参数指定模型的下载路径。
from modelscope import snapshot_download
model_dir = snapshot_download('deepseek-ai/DeepSeek-R1-Distill-Qwen-7B', cache_dir='/home/dubbo/model', revision='master')
代码编写
# the path of a model. It could be one of the following options:
# 1. A local directory path of a turbomind model
# 2. The model_id of a lmdeploy-quantized model
# 3. The model_id of a model hosted inside a model repository
model = 'deepseek-ai/DeepSeek-R1-Distill-Qwen-7B'
backend_config = TurbomindEngineConfig(
cache_max_entry_count=0.2,
max_context_token_num=20544,
session_len=20544
)
gen_config = GenerationConfig(
top_p=0.95,
temperature=0.6,
max_new_tokens=8192,
stop_token_ids=[151329, 151336, 151338],
do_sample=True # enable sampling
)
class DeepSeekAiServicer:
def __init__(self, model: str, backend_config: TurbomindEngineConfig, gen_config: GenerationConfig):
self.llm = pipeline(model, backend_config=backend_config)
self.gen_config = gen_config
def chat(self, content):
# According to DeepSeek's official recommendations,
# Each prompt needs to end with <think>\n
# If it is a mathematical reasoning content, it is recommended to include (in both Chinese and English):
# Please reason step by step, and put your final answer within \boxed{}.
prompts = [{
"role": "user",
"content": "What is the meaning of life?<think>\n"
}]
# Response Example:
# "<think> The meaning of life is to be happy. </think> I think the meaning of life is to be happy."
response = self.llm(prompts, gen_config=self.gen_config)
return response
这样一来,我们就能快速构建、部署和运行 DeepSeek R1,并且还能根据需求灵活地进行二次开发!
利用 Dubbo 多语言 SDK 无成本接入现有业务
使用 Python 对 DeepSeek R1 进行原生部署,使 AI 开发者能够更深入地进行二次开发,最大限度地发挥 DeepSeek R1 的全部潜力。然而,对于国内企业级开发来说,极少有企业会选择 Python 作为业务后端开发语言,更多的是选择性能更佳、Web 生态更成熟的 Java 或 Golang。因此,如何兼顾 AI 模型的开发、部署与现有业务平台的无缝对接,成为企业面临的一大挑战。
近期,Dubbo-Python 发布了 3.0.0b1 [ 4] 版本,支持 Dubbo 3 的核心基础功能。更重要的是,Dubbo-Python 实现了与 Dubbo-Java 的跨语言调用。此功能有效解决了 AI 服务接入现有业务系统的难题。
此外,使用 Dubbo 进行 AI 开发还有以下优点:
- 无学习、无接入成本: 大多数推理框架实现的在线服务通常采用 OpenAI 风格或与 Web 框架(如 FastAPI)结合使用。而 Dubbo 提供的跨语言调用方式与同语言直接调用无异,因此用户无需重新学习或适配,便可无缝接入现有系统。
- 流式 RPC 调用,支持流式推理: Dubbo 提供丰富且灵活的 RPC 调用方式,包括请求-响应和流式调用。通过流式 RPC 调用结合推理框架的流式推理功能,用户可以在推理过程中实时接收部分结果,避免等待完整推理完成后的长时间延迟,同时规避大文本响应可能引发的一系列风险和问题。
DeepSeek R1 部署(Python 侧)
使用 Dubbo-Python 为 DeepSeek R1 提供服务暴露能力,其模型部署和调用方式与前述代码基本相同,只是将模型推理改为了流式推理,以更好地支持实时响应和大文本输出。
具体代码如下:
"""
The code related to the configuration is the same as above.
"""
class DeepSeekAiServicer:
def __init__(self, model: str, backend_config: TurbomindEngineConfig, gen_config: GenerationConfig):
self.llm = pipeline(model, backend_config=backend_config)
self.gen_config = gen_config
def chat(self, stream):
# read request from stream
request = stream.read()
print(f"Received request: {request}")
# prepare prompts
prompts = [{
"role": request.role,
"content": request.content + "<think>\n"
}]
is_think = False
# perform streaming inference
for item in self.llm.stream_infer(prompts, gen_config=gen_config):
# update think status
if item.text == "<think>":
is_think = True
continue
elif item.text == "</think>":
is_think = False
continue
# According to the state of thought, decide the content of the reply.
if is_think:
# send thought
stream.write(chat_pb2.ChatReply(think=item.text, answer=""))
else:
# send answer
stream.write(chat_pb2.ChatReply(think="", answer=item.text))
stream.done_writing()
def build_server_handler():
# build a method handler
deepseek_ai_servicer = DeepSeekAiServicer(model, backend_config, gen_config)
method_handler = RpcMethodHandler.server_stream(
deepseek_ai_servicer.chat,
method_name="chat",
request_deserializer=chat_pb2.ChatRequest.FromString,
response_serializer=chat_pb2.ChatReply.SerializeToString,
)
# build a service handler
service_handler = RpcServiceHandler(
service_name="org.apache.dubbo.samples.llm.api.DeepSeekAiService",
method_handlers=[method_handler],
)
return service_handler
if __name__ == '__main__':
# build a service handler
service_handler = build_server_handler()
service_config = ServiceConfig(service_handler=service_handler)
# Configure the Zookeeper registry
registry_config = RegistryConfig.from_url("zookeeper://zookeeper:2181")
bootstrap = Dubbo(registry_config=registry_config)
# Create and start the server
bootstrap.create_server(service_config).start()
# 30days
sleep(30 * 24 * 60 * 60)
业务接入(Java 侧)
对于现有的业务平台,无需进行复杂的改动,只需构建一个 Consumer 即可完成服务调用,实现无缝对接。
@Component
public class Consumer implements CommandLineRunner {
@DubboReference
private DeepSeekAiService deepSeekAiService;
@Override
public void run(String... args) throws Exception {
ChatRequest request = ChatRequest.newBuilder()
.setRole("user")
.setContent("你好,你是谁?你能帮我制定一个dubbo学习计划嘛?")
.build();
deepSeekAiService.chat(request, new StreamObserver<ChatReply>() {
private boolean isThinkPrinted = false;
private boolean isAnswerPrinted = false;
@Override
public void onNext(ChatReply value) {
printSection("Think", value.getThink(), isThinkPrinted);
printSection("Answer", value.getAnswer(), isAnswerPrinted);
}
@Override
public void onError(Throwable t) {
System.err.println("Error received: " + t.getMessage());
t.printStackTrace();
}
@Override
public void onCompleted() {
System.out.println("------------ Chat Completed ------------");
}
private void printSection(String label, String content, boolean isPrinted) {
if (!content.isEmpty()) {
if (!isPrinted) {
System.out.println("------------- " + label + " -------------");
if (Objects.equals("Think", label)){
isThinkPrinted = true;
} else {
isAnswerPrinted = true;
}
}
System.out.print(content);
}
}
});
}
}
总结
本文介绍了如何利用 Apache Dubbo 的多语言 SDK 充分释放 DeepSeek R1 的全部潜力,助力 AI 开发的高效落地。
通过本地部署和原生部署 DeepSeek R1,开发者能够根据业务需求灵活进行模型微调和二次开发,确保业务连续性,同时降低数据泄露风险。
通过 Dubbo 的多语言支持,开发者可以轻松将 DeepSeek R1 接入现有业务系统,实现无缝对接。Dubbo 的流式 RPC 调用功能进一步提升了推理过程的实时响应能力,避免了长时间延迟和大文本响应带来的问题。
总的来说,结合 Dubbo 的多语言 SDK 和 DeepSeek R1 的强大推理能力,开发者可以在保证高性能和高安全性的同时,灵活地进行模型定制和业务集成,为 AI 开发的高效落地提供了强有力的支持。
如果您对 Dubbo 的使用有任何疑问或需要进一步的技术支持,欢迎联系 Dubbo 社区。
Dubbo 社区联系方式:
- 官网:https://dubbo.apache.org
- GitHub:https://github.com/apache/dubbo
- Dubbo Python:https://github.com/apache/dubbo-python
- 社区交流钉钉群号:105120013829
相关链接:
[1] DeepSeek官方仓库
https://github.com/deepseek-ai
[2] LMDeploy
https://github.com/InternLM/lmdeploy
[3] NVIDIA 容器工具包
https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/index.html
[4] 3.0.0b1
https://github.com/apache/dubbo-python/releases/tag/3.0.0b1