From d9089cb1a4b18d21573a83dc8ae2ab38284526fe Mon Sep 17 00:00:00 2001 From: Lin Date: Sun, 9 Apr 2023 13:02:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E5=86=85?= =?UTF-8?q?=E7=BD=AE=E7=9A=84=20AbortSignal.timeout=20=E4=BB=A3=E6=9B=BF?= =?UTF-8?q?=E8=87=AA=E7=BC=96=E5=86=99=E7=9A=84=20fetchWithTimeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/index.ts | 39 +++++++++++++++++++-------------------- src/utils/index.ts | 16 ---------------- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/pages/api/index.ts b/src/pages/api/index.ts index 1e7169e9d..60a001cd0 100644 --- a/src/pages/api/index.ts +++ b/src/pages/api/index.ts @@ -3,7 +3,7 @@ import type { ParsedEvent, ReconnectInterval } from "eventsource-parser" import { createParser } from "eventsource-parser" import type { ChatMessage, Model } from "~/types" import { countTokens } from "~/utils/tokens" -import { splitKeys, randomKey, fetchWithTimeout } from "~/utils" +import { splitKeys, randomKey } from "~/utils" import { defaultMaxInputTokens, defaultModel } from "~/system" export const config = { @@ -44,7 +44,9 @@ export const baseURL = import.meta.env.NOGFW "" ) -const timeout = Number(import.meta.env.TIMEOUT) +const timeout = isNaN(+import.meta.env.TIMEOUT) + ? 30 * 1000 + : +import.meta.env.TIMEOUT let maxInputTokens = defaultMaxInputTokens const _ = import.meta.env.MAX_INPUT_TOKENS @@ -132,24 +134,21 @@ export const post: APIRoute = async context => { const encoder = new TextEncoder() const decoder = new TextDecoder() - const rawRes = await fetchWithTimeout( - `https://${baseURL}/v1/chat/completions`, - { - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${apiKey}` - }, - timeout: !timeout || Number.isNaN(timeout) ? 30000 : timeout, - method: "POST", - body: JSON.stringify({ - model: model || "gpt-3.5-turbo", - messages: messages.map(k => ({ role: k.role, content: k.content })), - temperature, - // max_tokens: 4096 - tokens, - stream: true - }) - } - ).catch(err => { + const rawRes = await fetch(`https://${baseURL}/v1/chat/completions`, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}` + }, + signal: AbortSignal.timeout(timeout), + method: "POST", + body: JSON.stringify({ + model: model || "gpt-3.5-turbo", + messages: messages.map(k => ({ role: k.role, content: k.content })), + temperature, + // max_tokens: 4096 - tokens, + stream: true + }) + }).catch(err => { return new Response( JSON.stringify({ error: { diff --git a/src/utils/index.ts b/src/utils/index.ts index 8e46ed741..7f1a7b42b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -78,19 +78,3 @@ export function splitKeys(keys: string) { export function randomKey(keys: string[]) { return keys.length ? keys[Math.floor(Math.random() * keys.length)] : "" } - -export async function fetchWithTimeout( - input: RequestInfo | URL, - init?: (RequestInit & { timeout?: number }) | undefined -) { - const { timeout = 500 } = init ?? {} - - const controller = new AbortController() - const id = setTimeout(() => controller.abort(), timeout) - const response = await fetch(input, { - ...init, - signal: controller.signal - }) - clearTimeout(id) - return response -}