Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 使用内置的 AbortSignal.timeout 代替自编写的 fetchWithTimeout #176

Merged
merged 1 commit into from Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}