diff --git a/.changeset/eleven-buckets-carry.md b/.changeset/eleven-buckets-carry.md new file mode 100644 index 0000000000000000000000000000000000000000..0d58e0172f5abf21dfc5535785db3ee0da7e3bfe --- /dev/null +++ b/.changeset/eleven-buckets-carry.md @@ -0,0 +1,5 @@ +--- +"llamaindex": patch +--- + +Renamed ListIndex to SummaryIndex to better indicate its use. diff --git a/examples/listIndex.ts b/apps/simple/summaryIndex.ts similarity index 86% rename from examples/listIndex.ts rename to apps/simple/summaryIndex.ts index aabae237ebd152ebb40b083dc84fceaa7bf3c26f..d3fe7736b9a64934359b251bad853260aac796e9 100644 --- a/examples/listIndex.ts +++ b/apps/simple/summaryIndex.ts @@ -1,9 +1,9 @@ import { Document, - ListIndex, ListRetrieverMode, - serviceContextFromDefaults, SimpleNodeParser, + SummaryIndex, + serviceContextFromDefaults, } from "llamaindex"; import essay from "./essay"; @@ -14,7 +14,9 @@ async function main() { }), }); const document = new Document({ text: essay, id_: "essay" }); - const index = await ListIndex.fromDocuments([document], { serviceContext }); + const index = await SummaryIndex.fromDocuments([document], { + serviceContext, + }); const queryEngine = index.asQueryEngine({ retriever: index.asRetriever({ mode: ListRetrieverMode.LLM }), }); diff --git a/apps/simple/listIndex.ts b/examples/summaryIndex.ts similarity index 98% rename from apps/simple/listIndex.ts rename to examples/summaryIndex.ts index aabae237ebd152ebb40b083dc84fceaa7bf3c26f..9045d8ef517472e0cd74e87cc90d72490d3e8c83 100644 --- a/apps/simple/listIndex.ts +++ b/examples/summaryIndex.ts @@ -1,9 +1,8 @@ import { Document, - ListIndex, ListRetrieverMode, - serviceContextFromDefaults, SimpleNodeParser, + serviceContextFromDefaults, } from "llamaindex"; import essay from "./essay"; diff --git a/packages/core/src/indices/index.ts b/packages/core/src/indices/index.ts index b8c28464271f9628c9e840827a8c0d81dce2d919..34acb492fabaafa076e059cd6463532847fcabdb 100644 --- a/packages/core/src/indices/index.ts +++ b/packages/core/src/indices/index.ts @@ -1,3 +1,3 @@ export * from "./BaseIndex"; -export * from "./list"; +export * from "./summary"; export * from "./vectorStore"; diff --git a/packages/core/src/indices/list/index.ts b/packages/core/src/indices/list/index.ts deleted file mode 100644 index f8d0b8d5eae44cf561fd8483fbabf6dc716260d0..0000000000000000000000000000000000000000 --- a/packages/core/src/indices/list/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export { ListIndex, ListRetrieverMode } from "./ListIndex"; -export { - ListIndexRetriever, - ListIndexLLMRetriever, -} from "./ListIndexRetriever"; diff --git a/packages/core/src/indices/list/ListIndex.ts b/packages/core/src/indices/summary/SummaryIndex.ts similarity index 83% rename from packages/core/src/indices/list/ListIndex.ts rename to packages/core/src/indices/summary/SummaryIndex.ts index 21b6ad40bfd6449126ea4fd23123e9279d3b40ae..fcf4e03260b4944d9b7fafeded5de79ea82a1262 100644 --- a/packages/core/src/indices/list/ListIndex.ts +++ b/packages/core/src/indices/summary/SummaryIndex.ts @@ -10,11 +10,11 @@ import { ServiceContext, serviceContextFromDefaults, } from "../../ServiceContext"; -import { BaseDocumentStore, RefDocInfo } from "../../storage/docStore/types"; import { StorageContext, storageContextFromDefaults, } from "../../storage/StorageContext"; +import { BaseDocumentStore, RefDocInfo } from "../../storage/docStore/types"; import { BaseIndex, BaseIndexInit, @@ -22,17 +22,17 @@ import { IndexStructType, } from "../BaseIndex"; import { - ListIndexLLMRetriever, - ListIndexRetriever, -} from "./ListIndexRetriever"; + SummaryIndexLLMRetriever, + SummaryIndexRetriever, +} from "./SummaryIndexRetriever"; -export enum ListRetrieverMode { +export enum SummaryRetrieverMode { DEFAULT = "default", // EMBEDDING = "embedding", LLM = "llm", } -export interface ListIndexOptions { +export interface SummaryIndexOptions { nodes?: BaseNode[]; indexStruct?: IndexList; indexId?: string; @@ -41,14 +41,14 @@ export interface ListIndexOptions { } /** - * A ListIndex keeps nodes in a sequential list structure + * A SummaryIndex keeps nodes in a sequential order for use with summarization. */ -export class ListIndex extends BaseIndex<IndexList> { +export class SummaryIndex extends BaseIndex<IndexList> { constructor(init: BaseIndexInit<IndexList>) { super(init); } - static async init(options: ListIndexOptions): Promise<ListIndex> { + static async init(options: SummaryIndexOptions): Promise<SummaryIndex> { const storageContext = options.storageContext ?? (await storageContextFromDefaults({})); const serviceContext = @@ -80,23 +80,23 @@ export class ListIndex extends BaseIndex<IndexList> { // check indexStruct type if (indexStruct && indexStruct.type !== IndexStructType.LIST) { throw new Error( - "Attempting to initialize ListIndex with non-list indexStruct", + "Attempting to initialize SummaryIndex with non-list indexStruct", ); } if (indexStruct) { if (options.nodes) { throw new Error( - "Cannot initialize VectorStoreIndex with both nodes and indexStruct", + "Cannot initialize SummaryIndex with both nodes and indexStruct", ); } } else { if (!options.nodes) { throw new Error( - "Cannot initialize VectorStoreIndex without nodes or indexStruct", + "Cannot initialize SummaryIndex without nodes or indexStruct", ); } - indexStruct = await ListIndex.buildIndexFromNodes( + indexStruct = await SummaryIndex.buildIndexFromNodes( options.nodes, storageContext.docStore, ); @@ -104,7 +104,7 @@ export class ListIndex extends BaseIndex<IndexList> { await indexStore.addIndexStruct(indexStruct); } - return new ListIndex({ + return new SummaryIndex({ storageContext, serviceContext, docStore, @@ -119,7 +119,7 @@ export class ListIndex extends BaseIndex<IndexList> { storageContext?: StorageContext; serviceContext?: ServiceContext; } = {}, - ): Promise<ListIndex> { + ): Promise<SummaryIndex> { let { storageContext, serviceContext } = args; storageContext = storageContext ?? (await storageContextFromDefaults({})); serviceContext = serviceContext ?? serviceContextFromDefaults({}); @@ -131,7 +131,7 @@ export class ListIndex extends BaseIndex<IndexList> { } const nodes = serviceContext.nodeParser.getNodesFromDocuments(documents); - const index = await ListIndex.init({ + const index = await SummaryIndex.init({ nodes, storageContext, serviceContext, @@ -139,14 +139,14 @@ export class ListIndex extends BaseIndex<IndexList> { return index; } - asRetriever(options?: { mode: ListRetrieverMode }): BaseRetriever { - const { mode = ListRetrieverMode.DEFAULT } = options ?? {}; + asRetriever(options?: { mode: SummaryRetrieverMode }): BaseRetriever { + const { mode = SummaryRetrieverMode.DEFAULT } = options ?? {}; switch (mode) { - case ListRetrieverMode.DEFAULT: - return new ListIndexRetriever(this); - case ListRetrieverMode.LLM: - return new ListIndexLLMRetriever(this); + case SummaryRetrieverMode.DEFAULT: + return new SummaryIndexRetriever(this); + case SummaryRetrieverMode.LLM: + return new SummaryIndexLLMRetriever(this); default: throw new Error(`Unknown retriever mode: ${mode}`); } @@ -253,4 +253,4 @@ export class ListIndex extends BaseIndex<IndexList> { } // Legacy -export type GPTListIndex = ListIndex; +export type ListIndex = SummaryIndex; diff --git a/packages/core/src/indices/list/ListIndexRetriever.ts b/packages/core/src/indices/summary/SummaryIndexRetriever.ts similarity index 85% rename from packages/core/src/indices/list/ListIndexRetriever.ts rename to packages/core/src/indices/summary/SummaryIndexRetriever.ts index d359e7b3ecf0a33e15a1c0d813a7debda56d7680..61d9f2180e26643ebe34ef6f23088d31a1365d98 100644 --- a/packages/core/src/indices/list/ListIndexRetriever.ts +++ b/packages/core/src/indices/summary/SummaryIndexRetriever.ts @@ -1,25 +1,25 @@ -import { BaseRetriever } from "../../Retriever"; +import _ from "lodash"; +import { globalsHelper } from "../../GlobalsHelper"; import { NodeWithScore } from "../../Node"; -import { ListIndex } from "./ListIndex"; +import { SimplePrompt, defaultChoiceSelectPrompt } from "../../Prompt"; +import { BaseRetriever } from "../../Retriever"; import { ServiceContext } from "../../ServiceContext"; +import { Event } from "../../callbacks/CallbackManager"; +import { SummaryIndex } from "./SummaryIndex"; import { - NodeFormatterFunction, ChoiceSelectParserFunction, + NodeFormatterFunction, defaultFormatNodeBatchFn, defaultParseChoiceSelectAnswerFn, } from "./utils"; -import { SimplePrompt, defaultChoiceSelectPrompt } from "../../Prompt"; -import _ from "lodash"; -import { globalsHelper } from "../../GlobalsHelper"; -import { Event } from "../../callbacks/CallbackManager"; /** - * Simple retriever for ListIndex that returns all nodes + * Simple retriever for SummaryIndex that returns all nodes */ -export class ListIndexRetriever implements BaseRetriever { - index: ListIndex; +export class SummaryIndexRetriever implements BaseRetriever { + index: SummaryIndex; - constructor(index: ListIndex) { + constructor(index: SummaryIndex) { this.index = index; } @@ -51,10 +51,10 @@ export class ListIndexRetriever implements BaseRetriever { } /** - * LLM retriever for ListIndex. + * LLM retriever for SummaryIndex which lets you select the most relevant chunks. */ -export class ListIndexLLMRetriever implements BaseRetriever { - index: ListIndex; +export class SummaryIndexLLMRetriever implements BaseRetriever { + index: SummaryIndex; choiceSelectPrompt: SimplePrompt; choiceBatchSize: number; formatNodeBatchFn: NodeFormatterFunction; @@ -62,12 +62,12 @@ export class ListIndexLLMRetriever implements BaseRetriever { serviceContext: ServiceContext; constructor( - index: ListIndex, + index: SummaryIndex, choiceSelectPrompt?: SimplePrompt, choiceBatchSize: number = 10, formatNodeBatchFn?: NodeFormatterFunction, parseChoiceSelectAnswerFn?: ChoiceSelectParserFunction, - serviceContext?: ServiceContext + serviceContext?: ServiceContext, ) { this.index = index; this.choiceSelectPrompt = choiceSelectPrompt || defaultChoiceSelectPrompt; @@ -95,7 +95,7 @@ export class ListIndexLLMRetriever implements BaseRetriever { // parseResult is a map from doc number to relevance score const parseResult = this.parseChoiceSelectAnswerFn( rawResponse, - nodesBatch.length + nodesBatch.length, ); const choiceNodeIds = nodeIdsBatch.filter((nodeId, idx) => { return `${idx}` in parseResult; @@ -128,3 +128,7 @@ export class ListIndexLLMRetriever implements BaseRetriever { return this.serviceContext; } } + +// Legacy +export type ListIndexRetriever = SummaryIndexRetriever; +export type ListIndexLLMRetriever = SummaryIndexLLMRetriever; diff --git a/packages/core/src/indices/summary/index.ts b/packages/core/src/indices/summary/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..b3f8dba2c7acb7d2feacd47294c33e0b4ac95f7c --- /dev/null +++ b/packages/core/src/indices/summary/index.ts @@ -0,0 +1,13 @@ +export { + SummaryRetrieverMode as ListRetrieverMode, + SummaryIndex, +} from "./SummaryIndex"; +export type { ListIndex } from "./SummaryIndex"; +export { + SummaryIndexLLMRetriever, + SummaryIndexRetriever, +} from "./SummaryIndexRetriever"; +export type { + ListIndexLLMRetriever, + ListIndexRetriever, +} from "./SummaryIndexRetriever"; diff --git a/packages/core/src/indices/list/utils.ts b/packages/core/src/indices/summary/utils.ts similarity index 100% rename from packages/core/src/indices/list/utils.ts rename to packages/core/src/indices/summary/utils.ts diff --git a/packages/core/src/tests/CallbackManager.test.ts b/packages/core/src/tests/CallbackManager.test.ts index ff04057da5de48b5f658840178d98a48e4a77be4..c3d9a98d4ac1f0f49ec64a638acea0941c9aae6f 100644 --- a/packages/core/src/tests/CallbackManager.test.ts +++ b/packages/core/src/tests/CallbackManager.test.ts @@ -1,18 +1,18 @@ -import { VectorStoreIndex } from "../indices/vectorStore/VectorStoreIndex"; import { OpenAIEmbedding } from "../Embedding"; -import { OpenAI } from "../llm/LLM"; import { Document } from "../Node"; +import { + ResponseSynthesizer, + SimpleResponseBuilder, +} from "../ResponseSynthesizer"; import { ServiceContext, serviceContextFromDefaults } from "../ServiceContext"; import { CallbackManager, RetrievalCallbackResponse, StreamCallbackResponse, } from "../callbacks/CallbackManager"; -import { ListIndex, ListRetrieverMode } from "../indices/list"; -import { - ResponseSynthesizer, - SimpleResponseBuilder, -} from "../ResponseSynthesizer"; +import { SummaryIndex } from "../indices/summary"; +import { VectorStoreIndex } from "../indices/vectorStore/VectorStoreIndex"; +import { OpenAI } from "../llm/LLM"; import { mockEmbeddingModel, mockLlmGeneration } from "./utility/mockOpenAI"; // Mock the OpenAI getOpenAISession function during testing @@ -65,10 +65,9 @@ describe("CallbackManager: onLLMStream and onRetrieve", () => { }); test("For VectorStoreIndex w/ a SimpleResponseBuilder", async () => { - const vectorStoreIndex = await VectorStoreIndex.fromDocuments( - [document], - { serviceContext } - ); + const vectorStoreIndex = await VectorStoreIndex.fromDocuments([document], { + serviceContext, + }); const queryEngine = vectorStoreIndex.asQueryEngine(); const query = "What is the author's name?"; const response = await queryEngine.query(query); @@ -132,21 +131,20 @@ describe("CallbackManager: onLLMStream and onRetrieve", () => { // both retrieval and streaming should have // the same parent event expect(streamCallbackData[0].event.parentId).toBe( - retrieveCallbackData[0].event.parentId + retrieveCallbackData[0].event.parentId, ); }); - test("For ListIndex w/ a ListIndexRetriever", async () => { - const listIndex = await ListIndex.fromDocuments( - [document], - { serviceContext }, - ); + test("For SummaryIndex w/ a SummaryIndexRetriever", async () => { + const summaryIndex = await SummaryIndex.fromDocuments([document], { + serviceContext, + }); const responseBuilder = new SimpleResponseBuilder(serviceContext); const responseSynthesizer = new ResponseSynthesizer({ serviceContext: serviceContext, responseBuilder, }); - const queryEngine = listIndex.asQueryEngine({ + const queryEngine = summaryIndex.asQueryEngine({ responseSynthesizer, }); const query = "What is the author's name?"; @@ -211,7 +209,7 @@ describe("CallbackManager: onLLMStream and onRetrieve", () => { // both retrieval and streaming should have // the same parent event expect(streamCallbackData[0].event.parentId).toBe( - retrieveCallbackData[0].event.parentId + retrieveCallbackData[0].event.parentId, ); }); });