正在显示
6 个修改的文件
包含
50 行增加
和
21 行删除
| @@ -26,6 +26,28 @@ public class ChatGPTApiController { | @@ -26,6 +26,28 @@ public class ChatGPTApiController { | ||
| 26 | 26 | ||
| 27 | protected final Logger logger = LoggerFactory.getLogger(this.getClass()); | 27 | protected final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 28 | 28 | ||
| 29 | + @ApiOperation(value = "发送免费的接口") | ||
| 30 | + @PostMapping("/sendFreeMessage") | ||
| 31 | + public String sendFreeMessage(@RequestBody String jsonStr) | ||
| 32 | + { | ||
| 33 | + int timeout = 3000000; | ||
| 34 | + Map<String,String> map = new HashMap<>(); | ||
| 35 | + map.put("Authorization","Bearer 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"); | ||
| 36 | + String str = HttpRequest.post("https://api.openai.com/v1/chat/completions").setReadTimeout(timeout).timeout(timeout).addHeaders(map).body(jsonStr).execute().body(); | ||
| 37 | + return str; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + @ApiOperation(value = "发送不免费的接口") | ||
| 41 | + @PostMapping("/sendNotFreeMessage") | ||
| 42 | + public String sendNotFreeMessage(@RequestBody String jsonStr) | ||
| 43 | + { | ||
| 44 | + int timeout = 3000000; | ||
| 45 | + Map<String,String> map = new HashMap<>(); | ||
| 46 | + map.put("Authorization","Bearer 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"); | ||
| 47 | + String str = HttpRequest.post("https://api.openai.com/v1/chat/completions").setReadTimeout(timeout).timeout(timeout).addHeaders(map).body(jsonStr).execute().body(); | ||
| 48 | + return str; | ||
| 49 | + } | ||
| 50 | + | ||
| 29 | @ApiOperation(value = "测试") | 51 | @ApiOperation(value = "测试") |
| 30 | @PostMapping("/sendMessage") | 52 | @PostMapping("/sendMessage") |
| 31 | public String sendMessage(HttpServletResponse response, @RequestBody ChatGPTApiDto chatGPTApiDto) | 53 | public String sendMessage(HttpServletResponse response, @RequestBody ChatGPTApiDto chatGPTApiDto) |
| @@ -37,10 +59,10 @@ public class ChatGPTApiController { | @@ -37,10 +59,10 @@ public class ChatGPTApiController { | ||
| 37 | jsonObject.put("model","gpt-3.5-turbo-0301"); | 59 | jsonObject.put("model","gpt-3.5-turbo-0301"); |
| 38 | jsonObject.put("messages",chatGPTApiDto.getMessageList()); | 60 | jsonObject.put("messages",chatGPTApiDto.getMessageList()); |
| 39 | 61 | ||
| 40 | -// String domainName = "api.openai.com"; | ||
| 41 | -// String apiDomain = "openai.yu2le.com/api"; | ||
| 42 | - String domainName = "chatgpt.njlaikun.com"; | ||
| 43 | - String apiDomain = "localhost:8082"; | 62 | + String domainName = "api.openai.com"; |
| 63 | + String apiDomain = "openai.yu2le.com/api"; | ||
| 64 | +// String domainName = "chatgpt.njlaikun.com"; | ||
| 65 | +// String apiDomain = "localhost:8082"; | ||
| 44 | 66 | ||
| 45 | Map<String,String> map = new HashMap<>(); | 67 | Map<String,String> map = new HashMap<>(); |
| 46 | if(chatGPTApiDto.getIsfree()) | 68 | if(chatGPTApiDto.getIsfree()) |
| @@ -50,6 +72,7 @@ public class ChatGPTApiController { | @@ -50,6 +72,7 @@ public class ChatGPTApiController { | ||
| 50 | map.put("Authorization","Bearer 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"); | 72 | map.put("Authorization","Bearer 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"); |
| 51 | } | 73 | } |
| 52 | map.put("Content-Type","application/json"); | 74 | map.put("Content-Type","application/json"); |
| 75 | + logger.info("开始请求openai接口:{}",JSONObject.toJSON(chatGPTApiDto)); | ||
| 53 | String str = HttpRequest.post("https://"+domainName+"/v1/chat/completions").setReadTimeout(timeout).timeout(timeout).addHeaders(map).body(jsonObject.toString()).execute().body(); | 76 | String str = HttpRequest.post("https://"+domainName+"/v1/chat/completions").setReadTimeout(timeout).timeout(timeout).addHeaders(map).body(jsonObject.toString()).execute().body(); |
| 54 | logger.info("返回的数据:{}",str); | 77 | logger.info("返回的数据:{}",str); |
| 55 | if(JSONUtil.isTypeJSONObject(str)) | 78 | if(JSONUtil.isTypeJSONObject(str)) |
| @@ -7,40 +7,34 @@ import com.ruoyi.common.core.controller.BaseController; | @@ -7,40 +7,34 @@ import com.ruoyi.common.core.controller.BaseController; | ||
| 7 | import com.ruoyi.common.core.domain.AjaxResult; | 7 | import com.ruoyi.common.core.domain.AjaxResult; |
| 8 | import com.ruoyi.common.core.domain.Message; | 8 | import com.ruoyi.common.core.domain.Message; |
| 9 | import com.ruoyi.common.core.domain.MessageCode; | 9 | import com.ruoyi.common.core.domain.MessageCode; |
| 10 | +import com.ruoyi.common.core.redis.RedisCache; | ||
| 10 | import com.ruoyi.common.utils.Arith; | 11 | import com.ruoyi.common.utils.Arith; |
| 11 | import com.ruoyi.common.utils.DateUtils; | 12 | import com.ruoyi.common.utils.DateUtils; |
| 12 | import com.ruoyi.common.utils.SecurityUtils; | 13 | import com.ruoyi.common.utils.SecurityUtils; |
| 13 | import com.ruoyi.common.utils.StringUtils; | 14 | import com.ruoyi.common.utils.StringUtils; |
| 14 | import com.ruoyi.common.utils.bean.BeanUtils; | 15 | import com.ruoyi.common.utils.bean.BeanUtils; |
| 15 | -import com.ruoyi.common.utils.html.HttpUtils; | ||
| 16 | -import com.ruoyi.common.utils.http.OkHttpUtils; | ||
| 17 | import com.ruoyi.system.login.dto.OpenAiLoginUser; | 16 | import com.ruoyi.system.login.dto.OpenAiLoginUser; |
| 18 | import com.ruoyi.system.login.dto.OpenAiUserInfo; | 17 | import com.ruoyi.system.login.dto.OpenAiUserInfo; |
| 18 | +import com.ruoyi.system.login.service.TokenService; | ||
| 19 | import com.ruoyi.system.service.PublicService; | 19 | import com.ruoyi.system.service.PublicService; |
| 20 | import com.theokanning.openai.Usage; | 20 | import com.theokanning.openai.Usage; |
| 21 | import com.theokanning.openai.completion.CompletionChoice; | 21 | import com.theokanning.openai.completion.CompletionChoice; |
| 22 | -import com.theokanning.openai.completion.CompletionResult; | ||
| 23 | import com.zhonglai.luhui.openai.dto.*; | 22 | import com.zhonglai.luhui.openai.dto.*; |
| 24 | import com.zhonglai.luhui.openai.service.VipServiceImpl; | 23 | import com.zhonglai.luhui.openai.service.VipServiceImpl; |
| 25 | import com.zhonglai.luhui.openai.utils.OpenAiUtils; | 24 | import com.zhonglai.luhui.openai.utils.OpenAiUtils; |
| 26 | -import com.zhonglai.luhui.openai.utils.SysConfigKeyType; | ||
| 27 | import io.swagger.annotations.Api; | 25 | import io.swagger.annotations.Api; |
| 28 | import io.swagger.annotations.ApiOperation; | 26 | import io.swagger.annotations.ApiOperation; |
| 29 | -import org.apache.catalina.Session; | ||
| 30 | -import org.apache.commons.io.input.ReaderInputStream; | ||
| 31 | import org.springframework.beans.factory.annotation.Autowired; | 27 | import org.springframework.beans.factory.annotation.Autowired; |
| 32 | import org.springframework.transaction.annotation.Transactional; | 28 | import org.springframework.transaction.annotation.Transactional; |
| 33 | import org.springframework.web.bind.annotation.*; | 29 | import org.springframework.web.bind.annotation.*; |
| 34 | 30 | ||
| 35 | import javax.servlet.http.HttpServletRequest; | 31 | import javax.servlet.http.HttpServletRequest; |
| 36 | -import javax.servlet.http.HttpServletResponse; | ||
| 37 | import javax.servlet.http.HttpSession; | 32 | import javax.servlet.http.HttpSession; |
| 38 | import java.io.ByteArrayOutputStream; | 33 | import java.io.ByteArrayOutputStream; |
| 39 | import java.io.IOException; | 34 | import java.io.IOException; |
| 40 | import java.io.InputStream; | 35 | import java.io.InputStream; |
| 41 | import java.math.BigDecimal; | 36 | import java.math.BigDecimal; |
| 42 | import java.util.*; | 37 | import java.util.*; |
| 43 | -import java.util.stream.Collectors; | ||
| 44 | 38 | ||
| 45 | @Api(tags = "chatGPT接口") | 39 | @Api(tags = "chatGPT接口") |
| 46 | @RestController | 40 | @RestController |
| @@ -54,6 +48,9 @@ public class ChatGPTController extends BaseController { | @@ -54,6 +48,9 @@ public class ChatGPTController extends BaseController { | ||
| 54 | 48 | ||
| 55 | private static String sessionkey = "CHAT_HISTORY_CONTEXT";//上下文关联存放地址 | 49 | private static String sessionkey = "CHAT_HISTORY_CONTEXT";//上下文关联存放地址 |
| 56 | 50 | ||
| 51 | + @Autowired | ||
| 52 | + private TokenService tokenService; | ||
| 53 | + | ||
| 57 | @ApiOperation("与AI机器进行聊天(废弃)") | 54 | @ApiOperation("与AI机器进行聊天(废弃)") |
| 58 | @RequestMapping(value = "/aiChatbot",method = RequestMethod.POST) | 55 | @RequestMapping(value = "/aiChatbot",method = RequestMethod.POST) |
| 59 | public AjaxResult aiChatbot(HttpServletRequest httpServletRequest, @RequestBody String data) | 56 | public AjaxResult aiChatbot(HttpServletRequest httpServletRequest, @RequestBody String data) |
| @@ -250,7 +247,9 @@ public class ChatGPTController extends BaseController { | @@ -250,7 +247,9 @@ public class ChatGPTController extends BaseController { | ||
| 250 | JSONObject jsonObject = new JSONObject(); | 247 | JSONObject jsonObject = new JSONObject(); |
| 251 | jsonObject.put("model","gpt-3.5-turbo-0301"); | 248 | jsonObject.put("model","gpt-3.5-turbo-0301"); |
| 252 | jsonObject.put("messages",messageList); | 249 | jsonObject.put("messages",messageList); |
| 253 | - String str = HttpUtil.post("https://chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString()); | 250 | + String str = HttpUtil.post("http://23.224.171.145:8086/chatGPTApi/sendNotFreeMessage",jsonObject.toString()); |
| 251 | +// String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString()); | ||
| 252 | + | ||
| 254 | logger.info("返回的数据:{}",str); | 253 | logger.info("返回的数据:{}",str); |
| 255 | CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class); | 254 | CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class); |
| 256 | return completionResult; | 255 | return completionResult; |
| @@ -267,7 +266,9 @@ public class ChatGPTController extends BaseController { | @@ -267,7 +266,9 @@ public class ChatGPTController extends BaseController { | ||
| 267 | JSONObject jsonObject = new JSONObject(); | 266 | JSONObject jsonObject = new JSONObject(); |
| 268 | jsonObject.put("model","gpt-3.5-turbo-0301"); | 267 | jsonObject.put("model","gpt-3.5-turbo-0301"); |
| 269 | jsonObject.put("messages",messageList); | 268 | jsonObject.put("messages",messageList); |
| 270 | - String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString()); | 269 | + String str = HttpUtil.post("https://23.224.171.145:8086/chatGPTApi/sendFreeMessage",jsonObject.toString()); |
| 270 | +// String str = HttpUtil.post("https://free.chatgpt.njlaikun.com/v1/chat/completions",jsonObject.toString()); | ||
| 271 | + | ||
| 271 | logger.info("返回的数据:{}",str); | 272 | logger.info("返回的数据:{}",str); |
| 272 | CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class); | 273 | CompletionResult3_5 completionResult = JSONObject.parseObject(str, CompletionResult3_5.class); |
| 273 | return completionResult; | 274 | return completionResult; |
| @@ -107,7 +107,7 @@ public class ChatGPTStreamController extends BaseController { | @@ -107,7 +107,7 @@ public class ChatGPTStreamController extends BaseController { | ||
| 107 | PrintWriter pout = null; | 107 | PrintWriter pout = null; |
| 108 | BufferedReader in = null; | 108 | BufferedReader in = null; |
| 109 | try { | 109 | try { |
| 110 | - URL url = new URL("http://localhost:8086/chatGPTApi/sendMessage"); | 110 | + URL url = new URL("http://23.224.171.145:8086/chatGPTApi/sendMessage"); |
| 111 | // 打开和URL之间的连接 | 111 | // 打开和URL之间的连接 |
| 112 | URLConnection conn = url.openConnection(); | 112 | URLConnection conn = url.openConnection(); |
| 113 | // 设置通用的请求属性 | 113 | // 设置通用的请求属性 |
| @@ -8,6 +8,8 @@ import com.ruoyi.common.utils.StringUtils; | @@ -8,6 +8,8 @@ import com.ruoyi.common.utils.StringUtils; | ||
| 8 | import com.ruoyi.common.utils.file.FileUploadUtils; | 8 | import com.ruoyi.common.utils.file.FileUploadUtils; |
| 9 | import com.ruoyi.common.utils.file.FileUtils; | 9 | import com.ruoyi.common.utils.file.FileUtils; |
| 10 | import com.ruoyi.framework.config.ServerConfig; | 10 | import com.ruoyi.framework.config.ServerConfig; |
| 11 | +import com.ruoyi.system.login.dto.OpenAiLoginUser; | ||
| 12 | +import com.ruoyi.system.login.dto.OpenAiUserInfo; | ||
| 11 | import com.ruoyi.system.service.PublicService; | 13 | import com.ruoyi.system.service.PublicService; |
| 12 | import io.swagger.annotations.Api; | 14 | import io.swagger.annotations.Api; |
| 13 | import io.swagger.annotations.ApiImplicitParam; | 15 | import io.swagger.annotations.ApiImplicitParam; |
| @@ -39,7 +41,9 @@ public class UserInfoController extends BaseController { | @@ -39,7 +41,9 @@ public class UserInfoController extends BaseController { | ||
| 39 | @GetMapping("/getUserInfo") | 41 | @GetMapping("/getUserInfo") |
| 40 | public AjaxResult getUserInfo() | 42 | public AjaxResult getUserInfo() |
| 41 | { | 43 | { |
| 42 | - return AjaxResult.success(getLoginUser().getUser()); | 44 | + //更新缓存积分 |
| 45 | + OpenAiUserInfo openAiUserInfo = publicService.getObjectForTableName(OpenAiUserInfo.class,"id",getUserId().toString(),"`lk_openai`.`user_info`"); | ||
| 46 | + return AjaxResult.success(openAiUserInfo); | ||
| 43 | } | 47 | } |
| 44 | 48 | ||
| 45 | @ApiOperation("修改头像") | 49 | @ApiOperation("修改头像") |
| @@ -97,11 +97,6 @@ public class LoginService { | @@ -97,11 +97,6 @@ public class LoginService { | ||
| 97 | AsyncManager.me().execute(AsyncFactory.recordLogininfor(user, SysLogininforType.openAi, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); | 97 | AsyncManager.me().execute(AsyncFactory.recordLogininfor(user, SysLogininforType.openAi, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); |
| 98 | OpenAiLoginUser loginUser = (OpenAiLoginUser) authentication.getPrincipal(); | 98 | OpenAiLoginUser loginUser = (OpenAiLoginUser) authentication.getPrincipal(); |
| 99 | loginUser.setSysLogininforType(SysLogininforType.openAi); | 99 | loginUser.setSysLogininforType(SysLogininforType.openAi); |
| 100 | -// OpenAiUserInfo openAiUserInfo = new OpenAiUserInfo(); | ||
| 101 | -// openAiUserInfo.setId(loginUser.getUserId().intValue()); | ||
| 102 | -// openAiUserInfo.s(IpUtils.getIpAddr(ServletUtils.getRequest())); | ||
| 103 | -// openAiUserInfo.setLastLoginTime(DateUtils.getNowTimeMilly()); | ||
| 104 | -// publicService.updateObjectByTable(openAiUserInfo,"id","`lk_openai`.`user_info`"); | ||
| 105 | return tokenService.createToken(loginUser); | 100 | return tokenService.createToken(loginUser); |
| 106 | } | 101 | } |
| 107 | 102 |
| @@ -126,6 +126,12 @@ public class TokenService | @@ -126,6 +126,12 @@ public class TokenService | ||
| 126 | return createToken(claims); | 126 | return createToken(claims); |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | + public void upUser(BaseLoginUser loginUser) | ||
| 130 | + { | ||
| 131 | + String userKey = getTokenKey(loginUser.getToken()); | ||
| 132 | + redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES); | ||
| 133 | + } | ||
| 134 | + | ||
| 129 | /** | 135 | /** |
| 130 | * 验证令牌有效期,相差不足20分钟,自动刷新缓存 | 136 | * 验证令牌有效期,相差不足20分钟,自动刷新缓存 |
| 131 | * | 137 | * |
-
请 注册 或 登录 后发表评论