Skip to content

Commit

Permalink
refactor: remove parentEvent (step 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Apr 1, 2024
1 parent a6dfa30 commit f66f705
Show file tree
Hide file tree
Showing 23 changed files with 36 additions and 331 deletions.
40 changes: 0 additions & 40 deletions packages/core/src/GlobalsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { encodingForModel } from "js-tiktoken";

import { randomUUID } from "@llamaindex/env";
import type {
Event,
EventTag,
EventType,
} from "./callbacks/CallbackManager.js";

export enum Tokenizers {
CL100K_BASE = "cl100k_base",
}
Expand Down Expand Up @@ -51,39 +44,6 @@ class GlobalsHelper {

return this.defaultTokenizer!.decode.bind(this.defaultTokenizer);
}

/**
* @deprecated createEvent will be removed in the future,
* please use `new CustomEvent(eventType, { detail: payload })` instead.
*
* Also, `parentEvent` will not be used in the future,
* use `AsyncLocalStorage` to track parent events instead.
* @example - Usage of `AsyncLocalStorage`:
* let id = 0;
* const asyncLocalStorage = new AsyncLocalStorage<number>();
* asyncLocalStorage.run(++id, async () => {
* setTimeout(() => {
* console.log('parent event id:', asyncLocalStorage.getStore()); // 1
* }, 1000)
* });
*/
createEvent({
parentEvent,
type,
tags,
}: {
parentEvent?: Event;
type: EventType;
tags?: EventTag[];
}): Event {
return {
id: randomUUID(),
type,
// inherit parent tags if tags not set
tags: tags || parentEvent?.tags,
parentId: parentEvent?.id,
};
}
}

export const globalsHelper = new GlobalsHelper();
5 changes: 0 additions & 5 deletions packages/core/src/Retriever.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import type { Event } from "./callbacks/CallbackManager.js";
import type { NodeWithScore } from "./Node.js";
import type { ServiceContext } from "./ServiceContext.js";

export type RetrieveParams = {
query: string;
/**
* @deprecated will be removed in the next major version
*/
parentEvent?: Event;
preFilters?: unknown;
};

Expand Down
24 changes: 2 additions & 22 deletions packages/core/src/callbacks/CallbackManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,6 @@ declare module "llamaindex" {
}

//#region @deprecated remove in the next major version
/*
An event is a wrapper that groups related operations.
For example, during retrieve and synthesize,
a parent event wraps both operations, and each operation has it's own
event. In this case, both sub-events will share a parentId.
*/

export type EventTag = "intermediate" | "final";
export type EventType = "retrieve" | "llmPredict" | "wrapper";
export interface Event {
id: string;
type: EventType;
tags?: EventTag[];
parentId?: string;
}

interface BaseCallbackResponse {
event: Event;
}

//Specify StreamToken per mainstream LLM
export interface DefaultStreamToken {
id: string;
Expand Down Expand Up @@ -68,13 +48,13 @@ export type AnthropicStreamToken = Anthropic.Completion;

//StreamCallbackResponse should let practitioners implement callbacks out of the box...
//When custom streaming LLMs are involved, people are expected to write their own StreamCallbackResponses
export interface StreamCallbackResponse extends BaseCallbackResponse {
export interface StreamCallbackResponse {
index: number;
isDone?: boolean;
token?: DefaultStreamToken;
}

export interface RetrievalCallbackResponse extends BaseCallbackResponse {
export interface RetrievalCallbackResponse {
query: string;
nodes: NodeWithScore[];
}
Expand Down
8 changes: 1 addition & 7 deletions packages/core/src/cloud/LlamaCloudRetriever.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { PlatformApi, PlatformApiClient } from "@llamaindex/cloud";
import { globalsHelper } from "../GlobalsHelper.js";
import type { NodeWithScore } from "../Node.js";
import { ObjectType, jsonToNode } from "../Node.js";
import type { BaseRetriever, RetrieveParams } from "../Retriever.js";
Expand Down Expand Up @@ -53,7 +52,6 @@ export class LlamaCloudRetriever implements BaseRetriever {

async retrieve({
query,
parentEvent,
preFilters,
}: RetrieveParams): Promise<NodeWithScore[]> {
const pipelines = await (
Expand All @@ -77,13 +75,9 @@ export class LlamaCloudRetriever implements BaseRetriever {

const nodes = this.resultNodesToNodeWithScore(results.retrievalNodes);

Settings.callbackManager.onRetrieve({
Settings.callbackManager.dispatchEvent("retrieve", {
query,
nodes,
event: globalsHelper.createEvent({
parentEvent,
type: "retrieve",
}),
});

return nodes;
Expand Down
13 changes: 1 addition & 12 deletions packages/core/src/engines/chat/ContextChatEngine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { randomUUID } from "@llamaindex/env";
import type { ChatHistory } from "../../ChatHistory.js";
import { getHistory } from "../../ChatHistory.js";
import type { ContextSystemPrompt } from "../../Prompt.js";
import { Response } from "../../Response.js";
import type { BaseRetriever } from "../../Retriever.js";
import type { Event } from "../../callbacks/CallbackManager.js";
import type { ChatMessage, ChatResponseChunk, LLM } from "../../llm/index.js";
import { OpenAI } from "../../llm/index.js";
import type { MessageContent } from "../../llm/types.js";
Expand Down Expand Up @@ -67,21 +65,14 @@ export class ContextChatEngine extends PromptMixin implements ChatEngine {
const chatHistory = params.chatHistory
? getHistory(params.chatHistory)
: this.chatHistory;
const parentEvent: Event = {
id: randomUUID(),
type: "wrapper",
tags: ["final"],
};
const requestMessages = await this.prepareRequestMessages(
message,
chatHistory,
parentEvent,
);

if (stream) {
const stream = await this.chatModel.chat({
messages: requestMessages.messages,
parentEvent,
stream: true,
});
return streamConverter(
Expand All @@ -98,7 +89,6 @@ export class ContextChatEngine extends PromptMixin implements ChatEngine {
}
const response = await this.chatModel.chat({
messages: requestMessages.messages,
parentEvent,
});
chatHistory.addMessage(response.message);
return new Response(response.message.content, requestMessages.nodes);
Expand All @@ -111,14 +101,13 @@ export class ContextChatEngine extends PromptMixin implements ChatEngine {
private async prepareRequestMessages(
message: MessageContent,
chatHistory: ChatHistory,
parentEvent?: Event,
) {
chatHistory.addMessage({
content: message,
role: "user",
});
const textOnly = extractText(message);
const context = await this.contextGenerator.generate(textOnly, parentEvent);
const context = await this.contextGenerator.generate(textOnly);
const nodes = context.nodes.map((r) => r.node);
const messages = await chatHistory.requestMessages(
context ? [context.message] : undefined,
Expand Down
12 changes: 1 addition & 11 deletions packages/core/src/engines/chat/DefaultContextGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { randomUUID } from "@llamaindex/env";
import type { NodeWithScore, TextNode } from "../../Node.js";
import type { ContextSystemPrompt } from "../../Prompt.js";
import { defaultContextSystemPrompt } from "../../Prompt.js";
import type { BaseRetriever } from "../../Retriever.js";
import type { Event } from "../../callbacks/CallbackManager.js";
import type { BaseNodePostprocessor } from "../../postprocessors/index.js";
import { PromptMixin } from "../../prompts/index.js";
import type { Context, ContextGenerator } from "./types.js";
Expand Down Expand Up @@ -56,17 +54,9 @@ export class DefaultContextGenerator
return nodesWithScore;
}

async generate(message: string, parentEvent?: Event): Promise<Context> {
if (!parentEvent) {
parentEvent = {
id: randomUUID(),
type: "wrapper",
tags: ["final"],
};
}
async generate(message: string): Promise<Context> {
const sourceNodesWithScore = await this.retriever.retrieve({
query: message,
parentEvent,
});

const nodes = await this.applyNodePostprocessors(
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/engines/chat/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ChatHistory } from "../../ChatHistory.js";
import type { BaseNode, NodeWithScore } from "../../Node.js";
import type { Response } from "../../Response.js";
import type { Event } from "../../callbacks/CallbackManager.js";
import type { ChatMessage } from "../../llm/index.js";
import type { MessageContent } from "../../llm/types.js";
import type { ToolOutput } from "../../tools/types.js";
Expand Down Expand Up @@ -56,7 +55,7 @@ export interface Context {
* A ContextGenerator is used to generate a context based on a message's text content
*/
export interface ContextGenerator {
generate(message: string, parentEvent?: Event): Promise<Context>;
generate(message: string): Promise<Context>;
}

export enum ChatResponseMode {
Expand Down
14 changes: 2 additions & 12 deletions packages/core/src/engines/query/RetrieverQueryEngine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { randomUUID } from "@llamaindex/env";
import type { NodeWithScore } from "../../Node.js";
import type { Response } from "../../Response.js";
import type { BaseRetriever } from "../../Retriever.js";
import type { Event } from "../../callbacks/CallbackManager.js";
import type { BaseNodePostprocessor } from "../../postprocessors/index.js";
import { PromptMixin } from "../../prompts/Mixin.js";
import type { BaseSynthesizer } from "../../synthesizers/index.js";
Expand Down Expand Up @@ -62,10 +60,9 @@ export class RetrieverQueryEngine
return nodesWithScore;
}

private async retrieve(query: string, parentEvent: Event) {
private async retrieve(query: string) {
const nodes = await this.retriever.retrieve({
query,
parentEvent,
preFilters: this.preFilters,
});

Expand All @@ -78,24 +75,17 @@ export class RetrieverQueryEngine
params: QueryEngineParamsStreaming | QueryEngineParamsNonStreaming,
): Promise<Response | AsyncIterable<Response>> {
const { query, stream } = params;
const parentEvent: Event = params.parentEvent || {
id: randomUUID(),
type: "wrapper",
tags: ["final"],
};
const nodesWithScore = await this.retrieve(query, parentEvent);
const nodesWithScore = await this.retrieve(query);
if (stream) {
return this.responseSynthesizer.synthesize({
query,
nodesWithScore,
parentEvent,
stream: true,
});
}
return this.responseSynthesizer.synthesize({
query,
nodesWithScore,
parentEvent,
});
}
}
27 changes: 2 additions & 25 deletions packages/core/src/engines/query/SubQuestionQueryEngine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { randomUUID } from "@llamaindex/env";
import type { NodeWithScore } from "../../Node.js";
import { TextNode } from "../../Node.js";
import { LLMQuestionGenerator } from "../../QuestionGenerator.js";
import type { Response } from "../../Response.js";
import type { ServiceContext } from "../../ServiceContext.js";
import type { Event } from "../../callbacks/CallbackManager.js";
import { PromptMixin } from "../../prompts/Mixin.js";
import type { BaseSynthesizer } from "../../synthesizers/index.js";
import {
Expand Down Expand Up @@ -86,23 +84,8 @@ export class SubQuestionQueryEngine
const { query, stream } = params;
const subQuestions = await this.questionGen.generate(this.metadatas, query);

// groups final retrieval+synthesis operation
const parentEvent: Event = params.parentEvent || {
id: randomUUID(),
type: "wrapper",
tags: ["final"],
};

// groups all sub-queries
const subQueryParentEvent: Event = {
id: randomUUID(),
parentId: parentEvent.id,
type: "wrapper",
tags: ["intermediate"],
};

const subQNodes = await Promise.all(
subQuestions.map((subQ) => this.querySubQ(subQ, subQueryParentEvent)),
subQuestions.map((subQ) => this.querySubQ(subQ)),
);

const nodesWithScore = subQNodes
Expand All @@ -112,21 +95,16 @@ export class SubQuestionQueryEngine
return this.responseSynthesizer.synthesize({
query,
nodesWithScore,
parentEvent,
stream: true,
});
}
return this.responseSynthesizer.synthesize({
query,
nodesWithScore,
parentEvent,
});
}

private async querySubQ(
subQ: SubQuestion,
parentEvent?: Event,
): Promise<NodeWithScore | null> {
private async querySubQ(subQ: SubQuestion): Promise<NodeWithScore | null> {
try {
const question = subQ.subQuestion;

Expand All @@ -140,7 +118,6 @@ export class SubQuestionQueryEngine

const responseText = await queryEngine?.call?.({
query: question,
parentEvent,
});

if (!responseText) {
Expand Down
Loading

0 comments on commit f66f705

Please sign in to comment.