作者 钟来

Default Changelist

1 package com.zhonglai.luhui.openai.controller; 1 package com.zhonglai.luhui.openai.controller;
2 2
  3 +import cn.hutool.http.HttpRequest;
3 import cn.hutool.http.HttpUtil; 4 import cn.hutool.http.HttpUtil;
4 import com.alibaba.fastjson.JSON; 5 import com.alibaba.fastjson.JSON;
5 import com.alibaba.fastjson.JSONObject; 6 import com.alibaba.fastjson.JSONObject;
@@ -140,12 +141,13 @@ public class ChatGPTController extends BaseController { @@ -140,12 +141,13 @@ public class ChatGPTController extends BaseController {
140 141
141 //获取返回参数 142 //获取返回参数
142 CompletionResult3_5 completionResult3_5 = null; 143 CompletionResult3_5 completionResult3_5 = null;
143 - if(vipService.isfree(openAiUserInfo.getVip_level()))  
144 - {  
145 - completionResult3_5 = sendFreeGPTAi(messageList);  
146 - }else{  
147 - completionResult3_5 = sendGPTAi(messageList);  
148 - } 144 + completionResult3_5 = sendGptAi(messageList,vipService.isfree(openAiUserInfo.getVip_level()));
  145 +// if(vipService.isfree(openAiUserInfo.getVip_level()))
  146 +// {
  147 +// completionResult3_5 = sendFreeGPTAi(messageList);
  148 +// }else{
  149 +// completionResult3_5 = sendGPTAi(messageList);
  150 +// }
149 151
150 Usage usage = completionResult3_5.getUsage(); 152 Usage usage = completionResult3_5.getUsage();
151 153
@@ -232,6 +234,30 @@ public class ChatGPTController extends BaseController { @@ -232,6 +234,30 @@ public class ChatGPTController extends BaseController {
232 System.out.println(r.doubleValue()); 234 System.out.println(r.doubleValue());
233 } 235 }
234 236
  237 + private CompletionResult3_5 sendGptAi(List<ChatRoomMessages> messageList,boolean isfree)
  238 + {
  239 + int timeout = 3000000;
  240 +
  241 + Map<String,String> map = new HashMap<>();
  242 + if(isfree)
  243 + {
  244 + map.put("Authorization","Bearer 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");
  245 + }else{
  246 + map.put("Authorization","Bearer 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");
  247 + }
  248 + map.put("Content-Type","application/json");
  249 +
  250 + JSONObject jsonObject = new JSONObject();
  251 + jsonObject.put("model","gpt-3.5-turbo-0301");
  252 + jsonObject.put("messages",messageList);
  253 +
  254 + logger.info("开始请求openai接口:{}",jsonObject);
  255 + String str = HttpRequest.post("https://api.openai.com/v1/chat/completions").setReadTimeout(timeout).timeout(timeout).addHeaders(map).body(jsonObject.toString()).execute().body();
  256 + logger.info("返回的数据:{}",str);
  257 + CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class);
  258 + return completionResult;
  259 + }
  260 +
235 261
236 /** 262 /**
237 * 付费接口 263 * 付费接口
@@ -244,8 +270,8 @@ public class ChatGPTController extends BaseController { @@ -244,8 +270,8 @@ public class ChatGPTController extends BaseController {
244 JSONObject jsonObject = new JSONObject(); 270 JSONObject jsonObject = new JSONObject();
245 jsonObject.put("model","gpt-3.5-turbo-0301"); 271 jsonObject.put("model","gpt-3.5-turbo-0301");
246 jsonObject.put("messages",messageList); 272 jsonObject.put("messages",messageList);
247 -// String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendNotFreeMessage",jsonObject.toString());  
248 - String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString()); 273 + String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendNotFreeMessage",jsonObject.toString());
  274 +// String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString());
249 275
250 logger.info("返回的数据:{}",str); 276 logger.info("返回的数据:{}",str);
251 CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class); 277 CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class);
@@ -263,8 +289,8 @@ public class ChatGPTController extends BaseController { @@ -263,8 +289,8 @@ public class ChatGPTController extends BaseController {
263 JSONObject jsonObject = new JSONObject(); 289 JSONObject jsonObject = new JSONObject();
264 jsonObject.put("model","gpt-3.5-turbo-0301"); 290 jsonObject.put("model","gpt-3.5-turbo-0301");
265 jsonObject.put("messages",messageList); 291 jsonObject.put("messages",messageList);
266 -// String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendFreeMessage",jsonObject.toString());  
267 - String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString()); 292 + String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendFreeMessage",jsonObject.toString());
  293 +// String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString());
268 294
269 logger.info("返回的数据:{}",str); 295 logger.info("返回的数据:{}",str);
270 CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class); 296 CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class);