Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
[fix] 修复请求失败引起的bug消息通知
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashu1024 committed Jul 11, 2023
1 parent 29d63d2 commit ca504d4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/zjs/feishubot/entity/gpt/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public class ErrorCode {
*/
public static final int BUSY = 2;

public static final int RESPONSE_ERROR = 5;

public static final Map<Integer, String> map = new HashMap<>();

static {
map.put(INVALID_JWT, "无效的access token");
map.put(BUSY, "账号繁忙中");
map.put(INVALID_API_KEY, "无效的api key");
map.put(CHAT_LIMIT, "4.0接口被限制了");
map.put(RESPONSE_ERROR, "响应错误");
}


Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/zjs/feishubot/handler/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,15 @@ public void process(P2MessageReceiveV1 event) throws Exception {
Map<String, String> selections = createSelection(conversation);

String modelParam = Models.modelMap.get(model).getSlug();


if (newChat) {
log.info("新建会话");
conversation.setTitle(title);
String finalTitle = title;
chatService.newChat(text, modelParam, answer -> {
processAnswer(answer, finalTitle, chatId, chatService, messageId, event, model, selections);
});

} else {
log.info("继续会话");
title = conversation.getTitle();
Expand All @@ -192,7 +193,9 @@ public void process(P2MessageReceiveV1 event) throws Exception {
processAnswer(answer, finalTitle1, chatId, chatService, messageId, event, model, selections);
});
}
log.info("服务完成,account: {} ,model:{},chatId:{}",chatService.getAccount(),model,chatId);


log.info("服务完成,account: {} ,model:{},chatId:{}", chatService.getAccount(), model, chatId);
}

private Map<String, String> createSelection(Conversation conversation) {
Expand All @@ -215,7 +218,6 @@ private Map<String, String> createSelection(Conversation conversation) {
Conversation plusModelConversation = new Conversation();
plusModelConversation.setModel(modelTitle);
selections.put(modelTitle, JSONUtil.toJsonStr(plusModelConversation));

}
return selections;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/zjs/feishubot/util/chatgpt/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class ChatService {
private volatile Status status;
private Semaphore semaphore = new Semaphore(1);

private StringBuilder errorString;

private String proxyUrl;

private static final String LOGIN_URL = "/chatgpt/login";
Expand Down Expand Up @@ -164,8 +166,13 @@ private void post(String param, String urlStr, AnswerProcess process) {
// 获取并处理响应
int status = connection.getResponseCode();
Reader streamReader = null;
boolean error = false;
errorString = new StringBuilder();
if (status > 299) {
streamReader = new InputStreamReader(connection.getErrorStream());
log.error("请求失败,状态码:{}", status);

error = true;
} else {
streamReader = new InputStreamReader(connection.getInputStream());
}
Expand All @@ -178,6 +185,12 @@ private void post(String param, String urlStr, AnswerProcess process) {
if (line.length() == 0) {
continue;
}
if (error) {
errorString.append(line);
log.error(line);

continue;
}

try {
count++;
Expand Down Expand Up @@ -205,6 +218,14 @@ private void post(String param, String urlStr, AnswerProcess process) {
}
}

if (error) {
answer = new Answer();
answer.setError(errorString.toString());
answer.setErrorCode(ErrorCode.RESPONSE_ERROR);
answer.setSuccess(false);
TaskPool.addTask(new Task(process, answer, this.account));
}

reader.close();
connection.disconnect();
} catch (Exception e) {
Expand Down

0 comments on commit ca504d4

Please sign in to comment.