14
14
* limitations under the License.
15
15
*/
16
16
import type * as openai from "openai" ;
17
- import {
18
- context ,
19
- trace ,
20
- Span ,
21
- Attributes ,
22
- SpanKind ,
23
- SpanStatusCode ,
24
- } from "@opentelemetry/api" ;
17
+ import { context , trace , Span , Attributes , SpanKind } from "@opentelemetry/api" ;
25
18
import {
26
19
InstrumentationBase ,
27
20
InstrumentationModuleDefinition ,
@@ -46,6 +39,7 @@ import type {
46
39
import type { Stream } from "openai/streaming" ;
47
40
import { version } from "../package.json" ;
48
41
import { encodingForModel , TiktokenModel , Tiktoken } from "js-tiktoken" ;
42
+ import { APIPromise } from "openai/core" ;
49
43
50
44
export class OpenAIInstrumentation extends InstrumentationBase {
51
45
protected declare _config : OpenAIInstrumentationConfig ;
@@ -338,13 +332,13 @@ export class OpenAIInstrumentation extends InstrumentationBase {
338
332
span : Span ;
339
333
type : "chat" ;
340
334
params : ChatCompletionCreateParamsStreaming ;
341
- promise : Promise < Stream < ChatCompletionChunk > > ;
335
+ promise : APIPromise < Stream < ChatCompletionChunk > > ;
342
336
}
343
337
| {
344
338
span : Span ;
345
339
params : CompletionCreateParamsStreaming ;
346
340
type : "completion" ;
347
- promise : Promise < Stream < Completion > > ;
341
+ promise : APIPromise < Stream < Completion > > ;
348
342
} ) {
349
343
if ( type === "chat" ) {
350
344
const result : ChatCompletion = {
@@ -532,63 +526,49 @@ export class OpenAIInstrumentation extends InstrumentationBase {
532
526
type : "chat" | "completion" ,
533
527
version : "v3" | "v4" ,
534
528
span : Span ,
535
- promise : Promise < T > ,
536
- ) : Promise < T > {
537
- return promise
538
- . then ( ( result ) => {
539
- return new Promise < T > ( ( resolve ) => {
540
- if ( version === "v3" ) {
541
- if ( type === "chat" ) {
542
- this . _addLogProbsEvent (
543
- span ,
544
- ( ( result as any ) . data as ChatCompletion ) . choices [ 0 ] . logprobs ,
545
- ) ;
546
- this . _endSpan ( {
547
- type,
548
- span,
549
- result : ( result as any ) . data as ChatCompletion ,
550
- } ) ;
551
- } else {
552
- this . _addLogProbsEvent (
553
- span ,
554
- ( ( result as any ) . data as Completion ) . choices [ 0 ] . logprobs ,
555
- ) ;
556
- this . _endSpan ( {
557
- type,
558
- span,
559
- result : ( result as any ) . data as Completion ,
560
- } ) ;
561
- }
562
- } else {
563
- if ( type === "chat" ) {
564
- this . _addLogProbsEvent (
565
- span ,
566
- ( result as ChatCompletion ) . choices [ 0 ] . logprobs ,
567
- ) ;
568
- this . _endSpan ( { type, span, result : result as ChatCompletion } ) ;
569
- } else {
570
- this . _addLogProbsEvent (
571
- span ,
572
- ( result as Completion ) . choices [ 0 ] . logprobs ,
573
- ) ;
574
- this . _endSpan ( { type, span, result : result as Completion } ) ;
575
- }
576
- }
577
- resolve ( result ) ;
578
- } ) ;
579
- } )
580
- . catch ( ( error : Error ) => {
581
- return new Promise < T > ( ( _ , reject ) => {
582
- span . setStatus ( {
583
- code : SpanStatusCode . ERROR ,
584
- message : error . message ,
529
+ promise : APIPromise < T > ,
530
+ ) : APIPromise < T > {
531
+ return promise . _thenUnwrap ( ( result ) => {
532
+ if ( version === "v3" ) {
533
+ if ( type === "chat" ) {
534
+ this . _addLogProbsEvent (
535
+ span ,
536
+ ( ( result as any ) . data as ChatCompletion ) . choices [ 0 ] . logprobs ,
537
+ ) ;
538
+ this . _endSpan ( {
539
+ type,
540
+ span,
541
+ result : ( result as any ) . data as ChatCompletion ,
542
+ } ) ;
543
+ } else {
544
+ this . _addLogProbsEvent (
545
+ span ,
546
+ ( ( result as any ) . data as Completion ) . choices [ 0 ] . logprobs ,
547
+ ) ;
548
+ this . _endSpan ( {
549
+ type,
550
+ span,
551
+ result : ( result as any ) . data as Completion ,
585
552
} ) ;
586
- span . recordException ( error ) ;
587
- span . end ( ) ;
553
+ }
554
+ } else {
555
+ if ( type === "chat" ) {
556
+ this . _addLogProbsEvent (
557
+ span ,
558
+ ( result as ChatCompletion ) . choices [ 0 ] . logprobs ,
559
+ ) ;
560
+ this . _endSpan ( { type, span, result : result as ChatCompletion } ) ;
561
+ } else {
562
+ this . _addLogProbsEvent (
563
+ span ,
564
+ ( result as Completion ) . choices [ 0 ] . logprobs ,
565
+ ) ;
566
+ this . _endSpan ( { type, span, result : result as Completion } ) ;
567
+ }
568
+ }
588
569
589
- reject ( error ) ;
590
- } ) ;
591
- } ) ;
570
+ return result ;
571
+ } ) ;
592
572
}
593
573
594
574
private _endSpan ( {
0 commit comments