@@ -3,7 +3,7 @@ import type { ParsedEvent, ReconnectInterval } from "eventsource-parser"
3
3
import { createParser } from "eventsource-parser"
4
4
import type { ChatMessage , Model } from "~/types"
5
5
import { countTokens } from "~/utils/tokens"
6
- import { splitKeys , randomKey } from "~/utils"
6
+ import { splitKeys , randomKey , fetchWithTimeout } from "~/utils"
7
7
import { defaultMaxInputTokens , defaultModel } from "~/system"
8
8
9
9
export const config = {
@@ -44,9 +44,7 @@ export const baseURL = import.meta.env.NOGFW
44
44
""
45
45
)
46
46
47
- const timeout = isNaN ( + import . meta. env . TIMEOUT )
48
- ? 30 * 1000
49
- : + import . meta. env . TIMEOUT
47
+ const timeout = Number ( import . meta. env . TIMEOUT )
50
48
51
49
let maxInputTokens = defaultMaxInputTokens
52
50
const _ = import . meta. env . MAX_INPUT_TOKENS
@@ -134,21 +132,24 @@ export const post: APIRoute = async context => {
134
132
const encoder = new TextEncoder ( )
135
133
const decoder = new TextDecoder ( )
136
134
137
- const rawRes = await fetch ( `https://${ baseURL } /v1/chat/completions` , {
138
- headers : {
139
- "Content-Type" : "application/json" ,
140
- Authorization : `Bearer ${ apiKey } `
141
- } ,
142
- signal : AbortSignal . timeout ( timeout ) ,
143
- method : "POST" ,
144
- body : JSON . stringify ( {
145
- model : model || "gpt-3.5-turbo" ,
146
- messages : messages . map ( k => ( { role : k . role , content : k . content } ) ) ,
147
- temperature,
148
- // max_tokens: 4096 - tokens,
149
- stream : true
150
- } )
151
- } ) . catch ( err => {
135
+ const rawRes = await fetchWithTimeout (
136
+ `https://${ baseURL } /v1/chat/completions` ,
137
+ {
138
+ headers : {
139
+ "Content-Type" : "application/json" ,
140
+ Authorization : `Bearer ${ apiKey } `
141
+ } ,
142
+ timeout : ! timeout || Number . isNaN ( timeout ) ? 30000 : timeout ,
143
+ method : "POST" ,
144
+ body : JSON . stringify ( {
145
+ model : model || "gpt-3.5-turbo" ,
146
+ messages : messages . map ( k => ( { role : k . role , content : k . content } ) ) ,
147
+ temperature,
148
+ // max_tokens: 4096 - tokens,
149
+ stream : true
150
+ } )
151
+ }
152
+ ) . catch ( err => {
152
153
return new Response (
153
154
JSON . stringify ( {
154
155
error : {
0 commit comments