8000 working · traceloop/openllmetry-js@57f39e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57f39e7

Browse files
committed
working
1 parent 4a20088 commit 57f39e7

File tree

1 file changed

+40
-53
lines changed

1 file changed

+40
-53
lines changed

packages/instrumentation-openai/src/instrumentation.ts

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
Span,
2121
Attributes,
2222
SpanKind,
23-
SpanStatusCode,
23+
// SpanStatusCode,
2424
} from "@opentelemetry/api";
2525
import {
2626
InstrumentationBase,
@@ -535,60 +535,47 @@ export class OpenAIInstrumentation extends InstrumentationBase {
535535
span: Span,
536536
promise: APIPromise<T>,
537537
): APIPromise<T> {
538-
return new APIPromise<T>(
539-
promise
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,
538+
return promise._thenUnwrap((result) => {
539+
if (version === "v3") {
540+
if (type === "chat") {
541+
this._addLogProbsEvent(
542+
span,
543+
((result as any).data as ChatCompletion).choices[0].logprobs,
544+
);
545+
this._endSpan({
546+
type,
547+
span,
548+
result: (result as any).data as ChatCompletion,
549+
});
550+
} else {
551+
this._addLogProbsEvent(
552+
span,
553+
((result as any).data as Completion).choices[0].logprobs,
554+
);
555+
this._endSpan({
556+
type,
557+
span,
558+
result: (result as any).data as Completion,
585559
});
586-
span.recordException(error);
587-
span.end();
560+
}
561+
} else {
562+
if (type === "chat") {
563+
this._addLogProbsEvent(
564+
span,
565+
(result as ChatCompletion).choices[0].logprobs,
566+
);
567+
this._endSpan({ type, span, result: result as ChatCompletion });
568+
} else {
569+
this._addLogProbsEvent(
570+
span,
571+
(result as Completion).choices[0].logprobs,
572+
);
573+
this._endSpan({ type, span, result: result as Completion });
574+
}
575+
}
588576

589-
throw error;
590-
})
591-
);
577+
return result;
578+
});
592579
}
593580

594581
private _endSpan({

0 commit comments

Comments
 (0)
0