文章目录
第五章 Spring AI调用Ollama
5.1 下载并安装 Ollama
官网:https://ollama.com/
5.1.1 下载Ollama
Ollama 是一个用于本地化部署和管理大型语言模型(LLM)的工具。它支持多种开源模型(如 LLaMA、Alpaca 等),并提供了简单的 API 接口,方便开发者调用。Ollama可以让你在自己的电脑上运行各种强大的 AI 模型,就像运行普通软件一样简单。
5.1.2 安装Ollama
-
模型默认安装在C盘,可以修改安装路径
-
点击环境变量,选择下面新建一个系统环境变量 OLLAMA_MODELS ,然后指定想要安装模型的路径 ,比如 “D:\Deepseek”
- 需要重启 Ollama生效
5.1.3 拉取 DeepSeek 模型
硬件配置建议
-
GPU选择(根据模型大小灵活调整):
入门配置:NVIDIA显卡(≥8GB显存) → 适合7B/8B模型。
高性能配置:NVIDIA显卡(≥16GB显存) → 支持14B大模型。
无独立显卡用户:可使用CPU模式运行(速度约为GPU的20%)。 -
内存要求:≥16GB(推荐32GB,处理长文本时不易卡顿)
-
存储空间:≥50GB可用空间(建议SSD硬盘,加快模型加载速度)
-
操作系统:
Windows 10/11(21H2及以上版本)。
macOS Ventura 13.4+。
Ubuntu 22.04 LTS/24.04 LTS
选择适合自己的版本 https://ollama.com/library/deepseek-r1
以windows为例,根据不同版本,执行不同的命令拉取模型
比如,下载1.5b,执行下面命令
ollama pull deepseek-r1:1.5b
5.1.4 启动Ollama服务测试
启动 Ollama 服务,默认会监听 http://localhost:11434
ollama run deepseek-r1:1.5b
5.2 Spring AI代码测试
5.2.1 创建SpringBoot工程
<?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 http://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.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>springai-deepseek</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-ai.version>1.0.0-M5</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--下载spring-ai相关包需要用到的仓库地址-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>
5.2.2 创建配置文件
application.properties
server.port=8899
spring.application.name=spring-ai-deepseek-demo
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=deepseek-r1:1.5b
spring.ai.ollama.chat.options.temperature=0.7
5.2.3 创建启动类
@SpringBootApplication
public class SpringAiDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAiDemoApplication.class, args);
}
}
5.2.4 创建Controller
@RestController
public class ChatDeepSeekController {
@Autowired
private OllamaChatModel ollamaChatModel;
@GetMapping("/ai/test")
public String generate(@RequestParam(value = "message", defaultValue = "hello")
String message) {
String response = this.ollamaChatModel.call(message);
System.out.println("response : "+response);
return response;
}
}