作者 钟来

模块整理

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