SpringAI系列2:Spring AI + Ollama 本地大模型入门
SpringAI系列2:Spring AI + Ollama 本地大模型入门
文章目录
- SpringAI系列2:Spring AI + Ollama 本地大模型入门
- 前言
- 一、什么是ollama
- 二、Windows环境下的安装配置
- 三、SpringAI接入ollama
- 🎉四、结语:AI的大门,从此刻开始打开
- 🔜 五、下期预告
前言
提示:这里可以添加本文要记录的大概内容:
调用大模型需要配置apiKey,需要充钱,作为一名“白嫖党”,这可不能接受,能不能有不花钱还能练手的方式呢?别急,今天就来为大家解密这一“白嫖”方式,使用ollama部署本地大模型并使用SpringAI进行调用,废话不多说,开始我们springAI系列的第二讲《Spring AI + Ollama 本地大模型入门》
📚 Ollama官网:https://ollama.com/download/linux
一、什么是ollama
Ollama 是一个开源项目,旨在让在本地设备(如个人电脑或服务器)上运行大型语言模型(LLMs)变得更加简单和高效
Ollama 允许用户在自己的机器上下载并运行开源大语言模型,无需依赖云端 API,保障数据隐私和离线使用
官方维护了一个模型库 https://ollama.com/library,用户可以一键拉取热门开源模型
二、Windows环境下的安装配置
2.1 系统要求
● 操作系统:Windows 10以上
● 内存建议:
○ 运行 7B 模型:至少 8GB RAM(推荐 16GB+)
○ 运行 13B+ 模型:32GB+ RAM
● 磁盘空间:模型通常占用 4GB–50GB(例如 Llama3 8B 约 4.7GB)
💡 Ollama 会自动利用 NVIDIA GPU(通过 CUDA)或 Intel/AMD 显卡(部分支持),但即使只有 CPU 也能运行。
2.2 安装
2.2.1 访问 https://ollama.com/download/windows

一路next安装即可,安装完成之后Ollama 会自动在后台启动

2.3 使用
2.3.1 模型的拉取和运行
访问模型库 https://ollama.com/library?spm=5176.28103460.0.0.96a07551zaeVW4

使用ollama pull 模型名 拉取需要的模型

使用ollama run 模型名 即可运行该模型 运行后浏览器访问http://localhost:11434/,显示如下:

📣 也可以直接执行run命令,如果没有该模型,默认会先拉取
2.3.2 其他常用命令
| 命令 | 说明 |
|---|---|
ollama list | 查看已安装的模型 |
ollama pull mistral | 预先下载模型(不立即运行) |
ollama rm llama3 | 删除某个模型 |
ollama run phi3 | 运行微软的轻量模型 Phi-3(适合低配电脑) |
2.3.3 对话模式
使用ollama run 模型名运行该模型后即可进去对话模式

