作者 钟来

模块整理

... ... @@ -14,6 +14,7 @@ import okhttp3.sse.EventSourceListener;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.net.URLEncoder;
import java.util.Objects;
/**
... ... @@ -55,7 +56,7 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
@SneakyThrows
@Override
public void onEvent(EventSource eventSource, String id, String type, String data) {
log.info("OpenAI返回数据{}:{}",type, data);
log.info("OpenAI返回数据:{}", data);
tokens += 1;
if (data.equals("[DONE]")) {
log.info("OpenAI返回数据结束了");
... ... @@ -82,13 +83,18 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
Message delta = completionResponse.getChoices().get(0).getDelta();
if(null != delta.getContent())
{
String content = delta.getContent();
if(isHaveData)
{
sseEmitter.send(delta.getContent(), MediaType.TEXT_EVENT_STREAM);
// if(content.startsWith("\n") || content.endsWith("\n") )
// {
// content = content.replace("\n","~n~");
// }
sseEmitter.send(URLEncoder.encode(content,"utf-8").replaceAll("\\+", "%20"), MediaType.TEXT_EVENT_STREAM);
}else{
sseEmitter.send(new MyEvent().data(delta.getContent(), MediaType.TEXT_EVENT_STREAM));
sseEmitter.send(new MyEvent().data(content, MediaType.TEXT_EVENT_STREAM));
}
contents.append(delta.getContent());
contents.append(content);
}
// sseEmitter.send(SseEmitter.event()
// .id(completionResponse.getId())
... ...
... ... @@ -39,6 +39,7 @@ public class SseServiceImpl implements SseService {
@Override
public SseEmitter createSse(String uid) {
log.info("[{}]开始创建sse链接", uid);
//默认30秒超时,设置为0L则永不超时
SseEmitter sseEmitter = new SseEmitter(0l);
//完成后回调
... ... @@ -66,11 +67,6 @@ public class SseServiceImpl implements SseService {
}
}
);
// try {
// sseEmitter.send(SseEmitter.event().reconnectTime(5000));
// } catch (IOException e) {
// e.printStackTrace();
// }
LocalCache.CACHE.put(uid, sseEmitter);
log.info("[{}]创建sse连接成功!", uid);
return sseEmitter;
... ...
... ... @@ -12,7 +12,10 @@ import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
@ComponentScan(basePackages = {
"com.ruoyi.common",
... ...
... ... @@ -2,6 +2,7 @@ package com.zhonglai.luhui.openai.controller;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.exception.BaseException;
import com.zhonglai.luhui.action.BaseController;
import com.zhonglai.luhui.chatgpt.controller.request.ChatRequest;
... ... @@ -84,6 +85,10 @@ public class ChatGPTStreamController extends BaseController {
completionChoiceMessage3_5.setContent(stringBuffer.toString());
throw new BaseException("您的余额不足请联系管理员或者充值");
}
if(vipService.isVipModel(openAiUserInfo.getVip_level()))
{
chatRequest.setModel(ChatCompletion.Model.GPT_4_0314);
}
sseService.sseChat(true,user_id,uid, chatRequest, chatRequest.getModel(),completeCallback);
}catch (Exception e)
{
... ... @@ -107,7 +112,7 @@ public class ChatGPTStreamController extends BaseController {
}
@ApiOperation(value = "聊天接口返回data")
@ApiOperation(value = "聊天接口返回data")
@Transactional
@PostMapping("/chatNotData")
@ResponseBody
... ... @@ -121,6 +126,7 @@ public class ChatGPTStreamController extends BaseController {
SseEmitter sseEmitter = sseService.createSse(uid);
scheduledExecutorService.schedule(() -> {
log.info("{}开始启动业务程序", uid);
try{
OpenAiUserInfo openAiUserInfo = (OpenAiUserInfo) userInfo.getUser();
//验证余额是否充足
... ... @@ -134,6 +140,7 @@ public class ChatGPTStreamController extends BaseController {
completionChoiceMessage3_5.setContent(stringBuffer.toString());
throw new BaseException("您的余额不足请联系管理员或者充值");
}
log.info("{}验证通过开始发送chatgpt请求", uid);
sseService.sseChat(false,user_id,uid, chatRequest, chatRequest.getModel(),completeCallback);
}catch (Exception e)
{
... ...
... ... @@ -24,6 +24,7 @@ public class VipServiceImpl {
private static List<Boolean> chargingList = new ArrayList<>(); //是否计费
private static List<Boolean> timingList = new ArrayList<>(); //是否计计时
private static List<Boolean> modelList = new ArrayList<>(); //是否使用最新模型
@PostConstruct
public void init()
{
... ... @@ -34,6 +35,7 @@ public class VipServiceImpl {
* vip3(包月用户,使用vip2的接口)
* vip4(包季,使用vip2的接口)
* vip5(包年,使用vip2的接口)
* vip6 4.0接口
*/
freeList.add(0,true);
... ... @@ -42,6 +44,7 @@ public class VipServiceImpl {
freeList.add(3,false);
freeList.add(4,false);
freeList.add(5,false);
freeList.add(6,false);
chargingList.add(0,true);
chargingList.add(1,true);
... ... @@ -49,6 +52,7 @@ public class VipServiceImpl {
chargingList.add(3,true);
chargingList.add(4,false);
chargingList.add(5,false);
chargingList.add(5,false);
timingList.add(0,false);
timingList.add(1,false);
... ... @@ -56,6 +60,15 @@ public class VipServiceImpl {
timingList.add(3,true);
timingList.add(4,true);
timingList.add(5,true);
timingList.add(6,true);
modelList.add(0,false);
modelList.add(1,false);
modelList.add(2,false);
modelList.add(3,false);
modelList.add(4,false);
modelList.add(5,false);
modelList.add(6,true);
}
public boolean isfree(Integer vipLevel)
{
... ... @@ -70,6 +83,11 @@ public class VipServiceImpl {
return timingList.get(vipLevel);
}
public boolean isVipModel(Integer vipLevel)
{
return modelList.get(vipLevel);
}
public BigDecimal[] getUnitprice()
{
List<Map<String,Object>> unitpriceList = publicService.getObjectListBySQL("select `key`,`value` from `lk_openai`.`sys_config` where `key_type`='"+ SysConfigKeyType.gpt_3_5_unitprice+"'");
... ...
... ... @@ -11,6 +11,17 @@
<artifactId>lh-modules</artifactId>
<modules>
<module>lh-admin</module>
<module>lh-afms</module>
<module>lh-alarm</module>
<module>lh-api</module>
<module>lh-backups</module>
<module>lh-log</module>
<module>lh-mqtt-service</module>
<module>lh-openai</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
... ...
... ... @@ -8,24 +8,41 @@
<artifactId>Luhui</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>ruoyi-framework</module>
<module>lh-admin</module>
<module>lh-mqtt-service</module>
<module>lh-api</module>
<module>lh-central-control</module>
<module>lh-backups</module>
<module>lh-alarm</module>
<module>lh-log</module>
<module>lh-chatgpt-api</module>
<module>lh-common</module>
<module>lh-jar</module>
<module>lh-jar/lh-jar-sys-service</module>
<module>lh-modules</module>
<module>lh-common/lh-common-swagger</module>
<module>lh-common/lh-common-datasource</module>
<module>lh-common/lh-common-firewall</module>
<module>lh-common/lh-common-log</module>
<module>lh-common/lh-common-swagger</module>
<module>lh-common/lh-domain</module>
<module>lh-common/lh-public-dao</module>
<module>lh-common/lh-quartz</module>
<module>lh-common/ruoyi-auth</module>
<module>lh-common/ruoyi-common</module>
<module>lh-common/ruoyi-common-core</module>
<module>lh-common/ruoyi-common-redis</module>
<module>lh-common/ruoyi-common-security</module>
<module>lh-common/ruoyi-framework</module>
<module>lh-common/ruoyi-generator</module>
<module>lh-common/ruoyi-system</module>
<module>lh-jar</module>
<module>lh-jar/lh-jar-action</module>
<module>lh-jar/lh-jar-chatgpt</module>
<module>lh-jar/lh-jar-rocketmq</module>
<module>lh-jar/lh-jar-sys-service</module>
<module>lh-modules</module>
<module>lh-modules/lh-admin</module>
<module>lh-modules/lh-afms</module>
<module>lh-modules/lh-alarm</module>
<module>lh-modules/lh-api</module>
<module>lh-modules/lh-backups</module>
<module>lh-modules/lh-log</module>
<module>lh-modules/lh-mqtt-service</module>
<module>lh-modules/lh-openai</module>
</modules>
<packaging>pom</packaging>
... ...