欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > SpringAI快速上手

SpringAI快速上手

2024/10/24 1:54:45 来源:https://blog.csdn.net/weixin_74261199/article/details/142985257  浏览:    关键词:SpringAI快速上手

 一、导入依赖

 镜像(导入maven依赖)

 <repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></repository></repositories>

 依赖(导入maven依赖)

<!-- SpringAI -->
<dependency><groupId>org.springframework.experimental.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>0.7.1-SNAPSHOT</version>
</dependency>
​
<!-- 测试依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

二、申请AI免费的 Key

前提:有一个自己的 github账号

Key申请位置:GitCode - 全球开发者的开源社区,开源代码托管平台

一直往下翻,找到申请领取Key,如:

 或者点击链接: 申请领取内测免费API Key

三、配置ai-key与base-url

将 api-key 的位置替换成自己申请的 key

spring:ai:openai:api-key: 	#	替换成自己申请的keybase-url: https://api.chatanywhere.tech

四、快速上手AI

测试类中通过 AiClient 调用,如:

import org.junit.jupiter.api.Test;
import org.springframework.ai.client.AiClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringaiApplicationTests {@Autowiredprivate  AiClient aiClient;@Testvoid testAiClient() {//	问题String ask = "庆余年是什么电视剧";//	回答String answer = aiClient.generate(ask);//	输出打印System.out.println(answer);}
}

显示效果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com