@@ -5,6 +5,7 @@ import test from "ava";
5
5
import { OpenAI } from "../src" ;
6
6
import { yieldStream } from "yield-stream" ;
7
7
import { DECODER } from "../src/globs/shared" ;
8
+ import nodeFetch from "node-fetch" ;
8
9
9
10
test . serial ( "'completions' endpoint" , async ( t ) => {
10
11
const stream = await OpenAI ( "completions" , {
@@ -237,3 +238,28 @@ test.serial("cancelling streams", async (t) => {
237
238
238
239
t . is ( i , 5 , "Stream should have been cancelled after 5 chunks." ) ;
239
240
} ) ;
241
+
242
+ test . serial ( "should work with custom fetch" , async ( t ) => {
243
+ let didUseMock = false ;
244
+
245
+ const mockFetch : typeof nodeFetch = ( ...params ) => {
246
+ didUseMock = true ;
247
+ return nodeFetch ( ...params ) ;
248
+ } ;
249
+
250
+ const stream = await OpenAI ( "chat" , {
251
+ model : "gpt-3.5-turbo" ,
252
+ messages : [
253
+ { role : "system" , content : "You are a helpful assistant." } ,
254
+ { role : "user" , content : "This is a test message, say hello!" } ,
255
+ ] ,
256
+ } , { fetch : mockFetch } ) ;
257
+
258
+ const DECODER = new TextDecoder ( ) ;
259
+ for await ( const serialized of yieldStream ( stream ) ) {
260
+ const string = DECODER . decode ( serialized ) ;
261
+ console . log ( string ) ;
262
+ }
263
+
264
+ t . true ( didUseMock , "Did not use custom fetch." ) ;
265
+ } ) ;
0 commit comments