deepseek本地部署及java、python調(diào)用步驟詳解
1.下載Ollama
(需要科學(xué)上網(wǎng))
https://ollama.com/
2.拉取模型
輸入命令
ollama pull deepseek-v3


由于v3太大,改為r1,命令為:
ollama run deepseek-r1:1.5b
查看安裝的模型
ollama ls
查看啟動(dòng)的模型
ollama ps

對(duì)話框輸入/bye退出

3.Java調(diào)用
目前僅支持jdk17以上版本使用,本文使用的是jdk21,springboot版本為3.3.6版本過高、過低時(shí)都無法正常啟動(dòng)
3.1引入pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo21</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo21</name>
<description>demo21</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springboot.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
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創(chuàng)建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就業(yè)前景
*/
@GetMapping("/ollama/chat/v1")
public String ollamaChat(@RequestParam String msg) {
return this.ollamaChatClient.call(msg);
}
/**
* http://localhost:8088/ollama/chat/v2?msg=java就業(yè)前景
*/
@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就業(yè)前景
*/
@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調(diào)用
pip引入
pip install ollama
創(chuàng)建.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'總耗時(shí):{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'總耗時(shí):{chunk['total_duration']}')
print('-----------------------------------------')
if __name__ == '__main__':
# 流式輸出
api_generate(text='python就業(yè)前景')
api_chat(text='python就業(yè)前景')
# 非流式輸出
content = ollama.generate(model='deepseek-r1:1.5b', prompt='python就業(yè)前景')
print(content)
content = ollama.chat(model='deepseek-r1:1.5b', messages=[{"role":"user","content":'python就業(yè)前景'}])
print(content)總結(jié)
到此這篇關(guān)于deepseek本地部署及java、python調(diào)用的文章就介紹到這了,更多相關(guān)deepseek本地部署java、python調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java連接sql server 2008數(shù)據(jù)庫代碼
Java的學(xué)習(xí),很重要的一點(diǎn)是對(duì)數(shù)據(jù)庫進(jìn)行操作。2013-03-03
使用sharding-jdbc實(shí)現(xiàn)水平分表的示例代碼
本文主要介紹了sharding-jdbc實(shí)現(xiàn)水平分表,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Java基于Snmp4j庫實(shí)現(xiàn)SNMP協(xié)議的調(diào)用方式
本文分兩部分:首先確保主機(jī)與SNMP設(shè)備通信,需配置路由表以解決跨網(wǎng)段問題;其次介紹Java實(shí)現(xiàn)SNMP協(xié)議的簡(jiǎn)單操作及流量監(jiān)控,涵蓋依賴導(dǎo)入、連接參數(shù)設(shè)置、流量數(shù)據(jù)采集與日志記錄2025-09-09
java構(gòu)建OAuth2授權(quán)服務(wù)器
本文主要介紹了java構(gòu)建OAuth2授權(quán)服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
解析Apache Dubbo的SPI實(shí)現(xiàn)機(jī)制
SPI全稱為Service Provider Interface,對(duì)應(yīng)中文為服務(wù)發(fā)現(xiàn)機(jī)制。SPI類似一種可插拔機(jī)制,首先需要定義一個(gè)接口或一個(gè)約定,然后不同的場(chǎng)景可以對(duì)其進(jìn)行實(shí)現(xiàn),調(diào)用方在使用的時(shí)候無需過多關(guān)注具體的實(shí)現(xiàn)細(xì)節(jié)2021-06-06
java固定大小隊(duì)列的幾種實(shí)現(xiàn)方式詳解
隊(duì)列的特點(diǎn)是節(jié)點(diǎn)的排隊(duì)次序和出隊(duì)次序按入隊(duì)時(shí)間先后確定,即先入隊(duì)者先出隊(duì),后入隊(duì)者后出隊(duì),這篇文章主要給大家介紹了關(guān)于java固定大小隊(duì)列的幾種實(shí)現(xiàn)方式,需要的朋友可以參考下2021-07-07
Spring Boot2.0實(shí)現(xiàn)靜態(tài)資源版本控制詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot2.0實(shí)現(xiàn)靜態(tài)資源版本控制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

