Skip to content

Commit

Permalink
chore(instr-openai): avoid '@typescript-eslint/no-explicit-any' warnings
Browse files Browse the repository at this point in the history
... because GH has a 'feature' called 'annotations' to show these warnings
on all PR diffs, even if that file was not part of that PR. There isn't
a way to turn this misfeature off in GH, so this is the tail wagging the dog
a bit.
  • Loading branch information
trentm committed Dec 19, 2024
1 parent 8fddaf7 commit 8e74ee4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
28 changes: 23 additions & 5 deletions packages/instrumentation-openai/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ import {

// Use `DEBUG=elastic-opentelemetry-instrumentation-openai ...` for debug output.
// Or use `node --env-file ./dev.env ...` in this repo.
import createDebug from 'debug';
const debug = createDebug('elastic-opentelemetry-instrumentation-openai');
import Debug from 'debug';
const debug = Debug('elastic-opentelemetry-instrumentation-openai');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(debug as any).inspectOpts = { depth: 50, colors: true };

export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumentationConfig> {
Expand Down Expand Up @@ -157,16 +158,19 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta

_getPatchedChatCompletionsCreate() {
const self = this;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (original: (...args: unknown[]) => any) => {
// https://platform.openai.com/docs/api-reference/chat/create
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function patchedCreate(this: any, ...args: unknown[]) {
if (!self.isEnabled) {
return original.apply(this, args);
}

debug('OpenAI.Chat.Completions.create args: %O', args);
const params =
args[0] as any; /* type ChatCompletionCreateParamsStreaming */
/** type ChatCompletionCreateParamsStreaming */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const params = args[0] as any;
const config = self.getConfig();
const startNow = performance.now();

Expand All @@ -183,6 +187,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
}
const { span, ctx, commonAttrs } = startInfo;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const apiPromise: Promise<any> = context.with(ctx, () =>
original.apply(this, args)
);
Expand Down Expand Up @@ -239,7 +244,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
* @param {import('openai').OpenAI.ChatCompletionCreateParams} params
*/
_startChatCompletionsSpan(
params: any,
params: any, // eslint-disable-line @typescript-eslint/no-explicit-any
config: OpenAIInstrumentationConfig,
baseURL: string | undefined
) {
Expand Down Expand Up @@ -282,6 +287,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta

// Capture prompts as log events.
const timestamp = Date.now();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params.messages.forEach((msg: any) => {
// `msg` is Array<import('openai/resources/chat/completions').ChatCompletionMessageParam>
let body;
Expand Down Expand Up @@ -328,6 +334,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
} as GenAIAssistantMessageEventBody;
} else {
body = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tool_calls: msg.tool_calls.map((tc: any) => {
return {
id: tc.id,
Expand Down Expand Up @@ -388,6 +395,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
* @param {OpenAIInstrumentationConfig} config
*/
async *_onChatCompletionsStreamIterator(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
streamIter: AsyncIterator<any>,
span: Span,
startNow: number,
Expand All @@ -401,6 +409,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
let finishReason;
const contentParts = [];
const toolCalls = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for await (const chunk of streamIter as any) {
yield chunk;

Expand Down Expand Up @@ -520,6 +529,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
span: Span,
startNow: number,
commonAttrs: Attributes,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result: any,
config: OpenAIInstrumentationConfig,
ctx: Context
Expand All @@ -528,6 +538,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
try {
span.setAttribute(
ATTR_GEN_AI_RESPONSE_FINISH_REASONS,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result.choices.map((c: any) => c.finish_reason)
);
span.setAttribute(ATTR_GEN_AI_RESPONSE_ID, result.id);
Expand All @@ -542,6 +553,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
);

// Capture choices as log events.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result.choices.forEach((choice: any) => {
let message: Partial<GenAIMessage>;
if (config.captureMessageContent) {
Expand All @@ -553,6 +565,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
} else {
message = {};
if (choice.tool_calls) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
message.tool_calls = choice.message.tool_calls.map((tc: any) => {
return {
id: tc.id,
Expand Down Expand Up @@ -640,8 +653,10 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta

_getPatchedEmbeddingsCreate() {
const self = this;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (original: any) => {
// https://platform.openai.com/docs/api-reference/embeddings/create
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function patchedCreate(this: any, ...args: unknown[]) {
if (!self.isEnabled) {
return original.apply(this, args);
Expand All @@ -664,6 +679,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
const apiPromise = context.with(ctx, () => original.apply(this, args));

apiPromise
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.then((result: any) => {
self._onEmbeddingsCreateResult(span, startNow, commonAttrs, result);
})
Expand All @@ -682,6 +698,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
*
* @param {OpenAIInstrumentationConfig} config
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_startEmbeddingsSpan(params: any, baseURL: string | undefined) {
// Attributes common to span, metrics, log events.
const commonAttrs: Attributes = {
Expand Down Expand Up @@ -718,6 +735,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<OpenAIInstrumenta
span: Span,
startNow: number,
commonAttrs: Attributes,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result: any
) {
debug('OpenAI.Embeddings.create result: %O', result);
Expand Down
8 changes: 6 additions & 2 deletions packages/instrumentation-openai/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export function getEnvBool(
}
}

const SERVER_PORT_FROM_URL_PROTOCOL = { 'https:': 443, 'http:': 80 };
type PortFromProtocol = { [key: string]: number };
const SERVER_PORT_FROM_URL_PROTOCOL: PortFromProtocol = {
'https:': 443,
'http:': 80,
};

/**
* Return span/metric attributes from the given OpenAI client baseURL.
Expand Down Expand Up @@ -93,6 +97,6 @@ export function getAttrsFromBaseURL(
[ATTR_SERVER_ADDRESS]: u.hostname,
[ATTR_SERVER_PORT]: u.port
? Number(u.port)
: (SERVER_PORT_FROM_URL_PROTOCOL as any)[u.protocol],
: SERVER_PORT_FROM_URL_PROTOCOL[u.protocol],
};
}

0 comments on commit 8e74ee4

Please sign in to comment.