-
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
Open
cjunkin
wants to merge
13
commits into
llama-index-ts
Choose a base branch
from
llama-index-ts-llm
base: llama-index-ts
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f6ba9e3
setup CI
mikeldking 787abb3
feat(llamaindex.TS): llama-index-ts initial package creation (#497)
mikeldking ae7af3f
feat(js): llama-index-ts initial retrieval span support (#643)
cjunkin 337fb86
feat(js): llama-index-ts embeddings support (#761)
cjunkin 10dbe6a
initial llm span support
cjunkin daea977
llm complete support
cjunkin 5680e56
llm tests, updated other tests, removed completion functionality for now
cjunkin ba3802d
complete check
cjunkin 04b27e4
upped version
cjunkin 3cceb5c
upped version
cjunkin 3bed245
pin llamaindex version to 0.5.x
cjunkin 6cb6613
safe method calling, refactor methods and file org
cjunkin 82434d2
fix type names
cjunkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
js/packages/openinference-instrumentation-langchain/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
js/packages/openinference-instrumentation-llama-index/examples/instrumentation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
29 changes: 29 additions & 0 deletions
29
js/packages/openinference-instrumentation-llama-index/examples/llamaindex-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import "./instrumentation"; | ||
import fs from "fs/promises"; | ||
import { Document, VectorStoreIndex } from "llamaindex"; | ||
|
||
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(); |
1 change: 1 addition & 0 deletions
1
js/packages/openinference-instrumentation-llama-index/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./instrumentation"; |
129 changes: 129 additions & 0 deletions
129
js/packages/openinference-instrumentation-llama-index/src/instrumentation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import type * as llamaindex from "llamaindex"; | ||
|
||
import { | ||
InstrumentationBase, | ||
InstrumentationConfig, | ||
InstrumentationModuleDefinition, | ||
InstrumentationNodeModuleDefinition, | ||
} from "@opentelemetry/instrumentation"; | ||
import { diag } from "@opentelemetry/api"; | ||
import { | ||
isRetrieverPrototype, | ||
isEmbeddingPrototype, | ||
isLLMPrototype, | ||
patchQueryEngineQueryMethod, | ||
patchRetrieveMethod, | ||
patchQueryEmbeddingMethod, | ||
patchLLMChat, | ||
} from "./utils"; | ||
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.1.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 => { | ||
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
js/packages/openinference-instrumentation-llama-index/src/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as llamaindex from "llamaindex"; | ||
import { BaseRetriever } from "llamaindex"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type GenericFunction = (...args: any[]) => any; | ||
|
||
export type SafeFunction<T extends GenericFunction> = ( | ||
...args: Parameters<T> | ||
) => ReturnType<T> | null; | ||
|
||
export type ObjectWithModel = { model: string }; | ||
|
||
export type RetrieverQueryEngineQueryMethodType = | ||
typeof llamaindex.RetrieverQueryEngine.prototype.query; | ||
|
||
export type RetrieverRetrieveMethodType = BaseRetriever["retrieve"]; | ||
|
||
export type QueryEmbeddingMethodType = | ||
typeof llamaindex.BaseEmbedding.prototype.getQueryEmbedding; | ||
|
||
export type LLMChatMethodType = llamaindex.LLM["chat"]; | ||
export type LLMCompleteMethodType = llamaindex.LLM["complete"]; | ||
export type LLMObject = { metadata: llamaindex.LLMMetadata }; | ||
cjunkin marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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 comment
The 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
(this: unknown, params: LLMChatParamsNonStreaming<object, object>) => Promise<ChatResponse<object>>
. Should I assign this to a type variable and specify a strict return type here?