From 9c330402507fa71e8f6b6c2edac6666676d1f976 Mon Sep 17 00:00:00 2001 From: xiang <1984871009@qq.com> Date: Sun, 7 May 2023 01:10:52 +0800 Subject: [PATCH] feat: add more notice when error --- README.md | 24 ++++++++++++++++++++++-- index.ts | 2 +- model/forefront/index.ts | 40 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f1bd17a7..ff0898c7 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,30 @@ docker-compose up --build -d ## Test with curl +### params in query + +``` +prompt: string; // required +``` + +forefront options +``` +chatId?: string; +actionType?: Action; // 'new' or 'continue' +defaultPersona?: string; +gptmodel?: Model; // gpt-4 or gpt-3.5-turbo +resignup?: number; // default 0 if set 1, auto sign up when gpt4 times use up +// event: error +// data: GPT-4 rate limit exceeded (>5 messages every 3 hours). Time remaining: 179 minutes +// if you see this try set resignup=1 or use gpt-3.5-turbo +``` + +### test now! + common request ```shell # test default model chat.forefront.at -curl "http://127.0.0.1:3000/ask?prompt=hello" +curl "http://127.0.0.1:3000/ask?prompt=hello&model=forefront&gptmodel=gpt-4&resignup=0" # test you.com curl "http://127.0.0.1:3000/ask?prompt=hello&model=you" @@ -51,7 +71,7 @@ curl "http://127.0.0.1:3000/ask?prompt=hello&model=you" request event-stream ```shell # test default model chat.forefront.at -curl "http://127.0.0.1:3000/ask/stream?prompt=hello" +curl "http://127.0.0.1:3000/ask/stream?prompt=hello&model=forefront&gptmodel=gpt-4&resignup=0" # test you curl "http://127.0.0.1:3000/ask/stream?prompt=hello&model=you" diff --git a/index.ts b/index.ts index f9eb8c25..55ebd650 100644 --- a/index.ts +++ b/index.ts @@ -51,7 +51,7 @@ router.get('/ask/stream', async (ctx) => { return; } ctx.set({ - "Content-Type": "text/event-stream", + "Content-Type": "text/event-stream;charset=utf-8", "Cache-Control": "no-cache", "Connection": "keep-alive", }); diff --git a/model/forefront/index.ts b/model/forefront/index.ts index e5474a3a..d0218e81 100644 --- a/model/forefront/index.ts +++ b/model/forefront/index.ts @@ -17,7 +17,7 @@ interface ForefrontRequest extends Request { defaultPersona?: string; gptmodel?: Model; // if set true, auto sign up when gpt4 times use up - resignup?: boolean; + resignup?: string; } } @@ -59,6 +59,7 @@ export class Forefront extends Chat { private client: AxiosInstance | undefined; private session: ForefrontSessionInfo | undefined; private keepAliveInterval: NodeJS.Timer | undefined = undefined; + private gpt4times: number = 0; constructor(options?: ChatOptions) { super(options); @@ -73,7 +74,7 @@ export class Forefront extends Chat { res.text.pipe(es.split(/\r?\n\r?\n/)).pipe(es.map(async (chunk: any, cb: any) => { const str = chunk.replace('data: ', ''); if (!str || str === '[DONE]') { - cb(null, false); + cb(null, ''); return; } const data = parseJSON(str, {}) as ChatCompletionChunk; @@ -107,6 +108,7 @@ export class Forefront extends Chat { actionType = Action.new, defaultPersona = '607e41fe-95be-497e-8e97-010a59b2e2c0', gptmodel = Model.gpt4, + resignup = 0 } = req.options || {}; const jsonData = { text: req.prompt, @@ -128,10 +130,28 @@ export class Forefront extends Chat { } } as AxiosRequestConfig ); - return {text: response.data}; + const stream = response.data.pipe(es.split(/\r?\n\r?\n/)).pipe(es.map(async (chunk: any, cb: any) => { + const str = chunk.replace('data: ', ''); + if (!str || str === '[DONE]') { + cb(null, ''); + return; + } + if (str.indexOf('event: error') !== -1) { + cb(null, 'GPT-4 rate limit exceeded (>5 messages every 3 hours). Time remaining: 180 minutes; try set resignup=true in query') + return; + } + const data = parseJSON(str, {}) as ChatCompletionChunk; + if (!data.choices) { + cb(null, ''); + return; + } + const [{delta: {content}}] = data.choices; + cb(null, content); + })) + return {text: stream}; } catch (e: any) {// session will expire very fast, I cannot know what reason if (e.response.status === 401) { - if (req.options?.resignup) { + if (+resignup) { this.client = undefined; // do not retry auto, avoid loss control throw new Error('retry again, will sign up again'); @@ -139,6 +159,18 @@ export class Forefront extends Chat { throw new Error('try change model to gpt-3.5-turbo or set resignup = true') } throw e; + } finally { + if (req.options?.gptmodel === Model.gpt4) { + this.gpt4times += 1; + if (this.gpt4times === 5) { + if (+resignup) { + this.client = undefined; + this.gpt4times = 0; + } else { + throw new Error('try set resignup=true in query'); + } + } + } } }