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 项目,引入以下依赖


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-webflux



    org.springframework.ai
    spring-ai-starter-model-ollama


    
        spring-milestones
        Spring Milestones
        https://repo.spring.io/milestone
        
            false
        
    
    
        spring-snapshots
        Spring Snapshots
        https://repo.spring.io/snapshot
        
            false
        
    

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 代码

@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、引入以下依赖


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-webflux



    com.alibaba.cloud.ai
    spring-ai-alibaba-starter
    1.0.0-M5.1

    
        spring-milestones
        Spring Milestones
        https://repo.spring.io/milestone
        
            false
        
    
    
        spring-snapshots
        Spring Snapshots
        https://repo.spring.io/snapshot
        
            false
        
    

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 代码

@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、引入以下依赖


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-webflux



    org.springframework.ai
    spring-ai-starter-model-openai

    
        spring-milestones
        Spring Milestones
        https://repo.spring.io/milestone
        
            false
        
    
    
        spring-snapshots
        Spring Snapshots
        https://repo.spring.io/snapshot
        
            false
        
    

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 代码

@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 的依赖包,* 就会全部重定向到阿里云的仓库去获取。


   aliyun-maven
   Aliyun Maven Repository
   https://maven.aliyun.com/repository/public/
   *,!spring-snapshots,!central-portal-snapshots
   

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

posted @ 2025-10-22 14:42  wzzkaifa  阅读(55)  评论(0)    收藏  举报