forked from run-llama/LlamaIndexTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseIndex.ts
148 lines (134 loc) · 4.34 KB
/
BaseIndex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import type {
BaseChatEngine,
ContextChatEngineOptions,
} from "@llamaindex/core/chat-engine";
import type { ToolMetadata } from "@llamaindex/core/llms";
import type { BaseQueryEngine } from "@llamaindex/core/query-engine";
import type { BaseSynthesizer } from "@llamaindex/core/response-synthesizers";
import type { BaseRetriever } from "@llamaindex/core/retriever";
import type { BaseNode, Document } from "@llamaindex/core/schema";
import type { BaseDocumentStore } from "@llamaindex/core/storage/doc-store";
import type { BaseIndexStore } from "@llamaindex/core/storage/index-store";
import type { JSONSchemaType } from "ajv";
import { runTransformations } from "../ingestion/IngestionPipeline.js";
import { Settings } from "../Settings.js";
import type { StorageContext } from "../storage/StorageContext.js";
import {
type QueryEngineParam,
QueryEngineTool,
} from "../tools/QueryEngineTool.js";
export interface BaseIndexInit<T> {
storageContext: StorageContext;
docStore: BaseDocumentStore;
indexStore?: BaseIndexStore | undefined;
indexStruct: T;
}
/**
* Common parameter type for queryTool and asQueryTool
*/
export type QueryToolParams = (
| {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any;
retriever?: never;
}
| {
options?: never;
retriever?: BaseRetriever;
}
) & {
responseSynthesizer?: BaseSynthesizer;
metadata?: ToolMetadata<JSONSchemaType<QueryEngineParam>> | undefined;
};
/**
* Indexes are the data structure that we store our nodes and embeddings in so
* they can be retrieved for our queries.
*/
export abstract class BaseIndex<T> {
storageContext: StorageContext;
docStore: BaseDocumentStore;
indexStore?: BaseIndexStore | undefined;
indexStruct: T;
constructor(init: BaseIndexInit<T>) {
this.storageContext = init.storageContext;
this.docStore = init.docStore;
this.indexStore = init.indexStore;
this.indexStruct = init.indexStruct;
}
/**
* Create a new retriever from the index.
* @param options
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
abstract asRetriever(options?: any): BaseRetriever;
/**
* Create a new query engine from the index. It will also create a retriever
* and response synthezier if they are not provided.
* @param options you can supply your own custom Retriever and ResponseSynthesizer
*/
abstract asQueryEngine(options?: {
retriever?: BaseRetriever;
responseSynthesizer?: BaseSynthesizer;
}): BaseQueryEngine;
/**
* Create a new chat engine from the index.
* @param options
*/
abstract asChatEngine(
options?: Omit<ContextChatEngineOptions, "retriever">,
): BaseChatEngine;
/**
* Returns a query tool by calling asQueryEngine.
* Either options or retriever can be passed, but not both.
* If options are provided, they are passed to generate a retriever.
*/
asQueryTool(params: QueryToolParams): QueryEngineTool {
if (params.options) {
params.retriever = this.asRetriever(params.options);
}
return new QueryEngineTool({
queryEngine: this.asQueryEngine(params),
metadata: params?.metadata,
});
}
/**
* Insert a document into the index.
* @param document
*/
async insert(document: Document) {
const nodes = await runTransformations([document], [Settings.nodeParser]);
await this.insertNodes(nodes);
await this.docStore.setDocumentHash(document.id_, document.hash);
}
abstract insertNodes(nodes: BaseNode[]): Promise<void>;
abstract deleteRefDoc(
refDocId: string,
deleteFromDocStore?: boolean,
): Promise<void>;
/**
* Alias for asRetriever
* @param options
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
retriever(options?: any): BaseRetriever {
return this.asRetriever(options);
}
/**
* Alias for asQueryEngine
* @param options you can supply your own custom Retriever and ResponseSynthesizer
*/
queryEngine(options?: {
retriever?: BaseRetriever;
responseSynthesizer?: BaseSynthesizer;
}): BaseQueryEngine {
return this.asQueryEngine(options);
}
/**
* Alias for asQueryTool
* Either options or retriever can be passed, but not both.
* If options are provided, they are passed to generate a retriever.
*/
queryTool(params: QueryToolParams): QueryEngineTool {
return this.asQueryTool(params);
}
}