deepseek本地部署+java、python调用 目前仅支持jdk17以上版本使用,版本过高、过低时都无法正常启动。对话框输入/bye退出。

1.下载Ollama

(需要科学上网)

https://ollama.com/

2.拉取模型

输入命令

ollama pull deepseek-v3

由于v3太大,改为r1,命令为:

ollama run deepseek-r1:1.5b

查看安装的模型

ollama ls

查看启动的模型

ollama ps


对话框输入/bye退出

3.Java调用

目前仅支持jdk17以上版本使用,本文使用的是jdk21,springboot版本为3.3.6
版本过高、过低时都无法正常启动

3.1引入pom


 4.0.0
 
 org.springframework.boot
 spring-boot-starter-parent
 3.3.6
 
 
 com.example
 demo21
 0.0.1-SNAPSHOT
 demo21
 demo21
 
 21
 
 
 
 io.springboot.ai
 spring-ai-ollama-spring-boot-starter
 1.0.3
 
 
 org.springframework.boot
 spring-boot-starter-web
 
 
 org.projectlombok
 lombok
 true
 
 
 
 
 
 org.apache.maven.plugins
 maven-compiler-plugin
 
 
 
 org.projectlombok
 lombok
 
 
 
 
 
 org.springframework.boot
 spring-boot-maven-plugin
 
 
 
 org.projectlombok
 lombok
 
 
 
 
 
 

3.2配置application.yml

server:
 port: 8088
spring:
 application:
 name: demo21
 ai:
 ollama:
 base-url: http://localhost:11434
 chat:
 options:
 model: deepseek-r1:1.5b

3.2创建Controller

import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OllamaClientController {
 @Autowired
 @Qualifier("ollamaChatClient")
 private OllamaChatClient ollamaChatClient;
 /**
 * http://localhost:8088/ollama/chat/v1?msg=java就业前景
 */
 @GetMapping("/ollama/chat/v1")
 public String ollamaChat(@RequestParam String msg) {
 return this.ollamaChatClient.call(msg);
 }
 /**
 * http://localhost:8088/ollama/chat/v2?msg=java就业前景
 */
 @GetMapping("/ollama/chat/v2")
 public Object ollamaChatV2(@RequestParam String msg) {
 Prompt prompt = new Prompt(msg);
 ChatResponse chatResponse = ollamaChatClient.call(prompt);
 return chatResponse.getResult().getOutput().getContent();
 }
 /**
 * http://localhost:8088/ollama/chat/v3?msg=java就业前景
 */
 @GetMapping("/ollama/chat/v3")
 public Object ollamaChatV3(@RequestParam String msg) {
 Prompt prompt = new Prompt(
 msg,
 OllamaOptions.create()
 .withModel("deepseek-r1:1.5b")
 .withTemperature(0.4F));
 ChatResponse chatResponse = ollamaChatClient.call(prompt);
 return chatResponse.getResult().getOutput().getContent();
 }
}

4.python调用

pip引入

pip install ollama

创建.py文件

import ollama
# 流式输出
def api_generate(text: str):
 print(f'提问:{text}')
 stream = ollama.generate(
 stream=True,
 model='deepseek-r1:1.5b',
 prompt=text,
 )
 print('-----------------------------------------')
 for chunk in stream:
 if not chunk['done']:
 print(chunk['response'], end='', flush=True)
 else:
 print('\n')
 print('-----------------------------------------')
 print(f'总耗时:{chunk['total_duration']}')
 print('-----------------------------------------')
def api_chat(text: str):
 print(f'提问:{text}')
 stream = ollama.chat(
 stream=True,
 model='deepseek-r1:1.5b',
 messages=[{"role":"user","content":text}]
 )
 print('-----------------------------------------')
 for chunk in stream:
 if not chunk['done']:
 print(chunk['message'].content, end='', flush=True)
 else:
 print('\n')
 print('-----------------------------------------')
 print(f'总耗时:{chunk['total_duration']}')
 print('-----------------------------------------')
if __name__ == '__main__':
 # 流式输出
 api_generate(text='python就业前景')
 
 api_chat(text='python就业前景')
 # 非流式输出
 content = ollama.generate(model='deepseek-r1:1.5b', prompt='python就业前景')
 print(content)
 content = ollama.chat(model='deepseek-r1:1.5b', messages=[{"role":"user","content":'python就业前景'}])
 print(content)
作者:Most666原文地址:https://blog.csdn.net/weixin_42709585/article/details/145498039

%s 个评论

要回复文章请先登录注册