-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
retriever.ts
28 lines (27 loc) · 918 Bytes
/
retriever.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
import type { BaseRetrieverInterface } from "@langchain/core/retrievers";
import { z } from "zod";
import { CallbackManagerForToolRun } from "@langchain/core/callbacks/manager";
import {
DynamicStructuredTool,
type DynamicStructuredToolInput,
} from "@langchain/core/tools";
import { formatDocumentsAsString } from "../util/document.js";
export function createRetrieverTool(
retriever: BaseRetrieverInterface,
input: Omit<DynamicStructuredToolInput, "func" | "schema">
) {
const func = async (
{ query }: { query: string },
runManager?: CallbackManagerForToolRun
) => {
const docs = await retriever.getRelevantDocuments(
query,
runManager?.getChild("retriever")
);
return formatDocumentsAsString(docs);
};
const schema = z.object({
query: z.string().describe("query to look up in retriever"),
});
return new DynamicStructuredTool({ ...input, func, schema });
}