Skip to content

Commit

Permalink
Merge pull request #176 from LinLin00000000/main
Browse files Browse the repository at this point in the history
refactor: 使用内置的 AbortSignal.timeout 代替自编写的 fetchWithTimeout
  • Loading branch information
ourongxing committed Apr 11, 2023
2 parents d2312a3 + d9089cb commit cda0250
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
39 changes: 19 additions & 20 deletions src/pages/api/index.ts
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down
16 changes: 0 additions & 16 deletions src/utils/index.ts
Expand Up @@ -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
}

1 comment on commit cda0250

@vercel
Copy link

@vercel vercel bot commented on cda0250 Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.