master
parent
01fbfdd5b6
commit
1c2ff32bfe
|
@ -2,5 +2,8 @@
|
||||||
<profile version="1.0">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
<inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" />
|
<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>
|
</profile>
|
||||||
</component>
|
</component>
|
|
@ -11,7 +11,13 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
/**
|
||||||
|
* @description: pp
|
||||||
|
* @author: (扭曲)(爬行)(抽搐)
|
||||||
|
* @date: 2023/10/7 14:36
|
||||||
|
* @param:
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
@RestController
|
@RestController
|
||||||
public class GTPController {
|
public class GTPController {
|
||||||
|
|
||||||
|
@ -24,12 +30,12 @@ public class GTPController {
|
||||||
/**
|
/**
|
||||||
* 发送问题
|
* 发送问题
|
||||||
*
|
*
|
||||||
* @param question 问题
|
* @param ques 问题
|
||||||
* @return 星火大模型的回答
|
* @return 星火大模型的回答
|
||||||
*/
|
*/
|
||||||
@GetMapping("/sendQuestion")
|
@GetMapping("/send")
|
||||||
public Result sendQuestion(@RequestParam("question") String question) {
|
public Result send(@RequestParam("ques") String ques) {
|
||||||
String s = gtpService.sendQuestion(question);
|
String s = gtpService.send(ques);
|
||||||
return Result.success(s);
|
return Result.success(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,17 +7,12 @@ import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息对象
|
* @description: 工具类
|
||||||
*
|
* @author: (扭曲)(爬行)(抽搐)
|
||||||
* @author 狐狸半面添
|
* @date: 2023/10/7 14:34
|
||||||
* @create 2023-09-15 0:42
|
* @param:
|
||||||
*/
|
* @return:
|
||||||
/**
|
**/
|
||||||
* 消息对象
|
|
||||||
*
|
|
||||||
* @author 狐狸半面添
|
|
||||||
* @create 2023-09-15 0:42
|
|
||||||
*/
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
|
|
@ -11,19 +11,12 @@ import lombok.NoArgsConstructor;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description:
|
* @description: 工具类
|
||||||
* @author: 不愧是我
|
* @author: (扭曲)(爬行)(抽搐)
|
||||||
* @date: 2023/9/28 10:12
|
* @date: 2023/10/7 14:34
|
||||||
* @param:
|
* @param:
|
||||||
* @return:
|
* @return:
|
||||||
**/
|
**/
|
||||||
/**
|
|
||||||
* 请求参数
|
|
||||||
* 对应生成的 JSON 结构参考 resources/demo-json/request.json
|
|
||||||
*
|
|
||||||
* @author 狐狸半面添
|
|
||||||
* @create 2023-09-15 0:42
|
|
||||||
*/
|
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Data
|
@Data
|
||||||
public class RequestDTO {
|
public class RequestDTO {
|
||||||
|
|
|
@ -8,12 +8,12 @@ import lombok.NoArgsConstructor;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回参数
|
* @description: 工具类
|
||||||
* 对应生成的 JSON 结构参考 resources/demo-json/response.json
|
* @author: (扭曲)(爬行)(抽搐)
|
||||||
*
|
* @date: 2023/10/7 14:34
|
||||||
* @author 狐狸半面添
|
* @param:
|
||||||
* @create 2023-09-15 0:42
|
* @return:
|
||||||
*/
|
**/
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Data
|
@Data
|
||||||
public class ResponseDTO {
|
public class ResponseDTO {
|
||||||
|
|
|
@ -2,5 +2,5 @@ package com.sikadi.user.service;
|
||||||
|
|
||||||
public interface GTPService {
|
public interface GTPService {
|
||||||
|
|
||||||
String sendQuestion(String question);
|
String send(String ques);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,58 +16,54 @@ import java.util.Collections;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 修改
|
||||||
|
* @author: (扭曲)(爬行)(抽搐)
|
||||||
|
* @date: 2023/10/7 14:36
|
||||||
|
* @param:
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
@Service
|
@Service
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class GTPServiceImpl implements GTPService {
|
public class GTPServiceImpl implements GTPService {
|
||||||
|
|
||||||
@Resource
|
@Autowired
|
||||||
private XfXhStreamClient xfXhStreamClient;
|
private XfXhStreamClient xfXhStreamClient;
|
||||||
|
|
||||||
@Resource
|
@Autowired
|
||||||
private XfXhConfig xfXhConfig;
|
private XfXhConfig xfXhConfig;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String sendQuestion(String question) {
|
public String send(String ques) {
|
||||||
// 如果是无效字符串,则不对大模型进行请求
|
|
||||||
if (StrUtil.isBlank(question)) {
|
|
||||||
return "无效问题,请重新输入";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建消息对象
|
MsgDTO mag = MsgDTO.createUserMsg(ques);
|
||||||
MsgDTO msgDTO = MsgDTO.createUserMsg(question);
|
XfXhWebSocketListener xfx = new XfXhWebSocketListener();
|
||||||
// 创建监听器
|
WebSocket web = xfXhStreamClient.
|
||||||
XfXhWebSocketListener listener = new XfXhWebSocketListener();
|
sendMsg(
|
||||||
// 发送问题给大模型,生成 websocket 连接
|
UUID.randomUUID().
|
||||||
WebSocket webSocket = xfXhStreamClient.sendMsg(UUID.randomUUID().toString().substring(0, 10), Collections.singletonList(msgDTO), listener);
|
toString().
|
||||||
if (webSocket == null) {
|
substring(0, 10),
|
||||||
// 归还令牌
|
Collections.singletonList(mag), xfx);
|
||||||
|
if ( null == web) {
|
||||||
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
||||||
return "系统内部错误,请联系管理员";
|
return "错误";
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
int count = 0;
|
|
||||||
// 为了避免死循环,设置循环次数来定义超时时长
|
int a = 0;
|
||||||
int maxCount = xfXhConfig.getMaxResponseTime() * 5;
|
|
||||||
while (count <= maxCount) {
|
int b = xfXhConfig.getMaxResponseTime() * 5;
|
||||||
|
while (a <= b) {
|
||||||
Thread.sleep(200);
|
Thread.sleep(200);
|
||||||
if (listener.isWsCloseFlag()) {
|
if (xfx.isWsCloseFlag()) { break; }
|
||||||
break;
|
|
||||||
}
|
a++;
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
if (count > maxCount) {
|
return xfx.getAnswer().toString();
|
||||||
return "大模型响应超时,请联系管理员";
|
|
||||||
}
|
|
||||||
// 响应大模型的答案
|
|
||||||
return listener.getAnswer().toString();
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
log.error("错误:" + e.getMessage());
|
return "错误";
|
||||||
return "系统内部错误,请联系管理员";
|
|
||||||
} finally {
|
} finally {
|
||||||
// 关闭 websocket 连接
|
web.close(100, "");
|
||||||
webSocket.close(1000, "");
|
|
||||||
// 归还令牌
|
|
||||||
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,21 +29,12 @@ spring:
|
||||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
namespace: Sikadi
|
namespace: Sikadi
|
||||||
xfxh:
|
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
|
hostUrl: https://spark-api.xf-yun.com/v2.1/chat
|
||||||
# 发送请求时指定的访问领域,如果是 V1.5版本 设置为 general,如果是 V2版本 设置为 generalv2
|
|
||||||
domain: generalv2
|
domain: generalv2
|
||||||
# 核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高。取值 [0,1]
|
|
||||||
temperature: 0.5
|
temperature: 0.5
|
||||||
# 模型回答的tokens的最大长度,V1.5取值为[1,4096],V2.0取值为[1,8192]。
|
|
||||||
maxTokens: 2048
|
maxTokens: 2048
|
||||||
# 大模型回复问题的最大响应时长,单位 s
|
|
||||||
maxResponseTime: 30
|
maxResponseTime: 30
|
||||||
# 允许同时连接大模型的 websocket 数,如果是普通(免费)用户为 2,超过这个数连接响应会报错,具体参考官网。
|
|
||||||
QPS: 2
|
QPS: 2
|
||||||
# 用于权限验证,从服务接口认证信息中获取
|
|
||||||
appId: 9f22c545
|
appId: 9f22c545
|
||||||
# 用于权限验证,从服务接口认证信息中获取
|
|
||||||
apiKey: a8e17cdcbc9addd7f21e81dff71bc177
|
apiKey: a8e17cdcbc9addd7f21e81dff71bc177
|
||||||
# 用于权限验证,从服务接口认证信息中获取
|
|
||||||
apiSecret: N2NjMDU3ZTQwYjBkODI5ZGM3ZTVjYWY3
|
apiSecret: N2NjMDU3ZTQwYjBkODI5ZGM3ZTVjYWY3
|
||||||
|
|
Loading…
Reference in New Issue