|
...
|
...
|
@@ -26,8 +26,6 @@ import com.zhonglai.luhui.openai.utils.OpenAiUtils; |
|
|
|
import com.zhonglai.luhui.openai.utils.SysConfigKeyType;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
|
|
|
import org.apache.catalina.Session;
|
|
|
|
import org.apache.commons.io.input.ReaderInputStream;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
...
|
...
|
@@ -93,7 +91,7 @@ public class ChatGPTController extends BaseController { |
|
|
|
OpenAiLoginUser userInfo = (OpenAiLoginUser) getLoginUser();
|
|
|
|
Integer user_id= userInfo.getUserId().intValue();
|
|
|
|
String room_id = String.valueOf(user_id);
|
|
|
|
List<Map<String,Object>> list = publicService.getObjectListBySQL("SELECT * FROM `lk_openai`.`gpt_message` WHERE room_id="+room_id+" AND user_id="+user_id+" order by create_time desc 5");
|
|
|
|
List<Map<String,Object>> list = publicService.getObjectListBySQL("SELECT * FROM `lk_openai`.`gpt_message` WHERE room_id="+room_id+" AND user_id="+user_id+" order by create_time desc limit 5");
|
|
|
|
Collections.reverse(list);
|
|
|
|
return AjaxResult.success(list);
|
|
|
|
}
|
|
...
|
...
|
@@ -129,7 +127,7 @@ public class ChatGPTController extends BaseController { |
|
|
|
BigDecimal[] bs = vipService.getUnitprice();
|
|
|
|
BigDecimal openaiUnitprice = bs[0];
|
|
|
|
BigDecimal realityUnitprice = bs[1];
|
|
|
|
if(openaiUnitprice.intValue()==0 || realityUnitprice.intValue()==0)
|
|
|
|
if(openaiUnitprice.doubleValue()==0 || realityUnitprice.doubleValue()==0)
|
|
|
|
{
|
|
|
|
rlist.add(new CompletionChoiceMessage3_5("assistant","系统未配置流量单价,请联系管理员"));
|
|
|
|
return AjaxResult.success(rlist);
|
|
...
|
...
|
@@ -139,7 +137,7 @@ public class ChatGPTController extends BaseController { |
|
|
|
|
|
|
|
//上下文关联
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
List<ChatRoomMessages> messageList = Optional.ofNullable((List<Map<String, Object>>) session.getAttribute(sessionkey)).orElse(new ArrayList<>()).stream().map(item->mapToChatRoomMessages(item)).collect(Collectors.toList());
|
|
|
|
List<ChatRoomMessages> messageList = Optional.ofNullable((List<ChatRoomMessages>) session.getAttribute(sessionkey)).orElse(new ArrayList<>());
|
|
|
|
ChatRoomMessages chatRoomMessages = new ChatRoomMessages();
|
|
|
|
chatRoomMessages.setRole("user");
|
|
|
|
chatRoomMessages.setContent(data);
|
|
...
|
...
|
@@ -150,9 +148,9 @@ public class ChatGPTController extends BaseController { |
|
|
|
CompletionResult3_5 completionResult3_5 = null;
|
|
|
|
if(vipService.isfree(openAiUserInfo.getVip_level()))
|
|
|
|
{
|
|
|
|
completionResult3_5 = sendGPTAi(messageList);
|
|
|
|
}else{
|
|
|
|
completionResult3_5 = sendFreeGPTAi(messageList);
|
|
|
|
}else{
|
|
|
|
completionResult3_5 = sendGPTAi(messageList);
|
|
|
|
}
|
|
|
|
|
|
|
|
Usage usage = completionResult3_5.getUsage();
|
|
...
|
...
|
@@ -206,7 +204,7 @@ public class ChatGPTController extends BaseController { |
|
|
|
publicService.insertAllToTable(insertGptMessages,"`lk_openai`.`gpt_message`");
|
|
|
|
}
|
|
|
|
|
|
|
|
publicService.updateBySql("UPDATE `user_info` SET flow_packet_remain=flow_packet_remain-"+usage.getTotalTokens()+" WHERE id="+openAiUserInfo.getId());
|
|
|
|
publicService.updateBySql("UPDATE `lk_openai`.`user_info` SET flow_packet_remain=flow_packet_remain-"+usage.getTotalTokens()+" WHERE id="+openAiUserInfo.getId());
|
|
|
|
|
|
|
|
UserFlowPacketRemainLog userFlowPacketRemainLog = new UserFlowPacketRemainLog();
|
|
|
|
userFlowPacketRemainLog.setCreate_time(DateUtils.getNowTimeMilly());
|
|
...
|
...
|
@@ -214,22 +212,31 @@ public class ChatGPTController extends BaseController { |
|
|
|
userFlowPacketRemainLog.setType(2); //消费
|
|
|
|
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
stringBuffer.append("房间号:");
|
|
|
|
stringBuffer.append("房间号:");
|
|
|
|
stringBuffer.append(room_id);
|
|
|
|
stringBuffer.append(";");
|
|
|
|
stringBuffer.append("发送代币:");
|
|
|
|
stringBuffer.append(usage.getPromptTokens());
|
|
|
|
stringBuffer.append("返回代币:");
|
|
|
|
stringBuffer.append(";");
|
|
|
|
stringBuffer.append("返回代币:");
|
|
|
|
stringBuffer.append(usage.getCompletionTokens());
|
|
|
|
stringBuffer.append(";");
|
|
|
|
userFlowPacketRemainLog.setDescribe(stringBuffer.toString());
|
|
|
|
userFlowPacketRemainLog.setTotal_tokens(usage.getTotalTokens());
|
|
|
|
|
|
|
|
userFlowPacketRemainLog.setOpenai_money(openaiUnitprice.multiply(new BigDecimal(usage.getTotalTokens())).divide(new BigDecimal(1000),6));
|
|
|
|
userFlowPacketRemainLog.setReality_money(realityUnitprice.multiply(new BigDecimal(usage.getTotalTokens())).divide(new BigDecimal(1000),6));
|
|
|
|
userFlowPacketRemainLog.setOpenai_money((openaiUnitprice.multiply(new BigDecimal(usage.getTotalTokens()))).divide(new BigDecimal(1000),6,BigDecimal.ROUND_HALF_UP));
|
|
|
|
userFlowPacketRemainLog.setReality_money((realityUnitprice.multiply(new BigDecimal(usage.getTotalTokens()))).divide(new BigDecimal(1000),6,BigDecimal.ROUND_HALF_UP));
|
|
|
|
publicService.insertToTable(userFlowPacketRemainLog,"`lk_openai`.`user_flow_packet_remain_log`");
|
|
|
|
}
|
|
|
|
return AjaxResult.success(rlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
BigDecimal openaiUnitprice = new BigDecimal(0.03);
|
|
|
|
BigDecimal r = openaiUnitprice.multiply(new BigDecimal(61)).divide(new BigDecimal(1000),6);
|
|
|
|
System.out.println(r.doubleValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 付费接口
|
|
...
|
...
|
@@ -242,6 +249,7 @@ public class ChatGPTController extends BaseController { |
|
|
|
jsonObject.put("model","gpt-3.5-turbo-0301");
|
|
|
|
jsonObject.put("messages",messageList);
|
|
|
|
String str = HttpUtil.post("https://chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString());
|
|
|
|
logger.info("返回的数据:{}",str);
|
|
|
|
CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class);
|
|
|
|
return completionResult;
|
|
|
|
}
|
...
|
...
|
|