-
Notifications
You must be signed in to change notification settings - Fork 318
/
IngestionPipeline.ts
178 lines (164 loc) · 4.97 KB
/
IngestionPipeline.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import type { PlatformApiClient } from "@llamaindex/cloud";
import type { BaseNode, Document } from "../Node.js";
import { getPipelineCreate } from "../cloud/config.js";
import {
DEFAULT_PIPELINE_NAME,
DEFAULT_PROJECT_NAME,
type ClientParams,
} from "../cloud/types.js";
import { getAppBaseUrl, getClient } from "../cloud/utils.js";
import type { BaseReader } from "../readers/type.js";
import type { BaseDocumentStore } from "../storage/docStore/types.js";
import type { VectorStore } from "../storage/vectorStore/types.js";
import { IngestionCache, getTransformationHash } from "./IngestionCache.js";
import {
DocStoreStrategy,
createDocStoreStrategy,
} from "./strategies/index.js";
import type { TransformComponent } from "./types.js";
type IngestionRunArgs = {
documents?: Document[];
nodes?: BaseNode[];
};
type TransformRunArgs = {
inPlace?: boolean;
cache?: IngestionCache;
docStoreStrategy?: TransformComponent;
};
export async function runTransformations(
nodesToRun: BaseNode[],
transformations: TransformComponent[],
transformOptions: any = {},
{ inPlace = true, cache, docStoreStrategy }: TransformRunArgs = {},
): Promise<BaseNode[]> {
let nodes = nodesToRun;
if (!inPlace) {
nodes = [...nodesToRun];
}
if (docStoreStrategy) {
nodes = await docStoreStrategy.transform(nodes);
}
for (const transform of transformations) {
if (cache) {
const hash = getTransformationHash(nodes, transform);
const cachedNodes = await cache.get(hash);
if (cachedNodes) {
nodes = cachedNodes;
} else {
nodes = await transform.transform(nodes, transformOptions);
await cache.put(hash, nodes);
}
} else {
nodes = await transform.transform(nodes, transformOptions);
}
}
return nodes;
}
export class IngestionPipeline {
transformations: TransformComponent[] = [];
documents?: Document[];
reader?: BaseReader;
vectorStore?: VectorStore;
docStore?: BaseDocumentStore;
docStoreStrategy: DocStoreStrategy = DocStoreStrategy.UPSERTS;
cache?: IngestionCache;
disableCache: boolean = false;
client?: PlatformApiClient;
clientParams?: ClientParams;
projectName: string = DEFAULT_PROJECT_NAME;
name: string = DEFAULT_PIPELINE_NAME;
private _docStoreStrategy?: TransformComponent;
constructor(init?: Partial<IngestionPipeline> & ClientParams) {
Object.assign(this, init);
this.clientParams = { apiKey: init?.apiKey, baseUrl: init?.baseUrl };
if (!this.docStore) {
this.docStoreStrategy = DocStoreStrategy.NONE;
}
this._docStoreStrategy = createDocStoreStrategy(
this.docStoreStrategy,
this.docStore,
this.vectorStore,
);
if (!this.disableCache) {
this.cache = new IngestionCache();
}
}
async prepareInput(
documents?: Document[],
nodes?: BaseNode[],
): Promise<BaseNode[]> {
const inputNodes: BaseNode[][] = [];
if (documents) {
inputNodes.push(documents);
}
if (nodes) {
inputNodes.push(nodes);
}
if (this.documents) {
inputNodes.push(this.documents);
}
if (this.reader) {
inputNodes.push(await this.reader.loadData());
}
return inputNodes.flat();
}
async run(
args: IngestionRunArgs & TransformRunArgs = {},
transformOptions?: any,
): Promise<BaseNode[]> {
args.cache = args.cache ?? this.cache;
args.docStoreStrategy = args.docStoreStrategy ?? this._docStoreStrategy;
const inputNodes = await this.prepareInput(args.documents, args.nodes);
const nodes = await runTransformations(
inputNodes,
this.transformations,
transformOptions,
args,
);
if (this.vectorStore) {
const nodesToAdd = nodes.filter((node) => node.embedding);
await this.vectorStore.add(nodesToAdd);
}
return nodes;
}
private async getClient(): Promise<PlatformApiClient> {
if (!this.client) {
this.client = await getClient(this.clientParams);
}
return this.client;
}
async register(params: {
documents?: Document[];
nodes?: BaseNode[];
verbose?: boolean;
}): Promise<string> {
const client = await this.getClient();
const inputNodes = await this.prepareInput(params.documents, params.nodes);
const project = await client.project.upsertProject({
name: this.projectName,
});
if (!project.id) {
throw new Error("Project ID should be defined");
}
// upload
const pipeline = await client.project.upsertPipelineForProject(
project.id,
await getPipelineCreate({
pipelineName: this.name,
pipelineType: "PLAYGROUND",
transformations: this.transformations,
inputNodes,
}),
);
if (!pipeline.id) {
throw new Error("Pipeline ID must be defined");
}
// Print playground URL if not running remote
if (params.verbose) {
console.log(
`Pipeline available at: ${getAppBaseUrl(this.clientParams?.baseUrl)}/project/${project.id}/playground/${pipeline.id}`,
);
}
return pipeline.id;
}
}