|
|
|
package com.zhonglai.luhui.openai.controller;
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
...
|
...
|
@@ -140,12 +141,13 @@ public class ChatGPTController extends BaseController { |
|
|
|
|
|
|
|
//获取返回参数
|
|
|
|
CompletionResult3_5 completionResult3_5 = null;
|
|
|
|
if(vipService.isfree(openAiUserInfo.getVip_level()))
|
|
|
|
{
|
|
|
|
completionResult3_5 = sendFreeGPTAi(messageList);
|
|
|
|
}else{
|
|
|
|
completionResult3_5 = sendGPTAi(messageList);
|
|
|
|
}
|
|
|
|
completionResult3_5 = sendGptAi(messageList,vipService.isfree(openAiUserInfo.getVip_level()));
|
|
|
|
// if(vipService.isfree(openAiUserInfo.getVip_level()))
|
|
|
|
// {
|
|
|
|
// completionResult3_5 = sendFreeGPTAi(messageList);
|
|
|
|
// }else{
|
|
|
|
// completionResult3_5 = sendGPTAi(messageList);
|
|
|
|
// }
|
|
|
|
|
|
|
|
Usage usage = completionResult3_5.getUsage();
|
|
|
|
|
|
...
|
...
|
@@ -215,7 +217,7 @@ public class ChatGPTController extends BaseController { |
|
|
|
stringBuffer.append("返回代币:");
|
|
|
|
stringBuffer.append(usage.getCompletionTokens());
|
|
|
|
stringBuffer.append(";");
|
|
|
|
|
|
|
|
|
|
|
|
userFlowPacketRemainLog.setDescribe(stringBuffer.toString());
|
|
|
|
userFlowPacketRemainLog.setTotal_tokens(usage.getTotalTokens());
|
|
|
|
|
|
...
|
...
|
@@ -232,6 +234,30 @@ public class ChatGPTController extends BaseController { |
|
|
|
System.out.println(r.doubleValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
private CompletionResult3_5 sendGptAi(List<ChatRoomMessages> messageList,boolean isfree)
|
|
|
|
{
|
|
|
|
int timeout = 3000000;
|
|
|
|
|
|
|
|
Map<String,String> map = new HashMap<>();
|
|
|
|
if(isfree)
|
|
|
|
{
|
|
|
|
map.put("Authorization","Bearer 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");
|
|
|
|
}else{
|
|
|
|
map.put("Authorization","Bearer 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");
|
|
|
|
}
|
|
|
|
map.put("Content-Type","application/json");
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
jsonObject.put("model","gpt-3.5-turbo-0301");
|
|
|
|
jsonObject.put("messages",messageList);
|
|
|
|
|
|
|
|
logger.info("开始请求openai接口:{}",jsonObject);
|
|
|
|
String str = HttpRequest.post("https://api.openai.com/v1/chat/completions").setReadTimeout(timeout).timeout(timeout).addHeaders(map).body(jsonObject.toString()).execute().body();
|
|
|
|
logger.info("返回的数据:{}",str);
|
|
|
|
CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class);
|
|
|
|
return completionResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 付费接口
|
|
...
|
...
|
@@ -244,8 +270,8 @@ public class ChatGPTController extends BaseController { |
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
jsonObject.put("model","gpt-3.5-turbo-0301");
|
|
|
|
jsonObject.put("messages",messageList);
|
|
|
|
// String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendNotFreeMessage",jsonObject.toString());
|
|
|
|
String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString());
|
|
|
|
String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendNotFreeMessage",jsonObject.toString());
|
|
|
|
// String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString());
|
|
|
|
|
|
|
|
logger.info("返回的数据:{}",str);
|
|
|
|
CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class);
|
|
...
|
...
|
@@ -263,8 +289,8 @@ public class ChatGPTController extends BaseController { |
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
jsonObject.put("model","gpt-3.5-turbo-0301");
|
|
|
|
jsonObject.put("messages",messageList);
|
|
|
|
// String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendFreeMessage",jsonObject.toString());
|
|
|
|
String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString());
|
|
|
|
String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendFreeMessage",jsonObject.toString());
|
|
|
|
// String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString());
|
|
|
|
|
|
|
|
logger.info("返回的数据:{}",str);
|
|
|
|
CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class);
|
...
|
...
|
|