当前位置: 首页 > news >正文

使用spring-ai-alibaba接入大模型

  spring-ai-alibaba 是Spring AI生态里与阿里巴巴相关的组件,借助它能够实现接入各类大模型。以下为你详细介绍如何使用 spring-ai-alibaba 接入不同大模型:

接入open ai

项目环境准备

        首先要创建一个Spring Boot项目,并且在 pom.xml 里添加必要的依赖。接入openapi为例(我这里使用硅基流动和openapi一样)官网:SiliconFlow

<?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 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.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.sqz</groupId><artifactId>spring-ai-deepseek</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-ai-deepseek</name><description>spring-ai-deepseek</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>21</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--接入openapi--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>1.0.0-M6</version></dependency><!--接入ollama--><!--<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId><version>1.0.0-M6</version></dependency>--><!--接入 通义千问--><!--   <dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter</artifactId><version>1.0.0-M6.1</version></dependency>--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><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><repository><id>aliyunmaven</id><name>aliyun</name><url>https://maven.aliyun.com/repository/public</url></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories>
</project>

配置认证信息

在 application.properties 或者 application.yml 中配置硅基流动认证信息,例如:

spring:application:name: alibaba-ai-demoai:openai:api-key: your-api-key  #apiKey到硅基流动官网获取免费的base-url: https://api.siliconflow.cnchat:options:model: Qwen/QwQ-32B  #使用通义千问模型

这里的 your-api-key 要替换成自己获取的API Key。

 编写代码接入大模型

下面是一个简单的Java代码示例,展示了如何使用 spring-ai-alibaba 接入openapi模型:

package com.sqz.springaideepseek.controller;import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;@RestController
public class ChatController {private final ChatClient chatClient;public ChatController(ChatClient.Builder builder) {this.chatClient = builder.defaultSystem("你是一个万能助手,你可以回答任何问题。").build();}@GetMapping("/chat")public String chat(@RequestParam(value = "input") String input) {return this.chatClient.prompt().user(input).call().content();}/*** ChatClient 流式响应*/@GetMapping(value = "/stream/response", produces =  "text/event-stream;charset=UTF-8")public Flux<ServerSentEvent<String>> simpleChat(@RequestParam String message) {return chatClient.prompt().user(message).stream().content().map(content -> ServerSentEvent.<String>builder().data(content).build());}
}

测试:http://localhost:8080/chat?input=%E4%BD%A0%E6%98%AF%E8%B0%81

接入ollama 

将配置文件改为ollama配置:

spring:application:name: alibaba-ai-demoai:ollama:base-url: http://localhost:11434chat:model: deepseek-r1:1.5b

pom文件注释掉openapi,依赖 放开ollama注释

        <!--接入openapi--><!--<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>1.0.0-M6</version></dependency>--><!--接入ollama--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId><version>1.0.0-M6</version></dependency>

接入阿里云大模型

pom文件注释掉openapi和ollama,打开alibaba

<dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter</artifactId><version>1.0.0-M6.1</version>
</dependency>

修改配置文件

spring:application:name: alibaba-ai-demoai:dashscope:api-key: your-api-key

这里的 your-api-key 要替换成你自己在阿里云获取的API Key。就可以测试了

http://www.xdnf.cn/news/990739.html

相关文章:

  • mysql基本操作语句 增删改查基础语法速查表
  • MTK-USB模式动态设置
  • VScode安装配置PYQT6
  • MS7200+MS1824 HD转AV/S-Video/VGA/YPbPr/RGB888/BT601、656/BT1120转换器
  • Pandas时间数据处理:从基础到进阶的实战指南
  • 利用高分辨率卫星遥感数据以更智能、更快速的方式勘测评估能源开采现场
  • 第四章 文件管理
  • 软件测试用例设计总结
  • Position Embedding 有哪些方式?
  • @Indexed原理与实战
  • Java大模型开发入门 (3/15): 拥抱官方标准 - 使用OpenAI官方Java SDK调用DeepSeek
  • 航电系统之轨迹克隆技术篇
  • pyvis报错AttributeError: ‘NoneType‘ object has no attribute ‘render‘
  • python打卡day51@浙大疏锦行
  • 期权末日轮实值期权盈利未平仓怎么办?
  • 【多模态/T5】[特殊字符] 为什么视频生成模型还在用T5?聊聊模型选择的学问
  • Windows版PostgreSQL 安装 postgis扩展
  • 大数据下的分页通用架构设计:从随机IO到顺序IO
  • Gartner<Reference Architecture Brief: Data Integration>学习心得
  • 嵌入式程序存储结构
  • HW中常态化反钓鱼训练的具体战略部署
  • 【网络】每天掌握一个Linux命令 - netperf
  • 6. TypeScript 函数
  • 提升集装箱及金属包装容器制造交付效率:数字化项目管理系统的核心优势
  • 异常谋杀案--Java异常处理篇
  • 工程论文: TORL: Scaling Tool-Integrated RL
  • StackOverflowError
  • (javaSE)继承和多态:成员变量,super,子类构造方法,super和this,初始化, protected 继承方式 final关键字 继承与组合
  • Dify-7: RAG 知识系统
  • 什么是项目进度管理?项目进度管理有哪些核心功能?