望宸&刘军 2025-01-29 14:09 辽宁
如何基于开源工具部署大模型、构建测试应用、调用大模型能力的完整链路。
1月20日,DeepSeek R1 发布,在数学、代码、自然语言推理等任务上,性能比肩 OpenAI o1 正式版。
1月27日,Qwen2.5-1M:支持 100万 Token 上下文,其中 14B 的杯型在短文本任务上实现了和 GPT-4o-mini 相近的性能,同时上下文长度是 GPT-4o-mini 的八倍;长上下文任务在多个数据集上稳定超越 GPT-4o-mini。
1月20日,DeepSeek Janus-Pro 发布,多模态理解和生成模型,其中 7B 的杯型在 GenEval 和 DPG-Bench 基准测试中超过 OpenAI 的 DALL-E 3 和 Stable Diffusion。
1月28日,Qwen2.5-VL 发布,视觉语言模型,在文档理解、视觉问答、视频理解和视觉 Agent 等维度的多项指标超过 GPT-4o。
业内开始出现一种声音,开源 LLM 不再仅仅是闭源模型的追随者,而是开始主导 AI 发展的方向,而 DeepSeek 和 Qwen 是目前领跑的开源项目。本文将介绍如何基于开源工具部署大模型、构建测试应用、调用大模型能力的完整链路。
一、为什么选择 PC 或手机在本地部署?
二、为什么要选择 DeepSeek R1 蒸馏版?
三、本地部署 DeepSeek 蒸馏版和 Qwen2.5
# 安装Ollama
curl -fsSL https://ollama.com/install.sh | sh
# 运行DeepSeek蒸馏模型
ollama run deepseek-r1:7b
四、Spring AI Alibaba 创建应用,调用服务
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>1.0.0-M5</version>
</dependency>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
注入 ChatClient:
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
public String chat(String input) {
return this.chatClient.prompt()
.user(input)
.call()
.content();
}
}
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=deepseek-r1:7b
五、进阶玩法:生产环境
curl -sS https://higress.cn/ai-gateway/install.sh | bash
执行完命令后可以通过命令行初始化配置,可以看到,Higress 的 AI 网关能力支持对接国内外所有主流 LLM 模型供应商:
import json
from openai import OpenAI
client = OpenAI(
api_key=xxxxx, # 👉 可以通过Higress生成消费者Key实现API key的二次分租
base_url="http://127.0.0.1:8080/v1"
)
completion = client.chat.completions.create(
# model="qwen-max",
# model="gemini-1.5-pro",
model="deepseek-chat", # 👉 可以填写任意模型名称,Higress根据模型名称路由到对应供应商
messages=[
{"role": "user", "content": "你好"}
],
stream=True
)
for chunk in completion:
print(chunk.choices[0].delta)
如果您正在使用 Spring AI Alibaba 开发应用,只需要配置 OpenAI 相关依赖与参数,ChatClient 就会通过Higress 代理与后端模型交互:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-M5</version>
</dependency>
spring.ai.openai.base-url=http://127.0.0.1:8080/v1
spring.ai.openai.chat.model=deepseek-chat
spring.ai.openai.chat.api-key=xxxxx,
然后,在监控面板看到每个模型,以及每个消费者的 token 消耗情况以及调用延时:
Higress 的插件市场里还有很多开箱即用的插件,例如提示词模版,AI 缓存,数据脱敏,内容安全等等。
Spring AI Alibaba 钉群群号:105120009405;