|
|
@@ -22,6 +22,7 @@ import com.ejlchina.searcher.util.MapBuilder;
|
|
|
import com.ejlchina.searcher.util.MapUtils;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
@@ -135,34 +136,37 @@ public class MirrorController {
|
|
|
if (user == null) {
|
|
|
response.setStatus(401);
|
|
|
}
|
|
|
- //4.获取gpt-4 的规则限制
|
|
|
- //text-davinci-002-render-sha 3.5
|
|
|
- ConversationLimitResponse conversationLimitResponse = chatGptAccountService.conversationLimit(userToken, conversationRequest.getModel(), conversationRequest.getCarId(), user);
|
|
|
+ if(StringUtils.isNotBlank(conversationRequest.getCarId())){
|
|
|
+ //4.获取gpt-4 的规则限制
|
|
|
+ //text-davinci-002-render-sha 3.5
|
|
|
+ ConversationLimitResponse conversationLimitResponse = chatGptAccountService.conversationLimit(userToken, conversationRequest.getModel(), conversationRequest.getCarId(), user);
|
|
|
+
|
|
|
+ OutputStream out = null;
|
|
|
+ String messgae = "";
|
|
|
+ if (conversationLimitResponse.isLimited()) {
|
|
|
+ response.setStatus(429);
|
|
|
+ String json = "{\"detail\":{\"message\":\"您的账号已达到GPT-4的使用上限。您现在可以继续使用默认模型,或者重试在\",\"code\":\"model_cap_exceeded\",\"clears_in\":2687}}";
|
|
|
+ JSONObject res = new JSONObject(json);
|
|
|
+ res.putOpt("clears_in", conversationLimitResponse.getNextAvailableTime() / 1000);
|
|
|
+ messgae = res.toString();
|
|
|
+ } else {
|
|
|
+ response.setStatus(200);
|
|
|
+ }
|
|
|
+ //5.获取 conversationRequest 中的model 字段,判断是否为 gpt-4 如果为gpt-4 执行 查看是否达到限制方法
|
|
|
+ //6.如果达到限制,返回状态码 429 并返回文本 xxx
|
|
|
+ try {
|
|
|
+ out = response.getOutputStream();
|
|
|
+ IoKit.write(messgae, out);
|
|
|
+ } catch (Throwable e) {
|
|
|
+ log.error("conversationLimit write response error", e);
|
|
|
+ } finally {
|
|
|
+ IoKit.close(out);
|
|
|
+ }
|
|
|
+ }
|
|
|
//记录提问时间,车次,标题和模型
|
|
|
TASK_EXECUTOR.execute(() -> {
|
|
|
chatGptAccountService.saveConversationRecord(userToken, conversationRequest);
|
|
|
});
|
|
|
- OutputStream out = null;
|
|
|
- String messgae = "";
|
|
|
- if (conversationLimitResponse.isLimited()) {
|
|
|
- response.setStatus(429);
|
|
|
- String json = "{\"detail\":{\"message\":\"您的账号已达到GPT-4的使用上限。您现在可以继续使用默认模型,或者重试在\",\"code\":\"model_cap_exceeded\",\"clears_in\":2687}}";
|
|
|
- JSONObject res = new JSONObject(json);
|
|
|
- res.putOpt("clears_in", conversationLimitResponse.getNextAvailableTime() / 1000);
|
|
|
- messgae = res.toString();
|
|
|
- } else {
|
|
|
- response.setStatus(200);
|
|
|
- }
|
|
|
- //5.获取 conversationRequest 中的model 字段,判断是否为 gpt-4 如果为gpt-4 执行 查看是否达到限制方法
|
|
|
- //6.如果达到限制,返回状态码 429 并返回文本 xxx
|
|
|
- try {
|
|
|
- out = response.getOutputStream();
|
|
|
- IoKit.write(messgae, out);
|
|
|
- } catch (Throwable e) {
|
|
|
- log.error("conversationLimit write response error", e);
|
|
|
- } finally {
|
|
|
- IoKit.close(out);
|
|
|
- }
|
|
|
response.setStatus(200);
|
|
|
}
|
|
|
|
|
|
@@ -217,8 +221,20 @@ public class MirrorController {
|
|
|
*/
|
|
|
@GetMapping("/gpt/car/limited")
|
|
|
public Result<String> carLimited(String carId, Integer expTime) {
|
|
|
+ String authorization = request.getHeader("Authorization");
|
|
|
+ String userToken = "";
|
|
|
+ if(StringUtils.isNotBlank(authorization)){
|
|
|
+ try {
|
|
|
+ userToken = authorization.substring(7);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("获取用户token失败",e);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String finalUserToken = userToken;
|
|
|
TASK_EXECUTOR.execute(()->{
|
|
|
- chatGptAccountService.carLimited(carId, expTime);
|
|
|
+ chatGptAccountService.carLimited(carId, finalUserToken, expTime);
|
|
|
});
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
}
|