Spring-AI 接入(本地大模型 deepseek + 阿里云百炼 + 硅基流动)

引言​​

在人工智能技术迅猛发展的浪潮中,将智能化能力无缝融入企业级应用已成为提升产品竞争力的关键。Spring AI 作为 Spring 家族的新成员,为 Java 开发者带来了标准化、低门槛的 AI 集成体验。下面将介绍如何使用 Spring AI 框架分别接入本地模型 deepseek ,阿里云百炼平台 ,硅基流动。


一、环境准备

1. 环境要求

  • ​JDK​:17 及以上版本
  • ​构建工具​:Maven 3.6+ 或 Gradle
  • ​Spring Boot​:3.4+(建议选用最新稳定版本)

2. 所需下载应用

3. 获取阿里云百炼平台和硅基流动所需的密钥 api-key

二、本地大模型 deepseek 接入

1、将下载的 deepseek-R1 本地模型部署在 ollama 上运行起来,没有部署过的可以参考:本地用ollama部署DeepSeek大模型_ollama 安装 1.5模型-CSDN博客

2、创建一个 spring-boot 项目,引入以下依赖

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- ollama -->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>
复制代码
<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>

3、配置 application.yml 文件,其中 base-url 就是本地大模型在 ollama 运行的服务地址,model 填在 deepseek 下载的模型,这里下载的是 deepseek-r1:14b

复制代码
spring:
  application:
    name: ai-demo
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        options:
          model: deepseek-r1:14b

4、编写 java 代码

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>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- spring-ai alibaba -->
<dependency>
    <groupId>com.alibaba.cloud.ai</groupId>
    <artifactId>spring-ai-alibaba-starter</artifactId>
    <version>1.0.0-M5.1</version>
</dependency>
复制代码
<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>

3、配置 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-max

4、编写 java 代码

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>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- Spring AI OpenAI Starter -->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
复制代码
<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>

3、配置 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-Instruct

4、编写 java 代码

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 的接入其实是比较简单,但是要注意以下事项:

  1. 如果切换了模型,注意将其它模型的 maven 依赖注释掉,重新刷新 maven ,不然会有 jar 包冲突;
  2. Spring Boot 版本号和模型 spring-ai-starter 的版本号需要参考官网,或者尽量选高版本,避免不兼容问题;
  3. 如果 spring-ai 依赖包拉不下来,需要将 maven 仓库的 setting.xml 配置一下,把 <mirrorOf>*</mirrorOf> 换成 <mirrorOf>*,!spring-snapshots,!central-portal-snapshots</mirrorOf> ,因为阿里云仓库可能并没有 spring-ai 的依赖包,* 就会全部重定向到阿里云的仓库去获取。
java 复制代码
<!-- 阿里云 Maven 镜像 -->
<mirror>
   <id>aliyun-maven</id>
   <name>Aliyun Maven Repository</name>
   <url>https://maven.aliyun.com/repository/public/</url>
   <mirrorOf>*,!spring-snapshots,!central-portal-snapshots</mirrorOf>
   <!-- <mirrorOf>*</mirrorOf> -->
</mirror>

最后,如果觉得博主写的不错的话,可以点点关注!

相关推荐
AAA修煤气灶刘哥5 分钟前
Agent 面试题|上下文压缩怎么兼顾 token 开销和决策效果?
人工智能·后端·面试
AAA修煤气灶刘哥16 分钟前
Agent 面试题|工具调用结果不符合预期,什么时候该重规划?
人工智能·后端·面试
卿卿的产品经理日记16 分钟前
【AI产品经理实战】Day 48|AI 回答变短了,到底是谁的功劳?
人工智能·笔记·aigc·产品经理
用户30614292181118 分钟前
codex 配置第三方模型:四个字段填完,随时能切回官方
人工智能
似水流年QC20 分钟前
WebMP 入门:让 AI 更好地使用网页
人工智能
丘山录21 分钟前
Mac mini 16G 跑 Ollama,8 GiB 就崩,剩下全靠换页
人工智能
老炮写标书24 分钟前
把招标文件丢给大模型抽条款:JSON 输出、长文本切分和置信度兜底,我踩过的五个坑
人工智能
亦暖筑序26 分钟前
准备升级 AgentScope Java 2.0.5?这几个会话坑先避开
人工智能·后端·agent
明月醉窗台33 分钟前
[大模型应用(1)]大模型部署的系统基础
人工智能·大模型
AI前端工程狮35 分钟前
可缓存的前缀占 92.8%,一个时间戳只剩 12.3%:Agent 缓存为什么总不命中
javascript·人工智能·程序员