赞
踩
FastGPT 是一个基于 LLM 大语言模型的知识库问答系统,提供开箱即用的数据处理、模型调用等能力。同时可以通过 Flow 可视化进行工作流编排,从而实现复杂的问答场景!
真正实流式请求,SSE效果,SSE解释:SSE(Server-Sent Events)是一种用于实现服务器主动向客户端推送数据的技术,也被称为“事件流”(Event Stream)。它基于 HTTP 协议,利用了其长连接特性,在客户端与服务器之间建立一条持久化连接,并通过这条连接实现服务器向客户端的实时数据推送。
在使用JAVA连接FastGPT之前,需要有FastGPT环境,FastGPT环境搭建可以参考如下文章:
二、JAVA连接FastGPT关键代码
- Flux<String> response = webClient.post()
- .uri("/chat/completions")//请求uri
- .header("Authorization", "Bearer fastgpt-bfjg4AUxE2ocCUXXeT91proKcnc0t86y9mmPBraOp8waDnPZpQMrw")//设置成自己的key,获得key的方式可以在下文查看
- .header(HttpHeaders.ACCEPT, MediaType.TEXT_EVENT_STREAM_VALUE)//设置流式响应
- .contentType(MediaType.APPLICATION_JSON)
- .body(BodyInserters.fromValue(paramJson))
- .retrieve()
- .bodyToFlux(String.class)
- .flatMap(res->{
- if (StrUtil.equals("[DONE]",res)){//[DONE]是消息结束标识
- return Flux.empty();
- }
- ObjectMapper objectMapper = new ObjectMapper();
- try {
- //System.out.println(res);
- JsonNode jsonNode = objectMapper.readTree(res);
- Answer aiAnswer = objectMapper.treeToValue(jsonNode, Answer.class);
- List<Choices> choicesList = aiAnswer.getChoices();
- Choices choices = choicesList.get(0);
- Delta delta = choices.getDelta();
- String json = objectMapper.writeValueAsString(delta);
- System.out.println(json);
- return Flux.just(json);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
-
- return Flux.empty();
- });

服务启运之后,访问:http://localhost:8080/ck/chat?message=马云是谁
五:源代码下载
下载地址:链接:https://pan.baidu.com/s/1o9TPXNliZMYIqZHxGFK1FQ?pwd=2chg
提取码:2chg
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。