Skip to content

feat(cloud): composite retriever #1747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
},
"default": "./reader/dist/index.js"
},
"./retriever": {
"import": {
"types": "./reader/dist/index.d.ts",
"default": "./reader/dist/index.js"
},
"default": "./reader/dist/index.js"
},
".": {
"require": {
"types": "./reader/dist/index.d.cts",
Expand Down
22 changes: 22 additions & 0 deletions packages/cloud/src/api.ts
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
import { getEnv } from "@llamaindex/env";
import { client } from "./client";

export * from "./client";

export type ClientOptions = {
baseUrl?: string;
apiKey?: string;
};

export function initClient(options: ClientOptions) {
const baseUrl = options.baseUrl ?? "https://api.cloud.llamaindex.ai";
const apiKey = options.apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
if (!apiKey) {
throw new Error("Cannot find LlamaCloud API key");
}
client.setConfig({
baseUrl,
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
}
68 changes: 68 additions & 0 deletions packages/cloud/src/retriever.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { QueryType } from "@llamaindex/core/query-engine";
import type { Retriever } from "@llamaindex/core/retriever";
import { Document, type NodeWithScore } from "@llamaindex/core/schema";
import { extractText } from "@llamaindex/core/utils";
import {
listRetrieversApiV1RetrieversGet,
retrieveApiV1RetrieversRetrieverIdRetrievePost,
type CompositeRetrievalParams,
} from "./api";

type RetrieversParams =
| {
id: string;
}
| {
name: string;
};

export function compositeRetriever(params: RetrieversParams) {
return {
retrieve: async (
query: QueryType,
retrievalParams?: Omit<CompositeRetrievalParams, "query">,
): Promise<NodeWithScore[]> => {
let retrieverId: string | null = null;
if ("name" in params) {
const result = await listRetrieversApiV1RetrieversGet({
query: {
name: params.name,
},
throwOnError: true,
});
const retriever = result.data.find(
(retriever) => retriever.name === params.name,
);
if (retriever) {
retrieverId = retriever.id;
}
} else {
retrieverId = params.id;
}
if (!retrieverId) {
throw new Error("Retriever not found");
}
const result = await retrieveApiV1RetrieversRetrieverIdRetrievePost({
path: {
retriever_id: retrieverId,
},
body: {
...retrievalParams,
query: extractText(query),
},
throwOnError: true,
});
return (
result.data.nodes?.map(({ node, score }) => ({
node: new Document({
text: node.text,
metadata: node.metadata,
startCharIdx: node.start_char_idx ?? undefined,
endCharIdx: node.end_char_idx ?? undefined,
}),
score: score ?? undefined,
})) ?? []
);
},
} satisfies Retriever;
}
4 changes: 4 additions & 0 deletions packages/core/src/retriever/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type RetrieveEndEvent = {
nodes: NodeWithScore[];
};

export interface Retriever {
retrieve: (params: QueryType) => Promise<NodeWithScore[]>;
}

export abstract class BaseRetriever extends PromptMixin {
objectMap: Map<string, unknown> = new Map();

Expand Down
Loading