2.3.4 推荐Windows入门模型
| 模型 | 参数量 | 特点 |
|---|---|---|
phi3 | 3.8B | 微软出品,速度快,低资源消耗 |
gemma:2b | 2B | Google 轻量模型,响应快 |
llama3:8b | 8B | 综合能力强,适合日常使用 |
mistral:7b | 7B | 推理能力强,支持长上下文 |
三、SpringAI接入ollama
3.1 开发环境要求
jdk:17+
maven:3.8+
springboot:3.x
提示:本系列使用的都是maven,如果使用的是Gradle,需自行下载对应版本
3.2 maven pom.xml文件
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.5.6</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>spring-ai-test</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-ai-test</name><description>spring-ai-test</description><properties><java.version>17</java.version><spring.ai.version>1.1.0-M2</spring.ai.version><spring-ai-alibaba.version>1.0.0.2</spring-ai-alibaba.version></properties><dependencies><!--spring-boot-starter--><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><!-- Spring AI 对 OpenAI API 标准的客户端支持 (用于兼容 DeepSeek API) --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-openai</artifactId></dependency><!-- 阿里百炼 --><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-dashscope</artifactId></dependency><!-- DeepSeek --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-deepseek</artifactId></dependency><!-- ollama --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-ollama</artifactId></dependency></dependencies><dependencyManagement><dependencies><!-- 引入 Spring AI BOM 管理依赖版本 --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>${spring.ai.version}</version><type>pom</type><scope>import</scope></dependency><!-- 引入 Spring alibaba AI BOM 管理依赖版本 --><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-bom</artifactId><version>${spring-ai-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!--仓库镜像--><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><!-- 阿⾥云镜像,⽤来下载SpringAI之外的依赖 --><repository><id>aliyun</id><name>aliyun</name><url>https://maven.aliyun.com/repository/public</url></repository><repository><id>embabel-releases</id><url>https://repo.embabel.com/artifactory/libs-release</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository><repository><id>embabel-snapshots</id><url>https://repo.embabel.com/artifactory/libs-snapshot</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-clean-plugin</artifactId><version>2.5</version></plugin></plugins></build></project>提示:这个文件直接复制替换即可使用,如果没有配置大模型的apikey,配置文件中去掉大模型相关配置即可。
3.3 applicaton.properties配置文件
spring.application.name=spring-ai-test server.port=8088 #deepseek openai 配置 spring.ai.openai.api-key="你自己的deepseek apikey" spring.ai.openai.base-url=https://api.deepseek.com spring.ai.openai.chat.options.model=deepseek-chat #spring.ai.openai.chat.options.model=deepseek-reasoner#deepseek 配置 spring.ai.deepseek.api-key="你自己的deepseek apikey" spring.ai.deepseek.chat.options.model=deepseek-chat #spring.ai.deepseek.model=deepseek-reasoner#阿里百炼 配置 spring.ai.dashscope.api-key="你自己的阿里百炼apikey" spring.ai.chat.client.enabled=false #ollama 配置自己拉取的模型名称 spring.ai.ollama.chat.model = qwen3:0.6b 提示:这个文件直接复制替换即可使用,如果没有配置大模型的apikey,配置文件中去掉大模型相关配置即可。
3.4 Test springAI ollama
config配置类,增加对应的ChatClient
packagecom.example.springaitest.config;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.model.ChatModel;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassChatClientConfig{@BeanpublicChatClientdashscopeChatClient(@Qualifier("dashscopeChatModel")ChatModel dashscopeModel){returnChatClient.builder(dashscopeModel).build();}@BeanpublicChatClientopenAiChatClient(@Qualifier("openAiChatModel")ChatModel openAiModel){returnChatClient.builder(openAiModel).build();}@BeanpublicChatClientdeepSeekChatClient(@Qualifier("deepSeekChatModel")ChatModel deepSeekModel){returnChatClient.create(deepSeekModel);}@BeanpublicChatClientollamaChatClient(@Qualifier("ollamaChatModel")ChatModel openAiModel){returnChatClient.builder(openAiModel).build();}}提示:如果没有配置大模型的apikey,去掉非ollama代码即可。
packagecom.example.springaitest;importjakarta.annotation.Resource;importorg.junit.jupiter.api.Test;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.boot.test.context.SpringBootTest;@SpringBootTestpublicclassOllamaTest{@Resource(name ="ollamaChatClient")privateChatClient ollamaChatClient;@TestpublicvoidtestChatOptions1(){String call = ollamaChatClient.prompt().user("你好你是谁").call().content();System.out.println(call);}}
好了,通过上面的教程,我们就能使用ollama完成本地大模型的部署和使用springAI完成api的调用了!!
🎉四、结语:AI的大门,从此刻开始打开
“白嫖”的滋味是不是很爽?不用花钱,也能完成与AI的对话,这不单单是“白嫖”,更是你转向AI应用开发的道路。
🔜 五、下期预告
只完成简单的对话,API是否能让大模型记住我的话,实现上下文记忆?能否创建我自己的文本让大模型识别?别着急,一切尽在下一期《Spring AI 的进一步探索》
下期,请跟我一起去探索吧!
🎯 小白友好,全程保姆级教程,敬请期待!!!!
💬这里只有干货,没有废话,废话我也不会写,只写你直接复制就能跑起来的代码。
AI 不会取代 程序员,
但会取代不懂 AI 的 程序员。
关注我,走出拥抱AI的第一步。