Skip to content

Commit d255ea4

Browse files
committed
fix(instrumentation-llamaindex): patch OpenAI LLM from @llamaindex/openai
1 parent a869545 commit d255ea4

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

packages/instrumentation-llamaindex/src/instrumentation.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,29 @@ import { version } from "../package.json";
3636

3737
export class LlamaIndexInstrumentation extends InstrumentationBase {
3838
declare protected _config: LlamaIndexInstrumentationConfig;
39+
private customLLMInstrumentation!: CustomLLMInstrumentation;
3940

4041
constructor(config: LlamaIndexInstrumentationConfig = {}) {
4142
super("@traceloop/instrumentation-llamaindex", version, config);
43+
this.customLLMInstrumentation = new CustomLLMInstrumentation(
44+
this._config,
45+
this._diag,
46+
() => this.tracer,
47+
);
4248
}
4349

4450
public override setConfig(config: LlamaIndexInstrumentationConfig = {}) {
4551
super.setConfig(config);
4652
}
4753

48-
public manuallyInstrument(module: typeof llamaindex) {
54+
public manuallyInstrument(module: typeof llamaindex, openaiModule?: any) {
4955
this._diag.debug("Manually instrumenting llamaindex");
5056

5157
this.patch(module);
58+
59+
if (openaiModule) {
60+
this.patchOpenAI(openaiModule);
61+
}
5262
}
5363

5464
protected init(): InstrumentationModuleDefinition[] {
@@ -94,12 +104,6 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
94104
private patch(moduleExports: typeof llamaindex, moduleVersion?: string) {
95105
this._diag.debug(`Patching llamaindex@${moduleVersion}`);
96106

97-
const customLLMInstrumentation = new CustomLLMInstrumentation(
98-
this._config,
99-
this._diag,
100-
() => this.tracer, // this is on purpose. Tracer may change
101-
);
102-
103107
this._wrap(
104108
moduleExports.RetrieverQueryEngine.prototype,
105109
"query",
@@ -133,7 +137,7 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
133137
this._wrap(
134138
cls.prototype,
135139
"chat",
136-
customLLMInstrumentation.chatWrapper({ className: cls.name }),
140+
this.customLLMInstrumentation.chatWrapper({ className: cls.name }),
137141
);
138142
} else if (this.isEmbedding(cls.prototype)) {
139143
this._wrap(
@@ -202,7 +206,16 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
202206
private patchOpenAI(moduleExports: any, moduleVersion?: string) {
203207
this._diag.debug(`Patching @llamaindex/openai@${moduleVersion}`);
204208

205-
// Instrument OpenAIAgent if it exists
209+
if (moduleExports.OpenAI && this.isLLM(moduleExports.OpenAI.prototype)) {
210+
this._wrap(
211+
moduleExports.OpenAI.prototype,
212+
"chat",
213+
this.customLLMInstrumentation.chatWrapper({
214+
className: moduleExports.OpenAI.name,
215+
}),
216+
);
217+
}
218+
206219
if (moduleExports.OpenAIAgent && moduleExports.OpenAIAgent.prototype) {
207220
this._wrap(
208221
moduleExports.OpenAIAgent.prototype,
@@ -223,7 +236,10 @@ export class LlamaIndexInstrumentation extends InstrumentationBase {
223236
private unpatchOpenAI(moduleExports: any, moduleVersion?: string) {
224237
this._diag.debug(`Unpatching @llamaindex/openai@${moduleVersion}`);
225238

226-
// Unwrap OpenAIAgent if it exists
239+
if (moduleExports.OpenAI && moduleExports.OpenAI.prototype) {
240+
this._unwrap(moduleExports.OpenAI.prototype, "chat");
241+
}
242+
227243
if (moduleExports.OpenAIAgent && moduleExports.OpenAIAgent.prototype) {
228244
this._unwrap(moduleExports.OpenAIAgent.prototype, "chat");
229245
}

packages/sample-app/src/sample_llama_index_openai_agent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as llamaindex from "llamaindex";
22
import * as traceloop from "@traceloop/node-server-sdk";
3+
import * as llamaIndexOpenAI from "@llamaindex/openai";
34
import { OpenAIAgent, OpenAI as LLamaOpenAI } from "@llamaindex/openai";
45

56
traceloop.initialize({
@@ -8,6 +9,7 @@ traceloop.initialize({
89
disableBatch: true,
910
instrumentModules: {
1011
llamaIndex: llamaindex,
12+
llamaIndexOpenAI,
1113
},
1214
});
1315

packages/sample-app/src/sample_llamaindex.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import * as traceloop from "@traceloop/node-server-sdk";
2+
import * as llamaindex from "llamaindex";
23
import { VectorStoreIndex, Document, Settings } from "llamaindex";
4+
import * as llamaIndexOpenAI from "@llamaindex/openai";
35
import { OpenAIEmbedding, OpenAI } from "@llamaindex/openai";
46
import { readFile } from "fs/promises";
57

68
traceloop.initialize({
79
appName: "sample_llamaindex",
810
apiKey: process.env.TRACELOOP_API_KEY,
911
disableBatch: true,
12+
instrumentModules: {
13+
llamaIndex: llamaindex,
14+
llamaIndexOpenAI,
15+
},
1016
});
1117

1218
Settings.embedModel = new OpenAIEmbedding();

packages/traceloop-sdk/src/lib/interfaces/initialize-options.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export interface InitializeOptions {
9797
together?: typeof together.Together;
9898
langchain?: boolean;
9999
llamaIndex?: typeof llamaindex;
100+
llamaIndexOpenAI?: any;
100101
chromadb?: typeof chromadb;
101102
qdrant?: typeof qdrant;
102103
mcp?: typeof mcp;

packages/traceloop-sdk/src/lib/tracing/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ export const manuallyInitInstrumentations = (
218218
exceptionLogger,
219219
});
220220
instrumentations.push(llamaIndexInstrumentation);
221-
llamaIndexInstrumentation.manuallyInstrument(instrumentModules.llamaIndex);
221+
llamaIndexInstrumentation.manuallyInstrument(
222+
instrumentModules.llamaIndex,
223+
instrumentModules.llamaIndexOpenAI,
224+
);
222225
}
223226

224227
if (instrumentModules?.chromadb) {

0 commit comments

Comments
 (0)