@@ -3,10 +3,12 @@ import { fetchEventSource } from '@microsoft/fetch-event-source';
33export default class Api2d
44{
55 // 设置key和apiBaseUrl
6- constructor ( key = null , apiBaseUrl = null )
6+ constructor ( key = null , apiBaseUrl = null , timeout = 60000 )
77 {
88 this . key = key ;
9- this . apiBaseUrl = apiBaseUrl || ( key && key . startsWith ( "fk" ) ? "https://openai.api2d.net" :"https://api.openai.com" ) ;
9+ this . apiBaseUrl = apiBaseUrl || ( key && key . startsWith ( "fk" ) ? "https://stream.api2d.net" :"https://api.openai.com" ) ;
10+ this . timeout = timeout ;
11+ this . controller = new AbortController ( ) ;
1012 }
1113
1214 // set key
@@ -21,6 +23,16 @@ export default class Api2d
2123 this . apiBaseUrl = apiBaseUrl ;
2224 }
2325
26+ setTimeout ( timeout )
27+ {
28+ this . timeout = parseInt ( timeout ) || 60 * 1000 ;
29+ }
30+
31+ abort ( )
32+ {
33+ this . controller . abort ( ) ;
34+ }
35+
2436 // Completion
2537 async completion ( options )
2638 {
@@ -41,8 +53,15 @@ export default class Api2d
4153 return new Promise ( async ( resolve , reject ) => {
4254 try {
4355 let chars = "" ;
44- // console.log( "in stream" );
56+ console . log ( "in stream" ) ;
57+ // 使用 fetchEventSource 发送请求
58+ const timeout_handle = setTimeout ( ( ) => {
59+ this . controller . abort ( ) ;
60+ // throw new Error( "Timeout "+ this.timeout );
61+ reject ( new Error ( "Timeout " + this . timeout ) ) ;
62+ } , this . timeout ) ;
4563 const response = await fetchEventSource ( url , {
64+ signal : this . controller . signal ,
4665 method : "POST" ,
4766 headers : { ...headers , "Accept" : "text/event-stream" } ,
4867 body : JSON . stringify ( { ...restOptions , model :model || 'gpt-3.5-turbo' } ) ,
@@ -58,6 +77,10 @@ export default class Api2d
5877 {
5978 // console.log( 'DONE' );
6079 if ( onEnd ) onEnd ( chars ) ;
80+ if ( timeout_handle )
81+ {
82+ clearTimeout ( timeout_handle ) ;
83+ }
6184 resolve ( chars ) ;
6285 } else
6386 {
@@ -72,20 +95,30 @@ export default class Api2d
7295 throw new Error ( error ) ;
7396 }
7497 } ) ;
98+
99+ // const ret = await response.json();
100+
75101 } catch ( error ) {
76- // console.log( error );
102+ console . log ( error ) ;
77103 reject ( error ) ;
78104 }
79105 } ) ;
80106 } else
81107 {
82108 // 使用 fetch 发送请求
83109 const response = await fetch ( url , {
110+ signal : this . controller . signal ,
84111 method : "POST" ,
85112 headers : headers ,
86113 body : JSON . stringify ( { ...restOptions , model :model || 'gpt-3.5-turbo' } )
87114 } ) ;
88- return await response . json ( ) ;
115+ const timeout_handle = setTimeout ( ( ) => {
116+ this . controller . abort ( ) ;
117+
118+ } , this . timeout ) ;
119+ const ret = await response . json ( ) ;
120+ clearTimeout ( timeout_handle ) ;
121+ return ret ;
89122 }
90123 }
91124
@@ -101,11 +134,17 @@ export default class Api2d
101134 const { model, ...restOptions } = options ;
102135 // 使用 fetch 发送请求
103136 const response = await fetch ( url , {
137+ signal : this . controller . signal ,
104138 method : "POST" ,
105139 headers : headers ,
106140 body : JSON . stringify ( { ...restOptions , model :model || 'text-embedding-ada-002' } )
107141 } ) ;
108- return await response . json ( ) ;
142+ const timeout_handle = setTimeout ( ( ) => {
143+ this . controller . abort ( ) ;
144+ } , this . timeout ) ;
145+ const ret = await response . json ( ) ;
146+ clearTimeout ( timeout_handle ) ;
147+ return ret ;
109148 }
110149
111150 async billing ( )
@@ -116,10 +155,16 @@ export default class Api2d
116155 "Authorization" : "Bearer " + this . key
117156 } ;
118157 const response = await fetch ( url , {
158+ signal : this . controller . signal ,
119159 method : "GET" ,
120160 headers : headers
121161 } ) ;
122- return await response . json ( ) ;
162+ const timeout_handle = setTimeout ( ( ) => {
163+ this . controller . abort ( ) ;
164+ } , this . timeout ) ;
165+ const ret = await response . json ( ) ;
166+ clearTimeout ( timeout_handle ) ;
167+ return ret ;
123168 }
124169}
125170
0 commit comments