SpringBoot集成通义千问
1.开通通义千问,获取key
通过在阿里云官网直接搜索“通义千问”,点击“开通DashScope”进行服务激活
服务激活后,进入控制台
点击“创建新的API-KEY”按钮,并将获取的API-KEY复制保存
2.导入Maven依赖
<!--导入通义千问 DK -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.14.4</version>
</dependency>
<!--导入流式编程依赖-->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
注意事项
通义千问集成的依赖请使用仓库中最新的依赖,不然会导致项目在调用API时报错,具体错误为Gson转化异常的错误。
3.在yml中配置中配置key
ai:
api:
key: ###########################
4.进行config配置
@Configuration
public class AiConfiguration {
@Bean
public Generation generation(){
return new Generation();
}
5.进行Api调用的编写
@RestController
@RequestMapping("/ai")
public class AiController {
private final Generation generation;
@Value("${ai.api.key}")
private String appKey;
@Autowired
public AiController(Generation generation) {
this.generation = generation;
}
@PostMapping("/send")
public Flux<ServerSentEvent<String>> aiTalk(@RequestBody String question, HttpServletResponse response) throws NoApiKeyException, InputRequiredException {
// 构建ai对象
Message message = Message.builder().role(Role.USER.getValue()).content(question).build();
// 构建通义千问推送消息对象
GenerationParam qwenParam = GenerationParam.builder()
// 设置通义千问的类型模型
.model(Generation.Models.QWEN_PLUS)
// 转化为一个新的List集合
.messages(Arrays.asList(message))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.topP(0.8)
// 是否联网进行查询
.apiKey(appKey)
.build();
// 执行方法获取流式返回数据
Flowable<GenerationResult> result = generation.streamCall(qwenParam);
return Flux.from(result).map(m -> {
// GenerationResult对象中输出流(GenerationOutput)的choices是一个列表,存放着生成的数据。
String content = m.getOutput().getChoices().get(0).getMessage().getContent();
return ServerSentEvent.<String>builder().data(content).build();
}).publishOn(Schedulers.boundedElastic())
.doOnError(e -> {
Map<String, Object> map = new HashMap<>();
map.put("code", "400");
map.put("message", "has mistake "+e.getMessage());
try {
// 设置流式处理webFlux
response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);
response.setCharacterEncoding("UTF-8");
response.getOutputStream().print(JsonUtils.toJson(map));
} catch (IOException ex) {
ex.printStackTrace();
}
});
}
}
在本文中,我们使用了webFlux来获取流中的数据,并通过这种方式实现了消息推送。相比于直接返回数据的方式,这种方式能够及时获取数据,因此更加适合进行实时推送。尽管直接返回数据的方式也可以完成相同的任务,但由于需要等待所有数据都获取完毕才能进行渲染,相对而言耗时会更久,且显示不够及时。
实现了消息推送。相比于直接返回数据的方式,这种方式能够及时获取数据,因此更加适合进行实时推送。尽管直接返回数据的方式也可以完成相同的任务,但由于需要等待所有数据都获取完毕才能进行渲染,相对而言耗时会更久,且显示不够及时。
最后,需要说明的是,本文没有详细讲解使用webFlux进行数据渲染的方式。如果感兴趣的话,可以继续深入研究该主题。非常感谢大家的观看。