@@ -8,98 +8,98 @@ const ch = dc.tracingChannel('apm:openai:request')
8
8
9
9
const V4_PACKAGE_SHIMS = [
10
10
{
11
- file : 'resources/chat/completions.js ' ,
11
+ file : 'resources/chat/completions' ,
12
12
targetClass : 'Completions' ,
13
13
baseResource : 'chat.completions' ,
14
14
methods : [ 'create' ] ,
15
15
streamedResponse : true
16
16
} ,
17
17
{
18
- file : 'resources/completions.js ' ,
18
+ file : 'resources/completions' ,
19
19
targetClass : 'Completions' ,
20
20
baseResource : 'completions' ,
21
21
methods : [ 'create' ] ,
22
22
streamedResponse : true
23
23
} ,
24
24
{
25
- file : 'resources/embeddings.js ' ,
25
+ file : 'resources/embeddings' ,
26
26
targetClass : 'Embeddings' ,
27
27
baseResource : 'embeddings' ,
28
28
methods : [ 'create' ]
29
29
} ,
30
30
{
31
- file : 'resources/files.js ' ,
31
+ file : 'resources/files' ,
32
32
targetClass : 'Files' ,
33
33
baseResource : 'files' ,
34
34
methods : [ 'create' , 'del' , 'list' , 'retrieve' ]
35
35
} ,
36
36
{
37
- file : 'resources/files.js ' ,
37
+ file : 'resources/files' ,
38
38
targetClass : 'Files' ,
39
39
baseResource : 'files' ,
40
40
methods : [ 'retrieveContent' ] ,
41
41
versions : [ '>=4.0.0 <4.17.1' ]
42
42
} ,
43
43
{
44
- file : 'resources/files.js ' ,
44
+ file : 'resources/files' ,
45
45
targetClass : 'Files' ,
46
46
baseResource : 'files' ,
47
47
methods : [ 'content' ] , // replaced `retrieveContent` in v4.17.1
48
48
versions : [ '>=4.17.1' ]
49
49
} ,
50
50
{
51
- file : 'resources/images.js ' ,
51
+ file : 'resources/images' ,
52
52
targetClass : 'Images' ,
53
53
baseResource : 'images' ,
54
54
methods : [ 'createVariation' , 'edit' , 'generate' ]
55
55
} ,
56
56
{
57
- file : 'resources/fine-tuning/jobs/jobs.js ' ,
57
+ file : 'resources/fine-tuning/jobs/jobs' ,
58
58
targetClass : 'Jobs' ,
59
59
baseResource : 'fine_tuning.jobs' ,
60
60
methods : [ 'cancel' , 'create' , 'list' , 'listEvents' , 'retrieve' ] ,
61
61
versions : [ '>=4.34.0' ] // file location changed in 4.34.0
62
62
} ,
63
63
{
64
- file : 'resources/fine-tuning/jobs.js ' ,
64
+ file : 'resources/fine-tuning/jobs' ,
65
65
targetClass : 'Jobs' ,
66
66
baseResource : 'fine_tuning.jobs' ,
67
67
methods : [ 'cancel' , 'create' , 'list' , 'listEvents' , 'retrieve' ] ,
68
68
versions : [ '>=4.1.0 <4.34.0' ]
69
69
} ,
70
70
{
71
- file : 'resources/fine-tunes.js ' , // deprecated after 4.1.0
71
+ file : 'resources/fine-tunes' , // deprecated after 4.1.0
72
72
targetClass : 'FineTunes' ,
73
73
baseResource : 'fine-tune' ,
74
74
methods : [ 'cancel' , 'create' , 'list' , 'listEvents' , 'retrieve' ] ,
75
75
versions : [ '>=4.0.0 <4.1.0' ]
76
76
} ,
77
77
{
78
- file : 'resources/models.js ' ,
78
+ file : 'resources/models' ,
79
79
targetClass : 'Models' ,
80
80
baseResource : 'models' ,
81
81
methods : [ 'del' , 'list' , 'retrieve' ]
82
82
} ,
83
83
{
84
- file : 'resources/moderations.js ' ,
84
+ file : 'resources/moderations' ,
85
85
targetClass : 'Moderations' ,
86
86
baseResource : 'moderations' ,
87
87
methods : [ 'create' ]
88
88
} ,
89
89
{
90
- file : 'resources/audio/transcriptions.js ' ,
90
+ file : 'resources/audio/transcriptions' ,
91
91
targetClass : 'Transcriptions' ,
92
92
baseResource : 'audio.transcriptions' ,
93
93
methods : [ 'create' ]
94
94
} ,
95
95
{
96
- file : 'resources/audio/translations.js ' ,
96
+ file : 'resources/audio/translations' ,
97
97
targetClass : 'Translations' ,
98
98
baseResource : 'audio.translations' ,
99
99
methods : [ 'create' ]
100
100
} ,
101
101
{
102
- file : 'resources/chat/completions/completions.js ' ,
102
+ file : 'resources/chat/completions/completions' ,
103
103
targetClass : 'Completions' ,
104
104
baseResource : 'chat.completions' ,
105
105
methods : [ 'create' ] ,
@@ -267,82 +267,86 @@ function wrapStreamIterator (response, options, n, ctx) {
267
267
}
268
268
}
269
269
270
- for ( const shim of V4_PACKAGE_SHIMS ) {
271
- const { file, targetClass, baseResource, methods, versions, streamedResponse } = shim
272
- addHook ( { name : 'openai' , file, versions : versions || [ '>=4' ] } , exports => {
273
- const targetPrototype = exports [ targetClass ] . prototype
270
+ const extensions = [ '.js' , '.mjs' ]
274
271
275
- for ( const methodName of methods ) {
276
- shimmer . wrap ( targetPrototype , methodName , methodFn => function ( ) {
277
- if ( ! ch . start . hasSubscribers ) {
278
- return methodFn . apply ( this , arguments )
279
- }
272
+ for ( const extension of extensions ) {
273
+ for ( const shim of V4_PACKAGE_SHIMS ) {
274
+ const { file , targetClass , baseResource , methods , versions , streamedResponse } = shim
275
+ addHook ( { name : 'openai' , file : file + extension , versions : versions || [ '>=4' ] } , exports => {
276
+ const targetPrototype = exports [ targetClass ] . prototype
280
277
281
- // The OpenAI library lets you set `stream: true` on the options arg to any method
282
- // However, we only want to handle streamed responses in specific cases
283
- // chat.completions and completions
284
- const stream = streamedResponse && getOption ( arguments , 'stream' , false )
285
-
286
- // we need to compute how many prompts we are sending in streamed cases for completions
287
- // not applicable for chat completiond
288
- let n
289
- if ( stream ) {
290
- n = getOption ( arguments , 'n' , 1 )
291
- const prompt = getOption ( arguments , 'prompt' )
292
- if ( Array . isArray ( prompt ) && typeof prompt [ 0 ] !== 'number' ) {
293
- n *= prompt . length
278
+ for ( const methodName of methods ) {
279
+ shimmer . wrap ( targetPrototype , methodName , methodFn => function ( ) {
280
+ if ( ! ch . start . hasSubscribers ) {
281
+ return methodFn . apply ( this , arguments )
294
282
}
295
- }
296
283
297
- const client = this . _client || this . client
284
+ // The OpenAI library lets you set `stream: true` on the options arg to any method
285
+ // However, we only want to handle streamed responses in specific cases
286
+ // chat.completions and completions
287
+ const stream = streamedResponse && getOption ( arguments , 'stream' , false )
288
+
289
+ // we need to compute how many prompts we are sending in streamed cases for completions
290
+ // not applicable for chat completiond
291
+ let n
292
+ if ( stream ) {
293
+ n = getOption ( arguments , 'n' , 1 )
294
+ const prompt = getOption ( arguments , 'prompt' )
295
+ if ( Array . isArray ( prompt ) && typeof prompt [ 0 ] !== 'number' ) {
296
+ n *= prompt . length
297
+ }
298
+ }
298
299
299
- const ctx = {
300
- methodName : `${ baseResource } .${ methodName } ` ,
301
- args : arguments ,
302
- basePath : client . baseURL ,
303
- apiKey : client . apiKey
304
- }
300
+ const client = this . _client || this . client
305
301
306
- return ch . start . runStores ( ctx , ( ) => {
307
- const apiProm = methodFn . apply ( this , arguments )
302
+ const ctx = {
303
+ methodName : `${ baseResource } .${ methodName } ` ,
304
+ args : arguments ,
305
+ basePath : client . baseURL ,
306
+ apiKey : client . apiKey
307
+ }
308
+
309
+ return ch . start . runStores ( ctx , ( ) => {
310
+ const apiProm = methodFn . apply ( this , arguments )
308
311
309
- if ( baseResource === 'chat.completions' && typeof apiProm . _thenUnwrap === 'function' ) {
310
- // this should only ever be invoked from a client.beta.chat.completions.parse call
311
- shimmer . wrap ( apiProm , '_thenUnwrap' , origApiPromThenUnwrap => function ( ) {
312
- // TODO(sam.brenner): I wonder if we can patch the APIPromise prototype instead, although
313
- // we might not have access to everything we need...
312
+ if ( baseResource === 'chat.completions' && typeof apiProm . _thenUnwrap === 'function' ) {
313
+ // this should only ever be invoked from a client.beta.chat.completions.parse call
314
+ shimmer . wrap ( apiProm , '_thenUnwrap' , origApiPromThenUnwrap => function ( ) {
315
+ // TODO(sam.brenner): I wonder if we can patch the APIPromise prototype instead, although
316
+ // we might not have access to everything we need...
314
317
315
- // this is a new apipromise instance
316
- const unwrappedPromise = origApiPromThenUnwrap . apply ( this , arguments )
318
+ // this is a new apipromise instance
319
+ const unwrappedPromise = origApiPromThenUnwrap . apply ( this , arguments )
317
320
318
- shimmer . wrap ( unwrappedPromise , 'parse' , origApiPromParse => function ( ) {
319
- const parsedPromise = origApiPromParse . apply ( this , arguments )
320
- . then ( body => Promise . all ( [ this . responsePromise , body ] ) )
321
+ shimmer . wrap ( unwrappedPromise , 'parse' , origApiPromParse => function ( ) {
322
+ const parsedPromise = origApiPromParse . apply ( this , arguments )
323
+ . then ( body => Promise . all ( [ this . responsePromise , body ] ) )
321
324
322
- return handleUnwrappedAPIPromise ( parsedPromise , ctx , stream , n )
325
+ return handleUnwrappedAPIPromise ( parsedPromise , ctx , stream , n )
326
+ } )
327
+
328
+ return unwrappedPromise
323
329
} )
330
+ }
324
331
325
- return unwrappedPromise
332
+ // wrapping `parse` avoids problematic wrapping of `then` when trying to call
333
+ // `withResponse` in userland code after. This way, we can return the whole `APIPromise`
334
+ shimmer . wrap ( apiProm , 'parse' , origApiPromParse => function ( ) {
335
+ const parsedPromise = origApiPromParse . apply ( this , arguments )
336
+ . then ( body => Promise . all ( [ this . responsePromise , body ] ) )
337
+
338
+ return handleUnwrappedAPIPromise ( parsedPromise , ctx , stream , n )
326
339
} )
327
- }
328
340
329
- // wrapping `parse` avoids problematic wrapping of `then` when trying to call
330
- // `withResponse` in userland code after. This way, we can return the whole `APIPromise`
331
- shimmer . wrap ( apiProm , 'parse' , origApiPromParse => function ( ) {
332
- const parsedPromise = origApiPromParse . apply ( this , arguments )
333
- . then ( body => Promise . all ( [ this . responsePromise , body ] ) )
341
+ ch . end . publish ( ctx )
334
342
335
- return handleUnwrappedAPIPromise ( parsedPromise , ctx , stream , n )
343
+ return apiProm
336
344
} )
337
-
338
- ch . end . publish ( ctx )
339
-
340
- return apiProm
341
345
} )
342
- } )
343
- }
344
- return exports
345
- } )
346
+ }
347
+ return exports
348
+ } )
349
+ }
346
350
}
347
351
348
352
function handleUnwrappedAPIPromise ( apiProm , ctx , stream , n ) {
0 commit comments