mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-11 00:04:07 -04:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f8a1da7bda | |||
| 3296aedf36 | |||
| cd0cbe8f95 | |||
| 79437ea030 | |||
| 2fd8db407b | |||
| cb57ce837a | |||
| 825ca1bf74 | |||
| 4126582c4d | |||
| 1333d8e1d2 | |||
| 0c2d48f320 | |||
| b475a8c312 | |||
| dac7893d1c | |||
| d11c42a629 | |||
| 6dcd08cc81 | |||
| 79503bcf06 | |||
| da33143e02 | |||
| 8086b9fddc | |||
| aa46b5bbd3 | |||
| bb6169227d | |||
| 529347cb30 | |||
| bfcde39af8 | |||
| 304ab577bd | |||
| 026a3fe732 |
@@ -16,28 +16,25 @@ async function main() {
|
||||
|
||||
const index = await LlamaCloudIndex.fromDocuments({
|
||||
documents: [document],
|
||||
name: "test",
|
||||
projectName: "default",
|
||||
name: "test-pipeline",
|
||||
projectName: "Default",
|
||||
apiKey: process.env.LLAMA_CLOUD_API_KEY,
|
||||
baseUrl: process.env.LLAMA_CLOUD_BASE_URL,
|
||||
});
|
||||
|
||||
const queryEngine = index.asQueryEngine({
|
||||
denseSimilarityTopK: 5,
|
||||
similarityTopK: 5,
|
||||
});
|
||||
|
||||
const rl = readline.createInterface({ input, output });
|
||||
|
||||
while (true) {
|
||||
const query = await rl.question("Query: ");
|
||||
const stream = await queryEngine.query({
|
||||
const response = await queryEngine.query({
|
||||
query,
|
||||
stream: true,
|
||||
});
|
||||
console.log();
|
||||
for await (const chunk of stream) {
|
||||
process.stdout.write(chunk.response);
|
||||
}
|
||||
|
||||
console.log(response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import fs from "node:fs/promises";
|
||||
|
||||
import {
|
||||
Document,
|
||||
IngestionPipeline,
|
||||
OpenAIEmbedding,
|
||||
SimpleNodeParser,
|
||||
} from "llamaindex";
|
||||
|
||||
async function main() {
|
||||
// Load essay from abramov.txt in Node
|
||||
const path = "node_modules/llamaindex/examples/abramov.txt";
|
||||
|
||||
const essay = await fs.readFile(path, "utf-8");
|
||||
|
||||
// Create Document object with essay
|
||||
const document = new Document({ text: essay, id_: path });
|
||||
const pipeline = new IngestionPipeline({
|
||||
name: "pipeline",
|
||||
transformations: [
|
||||
new SimpleNodeParser({ chunkSize: 1024, chunkOverlap: 20 }),
|
||||
new OpenAIEmbedding({ apiKey: "api-key" }),
|
||||
],
|
||||
});
|
||||
|
||||
const pipelineId = await pipeline.register({
|
||||
documents: [document],
|
||||
verbose: true,
|
||||
});
|
||||
|
||||
console.log(`Pipeline with id ${pipelineId} successfully created.`);
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
@@ -6,25 +6,24 @@ import { LlamaCloudIndex } from "llamaindex";
|
||||
async function main() {
|
||||
const index = new LlamaCloudIndex({
|
||||
name: "test",
|
||||
projectName: "default",
|
||||
projectName: "Default",
|
||||
baseUrl: process.env.LLAMA_CLOUD_BASE_URL,
|
||||
apiKey: process.env.LLAMA_CLOUD_API_KEY,
|
||||
});
|
||||
|
||||
const queryEngine = index.asQueryEngine({
|
||||
denseSimilarityTopK: 5,
|
||||
similarityTopK: 5,
|
||||
});
|
||||
|
||||
const rl = readline.createInterface({ input, output });
|
||||
|
||||
while (true) {
|
||||
const query = await rl.question("Query: ");
|
||||
const stream = await queryEngine.query({
|
||||
const response = await queryEngine.query({
|
||||
query,
|
||||
stream: true,
|
||||
});
|
||||
console.log();
|
||||
for await (const chunk of stream) {
|
||||
process.stdout.write(chunk.response);
|
||||
}
|
||||
|
||||
console.log(response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
"@google/generative-ai": "^0.12.0",
|
||||
"@grpc/grpc-js": "^1.10.8",
|
||||
"@huggingface/inference": "^2.7.0",
|
||||
"@llamaindex/cloud": "0.0.5",
|
||||
"@llamaindex/cloud": "0.0.7",
|
||||
"@llamaindex/env": "workspace:*",
|
||||
"@mistralai/mistralai": "^0.4.0",
|
||||
"@pinecone-database/pinecone": "^2.2.2",
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { PlatformApi } from "@llamaindex/cloud";
|
||||
import type { Document } from "../Node.js";
|
||||
import type { BaseRetriever } from "../Retriever.js";
|
||||
import { RetrieverQueryEngine } from "../engines/query/RetrieverQueryEngine.js";
|
||||
@@ -23,6 +22,137 @@ export class LlamaCloudIndex {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
private async waitForPipelineIngestion(
|
||||
verbose = false,
|
||||
raiseOnError = false,
|
||||
): Promise<void> {
|
||||
const pipelineId = await this.getPipelineId(
|
||||
this.params.name,
|
||||
this.params.projectName,
|
||||
);
|
||||
|
||||
const client = await getClient({
|
||||
...this.params,
|
||||
baseUrl: this.params.baseUrl,
|
||||
});
|
||||
|
||||
if (verbose) {
|
||||
console.log("Waiting for pipeline ingestion: ");
|
||||
}
|
||||
|
||||
while (true) {
|
||||
const pipelineStatus =
|
||||
await client.pipelines.getPipelineStatus(pipelineId);
|
||||
|
||||
if (pipelineStatus.status === "SUCCESS") {
|
||||
if (verbose) {
|
||||
console.log("Pipeline ingestion completed successfully");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (pipelineStatus.status === "ERROR") {
|
||||
if (verbose) {
|
||||
console.error("Pipeline ingestion failed");
|
||||
}
|
||||
|
||||
if (raiseOnError) {
|
||||
throw new Error("Pipeline ingestion failed");
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
process.stdout.write(".");
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
|
||||
private async waitForDocumentIngestion(
|
||||
docIds: string[],
|
||||
verbose = false,
|
||||
raiseOnError = false,
|
||||
): Promise<void> {
|
||||
const pipelineId = await this.getPipelineId(
|
||||
this.params.name,
|
||||
this.params.projectName,
|
||||
);
|
||||
|
||||
const client = await getClient({
|
||||
...this.params,
|
||||
baseUrl: this.params.baseUrl,
|
||||
});
|
||||
|
||||
if (verbose) {
|
||||
console.log("Loading data: ");
|
||||
}
|
||||
|
||||
const pendingDocs = new Set(docIds);
|
||||
|
||||
while (pendingDocs.size) {
|
||||
const docsToRemove = new Set<string>();
|
||||
|
||||
for (const doc of pendingDocs) {
|
||||
const { status } = await client.pipelines.getPipelineDocumentStatus(
|
||||
pipelineId,
|
||||
doc,
|
||||
);
|
||||
|
||||
if (status === "NOT_STARTED" || status === "IN_PROGRESS") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (status === "ERROR") {
|
||||
if (verbose) {
|
||||
console.error(`Document ingestion failed for ${doc}`);
|
||||
}
|
||||
|
||||
if (raiseOnError) {
|
||||
throw new Error(`Document ingestion failed for ${doc}`);
|
||||
}
|
||||
}
|
||||
|
||||
docsToRemove.add(doc);
|
||||
}
|
||||
|
||||
for (const doc of docsToRemove) {
|
||||
pendingDocs.delete(doc);
|
||||
}
|
||||
|
||||
if (pendingDocs.size) {
|
||||
if (verbose) {
|
||||
process.stdout.write(".");
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
console.log("Done!");
|
||||
}
|
||||
|
||||
await this.waitForPipelineIngestion(verbose, raiseOnError);
|
||||
}
|
||||
|
||||
private async getPipelineId(
|
||||
name: string,
|
||||
projectName: string,
|
||||
): Promise<string> {
|
||||
const client = await getClient({
|
||||
...this.params,
|
||||
baseUrl: this.params.baseUrl,
|
||||
});
|
||||
|
||||
const pipelines = await client.pipelines.searchPipelines({
|
||||
projectName,
|
||||
pipelineName: name,
|
||||
});
|
||||
|
||||
return pipelines[0].id;
|
||||
}
|
||||
|
||||
static async fromDocuments(
|
||||
params: {
|
||||
documents: Document[];
|
||||
@@ -31,10 +161,10 @@ export class LlamaCloudIndex {
|
||||
} & CloudConstructorParams,
|
||||
): Promise<LlamaCloudIndex> {
|
||||
const defaultTransformations: TransformComponent[] = [
|
||||
new SimpleNodeParser(),
|
||||
new OpenAIEmbedding({
|
||||
apiKey: getEnv("OPENAI_API_KEY"),
|
||||
}),
|
||||
new SimpleNodeParser(),
|
||||
];
|
||||
|
||||
const appUrl = getAppBaseUrl(params.baseUrl);
|
||||
@@ -48,7 +178,7 @@ export class LlamaCloudIndex {
|
||||
transformations: params.transformations ?? defaultTransformations,
|
||||
});
|
||||
|
||||
const project = await client.project.upsertProject({
|
||||
const project = await client.projects.upsertProject({
|
||||
name: params.projectName ?? "default",
|
||||
});
|
||||
|
||||
@@ -56,10 +186,15 @@ export class LlamaCloudIndex {
|
||||
throw new Error("Project ID should be defined");
|
||||
}
|
||||
|
||||
const pipeline = await client.project.upsertPipelineForProject(
|
||||
project.id,
|
||||
pipelineCreateParams,
|
||||
);
|
||||
const pipeline = await client.pipelines.upsertPipeline({
|
||||
projectId: project.id,
|
||||
body: {
|
||||
name: params.name,
|
||||
configuredTransformations:
|
||||
pipelineCreateParams.configuredTransformations,
|
||||
pipelineType: pipelineCreateParams.pipelineType,
|
||||
},
|
||||
});
|
||||
|
||||
if (!pipeline.id) {
|
||||
throw new Error("Pipeline ID must be defined");
|
||||
@@ -69,94 +204,48 @@ export class LlamaCloudIndex {
|
||||
console.log(`Created pipeline ${pipeline.id} with name ${params.name}`);
|
||||
}
|
||||
|
||||
const executionsIds: {
|
||||
exectionId: string;
|
||||
dataSourceId: string;
|
||||
}[] = [];
|
||||
|
||||
for (const dataSource of pipeline.dataSources) {
|
||||
const dataSourceExection =
|
||||
await client.dataSource.createDataSourceExecution(dataSource.id);
|
||||
|
||||
if (!dataSourceExection.id) {
|
||||
throw new Error("Data Source Execution ID must be defined");
|
||||
}
|
||||
|
||||
executionsIds.push({
|
||||
exectionId: dataSourceExection.id,
|
||||
dataSourceId: dataSource.id,
|
||||
});
|
||||
}
|
||||
|
||||
let isDone = false;
|
||||
|
||||
while (!isDone) {
|
||||
const statuses = [];
|
||||
|
||||
for await (const execution of executionsIds) {
|
||||
const dataSourceExecution =
|
||||
await client.dataSource.getDataSourceExecution(
|
||||
execution.dataSourceId,
|
||||
execution.exectionId,
|
||||
);
|
||||
|
||||
statuses.push(dataSourceExecution.status);
|
||||
|
||||
if (
|
||||
statuses.every((status) => status === PlatformApi.StatusEnum.Success)
|
||||
) {
|
||||
isDone = true;
|
||||
if (params.verbose) {
|
||||
console.info("Data Source Execution completed");
|
||||
}
|
||||
break;
|
||||
} else if (
|
||||
statuses.some((status) => status === PlatformApi.StatusEnum.Error)
|
||||
) {
|
||||
throw new Error("Data Source Execution failed");
|
||||
} else {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
if (params.verbose) {
|
||||
process.stdout.write(".");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isDone = false;
|
||||
|
||||
const execution = await client.pipeline.runManagedPipelineIngestion(
|
||||
await client.pipelines.upsertBatchPipelineDocuments(
|
||||
pipeline.id,
|
||||
params.documents.map((doc) => ({
|
||||
metadata: doc.metadata,
|
||||
text: doc.text,
|
||||
excludedEmbedMetadataKeys: doc.excludedLlmMetadataKeys,
|
||||
excludedLlmMetadataKeys: doc.excludedEmbedMetadataKeys,
|
||||
id: doc.id_,
|
||||
})),
|
||||
);
|
||||
|
||||
const ingestionId = execution.id;
|
||||
|
||||
if (!ingestionId) {
|
||||
throw new Error("Ingestion ID must be defined");
|
||||
}
|
||||
|
||||
while (!isDone) {
|
||||
const pipelineStatus = await client.pipeline.getManagedIngestionExecution(
|
||||
while (true) {
|
||||
const pipelineStatus = await client.pipelines.getPipelineStatus(
|
||||
pipeline.id,
|
||||
ingestionId,
|
||||
);
|
||||
|
||||
if (pipelineStatus.status === PlatformApi.StatusEnum.Success) {
|
||||
isDone = true;
|
||||
|
||||
if (params.verbose) {
|
||||
console.info("Ingestion completed");
|
||||
}
|
||||
|
||||
if (pipelineStatus.status === "SUCCESS") {
|
||||
console.info(
|
||||
"Documents ingested successfully, pipeline is ready to use",
|
||||
);
|
||||
break;
|
||||
} else if (pipelineStatus.status === PlatformApi.StatusEnum.Error) {
|
||||
throw new Error("Ingestion failed");
|
||||
} else {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
if (params.verbose) {
|
||||
process.stdout.write(".");
|
||||
}
|
||||
}
|
||||
|
||||
if (pipelineStatus.status === "ERROR") {
|
||||
console.error(
|
||||
`Some documents failed to ingest, check your pipeline logs at ${appUrl}/project/${project.id}/deploy/${pipeline.id}`,
|
||||
);
|
||||
throw new Error("Some documents failed to ingest");
|
||||
}
|
||||
|
||||
if (pipelineStatus.status === "PARTIAL_SUCCESS") {
|
||||
console.info(
|
||||
`Documents ingestion partially succeeded, to check a more complete status check your pipeline at ${appUrl}/project/${project.id}/deploy/${pipeline.id}`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
if (params.verbose) {
|
||||
process.stdout.write(".");
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
|
||||
if (params.verbose) {
|
||||
@@ -190,4 +279,77 @@ export class LlamaCloudIndex {
|
||||
params?.nodePostprocessors,
|
||||
);
|
||||
}
|
||||
|
||||
async insert(document: Document) {
|
||||
const appUrl = getAppBaseUrl(this.params.baseUrl);
|
||||
|
||||
const client = await getClient({ ...this.params, baseUrl: appUrl });
|
||||
|
||||
const pipelineId = await this.getPipelineId(
|
||||
this.params.name,
|
||||
this.params.projectName,
|
||||
);
|
||||
|
||||
if (!pipelineId) {
|
||||
throw new Error("We couldn't find the pipeline ID for the given name");
|
||||
}
|
||||
|
||||
await client.pipelines.createBatchPipelineDocuments(pipelineId, [
|
||||
{
|
||||
metadata: document.metadata,
|
||||
text: document.text,
|
||||
excludedEmbedMetadataKeys: document.excludedLlmMetadataKeys,
|
||||
excludedLlmMetadataKeys: document.excludedEmbedMetadataKeys,
|
||||
id: document.id_,
|
||||
},
|
||||
]);
|
||||
|
||||
await this.waitForDocumentIngestion([document.id_]);
|
||||
}
|
||||
|
||||
async delete(document: Document) {
|
||||
const appUrl = getAppBaseUrl(this.params.baseUrl);
|
||||
|
||||
const client = await getClient({ ...this.params, baseUrl: appUrl });
|
||||
|
||||
const pipelineId = await this.getPipelineId(
|
||||
this.params.name,
|
||||
this.params.projectName,
|
||||
);
|
||||
|
||||
if (!pipelineId) {
|
||||
throw new Error("We couldn't find the pipeline ID for the given name");
|
||||
}
|
||||
|
||||
await client.pipelines.deletePipelineDocument(pipelineId, document.id_);
|
||||
|
||||
await this.waitForPipelineIngestion();
|
||||
}
|
||||
|
||||
async refreshDoc(document: Document) {
|
||||
const appUrl = getAppBaseUrl(this.params.baseUrl);
|
||||
|
||||
const client = await getClient({ ...this.params, baseUrl: appUrl });
|
||||
|
||||
const pipelineId = await this.getPipelineId(
|
||||
this.params.name,
|
||||
this.params.projectName,
|
||||
);
|
||||
|
||||
if (!pipelineId) {
|
||||
throw new Error("We couldn't find the pipeline ID for the given name");
|
||||
}
|
||||
|
||||
await client.pipelines.upsertBatchPipelineDocuments(pipelineId, [
|
||||
{
|
||||
metadata: document.metadata,
|
||||
text: document.text,
|
||||
excludedEmbedMetadataKeys: document.excludedLlmMetadataKeys,
|
||||
excludedLlmMetadataKeys: document.excludedEmbedMetadataKeys,
|
||||
id: document.id_,
|
||||
},
|
||||
]);
|
||||
|
||||
await this.waitForDocumentIngestion([document.id_]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { PlatformApi, PlatformApiClient } from "@llamaindex/cloud";
|
||||
import type { LlamaCloudApi, LlamaCloudApiClient } from "@llamaindex/cloud";
|
||||
import type { NodeWithScore } from "../Node.js";
|
||||
import { ObjectType, jsonToNode } from "../Node.js";
|
||||
import type { BaseRetriever, RetrieveParams } from "../Retriever.js";
|
||||
@@ -8,22 +8,23 @@ import { extractText } from "../llm/utils.js";
|
||||
import type { ClientParams, CloudConstructorParams } from "./types.js";
|
||||
import { DEFAULT_PROJECT_NAME } from "./types.js";
|
||||
import { getClient } from "./utils.js";
|
||||
|
||||
export type CloudRetrieveParams = Omit<
|
||||
PlatformApi.RetrievalParams,
|
||||
"query" | "searchFilters" | "pipelineId" | "className"
|
||||
LlamaCloudApi.RetrievalParams,
|
||||
"query" | "searchFilters" | "className" | "denseSimilarityTopK"
|
||||
> & { similarityTopK?: number };
|
||||
|
||||
export class LlamaCloudRetriever implements BaseRetriever {
|
||||
client?: PlatformApiClient;
|
||||
client?: LlamaCloudApiClient;
|
||||
clientParams: ClientParams;
|
||||
retrieveParams: CloudRetrieveParams;
|
||||
projectName: string = DEFAULT_PROJECT_NAME;
|
||||
pipelineName: string;
|
||||
|
||||
private resultNodesToNodeWithScore(
|
||||
nodes: PlatformApi.TextNodeWithScore[],
|
||||
nodes: LlamaCloudApi.TextNodeWithScore[],
|
||||
): NodeWithScore[] {
|
||||
return nodes.map((node: PlatformApi.TextNodeWithScore) => {
|
||||
return nodes.map((node: LlamaCloudApi.TextNodeWithScore) => {
|
||||
return {
|
||||
// Currently LlamaCloud only supports text nodes
|
||||
node: jsonToNode(node.node, ObjectType.TEXT),
|
||||
@@ -34,9 +35,6 @@ export class LlamaCloudRetriever implements BaseRetriever {
|
||||
|
||||
constructor(params: CloudConstructorParams & CloudRetrieveParams) {
|
||||
this.clientParams = { apiKey: params.apiKey, baseUrl: params.baseUrl };
|
||||
if (params.similarityTopK) {
|
||||
params.denseSimilarityTopK = params.similarityTopK;
|
||||
}
|
||||
this.retrieveParams = params;
|
||||
this.pipelineName = params.name;
|
||||
if (params.projectName) {
|
||||
@@ -44,7 +42,7 @@ export class LlamaCloudRetriever implements BaseRetriever {
|
||||
}
|
||||
}
|
||||
|
||||
private async getClient(): Promise<PlatformApiClient> {
|
||||
private async getClient(): Promise<LlamaCloudApiClient> {
|
||||
if (!this.client) {
|
||||
this.client = await getClient(this.clientParams);
|
||||
}
|
||||
@@ -56,23 +54,31 @@ export class LlamaCloudRetriever implements BaseRetriever {
|
||||
query,
|
||||
preFilters,
|
||||
}: RetrieveParams): Promise<NodeWithScore[]> {
|
||||
const pipelines = await (
|
||||
await this.getClient()
|
||||
).pipeline.searchPipelines({
|
||||
const client = await this.getClient();
|
||||
|
||||
const pipelines = await client?.pipelines.searchPipelines({
|
||||
projectName: this.projectName,
|
||||
pipelineName: this.pipelineName,
|
||||
});
|
||||
if (pipelines.length !== 1 && !pipelines[0]?.id) {
|
||||
|
||||
if (!pipelines) {
|
||||
throw new Error(
|
||||
`No pipeline found with name ${this.pipelineName} in project ${this.projectName}`,
|
||||
);
|
||||
}
|
||||
const results = await (
|
||||
await this.getClient()
|
||||
).pipeline.runSearch(pipelines[0].id, {
|
||||
|
||||
const pipeline = await client?.pipelines.getPipeline(pipelines[0].id);
|
||||
|
||||
if (!pipeline) {
|
||||
throw new Error(
|
||||
`No pipeline found with name ${this.pipelineName} in project ${this.projectName}`,
|
||||
);
|
||||
}
|
||||
|
||||
const results = await client?.pipelines.runSearch(pipeline.id, {
|
||||
...this.retrieveParams,
|
||||
query: extractText(query),
|
||||
searchFilters: preFilters as Record<string, unknown[]>,
|
||||
searchFilters: preFilters as LlamaCloudApi.MetadataFilters,
|
||||
});
|
||||
|
||||
const nodes = this.resultNodesToNodeWithScore(results.retrievalNodes);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { PlatformApi } from "@llamaindex/cloud";
|
||||
import { BaseNode, Document } from "../Node.js";
|
||||
import { LlamaCloudApi } from "@llamaindex/cloud";
|
||||
import { BaseNode } from "../Node.js";
|
||||
import { OpenAIEmbedding } from "../embeddings/OpenAIEmbedding.js";
|
||||
import type { TransformComponent } from "../ingestion/types.js";
|
||||
import { SimpleNodeParser } from "../nodeParsers/SimpleNodeParser.js";
|
||||
@@ -13,7 +13,7 @@ export type GetPipelineCreateParams = {
|
||||
|
||||
function getTransformationConfig(
|
||||
transformation: TransformComponent,
|
||||
): PlatformApi.ConfiguredTransformationItem {
|
||||
): LlamaCloudApi.ConfiguredTransformationItem {
|
||||
if (transformation instanceof SimpleNodeParser) {
|
||||
return {
|
||||
configurableTransformationType: "SENTENCE_AWARE_NODE_PARSER",
|
||||
@@ -41,44 +41,14 @@ function getTransformationConfig(
|
||||
throw new Error(`Unsupported transformation: ${typeof transformation}`);
|
||||
}
|
||||
|
||||
function getDataSourceConfig(node: BaseNode): PlatformApi.DataSourceCreate {
|
||||
if (node instanceof Document) {
|
||||
return {
|
||||
name: node.id_,
|
||||
sourceType: "DOCUMENT",
|
||||
component: {
|
||||
id: node.id_,
|
||||
text: node.text,
|
||||
textTemplate: node.textTemplate,
|
||||
startCharIdx: node.startCharIdx,
|
||||
endCharIdx: node.endCharIdx,
|
||||
metadataSeparator: node.metadataSeparator,
|
||||
excludedEmbedMetadataKeys: node.excludedEmbedMetadataKeys,
|
||||
excludedLlmMetadataKeys: node.excludedLlmMetadataKeys,
|
||||
extraInfo: node.metadata,
|
||||
},
|
||||
};
|
||||
}
|
||||
throw new Error(`Unsupported node: ${typeof node}`);
|
||||
}
|
||||
|
||||
export async function getPipelineCreate(
|
||||
params: GetPipelineCreateParams,
|
||||
): Promise<PlatformApi.PipelineCreate> {
|
||||
const {
|
||||
pipelineName,
|
||||
pipelineType,
|
||||
transformations = [],
|
||||
inputNodes = [],
|
||||
} = params;
|
||||
|
||||
const dataSources = inputNodes.map(getDataSourceConfig);
|
||||
): Promise<LlamaCloudApi.PipelineCreate> {
|
||||
const { pipelineName, pipelineType, transformations = [] } = params;
|
||||
|
||||
return {
|
||||
name: pipelineName,
|
||||
configuredTransformations: transformations.map(getTransformationConfig),
|
||||
dataSources,
|
||||
dataSinks: [],
|
||||
pipelineType,
|
||||
pipelineType: pipelineType,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
export * from "./LlamaCloudIndex.js";
|
||||
export * from "./LlamaCloudRetriever.js";
|
||||
export { LlamaCloudIndex } from "./LlamaCloudIndex.js";
|
||||
export {
|
||||
LlamaCloudRetriever,
|
||||
type CloudRetrieveParams,
|
||||
} from "./LlamaCloudRetriever.js";
|
||||
export type { CloudConstructorParams } from "./types.js";
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import type { ServiceContext } from "../ServiceContext.js";
|
||||
|
||||
export const DEFAULT_PIPELINE_NAME = "default";
|
||||
export const DEFAULT_PROJECT_NAME = "default";
|
||||
export const DEFAULT_PROJECT_NAME = "Default";
|
||||
export const DEFAULT_BASE_URL = "https://api.cloud.llamaindex.ai";
|
||||
|
||||
export type ClientParams = { apiKey?: string; baseUrl?: string };
|
||||
|
||||
export type CloudConstructorParams = {
|
||||
name: string;
|
||||
projectName?: string;
|
||||
projectName: string;
|
||||
serviceContext?: ServiceContext;
|
||||
} & ClientParams;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { PlatformApiClient } from "@llamaindex/cloud";
|
||||
import type { LlamaCloudApiClient } from "@llamaindex/cloud";
|
||||
import { getEnv } from "@llamaindex/env";
|
||||
import type { ClientParams } from "./types.js";
|
||||
import { DEFAULT_BASE_URL } from "./types.js";
|
||||
@@ -14,15 +14,23 @@ export function getAppBaseUrl(baseUrl?: string): string {
|
||||
export async function getClient({
|
||||
apiKey,
|
||||
baseUrl,
|
||||
}: ClientParams = {}): Promise<PlatformApiClient> {
|
||||
}: ClientParams = {}): Promise<LlamaCloudApiClient> {
|
||||
const { LlamaCloudApiClient } = await import("@llamaindex/cloud");
|
||||
|
||||
// Get the environment variables or use defaults
|
||||
baseUrl = getBaseUrl(baseUrl);
|
||||
apiKey = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
|
||||
|
||||
const { PlatformApiClient } = await import("@llamaindex/cloud");
|
||||
if (!apiKey) {
|
||||
throw new Error(
|
||||
"API Key is required for LlamaCloudIndex. Please pass the apiKey parameter",
|
||||
);
|
||||
}
|
||||
|
||||
return new PlatformApiClient({
|
||||
const client = new LlamaCloudApiClient({
|
||||
token: apiKey,
|
||||
environment: baseUrl,
|
||||
});
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
export * from "./agent/index.js";
|
||||
export * from "./callbacks/CallbackManager.js";
|
||||
export * from "./ChatHistory.js";
|
||||
export * from "./cloud/index.js";
|
||||
export * from "./constants.js";
|
||||
export * from "./embeddings/index.js";
|
||||
export * from "./EngineResponse.js";
|
||||
|
||||
@@ -13,3 +13,5 @@ export {
|
||||
|
||||
export { type VertexGeminiSessionOptions } from "./llm/gemini/types.js";
|
||||
export { GeminiVertexSession } from "./llm/gemini/vertex.js";
|
||||
// Fern only supports node.js runtime
|
||||
export * from "./cloud/index.js";
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { PlatformApiClient } from "@llamaindex/cloud";
|
||||
import {
|
||||
ModalityType,
|
||||
splitNodesByType,
|
||||
@@ -6,13 +5,6 @@ import {
|
||||
type Document,
|
||||
type Metadata,
|
||||
} 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 {
|
||||
@@ -77,16 +69,11 @@ export class IngestionPipeline {
|
||||
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) {
|
||||
constructor(init?: Partial<IngestionPipeline>) {
|
||||
Object.assign(this, init);
|
||||
this.clientParams = { apiKey: init?.apiKey, baseUrl: init?.baseUrl };
|
||||
if (!this.docStore) {
|
||||
this.docStoreStrategy = DocStoreStrategy.NONE;
|
||||
}
|
||||
@@ -142,52 +129,6 @@ export class IngestionPipeline {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
export async function addNodesToVectorStores(
|
||||
|
||||
Generated
+954
-526
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user