开始之前,如果你对大模型完全没了解过,建议阅读之前的大模型入门文章:
【SpringAI】(一)从实际场景入门大模型——适合Java宝宝的大模型应用开发
那么今天就开始写一个基于Spring AI程序的HelloWord!将大模型接入到咱们的JAVA程序中!
各位铁汁看的过程中一定要跟着实践一下啊!
一、SpringAI简介
目前大多数的大模型应用开发都是基于Python的,实际上Spring也发布了应用于AI领域的框架
,那就是SpringAI
。SpringAI提供了与多种大模型对接的功能。通过它,咱们能够轻易得将AI集成到Java程序中。
截止到当前(2024年10月15日),SpringAI官网还没有正式版,只有1.0.0-SNAPSHOT、1.0.0-M2、1.0.0-M3。建议大家选择M版本,SNAPSHOT版本表示处于开发中,可能不稳定。
最新的版本信息和相关文档大家可以到Spring官网去查看:SpringAI官网地址
二、大模型的选择-通义千问
1、通义千问
本次学习案例大模型选择的是阿里云的通义千问(Qwen)。它是一个大型语言模型系列,能够生成与给定提示相关的连续文本,并在各种任务上具有广泛的能力。
如果同学们想选择选择其他的大模型,当然也可以去尝试:
- 智谱AI大模型:智谱AI大模型官网链接
- 千帆大模型:千帆大模型官网链接
- Moonshot大模型:Moonshot大模型官网
2、申请API-KEY
要想使用通义千问,需要到阿里云百炼平台。
如果顶部如图显示,说明你需要开通这项服务。
开通成功后申请自己API-KEY。进行登录注册后,鼠标移动至右上角头像并点击API-KEY
之后再创建API-KEY,点击查看你就能看到你具体的API-KEY了。
对了,目前开通后会送定量限时的免费额度哦!大家快去试试!
三、搭建SpringAI项目
1、环境选择
由于SpringAI要求SringBoot版本必须是3.2.x或者3.3.x
,并且SringBoot 3.2.x 以上
的版本要求JDK版本是17
,所以我们要使用SpringAI,SpringBoot版本和JDK版本都应该满足需求。
2、导入相关依赖
2.1 导入SpringAI依赖并且配置仓库
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M2</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>
</repositories>
注意:导入依赖后可能会出现该错误:
Non-resolvable import POM: Failure to find org.springframework.ai:spring-ai-bom:pom:1.0.0-M2 in https://maven.aliyun.com/repository/public was cached in the local repository, resolution will not be reattempted until the update interval of alimaven has elapsed or updates are forced
该错误的原因大概率是因为你的maven镜像地址配置的阿里云仓库地址,然后目前阿里云仓库还没有SpringAI的坐标
,所以会找不到SpringAI的依赖。
基于这个原因,我们只需要修改Maven中的setting.xml
原有的镜像信息,不仅仅只有阿里云仓库地址,我们也可以引入其他的镜像地址。
修改后可参考:
<mirrors>
<!--在 mirrors 标签下 添加阿里云maven私服库-->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<mirror>
<id>ui</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://uk.maven.org/maven2/</url>
</mirror>
</mirrors>
2.2 导入通义千问依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.13.0</version>
</dependency>
如果想使用通义千问sdk其版本可查看:通义千问SDK其他版本
3、编码调用大模型API
编码如下。
注意要把API-KEY替换成你自己的API-KEY哦!例子中选择的模型是通义千问-Plus,你也可以选择通义千问的其他模型:模型列表
代码中systemMsg
和userMsg
组成Prompts发送给大模型,并且通过GenerationParam
设置API-KEY和具体调用的大模型。
import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
.content("你是个乐于助人的助手.")
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("你是谁?")
.build();
GenerationParam param = GenerationParam.builder()
.apiKey("你自己的API-KEY")
// 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
.model("qwen-plus")
.messages(Arrays.asList(systemMsg, userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
return gen.call(param);
}
public static void main(String[] args) {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("错误信息:"+e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
运行本段程序,可以看到大模型给我们返回的结果
恭喜你!你已经成功把大模型接入到Java程序中了!