mirror of
https://github.com/LLM-Red-Team/kimi-free-api.git
synced 2024-12-23 02:49:19 +08:00
增加流响应错误时重试机制
This commit is contained in:
parent
08a4b2e720
commit
1395278a6e
@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user