master
sikadi 2023-10-07 14:41:07 +08:00
parent 01fbfdd5b6
commit 1c2ff32bfe
8 changed files with 61 additions and 77 deletions

View File

@ -2,5 +2,8 @@
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliDeprecation" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliEqualsAvoidNull" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliMissingOverrideAnnotation" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

View File

@ -11,7 +11,13 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @description: pp
* @author: ()()()
* @date: 2023/10/7 14:36
* @param:
* @return:
**/
@RestController
public class GTPController {
@ -24,12 +30,12 @@ public class GTPController {
/**
*
*
* @param question
* @param ques
* @return
*/
@GetMapping("/sendQuestion")
public Result sendQuestion(@RequestParam("question") String question) {
String s = gtpService.sendQuestion(question);
@GetMapping("/send")
public Result send(@RequestParam("ques") String ques) {
String s = gtpService.send(ques);
return Result.success(s);
}

View File

@ -7,17 +7,12 @@ import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*
* @author
* @create 2023-09-15 0:42
*/
/**
*
*
* @author
* @create 2023-09-15 0:42
*/
* @description:
* @author: ()()()
* @date: 2023/10/7 14:34
* @param:
* @return:
**/
@Data
@AllArgsConstructor
@NoArgsConstructor

View File

@ -11,19 +11,12 @@ import lombok.NoArgsConstructor;
import java.util.List;
/**
* @description:
* @author:
* @date: 2023/9/28 10:12
* @description:
* @author: ()()()
* @date: 2023/10/7 14:34
* @param:
* @return:
**/
/**
*
* JSON resources/demo-json/request.json
*
* @author
* @create 2023-09-15 0:42
*/
@NoArgsConstructor
@Data
public class RequestDTO {

View File

@ -8,12 +8,12 @@ import lombok.NoArgsConstructor;
import java.util.List;
/**
*
* JSON resources/demo-json/response.json
*
* @author
* @create 2023-09-15 0:42
*/
* @description:
* @author: ()()()
* @date: 2023/10/7 14:34
* @param:
* @return:
**/
@NoArgsConstructor
@Data
public class ResponseDTO {

View File

@ -2,5 +2,5 @@ package com.sikadi.user.service;
public interface GTPService {
String sendQuestion(String question);
String send(String ques);
}

View File

@ -16,58 +16,54 @@ import java.util.Collections;
import java.util.UUID;
/**
* @description:
* @author: ()()()
* @date: 2023/10/7 14:36
* @param:
* @return:
**/
@Service
@Log4j2
public class GTPServiceImpl implements GTPService {
@Resource
@Autowired
private XfXhStreamClient xfXhStreamClient;
@Resource
@Autowired
private XfXhConfig xfXhConfig;
@Override
public String sendQuestion(String question) {
// 如果是无效字符串,则不对大模型进行请求
if (StrUtil.isBlank(question)) {
return "无效问题,请重新输入";
}
public String send(String ques) {
// 创建消息对象
MsgDTO msgDTO = MsgDTO.createUserMsg(question);
// 创建监听器
XfXhWebSocketListener listener = new XfXhWebSocketListener();
// 发送问题给大模型,生成 websocket 连接
WebSocket webSocket = xfXhStreamClient.sendMsg(UUID.randomUUID().toString().substring(0, 10), Collections.singletonList(msgDTO), listener);
if (webSocket == null) {
// 归还令牌
MsgDTO mag = MsgDTO.createUserMsg(ques);
XfXhWebSocketListener xfx = new XfXhWebSocketListener();
WebSocket web = xfXhStreamClient.
sendMsg(
UUID.randomUUID().
toString().
substring(0, 10),
Collections.singletonList(mag), xfx);
if ( null == web) {
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
return "系统内部错误,请联系管理员";
return "错误";
}
try {
int count = 0;
// 为了避免死循环,设置循环次数来定义超时时长
int maxCount = xfXhConfig.getMaxResponseTime() * 5;
while (count <= maxCount) {
int a = 0;
int b = xfXhConfig.getMaxResponseTime() * 5;
while (a <= b) {
Thread.sleep(200);
if (listener.isWsCloseFlag()) {
break;
}
count++;
if (xfx.isWsCloseFlag()) { break; }
a++;
}
if (count > maxCount) {
return "大模型响应超时,请联系管理员";
}
// 响应大模型的答案
return listener.getAnswer().toString();
return xfx.getAnswer().toString();
} catch (InterruptedException e) {
log.error("错误:" + e.getMessage());
return "系统内部错误,请联系管理员";
return "错误";
} finally {
// 关闭 websocket 连接
webSocket.close(1000, "");
// 归还令牌
web.close(100, "");
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
}
}

View File

@ -29,21 +29,12 @@ spring:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
namespace: Sikadi
xfxh:
# 服务引擎使用 讯飞星火认知大模型V2.0,如果使用 V1.5 需要将 hostUrl 修改为 https://spark-api.xf-yun.com/v1.1/chat
hostUrl: https://spark-api.xf-yun.com/v2.1/chat
# 发送请求时指定的访问领域,如果是 V1.5版本 设置为 general如果是 V2版本 设置为 generalv2
domain: generalv2
# 核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高。取值 [0,1]
temperature: 0.5
# 模型回答的tokens的最大长度V1.5取值为[1,4096]V2.0取值为[1,8192]。
maxTokens: 2048
# 大模型回复问题的最大响应时长,单位 s
maxResponseTime: 30
# 允许同时连接大模型的 websocket 数,如果是普通(免费)用户为 2超过这个数连接响应会报错具体参考官网。
QPS: 2
# 用于权限验证,从服务接口认证信息中获取
appId: 9f22c545
# 用于权限验证,从服务接口认证信息中获取
apiKey: a8e17cdcbc9addd7f21e81dff71bc177
# 用于权限验证,从服务接口认证信息中获取
apiSecret: N2NjMDU3ZTQwYjBkODI5ZGM3ZTVjYWY3