Skip to content

Commit

Permalink
feat: add more notice when error
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangsx committed May 6, 2023
1 parent 67008b7 commit 9c33040
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand Down
40 changes: 36 additions & 4 deletions model/forefront/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -128,17 +130,47 @@ 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');
}
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');
}
}
}
}
}

Expand Down

0 comments on commit 9c33040

Please sign in to comment.