关于调用阿里AI语言大模型接口,从0-1创建方法,整合到SpringBoot项目

AI模型调用(举一反三,后续其他平台大模型只需阅读api文档即可

作者:PG万般
日期:2024-02-24 (因为官方api文档会不定时更新,所以文章部分步骤会有改变)



前言

文章内容为原创,转载需要注明出处,仅用于学习交流
随着人工智能的不断发展,更多人关注到AI模型,百模大战下,如何运用大模型成为一个技术点


一、前置条件:注册阿里账号,获取相关参数

首先注册阿里账号:获取相关的 Access Key、Access Secret Key、Agent Key、AppId
这里不做具体介绍,主讲运用上。
官方有相关的文档介绍:官方直达链接

二、整合到SpringBoot项目-Java

1.在pom文件引入以下依赖:

		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>bailian20230601</artifactId>
			<version>1.1.2</version>
		</dependency
  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,下面是一个基本的实现示例,仅供参考,具体实现需要根据实际情况进行调整。 1. 引入RestTemplate和MyBatis-Plus的依赖 在pom.xml中分别添加以下依赖: ```xml <!-- RestTemplate --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>{version}</version> </dependency> ``` 其中,{version}指的是MyBatis-Plus的版本号。 2. 创建RestTemplate实例 在SpringBoot的配置类中,创建RestTemplate实例,并将其注入到需要使用的类中。 ```java @Configuration public class MyConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } ``` 3. 控制每秒调用次数 使用Semaphore来控制每秒调用次数。 ```java private Semaphore semaphore = new Semaphore(3); public String callThirdPartyApi() { if (semaphore.tryAcquire()) { String url = "http://example.com/api"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); semaphore.release(); return response.getBody(); } else { throw new RuntimeException("Too many requests"); } } ``` 这里的Semaphore初始化为3,表示每秒最多只能发起3个请求。tryAcquire()方法会尝试获取一个信号量,如果获取成功则发送HTTP请求,否则抛出异常。 4. 将响应报文写入MySQL数据库 使用MyBatis-Plus提供的ORM框架来将响应报文写入MySQL数据库。 首先,定义一个实体类来映射数据库中的表: ```java @Data @TableName("api_response") public class ApiResponse { @TableId(type = IdType.AUTO) private Long id; private String content; private LocalDateTime createTime; } ``` 其中,@TableName注解用来指定表名,@TableId注解用来指定主键,@IdType.AUTO表示自动生成主键。 然后,定义一个Mapper接口来和数据库进行交互: ```java @Mapper public interface ApiResponseMapper extends BaseMapper<ApiResponse> { } ``` 其中,BaseMapper是MyBatis-Plus提供的一个通用Mapper接口,继承它即可获得基本的CRUD操作。 最后,在接收到HTTP响应之后,将响应报文保存到数据库中: ```java @Autowired private ApiResponseMapper apiResponseMapper; public String callThirdPartyApi() { if (semaphore.tryAcquire()) { String url = "http://example.com/api"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); semaphore.release(); ApiResponse apiResponse = new ApiResponse(); apiResponse.setContent(response.getBody()); apiResponse.setCreateTime(LocalDateTime.now()); apiResponseMapper.insert(apiResponse); return response.getBody(); } else { throw new RuntimeException("Too many requests"); } } ``` 这里先将响应报文封装成ApiResponse对象,然后调用ApiResponseMapper的insert方法将其插入到数据库中。 以上就是一个基本的实现示例,具体实现需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PG万般

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值