feat: feat: Add optional wait time before throwing error

pull/525/head
Leo sun 2023-04-05 19:00:34 +09:00
rodzic 600b35eaec
commit 8b27d83d68
3 zmienionych plików z 30 dodań i 5 usunięć

Wyświetl plik

@ -130,6 +130,7 @@ export class ChatGPTAPI {
* @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)
* @param opts.onProgress - Optional callback which will be invoked every time the partial response is updated
* @param opts.abortSignal - Optional callback used to abort the underlying `fetch` call using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
* @param opts.waitTimeMsBeforeThrow - Optional the amount of time to wait before throwing an error(defaults to 0,means immediately)
* @param completionParams - Optional overrides to send to the [OpenAI chat completion API](https://platform.openai.com/docs/api-reference/chat/create). Options like `temperature` and `presence_penalty` can be tweaked to change the personality of the assistant.
*
* @returns The response from ChatGPT
@ -145,7 +146,8 @@ export class ChatGPTAPI {
onProgress,
stream = onProgress ? true : false,
completionParams,
conversationId
conversationId,
waitTimeMsBeforeThrow = 0
} = opts
let { abortSignal } = opts
@ -205,6 +207,7 @@ export class ChatGPTAPI {
}
if (stream) {
let waitTimeMsBeforeThrowId: NodeJS.Timeout | null = null
fetchSSE(
url,
{
@ -213,6 +216,11 @@ export class ChatGPTAPI {
body: JSON.stringify(body),
signal: abortSignal,
onMessage: (data: string) => {
if (waitTimeMsBeforeThrowId) {
clearTimeout(waitTimeMsBeforeThrowId)
waitTimeMsBeforeThrowId = null
}
if (data === '[DONE]') {
result.text = result.text.trim()
return resolve(result)
@ -239,8 +247,13 @@ export class ChatGPTAPI {
onProgress?.(result)
}
} catch (err) {
console.warn('OpenAI stream SEE event unexpected error', err)
return reject(err)
waitTimeMsBeforeThrowId = setTimeout(() => {
console.warn(
'OpenAI stream SEE event unexpected error',
err
)
return reject(err)
}, waitTimeMsBeforeThrow)
}
}
},

Wyświetl plik

@ -91,6 +91,7 @@ export class ChatGPTUnofficialProxyAPI {
* @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)
* @param opts.onProgress - Optional callback which will be invoked every time the partial response is updated
* @param opts.abortSignal - Optional callback used to abort the underlying `fetch` call using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
* @param opts.waitTimeMsBeforeThrow - Optional the amount of time to wait before throwing an error(defaults to 0,means immediately)
*
* @returns The response from ChatGPT
*/
@ -128,7 +129,8 @@ export class ChatGPTUnofficialProxyAPI {
messageId = uuidv4(),
action = 'next',
timeoutMs,
onProgress
onProgress,
waitTimeMsBeforeThrow = 0
} = opts
let { abortSignal } = opts
@ -180,6 +182,7 @@ export class ChatGPTUnofficialProxyAPI {
console.log('POST', url, { body, headers })
}
let waitTimeMsBeforeThrowId: NodeJS.Timeout | null = null
fetchSSE(
url,
{
@ -188,6 +191,11 @@ export class ChatGPTUnofficialProxyAPI {
body: JSON.stringify(body),
signal: abortSignal,
onMessage: (data: string) => {
if (waitTimeMsBeforeThrowId) {
clearTimeout(waitTimeMsBeforeThrowId)
waitTimeMsBeforeThrowId = null
}
if (data === '[DONE]') {
return resolve(result)
}
@ -218,7 +226,9 @@ export class ChatGPTUnofficialProxyAPI {
}
}
} catch (err) {
reject(err)
waitTimeMsBeforeThrowId = setTimeout(() => {
reject(err)
}, waitTimeMsBeforeThrow)
}
},
onError: (err) => {

Wyświetl plik

@ -45,6 +45,7 @@ export type SendMessageOptions = {
timeoutMs?: number
onProgress?: (partialResponse: ChatMessage) => void
abortSignal?: AbortSignal
waitTimeMsBeforeThrow?: number
completionParams?: Partial<
Omit<openai.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
>
@ -60,6 +61,7 @@ export type SendMessageBrowserOptions = {
timeoutMs?: number
onProgress?: (partialResponse: ChatMessage) => void
abortSignal?: AbortSignal
waitTimeMsBeforeThrow?: number
}
export interface ChatMessage {