master
parent
01fbfdd5b6
commit
1c2ff32bfe
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -2,5 +2,5 @@ package com.sikadi.user.service;
|
|||
|
||||
public interface GTPService {
|
||||
|
||||
String sendQuestion(String question);
|
||||
String send(String ques);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue