Skip to content

Commit

Permalink
feat: send retrieve start and end events (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusschiesser authored May 9, 2024
1 parent 0b12990 commit 24a39ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/callbacks/CallbackManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {
LLMStreamEvent,
LLMToolCallEvent,
LLMToolResultEvent,
RetrievalEndEvent,
RetrievalStartEvent,
} from "../llm/types.js";

export class LlamaIndexCustomEvent<T = any> extends CustomEvent<T> {
Expand Down Expand Up @@ -45,6 +47,8 @@ export interface LlamaIndexEventMaps {
* @deprecated
*/
retrieve: CustomEvent<RetrievalCallbackResponse>;
"retrieve-start": RetrievalStartEvent;
"retrieve-end": RetrievalEndEvent;
/**
* @deprecated
*/
Expand Down
38 changes: 19 additions & 19 deletions packages/core/src/indices/vectorStore/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type {
BaseNode,
Document,
Metadata,
NodeWithScore,
} from "../../Node.js";
import type { BaseNode, Document, NodeWithScore } from "../../Node.js";
import { ImageNode, ObjectType, splitNodesByType } from "../../Node.js";
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
import type { ServiceContext } from "../../ServiceContext.js";
import {
Settings,
embedModelFromSettingsOrContext,
nodeParserFromSettingsOrContext,
} from "../../Settings.js";
Expand All @@ -25,6 +19,7 @@ import {
createDocStoreStrategy,
} from "../../ingestion/strategies/index.js";
import { wrapEventCaller } from "../../internal/context/EventCaller.js";
import { getCallbackManager } from "../../internal/settings/CallbackManager.js";
import type { BaseNodePostprocessor } from "../../postprocessors/types.js";
import type { StorageContext } from "../../storage/StorageContext.js";
import { storageContextFromDefaults } from "../../storage/StorageContext.js";
Expand Down Expand Up @@ -411,18 +406,34 @@ export class VectorIndexRetriever implements BaseRetriever {
this.imageSimilarityTopK = imageSimilarityTopK ?? DEFAULT_SIMILARITY_TOP_K;
}

@wrapEventCaller
async retrieve({
query,
preFilters,
}: RetrieveParams): Promise<NodeWithScore[]> {
getCallbackManager().dispatchEvent("retrieve-start", {
payload: {
query,
},
});
let nodesWithScores = await this.textRetrieve(
query,
preFilters as MetadataFilters,
);
nodesWithScores = nodesWithScores.concat(
await this.textToImageRetrieve(query, preFilters as MetadataFilters),
);
this.sendEvent(query, nodesWithScores);
getCallbackManager().dispatchEvent("retrieve-end", {
payload: {
query,
nodes: nodesWithScores,
},
});
// send deprecated event
getCallbackManager().dispatchEvent("retrieve", {
query,
nodes: nodesWithScores,
});
return nodesWithScores;
}

Expand Down Expand Up @@ -459,17 +470,6 @@ export class VectorIndexRetriever implements BaseRetriever {
return this.buildNodeListFromQueryResult(result);
}

@wrapEventCaller
protected sendEvent(
query: string,
nodesWithScores: NodeWithScore<Metadata>[],
) {
Settings.callbackManager.dispatchEvent("retrieve", {
query,
nodes: nodesWithScores,
});
}

protected async buildVectorStoreQuery(
embedModel: BaseEmbedding,
query: string,
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/llm/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type { Tokenizers } from "../GlobalsHelper.js";
import type { NodeWithScore } from "../Node.js";
import type { BaseEvent } from "../internal/type.js";
import type { BaseTool, JSONObject, ToolOutput, UUID } from "../types.js";

export type RetrievalStartEvent = BaseEvent<{
query: string;
}>;
export type RetrievalEndEvent = BaseEvent<{
query: string;
nodes: NodeWithScore[];
}>;
export type LLMStartEvent = BaseEvent<{
id: UUID;
messages: ChatMessage[];
Expand Down

0 comments on commit 24a39ae

Please sign in to comment.