引言​​在人工智能技术迅猛发展的浪潮中将智能化能力无缝融入企业级应用已成为提升产品竞争力的关键。Spring AI 作为 Spring 家族的新成员为 Java 开发者带来了标准化、低门槛的 AI 集成体验。下面将介绍如何使用 Spring AI 框架分别接入本地模型 deepseek 阿里云百炼平台 硅基流动。一、环境准备1. 环境要求​​JDK​​17 及以上版本​​构建工具​​Maven 3.6 或 Gradle​​Spring Boot​​3.4建议选用最新稳定版本2. 所需下载应用olloma用户运行大模型语言https://ollama.comdeepSeek 语言模型下载 DeepSeek 模型 | DeepSeek v33. 获取阿里云百炼平台和硅基流动所需的密钥 api-key阿里云大模型服务平台百炼控制台硅基流动SiliconCloud二、本地大模型 deepseek 接入1、将下载的 deepseek-R1 本地模型部署在 ollama 上运行起来没有部署过的可以参考本地用ollama部署DeepSeek大模型_ollama 安装 1.5模型-CSDN博客2、创建一个 spring-boot 项目引入以下依赖dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-webflux/artifactId /dependency !-- ollama -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-model-ollama/artifactId/dependencyrepositories repository idspring-milestones/id nameSpring Milestones/name urlhttps://repo.spring.io/milestone/url snapshots enabledfalse/enabled /snapshots /repository repository idspring-snapshots/id nameSpring Snapshots/name urlhttps://repo.spring.io/snapshot/url releases enabledfalse/enabled /releases /repository /repositories3、配置 application.yml 文件其中 base-url 就是本地大模型在 ollama 运行的服务地址model 填在 deepseek 下载的模型这里下载的是 deepseek-r1:14bspring:application: name: ai-demo ai: ollama: base-url: http://localhost:11434 chat: options: model: deepseek-r1:14b4、编写 java 代码RestController RequestMapping(/ai) public class ChatController { private final ChatClient chatClient; public ChatController(ChatClient.Builder chatClient) { this.chatClient chatClient.build(); } GetMapping(/chat) public String chat(RequestParam(value input) String input) { return chatClient.prompt() .user(input) .call() .content(); } }5、跑起来测试大功告成三、阿里云百炼平台接入1、首先获取到之前创建的密钥将其放在 java 运行环境变量里面​​2、引入以下依赖dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-webflux/artifactId /dependency !-- spring-ai alibaba -- dependency groupIdcom.alibaba.cloud.ai/groupId artifactIdspring-ai-alibaba-starter/artifactId version1.0.0-M5.1/version /dependencyrepositories repository idspring-milestones/id nameSpring Milestones/name urlhttps://repo.spring.io/milestone/url snapshots enabledfalse/enabled /snapshots /repository repository idspring-snapshots/id nameSpring Snapshots/name urlhttps://repo.spring.io/snapshot/url releases enabledfalse/enabled /releases /repository /repositories3、配置 application.yml 文件其中api-key 为当前平台密钥可以直接从平台复制过来粘贴上去但是不推荐建议使用环境变量${AI_DASHSCOPE_API_KEY}base-url 阿里云线上服务地址model 这里填的是通义千问可以根据官网所有的模型进行切换spring: application: name: ai-demo ai: dashscope: api-key: ${AI_DASHSCOPE_API_KEY} base-url: https://dashscope.aliyuncs.com/compatible-mode/v1 chat: options: model: qwen-max4、编写 java 代码RestController RequestMapping(/ai) public class ChatController { private final ChatClient chatClient; public ChatController(ChatClient.Builder chatClient) { this.chatClient chatClient.build(); } GetMapping(/chat) public String chat(RequestParam(value input) String input) { return chatClient.prompt() .user(input) .call() .content(); } }5、跑起来测试大功告成四、硅基流动open-api平台接入1、首先获取到硅基流动创建的密钥将其放在 java 运行环境变量里面​​2、引入以下依赖dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-webflux/artifactId /dependency !-- Spring AI OpenAI Starter -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-model-openai/artifactId /dependencyrepositories repository idspring-milestones/id nameSpring Milestones/name urlhttps://repo.spring.io/milestone/url snapshots enabledfalse/enabled /snapshots /repository repository idspring-snapshots/id nameSpring Snapshots/name urlhttps://repo.spring.io/snapshot/url releases enabledfalse/enabled /releases /repository /repositories3、配置 application.yml 文件其中同样api-key 为硅基流动平台密钥可以直接从平台复制过来粘贴上去但是不推荐建议使用环境变量${AI_SILICONFLOW_API_KEY}base-url 硅基流动线上服务地址model 这里同样填的是通义千问可以根据官网所有的模型进行切换spring: application: name: ai-demo ai: openai: api-key: ${AI_SILICONFLOW_API_KEY} base-url: https://api.siliconflow.cn chat: options: model: Qwen/Qwen2.5-72B-Instruct4、编写 java 代码RestController RequestMapping(/ai) public class ChatController { private final ChatClient chatClient; public ChatController(ChatClient.Builder chatClient) { this.chatClient chatClient.build(); } GetMapping(/chat) public String chat(RequestParam(value input) String input) { return chatClient.prompt() .user(input) .call() .content(); } }5、跑起来测试大功告成五、总结spring AI 的接入其实是比较简单但是要注意以下事项如果切换了模型注意将其它模型的 maven 依赖注释掉重新刷新 maven 不然会有 jar 包冲突Spring Boot 版本号和模型 spring-ai-starter 的版本号需要参考官网或者尽量选高版本避免不兼容问题如果 spring-ai 依赖包拉不下来需要将 maven 仓库的 setting.xml 配置一下把 mirrorOf*/mirrorOf 换成 mirrorOf*,!spring-snapshots,!central-portal-snapshots/mirrorOf 因为阿里云仓库可能并没有 spring-ai 的依赖包* 就会全部重定向到阿里云的仓库去获取。!-- 阿里云 Maven 镜像 -- mirror idaliyun-maven/id nameAliyun Maven Repository/name urlhttps://maven.aliyun.com/repository/public//url mirrorOf*,!spring-snapshots,!central-portal-snapshots/mirrorOf !-- mirrorOf*/mirrorOf -- /mirror最后如果觉得博主写的不错的话可以点点关注