PageSourceSearch

https://yuluo-yx.github.io/assets/%E4%BD%BF%E7%94%A8-Ollama-%E9%83…g-AI-Alibaba-%E8%B0%83%E7%94%A8-BU4hHtaL.js

js yuluo-yx.github.io collected 2026-10-03 10:14:08 UTC 9,405 bytes, 368 lines download raw bytes

1const n=`---
2slug: 使用-Ollama-部署-DeepSeek-并使用-Spring-AI-Alibaba-调用
3title: 使用 Ollama 部署 DeepSeek 并使用 Spring AI Alibaba 调用
4date: 2025-02-16 20:26:55
5authors: yuluo
6tags: [LLM, Spring AI Alibaba, Ollama, DeepSeek, Java]
7keywords: [LLM, Spring AI Alibaba, Ollama, DeepSeek, Java]
8image: /img/ai/deepseek/dp.jpg
9---
10
11<!-- truncate -->
12
13## Ollama 部署 Deepseek R1
14
15官网:https://www.deepseek.com/
16
17Github:https://github.com/deepseek-ai
18
19Ollama:https://ollama.com/
20
21### Docker Compose
22
23部署一个 Ollama 和 open-webui 服务:
24
25\`\`\`yml
26services:
27
28   ollama:
29     volumes:
30       - ./models:/root/.ollama  # 将本地文件夹挂载到容器中的 /root/.ollama 目录 (模型下载位置)
31     container_name: ollama
32     pull_policy: always
33     tty: true
34     restart: unless-stopped
35     image: ollama/ollama:latest
36     ports:
37       - 11434:11434  # Ollama API 端口
38
39   open-webui:
40     build:
41       context: .
42       args:
43         OLLAMA_BASE_URL: '/ollama'
44       dockerfile: Dockerfile
45     image: ghcr.io/open-webui/open-webui:main
46     container_name: open-webui
47     volumes:
48       - ./open-webui:/app/backend/data  # 前端页面数据挂载位置
49     depends_on:
50       - ollama
51     ports:
52       - \${OPEN_WEBUI_PORT-3005}:8080
53     environment:
54       - 'OLLAMA_BASE_URL=http://ollama:11434'
55       - 'WEBUI_SECRET_KEY='
56     extra_hosts:
57       - host.docker.internal:host-gateway
58     restart: unless-stopped
59\`\`\`
60
61### 安装 DeepSeek-R1
62
63进入 docker ollama 容器下载 deepseek-r1 模型
64
65\`\`\`shell
66# 进入容器
67$ docker exec -it ollama bash
68
69# 查看 ollama 已有的模型(第一次下载没有正常)
70$ root@c5e5ff20a533:/# ollama list
71
72NAME            ID              SIZE    MODIFIED
73llama3:latest   365c0bd3c000    4.7 GB  7 months ago
74qwen:4b         d53d04290064    2.3 GB  7 months ago
75
76# 选择下载 8b(可以根据机器环境选择不同的模型)
77ollama run deepseek-r1:8b
78
79# 下载成功如下所示
80
81\`\`\`
82
83### open-webui 使用
84
85> **如果出现模型失败的错误,尝试更新先 ollama 版本之后重试!**
86
87在右上角选择模型:
88
89![Open WebUI 选择模型](/img/ai/deepseek/image-4.png)
90
91使用演示:
92
93![Open WebUI 调用](/img/ai/deepseek/image-1.png)
94
95看起来效果不错,歪瑞古德!
96
97## Spring AI Alibaba 调用
98
99接下来演示如何 Spring AI Alibaba 完成一个简单的 Chat 应用。
100
101##### pom.xml
102
103因为我们使用 ollama 部署 deepseek r1,所以这里使用 ollama starter。
104
105\`\`\`xml
106<?xml version="1.0" encoding="UTF-8"?>
107<project xmlns="http://maven.apache.org/POM/4.0.0"
108		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
109		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
110	<modelVersion>4.0.0</modelVersion>
111	<parent>
112		<groupId>indi.yuluo</groupId>
113		<artifactId>examples</artifactId>
114		<version>1.0-SNAPSHOT</version>
115	</parent>
116
117	<groupId>indi.yuluo.deepseek</groupId>
118	<artifactId>deepseek-r1-saa</artifactId>
119
120	<dependencies>
121		<dependency>
122			<groupId>org.springframework.boot</groupId>
123			<artifactId>spring-boot-starter-web</artifactId>
124			<version>3.3.4</version>
125		</dependency>
126
127		<dependency>
128			<groupId>org.springframework.ai</groupId>
129			<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
130			<version>1.0.0-M6</version>
131		</dependency>
132	</dependencies>
133
134	<repositories>
135		<repository>
136			<id>spring-milestones</id>
137			<name>Spring Milestones</name>
138			<url>https://repo.spring.io/milestone</url>
139			<snapshots>
140				<enabled>false</enabled>
141			</snapshots>
142		</repository>
143	</repositories>
144
145</project>
146\`\`\`
147
148##### application.yml
149
150\`\`\`yml
151server:
152  port: 8080
153
154spring:
155  application:
156    name: deepseek-r1-saa
157
158  ai:
159    ollama:
160      base-url: http://localhost:11434
161      chat:
162        model: deepseek-r1:8b
163\`\`\`
164
165##### 启动类
166
167\`\`\`java
168package indi.yuluo.deepseek;
169
170import org.springframework.boot.SpringApplication;
171import org.springframework.boot.autoconfigure.SpringBootApplication;
172
173/**
174 * @author yuluo
175 * @author <a href="mailto:[email protected]">yuluo</a>
176 */
177
178@SpringBootApplication
179public class DeepSeekChatApplication {
180
181    public static void main(String[] args) {
182
183       SpringApplication.run(DeepSeekChatApplication.class, args);
184    }
185
186}
187\`\`\`
188
189##### controller
190
191\`\`\`java
192package indi.yuluo.deepseek.controller;
193
194import jakarta.servlet.http.HttpServletResponse;
195import reactor.core.publisher.Flux;
196
197import org.springframework.ai.chat.client.ChatClient;
198import org.springframework.ai.chat.prompt.Prompt;
199import org.springframework.ai.ollama.OllamaChatModel;
200import org.springframework.web.bind.annotation.GetMapping;
201import org.springframework.web.bind.annotation.PathVariable;
202import org.springframework.web.bind.annotation.RequestMapping;
203import org.springframework.web.bind.annotation.RestController;
204
205/**
206 * @author yuluo
207 * @author <a href="mailto:[email protected]">yuluo</a>
208 */
209
210@RestController
211@RequestMapping("/deepseek/chat")
212public class DeepSeekController {
213
214	private final ChatClient chatClient;
215
216	public DeepSeekController (OllamaChatModel chatModel) {
217
218		this.chatClient = ChatClient.builder(chatModel).build();
219	}
220
221	@GetMapping("/{prompt}")
222	public Flux<String> chat(
223			@PathVariable(value = "prompt") String prompt,
224			HttpServletResponse response
225	) {
226
227		response.setCharacterEncoding("UTF-8");
228		return this.chatClient.prompt(new Prompt(prompt)).stream().content();
229	}
230
231}
232\`\`\`
233
234##### 浏览器请求测试
235
236![Spring AI Alibaba Call](/img/ai/deepseek/image-3.png)
237
238## DeepSeek4j 调用
239
240##### pom.xml
241
242\`\`\`xml
243<?xml version="1.0" encoding="UTF-8"?>
244<project xmlns="http://maven.apache.org/POM/4.0.0"
245        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
246        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
247    <modelVersion>4.0.0</modelVersion>
248    <parent>
249       <groupId>indi.yuluo</groupId>
250       <artifactId>examples</artifactId>
251       <version>1.0-SNAPSHOT</version>
252    </parent>
253
254    <groupId>indi.yuluo.deepseek</groupId>
255    <artifactId>deepseek4j</artifactId>
256
257    <dependencies>
258       <dependency>
259          <groupId>org.springframework.boot</groupId>
260          <artifactId>spring-boot-starter-web</artifactId>
261          <version>3.3.4</version>
262       </dependency>
263
264       <dependency>
265          <groupId>io.github.pig-mesh.ai</groupId>
266          <artifactId>deepseek-spring-boot-starter</artifactId>
267          <version>1.4.1</version>
268       </dependency>
269    </dependencies>
270
271</project>
272\`\`\`
273
274##### application.yml 配置
275
276\`\`\`yml
277deepseek:
278  base-url: http://127.0.0.1:11434/v1
279  model: deepseek-r1:8b
280  api-key: deepseek
281  default-system-prompt: false
282
283server:
284  port: 8080
285\`\`\`
286
287##### controller
288
289\`\`\`java
290package indi.yuluo.deepseek.controller;
291
292import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
293import jakarta.servlet.http.HttpServletResponse;
294import reactor.core.publisher.Flux;
295
296import org.springframework.beans.factory.annotation.Autowired;
297import org.springframework.web.bind.annotation.GetMapping;
298import org.springframework.web.bind.annotation.PathVariable;
299import org.springframework.web.bind.annotation.RequestMapping;
300import org.springframework.web.bind.annotation.RestController;
301
302/**
303 * @author yuluo
304 * @author <a href="mailto:[email protected]">yuluo</a>
305 */
306
307@RestController
308@RequestMapping("/deepseek4j")
309public class DeepSeek4JController {
310
311    @Autowired
312    private DeepSeekClient deepSeekClient;
313
314    @GetMapping(value = "/chat/{prompt}")
315    public Flux<String> chat(
316          @PathVariable(value = "prompt") String prompt,
317          HttpServletResponse response
318    ) {
319
320       response.setCharacterEncoding("UTF-8");
321
322       return deepSeekClient.chatFluxCompletion(prompt).map(
323             chatCompletionResponse -> {
324                System.out.println(chatCompletionResponse.choices().get(0).delta().toString());
325                return chatCompletionResponse.choices().get(0).delta().content();
326             }
327       );
328    }
329
330}
331\`\`\`
332
333### DeepSeek4j 透出 reasoning content
334
335当 \`default-system-prompt\` 为 false 时
336
337\`\`\`yml
338deepseek:
339  base-url: http://127.0.0.1:11434/v1
340  model: deepseek-r1:8b
341  api-key: deepseek
342  default-system-prompt: false
343\`\`\`
344
345输出为:
346
347\`\`\`text
348你好!很高兴见到你,有什么我可以帮忙的吗?无论是问题、建议还是闲聊,我都在这儿为你服务。😊
349\`\`\`
350
351当 \`default-system-prompt\` 为 true 时
352
353\`\`\`yml
354deepseek:
355  base-url: http://127.0.0.1:11434/v1
356  model: deepseek-r1:8b
357  api-key: deepseek
358  default-system-prompt: true
359\`\`\`
360
361输出为:
362
363\`\`\`text
364用户说“你好”,这是个常见的问候,我应该用中文回答,保持亲切。 我是DeepSeek-R1,由中国公司DeepSeek开发的AI助手,可以处理中文和英文查询。 接下来,我会详细介绍一下DeepSeek-R1的功能和特点,让用户有更全面的了解。 你好!我是由中国公司深度求索(DeepSeek)开发的智能助手DeepSeek-R1。我擅长通过文本对话方式为您提供信息,解答问题并进行交流。如有任何需要,我会尽力帮助您,同时确保回答准确、有条理地呈现给您。如果你有任何具体的需求或疑问,请随时告诉我!
365\`\`\`
366
367> 由此可见,似乎是由 prompt 控制的?
368`;export{n as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.