您的位置:首页 > 房产 > 家装 > java springboot 集成 阿里通义千问

java springboot 集成 阿里通义千问

2024/10/6 5:54:26 来源:https://blog.csdn.net/qq_25987725/article/details/140698040  浏览:    关键词:java springboot 集成 阿里通义千问

一、在阿里云https://www.aliyun.com官网直接搜索“通义千问”,点击“开通DashScope”进行服务激活
二、加入Maven依赖

<dependency><groupId>com.alibaba</groupId><artifactId>dashscope-sdk-java</artifactId><version>2.15.3</version>
</dependency>

三、在yml中配置中配置key

ai:api:key: ###########################

四、在config目录添加配置 TongYiAiConfiguration.java

@Configuration
public class TongYiAiConfiguration {@Beanpublic Generation generation(){return new Generation();}

五、实现聊天

@RestController
@RequestMapping("/aiChat")
public class ChatController {private final Generation generation;@Value("${ai.api.key}")private String appKey;@Autowiredpublic ChatController(Generation generation) {this.generation = generation;}@PostMapping("/send1")public GenerationResult chat1(@RequestBody String question, HttpServletResponse response) throws NoApiKeyException, InputRequiredException {List<Message> messages = new ArrayList<>();Message systemMsg =Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();Message userMsg = Message.builder().role(Role.USER.getValue()).content(question).build();messages.add(systemMsg);messages.add(userMsg);GenerationParam param =GenerationParam.builder().model(Generation.Models.QWEN_TURBO).messages(messages).resultFormat(GenerationParam.ResultFormat.MESSAGE).build();GenerationResult result = generation.call(param);return result;}@PostMapping("/send2")public Flux<ServerSentEvent<String>> chat2(@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 {// 设置流式处理webFluxresponse.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);response.setCharacterEncoding("UTF-8");response.getOutputStream().print(JsonUtils.toJson(map));} catch (IOException ex) {ex.printStackTrace();}});}}

版权声明:

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

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