增加流响应错误时重试机制

This commit is contained in:
Vinlic 2024-03-16 04:46:39 +08:00
parent 08a4b2e720
commit 1395278a6e
2 changed files with 101 additions and 64 deletions

View File

@ -14,6 +14,10 @@ import util from '@/lib/util.ts';
const MODEL_NAME = 'kimi';
// access_token有效期
const ACCESS_TOKEN_EXPIRES = 300;
// 最大重试次数
const MAX_RETRY_COUNT = 3;
// 重试延迟
const RETRY_DELAY = 5000;
// 伪装headers
const FAKE_HEADERS = {
'Accept': '*/*',
@ -163,8 +167,10 @@ async function removeConversation(convId: string, refreshToken: string) {
* @param messages gpt系列消息格式
* @param refreshToken access_token的refresh_token
* @param useSearch
* @param retryCount
*/
async function createCompletion(messages: any[], refreshToken: string, useSearch = true) {
async function createCompletion(messages: any[], refreshToken: string, useSearch = true, retryCount = 0) {
return (async () => {
logger.info(messages);
// 提取引用文件URL并上传kimi获得引用的文件ID列表
@ -202,6 +208,18 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch
.catch(err => console.error(err));
return answer;
})()
.catch(err => {
if(retryCount < MAX_RETRY_COUNT) {
logger.error(`Stream response error: ${err.message}`);
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
return (async () => {
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
return createCompletion(messages, refreshToken, useSearch, retryCount + 1);
})();
}
throw err;
});
}
/**
@ -210,8 +228,10 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch
* @param messages gpt系列消息格式
* @param refreshToken access_token的refresh_token
* @param useSearch
* @param retryCount
*/
async function createCompletionStream(messages: any[], refreshToken: string, useSearch = true) {
async function createCompletionStream(messages: any[], refreshToken: string, useSearch = true, retryCount = 0) {
return (async () => {
logger.info(messages);
// 提取引用文件URL并上传kimi获得引用的文件ID列表
@ -246,6 +266,18 @@ async function createCompletionStream(messages: any[], refreshToken: string, use
removeConversation(convId, refreshToken)
.catch(err => console.error(err));
});
})()
.catch(err => {
if(retryCount < MAX_RETRY_COUNT) {
logger.error(`Stream response error: ${err.message}`);
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
return (async () => {
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
return createCompletionStream(messages, refreshToken, useSearch, retryCount + 1);
})();
}
throw err;
});
}
/**

View File

@ -29,6 +29,11 @@ export default class Exception extends Error {
this.errmsg = _errmsg || errmsg;
}
compare(exception: (string | number)[]) {
const [errcode] = exception as [number, string];
return this.errcode == errcode;
}
setHTTPStatusCode(value: number) {
this.httpStatusCode = value;
return this;