-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(js): llama-index-ts llm support #829
base: llama-index-ts
Are you sure you want to change the base?
Changes from all commits
f6ba9e3
787abb3
ae7af3f
337fb86
10dbe6a
daea977
5680e56
ba3802d
04b27e4
3cceb5c
3bed245
6cb6613
82434d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { LlamaIndexInstrumentation } from "../src/index"; | ||
import { ConsoleSpanExporter } from "@opentelemetry/sdk-trace-base"; | ||
import { | ||
NodeTracerProvider, | ||
SimpleSpanProcessor, | ||
} from "@opentelemetry/sdk-trace-node"; | ||
import { Resource } from "@opentelemetry/resources"; | ||
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto"; | ||
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; | ||
import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api"; | ||
import { registerInstrumentations } from "@opentelemetry/instrumentation"; | ||
// For troubleshooting, set the log level to DiagLogLevel.DEBUG | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); | ||
|
||
const provider = new NodeTracerProvider({ | ||
resource: new Resource({ | ||
[SEMRESATTRS_SERVICE_NAME]: "llama-index-service", | ||
}), | ||
}); | ||
|
||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.addSpanProcessor( | ||
new SimpleSpanProcessor( | ||
new OTLPTraceExporter({ | ||
url: "http://localhost:6006/v1/traces", | ||
}), | ||
), | ||
); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [new LlamaIndexInstrumentation()], | ||
}); | ||
|
||
provider.register(); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("👀 OpenInference initialized"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import "./instrumentation"; | ||
import fs from "fs/promises"; | ||
import { VectorStoreIndex } from "llamaindex"; | ||
import { Document } from "@llamaindex/core/dist/schema"; | ||
|
||
async function main() { | ||
// Load essay from abramov.txt in Node | ||
const essay = await fs.readFile( | ||
"node_modules/llamaindex/examples/abramov.txt", | ||
"utf-8", | ||
); | ||
|
||
// Create Document object with essay | ||
const document = new Document({ text: essay }); | ||
|
||
// Split text and create embeddings. Store them in a VectorStoreIndex | ||
const index = await VectorStoreIndex.fromDocuments([document]); | ||
|
||
// Query the index | ||
const queryEngine = index.asQueryEngine(); | ||
const response = await queryEngine.query({ | ||
query: "What did the author do in college?", | ||
}); | ||
|
||
// Output response | ||
// eslint-disable-next-line no-console | ||
console.log(response.toString()); | ||
} | ||
|
||
main(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./instrumentation"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import type * as llamaindex from "llamaindex"; | ||
|
||
import { | ||
InstrumentationBase, | ||
InstrumentationConfig, | ||
InstrumentationModuleDefinition, | ||
InstrumentationNodeModuleDefinition, | ||
} from "@opentelemetry/instrumentation"; | ||
import { diag } from "@opentelemetry/api"; | ||
import { | ||
isRetrieverPrototype, | ||
isEmbeddingPrototype, | ||
isLLMPrototype, | ||
} from "./utils"; | ||
import { | ||
patchQueryEngineQueryMethod, | ||
patchRetrieveMethod, | ||
patchQueryEmbeddingMethod, | ||
patchLLMChat, | ||
} from "./patch"; | ||
import { VERSION } from "./version"; | ||
|
||
const MODULE_NAME = "llamaindex"; | ||
|
||
/** | ||
* Flag to check if the LlamaIndex module has been patched | ||
* Note: This is a fallback in case the module is made immutable (e.x. Deno, webpack, etc.) | ||
*/ | ||
let _isOpenInferencePatched = false; | ||
|
||
/** | ||
* function to check if instrumentation is enabled / disabled | ||
*/ | ||
export function isPatched() { | ||
return _isOpenInferencePatched; | ||
} | ||
|
||
export class LlamaIndexInstrumentation extends InstrumentationBase< | ||
typeof llamaindex | ||
> { | ||
constructor(config?: InstrumentationConfig) { | ||
super( | ||
"@arizeai/openinference-instrumentation-llama-index", | ||
VERSION, | ||
Object.assign({}, config), | ||
); | ||
} | ||
|
||
public manuallyInstrument(module: typeof llamaindex) { | ||
diag.debug(`Manually instrumenting ${MODULE_NAME}`); | ||
this.patch(module); | ||
} | ||
|
||
protected init(): InstrumentationModuleDefinition<typeof llamaindex> { | ||
const module = new InstrumentationNodeModuleDefinition<typeof llamaindex>( | ||
"llamaindex", | ||
[">=0.5.0"], | ||
this.patch.bind(this), | ||
this.unpatch.bind(this), | ||
); | ||
return module; | ||
} | ||
|
||
private patch(moduleExports: typeof llamaindex, moduleVersion?: string) { | ||
this._diag.debug(`Applying patch for ${MODULE_NAME}@${moduleVersion}`); | ||
if (_isOpenInferencePatched) { | ||
return moduleExports; | ||
} | ||
|
||
// TODO: Support streaming | ||
// TODO: Generalize to QueryEngine interface (RetrieverQueryEngine, RouterQueryEngine) | ||
this._wrap( | ||
moduleExports.RetrieverQueryEngine.prototype, | ||
"query", | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(original): any => { | ||
return patchQueryEngineQueryMethod(original, this.tracer); | ||
}, | ||
); | ||
|
||
for (const value of Object.values(moduleExports)) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const prototype = (value as any).prototype; | ||
|
||
if (isRetrieverPrototype(prototype)) { | ||
this._wrap(prototype, "retrieve", (original) => { | ||
return patchRetrieveMethod(original, this.tracer); | ||
}); | ||
} | ||
|
||
if (isEmbeddingPrototype(prototype)) { | ||
this._wrap(prototype, "getQueryEmbedding", (original) => { | ||
return patchQueryEmbeddingMethod(original, this.tracer); | ||
}); | ||
} | ||
|
||
if (isLLMPrototype(prototype)) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._wrap(prototype, "chat", (original): any => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the return type any here just because the variations on the types of "chat" tha can come from the different classes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming you're referring to the "any" return type. The return type would be the function of |
||
return patchLLMChat(original, this.tracer); | ||
}); | ||
} | ||
} | ||
_isOpenInferencePatched = true; | ||
return moduleExports; | ||
} | ||
|
||
private unpatch(moduleExports: typeof llamaindex, moduleVersion?: string) { | ||
this._diag.debug(`Un-patching ${MODULE_NAME}@${moduleVersion}`); | ||
this._unwrap(moduleExports.RetrieverQueryEngine.prototype, "query"); | ||
|
||
for (const value of Object.values(moduleExports)) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const prototype = (value as any).prototype; | ||
|
||
if (isRetrieverPrototype(prototype)) { | ||
this._unwrap(prototype, "retrieve"); | ||
} | ||
|
||
if (isEmbeddingPrototype(prototype)) { | ||
this._unwrap(prototype, "getQueryEmbedding"); | ||
} | ||
|
||
if (isLLMPrototype(prototype)) { | ||
this._unwrap(prototype, "chat"); | ||
} | ||
} | ||
|
||
_isOpenInferencePatched = false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct?