@@ -536,24 +536,58 @@ export class OpenAIInstrumentation extends InstrumentationBase {
536
536
promise : APIPromise < T > ,
537
537
) : APIPromise < T > {
538
538
return new APIPromise < T > (
539
- new Promise ( ( resolve , reject ) => {
540
539
promise
541
- . _thenUnwrap ( ( result ) => {
542
- const data = version === "v3" ? ( result as any ) . data : result ;
543
-
544
- this . _endSpan ( { type, span, result : data as any } ) ;
545
- return result ;
546
- } )
547
- . catch ( ( error : Error ) => {
548
- span . setStatus ( {
549
- code : SpanStatusCode . ERROR ,
550
- message : error . message ,
551
- } ) ;
552
- span . recordException ( error ) ;
553
- span . end ( ) ;
554
- throw error ;
540
+ . _thenUnwrap ( ( data , props ) => {
541
+ if ( version === "v3" ) {
542
+ if ( type === "chat" ) {
543
+ this . _addLogProbsEvent (
544
+ span ,
545
+ ( data as ChatCompletion ) . choices [ 0 ] . logprobs ,
546
+ ) ;
547
+ this . _endSpan ( {
548
+ type,
549
+ span,
550
+ result : data as ChatCompletion ,
551
+ } ) ;
552
+ } else {
553
+ this . _addLogProbsEvent (
554
+ span ,
555
+ ( data as Completion ) . choices [ 0 ] . logprobs ,
556
+ ) ;
557
+ this . _endSpan ( {
558
+ type,
559
+ span,
560
+ result : data as Completion ,
561
+ } ) ;
562
+ }
563
+ } else {
564
+ if ( type === "chat" ) {
565
+ this . _addLogProbsEvent (
566
+ span ,
567
+ ( data as ChatCompletion ) . choices [ 0 ] . logprobs ,
568
+ ) ;
569
+ this . _endSpan ( { type, span, result : data as ChatCompletion } ) ;
570
+ } else {
571
+ this . _addLogProbsEvent (
572
+ span ,
573
+ ( data as Completion ) . choices [ 0 ] . logprobs ,
574
+ ) ;
575
+ this . _endSpan ( { type, span, result : data as Completion } ) ;
576
+ }
577
+ }
578
+
579
+ return props ;
580
+ } )
581
+ . catch ( ( error : Error ) => {
582
+ span . setStatus ( {
583
+ code : SpanStatusCode . ERROR ,
584
+ message : error . message ,
555
585
} ) ;
556
- } ) ,
586
+ span . recordException ( error ) ;
587
+ span . end ( ) ;
588
+
589
+ throw error ;
590
+ } )
557
591
) ;
558
592
}
559
593
0 commit comments