java连接ai的全过程
首先新建一个项目
构建系统那一栏选择Maven
创建,
之后在想找的ai平台上获取示例代码
以千帆大模型为例,依次操作
进入千帆大模型平台
注册
左侧导航栏找到应用接口,点击
点击创建应用
然后随便起个名字写个描述,确定
在左侧导航栏找到模型广场,选择自己喜欢的模型
点击右上角API文档,文档中在线调试那一段找到示例代码
点进去,左侧选择自己刚刚选择的模型,右边代码区选择语言,把示例代码复制下来
粘贴在刚刚新建的Maven项目类中
其中accesskey和secretkey的获取需要在个人中心充值,然后开通服务,就能获得这两串很长的秘钥
秘钥替换代码中对应的部分
之后回到idea,找到Pom.Xml文件
在这段代码
<groupId>org.example</groupId>
//这里的AI是项目的名称
<artifactId>AI</artifactId>
<version>1.0-SNAPSHOT</version>
下面粘贴上这段代码
<dependencies>
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.0.9</version>
</dependency>
<dependencies>
注意如果原来这个位置有dependency要删掉
粘贴完上下文应该是这样的
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>AI</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.0.9</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
0.0.9.qianfan.baidubce.com是域名反转的结果,具体要自己去获取对应ai模型的这个值
到这里有可能会报错显示找不到依赖项
那我们需要添加一个镜像
选中Pom.xml文件右键点击Maven,选择 添加’setting,xml’ 选项
进入settings.xml文件
在下面这段代码
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
下面写上下面这段代码
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
之后刷新就可以了
Ps
刚刚的示例代码也可以做成方法,只不过不要忘了再建一个类写主方法哦