|
|
|
package com.zhonglai.luhui.openai.controller;
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.common.core.controller.BaseController;
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
|
import com.ruoyi.system.login.dto.OpenAiLoginUser;
|
|
|
|
import com.ruoyi.system.login.dto.OpenAiUserInfo;
|
|
|
|
import com.ruoyi.system.service.PublicService;
|
|
|
|
import com.theokanning.openai.Usage;
|
|
|
|
import com.zhonglai.luhui.openai.dto.*;
|
|
|
|
import com.zhonglai.luhui.openai.service.VipServiceImpl;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import okhttp3.*;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
@Api(tags = "chatGPT流接口")
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/chatGPTStream")
|
|
|
|
public class ChatGPTStreamController extends BaseController {
|
|
|
|
private static String sessionkey = "CHAT_HISTORY_CONTEXT";//上下文关联存放地址
|
|
|
|
@Autowired
|
|
|
|
private PublicService publicService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private VipServiceImpl vipService;
|
|
|
|
@ApiOperation(value = "chatgpt3.5",notes = "上下文关联,要实现上下问关联就需要记录整个聊天的提问记录,目前只支持相同session的上下文关联,就是说,如果聊天页面关闭以后再打开,是没有上下文关联的")
|
|
|
|
@Transactional
|
|
|
|
@RequestMapping(value = "/chatgptV3_5",method = RequestMethod.POST)
|
|
|
|
public void chatgptV3_5(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestBody ChatgptMessageDto chatgptMessageDto) throws IOException {
|
|
|
|
//跟进用户信息生成入口参数
|
|
|
|
OpenAiLoginUser userInfo = (OpenAiLoginUser) getLoginUser();
|
|
|
|
Integer user_id= userInfo.getUserId().intValue();
|
|
|
|
String room_id = String.valueOf(user_id);
|
|
|
|
|
|
|
|
OpenAiUserInfo openAiUserInfo = (OpenAiUserInfo) userInfo.getUser();
|
|
|
|
|
|
|
|
|
|
|
|
PrintWriter out = httpServletResponse.getWriter();
|
|
|
|
//验证余额是否充足
|
|
|
|
if(vipService.isCharging(openAiUserInfo.getVip_level()) && openAiUserInfo.getFlow_packet_remain()<=0)
|
|
|
|
{
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
stringBuffer.append("您的余额不足请联系管理员或者点击链接充值:\\n\\n");
|
|
|
|
stringBuffer.append("https://充值链接.com");
|
|
|
|
out.write(stringBuffer.toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BigDecimal[] bs = vipService.getUnitprice();
|
|
|
|
BigDecimal openaiUnitprice = bs[0];
|
|
|
|
BigDecimal realityUnitprice = bs[1];
|
|
|
|
if(openaiUnitprice.doubleValue()==0 || realityUnitprice.doubleValue()==0)
|
|
|
|
{
|
|
|
|
out.write("系统未配置流量单价,请联系管理员");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("{}生成聊天会话:",room_id);
|
|
|
|
|
|
|
|
//上下文关联
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
List<ChatRoomMessages> messageList = Optional.ofNullable((List<ChatRoomMessages>) session.getAttribute(sessionkey)).orElse(new ArrayList<>());
|
|
|
|
ChatRoomMessages chatRoomMessages = new ChatRoomMessages();
|
|
|
|
chatRoomMessages.setRole("user");
|
|
|
|
chatRoomMessages.setContent(chatgptMessageDto.getData());
|
|
|
|
messageList.add(chatRoomMessages);
|
|
|
|
session.setAttribute(sessionkey,messageList);
|
|
|
|
|
|
|
|
GptMessage gptMessage = new GptMessage();
|
|
|
|
gptMessage.setRoom_id(room_id);
|
|
|
|
gptMessage.setUser_id(user_id);
|
|
|
|
gptMessage.setSend_role(chatRoomMessages.getRole());
|
|
|
|
gptMessage.setSend_content(chatRoomMessages.getContent());
|
|
|
|
gptMessage.setCreate_time(DateUtils.getNowTimeMilly());
|
|
|
|
publicService.insertToTable(gptMessage,"`lk_openai`.`gpt_message`");
|
|
|
|
//获取返回参数
|
|
|
|
|
|
|
|
ChatGPTApiDto chatGPTApiDto = new ChatGPTApiDto();
|
|
|
|
chatGPTApiDto.setGptMessage_id(gptMessage.getId());
|
|
|
|
chatGPTApiDto.setMessageList(messageList);
|
|
|
|
chatGPTApiDto.setUser_id(user_id);
|
|
|
|
chatGPTApiDto.setRoom_id(room_id);
|
|
|
|
chatGPTApiDto.setIsfree(vipService.isfree(openAiUserInfo.getVip_level()));
|
|
|
|
HttpUtil.post("https://chatgpt.njlaikun.com/chatGPTApi/sendMessage", JSONObject.toJSONString(chatGPTApiDto));
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
.url("https://chatgpt.njlaikun.com/chatGPTApi/sendMessage")
|
|
|
|
.post(okhttp3.RequestBody.create(JSONObject.toJSONString(chatGPTApiDto), MediaType.parse("application/json;")))
|
|
|
|
.build();
|
|
|
|
OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
|
okHttpClient.newCall(request).enqueue(new Callback() {
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
|
|
|
|
InputStream inputStream = response.body().byteStream();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation(value = "历史记录",notes = "默认获取最新5条记录")
|
|
|
|
@Transactional
|
|
|
|
@RequestMapping(value = "/upUserFlowPacketRemain",method = RequestMethod.POST)
|
|
|
|
public AjaxResult upUserFlowPacketRemain(@RequestBody CompletionResult3_5 completionResult3_5 )
|
|
|
|
{
|
|
|
|
Usage usage = completionResult3_5.getUsage();
|
|
|
|
List<CompletionChoice3_5> list3_5 = completionResult3_5.getChoices();
|
|
|
|
|
|
|
|
if(null != list3_5 && list3_5.size() !=0 )
|
|
|
|
{
|
|
|
|
GptMessage gptMessage = new GptMessage();
|
|
|
|
gptMessage.setSend_size(0);
|
|
|
|
gptMessage.setPrompt_tokens(0l);
|
|
|
|
gptMessage.setCompletion_tokens(0l);
|
|
|
|
gptMessage.setTotal_tokens(0l);
|
|
|
|
gptMessage.setMessage_size(0);
|
|
|
|
for (CompletionChoice3_5 completionChoice:list3_5)
|
|
|
|
{
|
|
|
|
gptMessage.setSend_size(gptMessage.getSend_size()+gptMessage.getSend_content().length());
|
|
|
|
//统计代币
|
|
|
|
gptMessage.setCompletion_tokens(gptMessage.getCompletion_tokens()+usage.getCompletionTokens());
|
|
|
|
gptMessage.setPrompt_tokens(gptMessage.getPrompt_tokens()+usage.getPromptTokens());
|
|
|
|
gptMessage.setTotal_tokens(gptMessage.getTotal_tokens()+usage.getTotalTokens());
|
|
|
|
|
|
|
|
if(null != completionChoice.getMessage())
|
|
|
|
{
|
|
|
|
gptMessage.setMessage_role(completionChoice.getMessage().getRole());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(null != gptMessage.getMessage_content())
|
|
|
|
{
|
|
|
|
gptMessage.setMessage_size(gptMessage.getMessage_size()+gptMessage.getMessage_content().length());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
gptMessage.setId(completionResult3_5.getGptMessage_id());
|
|
|
|
publicService.updateObjectByTable(gptMessage,"id","`lk_openai`.`gpt_message`");
|
|
|
|
|
|
|
|
publicService.updateBySql("UPDATE `lk_openai`.`user_info` SET flow_packet_remain=flow_packet_remain-"+usage.getTotalTokens()+" WHERE id="+completionResult3_5.getUser_id());
|
|
|
|
|
|
|
|
UserFlowPacketRemainLog userFlowPacketRemainLog = new UserFlowPacketRemainLog();
|
|
|
|
userFlowPacketRemainLog.setCreate_time(DateUtils.getNowTimeMilly());
|
|
|
|
userFlowPacketRemainLog.setUser_id(completionResult3_5.getUser_id());
|
|
|
|
userFlowPacketRemainLog.setType(2); //消费
|
|
|
|
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
stringBuffer.append("房间号:");
|
|
|
|
stringBuffer.append(completionResult3_5.getRoom_id());
|
|
|
|
stringBuffer.append(";");
|
|
|
|
stringBuffer.append("发送代币:");
|
|
|
|
stringBuffer.append(usage.getPromptTokens());
|
|
|
|
stringBuffer.append(";");
|
|
|
|
stringBuffer.append("返回代币:");
|
|
|
|
stringBuffer.append(usage.getCompletionTokens());
|
|
|
|
stringBuffer.append(";");
|
|
|
|
|
|
|
|
userFlowPacketRemainLog.setDescribe(stringBuffer.toString());
|
|
|
|
userFlowPacketRemainLog.setTotal_tokens(usage.getTotalTokens());
|
|
|
|
|
|
|
|
BigDecimal[] bs = vipService.getUnitprice();
|
|
|
|
BigDecimal openaiUnitprice = bs[0];
|
|
|
|
BigDecimal realityUnitprice = bs[1];
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|