langchain 0.1.35 → 0.1.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chains/openai_functions/base.cjs +2 -0
- package/dist/chains/openai_functions/base.d.ts +2 -0
- package/dist/chains/openai_functions/base.js +2 -0
- package/dist/chains/query_constructor/index.cjs +5 -8
- package/dist/chains/query_constructor/index.d.ts +5 -4
- package/dist/chains/query_constructor/index.js +3 -6
- package/dist/chains/query_constructor/ir.cjs +15 -139
- package/dist/chains/query_constructor/ir.d.ts +1 -138
- package/dist/chains/query_constructor/ir.js +1 -132
- package/dist/chains/query_constructor/prompt.cjs +2 -2
- package/dist/chains/query_constructor/prompt.d.ts +1 -1
- package/dist/chains/query_constructor/prompt.js +1 -1
- package/dist/document_loaders/web/firecrawl.cjs +88 -0
- package/dist/document_loaders/web/firecrawl.d.ts +48 -0
- package/dist/document_loaders/web/firecrawl.js +81 -0
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/output_parsers/expression.cjs +1 -1
- package/dist/output_parsers/expression.d.ts +1 -1
- package/dist/output_parsers/expression.js +1 -1
- package/dist/retrievers/self_query/base.cjs +3 -136
- package/dist/retrievers/self_query/base.d.ts +1 -69
- package/dist/retrievers/self_query/base.js +1 -134
- package/dist/retrievers/self_query/chroma.cjs +9 -10
- package/dist/retrievers/self_query/chroma.d.ts +1 -1
- package/dist/retrievers/self_query/chroma.js +1 -2
- package/dist/retrievers/self_query/functional.cjs +2 -195
- package/dist/retrievers/self_query/functional.d.ts +1 -87
- package/dist/retrievers/self_query/functional.js +1 -194
- package/dist/retrievers/self_query/index.cjs +9 -13
- package/dist/retrievers/self_query/index.d.ts +11 -8
- package/dist/retrievers/self_query/index.js +7 -11
- package/dist/retrievers/self_query/pinecone.cjs +9 -10
- package/dist/retrievers/self_query/pinecone.d.ts +1 -1
- package/dist/retrievers/self_query/pinecone.js +1 -2
- package/dist/retrievers/self_query/supabase.cjs +28 -30
- package/dist/retrievers/self_query/supabase.d.ts +1 -2
- package/dist/retrievers/self_query/supabase.js +1 -3
- package/dist/retrievers/self_query/supabase_utils.cjs +2 -2
- package/dist/retrievers/self_query/supabase_utils.d.ts +1 -1
- package/dist/retrievers/self_query/supabase_utils.js +1 -1
- package/dist/retrievers/self_query/vectara.cjs +15 -17
- package/dist/retrievers/self_query/vectara.d.ts +1 -2
- package/dist/retrievers/self_query/vectara.js +1 -3
- package/dist/retrievers/self_query/weaviate.cjs +19 -21
- package/dist/retrievers/self_query/weaviate.d.ts +1 -2
- package/dist/retrievers/self_query/weaviate.js +1 -3
- package/dist/storage/in_memory.cjs +2 -81
- package/dist/storage/in_memory.d.ts +1 -49
- package/dist/storage/in_memory.js +1 -80
- package/dist/text_splitter.cjs +15 -727
- package/dist/text_splitter.d.ts +1 -77
- package/dist/text_splitter.js +1 -720
- package/document_loaders/web/firecrawl.cjs +1 -0
- package/document_loaders/web/firecrawl.d.cts +1 -0
- package/document_loaders/web/firecrawl.d.ts +1 -0
- package/document_loaders/web/firecrawl.js +1 -0
- package/package.json +22 -3
- package/dist/retrievers/self_query/utils.cjs +0 -94
- package/dist/retrievers/self_query/utils.d.ts +0 -29
- package/dist/retrievers/self_query/utils.js +0 -85
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type DocumentInterface } from "@langchain/core/documents";
|
|
2
|
+
import { BaseDocumentLoader } from "../base.js";
|
|
3
|
+
/**
|
|
4
|
+
* Interface representing the parameters for the Firecrawl loader. It
|
|
5
|
+
* includes properties such as the URL to scrape or crawl and the API key.
|
|
6
|
+
*/
|
|
7
|
+
interface FirecrawlLoaderParameters {
|
|
8
|
+
/**
|
|
9
|
+
* URL to scrape or crawl
|
|
10
|
+
*/
|
|
11
|
+
url: string;
|
|
12
|
+
/**
|
|
13
|
+
* API key for Firecrawl. If not provided, the default value is the value of the FIRECRAWL_API_KEY environment variable.
|
|
14
|
+
*/
|
|
15
|
+
apiKey?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Mode of operation. Can be either "crawl" or "scrape". If not provided, the default value is "crawl".
|
|
18
|
+
*/
|
|
19
|
+
mode?: "crawl" | "scrape";
|
|
20
|
+
params?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Class representing a document loader for loading data from
|
|
24
|
+
* Firecrawl (firecrawl.dev). It extends the BaseDocumentLoader class.
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const loader = new FireCrawlLoader({
|
|
28
|
+
* url: "{url}",
|
|
29
|
+
* apiKey: "{apiKey}",
|
|
30
|
+
* mode: "crawl"
|
|
31
|
+
* });
|
|
32
|
+
* const docs = await loader.load();
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class FireCrawlLoader extends BaseDocumentLoader {
|
|
36
|
+
private apiKey;
|
|
37
|
+
private url;
|
|
38
|
+
private mode;
|
|
39
|
+
private params?;
|
|
40
|
+
constructor(loaderParams: FirecrawlLoaderParameters);
|
|
41
|
+
/**
|
|
42
|
+
* Loads the data from the Firecrawl.
|
|
43
|
+
* @returns An array of Documents representing the retrieved data.
|
|
44
|
+
* @throws An error if the data could not be loaded.
|
|
45
|
+
*/
|
|
46
|
+
load(): Promise<DocumentInterface[]>;
|
|
47
|
+
}
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import FirecrawlApp from "@mendable/firecrawl-js";
|
|
2
|
+
import { Document } from "@langchain/core/documents";
|
|
3
|
+
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
4
|
+
import { BaseDocumentLoader } from "../base.js";
|
|
5
|
+
/**
|
|
6
|
+
* Class representing a document loader for loading data from
|
|
7
|
+
* Firecrawl (firecrawl.dev). It extends the BaseDocumentLoader class.
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const loader = new FireCrawlLoader({
|
|
11
|
+
* url: "{url}",
|
|
12
|
+
* apiKey: "{apiKey}",
|
|
13
|
+
* mode: "crawl"
|
|
14
|
+
* });
|
|
15
|
+
* const docs = await loader.load();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export class FireCrawlLoader extends BaseDocumentLoader {
|
|
19
|
+
constructor(loaderParams) {
|
|
20
|
+
super();
|
|
21
|
+
Object.defineProperty(this, "apiKey", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "url", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(this, "mode", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: void 0
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "params", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
const { apiKey = getEnvironmentVariable("FIRECRAWL_API_KEY"), url, mode = "crawl", params, } = loaderParams;
|
|
46
|
+
if (!apiKey) {
|
|
47
|
+
throw new Error("Firecrawl API key not set. You can set it as FIRECRAWL_API_KEY in your .env file, or pass it to Firecrawl.");
|
|
48
|
+
}
|
|
49
|
+
this.apiKey = apiKey;
|
|
50
|
+
this.url = url;
|
|
51
|
+
this.mode = mode;
|
|
52
|
+
this.params = params;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Loads the data from the Firecrawl.
|
|
56
|
+
* @returns An array of Documents representing the retrieved data.
|
|
57
|
+
* @throws An error if the data could not be loaded.
|
|
58
|
+
*/
|
|
59
|
+
async load() {
|
|
60
|
+
const app = new FirecrawlApp({ apiKey: this.apiKey });
|
|
61
|
+
let firecrawlDocs;
|
|
62
|
+
if (this.mode === "scrape") {
|
|
63
|
+
const response = await app.scrapeUrl(this.url, this.params);
|
|
64
|
+
if (!response.success) {
|
|
65
|
+
throw new Error(`Firecrawl: Failed to scrape URL. Error: ${response.error}`);
|
|
66
|
+
}
|
|
67
|
+
firecrawlDocs = [response.data];
|
|
68
|
+
}
|
|
69
|
+
else if (this.mode === "crawl") {
|
|
70
|
+
const response = await app.crawlUrl(this.url, this.params, true);
|
|
71
|
+
firecrawlDocs = response;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new Error(`Unrecognized mode '${this.mode}'. Expected one of 'crawl', 'scrape'.`);
|
|
75
|
+
}
|
|
76
|
+
return firecrawlDocs.map((doc) => new Document({
|
|
77
|
+
pageContent: doc.markdown || "",
|
|
78
|
+
metadata: doc.metadata || {},
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -93,6 +93,7 @@ exports.optionalImportEntrypoints = [
|
|
|
93
93
|
"langchain/document_loaders/web/hn",
|
|
94
94
|
"langchain/document_loaders/web/imsdb",
|
|
95
95
|
"langchain/document_loaders/web/figma",
|
|
96
|
+
"langchain/document_loaders/web/firecrawl",
|
|
96
97
|
"langchain/document_loaders/web/github",
|
|
97
98
|
"langchain/document_loaders/web/notiondb",
|
|
98
99
|
"langchain/document_loaders/web/notionapi",
|
|
@@ -90,6 +90,7 @@ export const optionalImportEntrypoints = [
|
|
|
90
90
|
"langchain/document_loaders/web/hn",
|
|
91
91
|
"langchain/document_loaders/web/imsdb",
|
|
92
92
|
"langchain/document_loaders/web/figma",
|
|
93
|
+
"langchain/document_loaders/web/firecrawl",
|
|
93
94
|
"langchain/document_loaders/web/github",
|
|
94
95
|
"langchain/document_loaders/web/notiondb",
|
|
95
96
|
"langchain/document_loaders/web/notionapi",
|
|
@@ -19,7 +19,7 @@ const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
|
19
19
|
const factory_js_1 = require("./expression_type_handlers/factory.cjs");
|
|
20
20
|
const base_js_1 = require("./expression_type_handlers/base.cjs");
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* We need to be able to handle the following cases:
|
|
23
23
|
* ExpressionStatement
|
|
24
24
|
* CallExpression
|
|
25
25
|
* Identifier | MemberExpression
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseOutputParser } from "@langchain/core/output_parsers";
|
|
2
2
|
import { ParsedType } from "./expression_type_handlers/types.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* We need to be able to handle the following cases:
|
|
5
5
|
* ExpressionStatement
|
|
6
6
|
* CallExpression
|
|
7
7
|
* Identifier | MemberExpression
|
|
@@ -2,7 +2,7 @@ import { BaseOutputParser } from "@langchain/core/output_parsers";
|
|
|
2
2
|
import { MasterHandler } from "./expression_type_handlers/factory.js";
|
|
3
3
|
import { ASTParser } from "./expression_type_handlers/base.js";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* We need to be able to handle the following cases:
|
|
6
6
|
* ExpressionStatement
|
|
7
7
|
* CallExpression
|
|
8
8
|
* Identifier | MemberExpression
|
|
@@ -1,139 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BasicTranslator = exports.BaseTranslator = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* Abstract class that provides a blueprint for creating specific
|
|
8
|
-
* translator classes. Defines two abstract methods: formatFunction and
|
|
9
|
-
* mergeFilters.
|
|
10
|
-
*/
|
|
11
|
-
class BaseTranslator extends ir_js_1.Visitor {
|
|
12
|
-
}
|
|
13
|
-
exports.BaseTranslator = BaseTranslator;
|
|
14
|
-
/**
|
|
15
|
-
* Class that extends the BaseTranslator class and provides concrete
|
|
16
|
-
* implementations for the abstract methods. Also declares three types:
|
|
17
|
-
* VisitOperationOutput, VisitComparisonOutput, and
|
|
18
|
-
* VisitStructuredQueryOutput, which are used as the return types for the
|
|
19
|
-
* visitOperation, visitComparison, and visitStructuredQuery methods
|
|
20
|
-
* respectively.
|
|
21
|
-
*/
|
|
22
|
-
class BasicTranslator extends BaseTranslator {
|
|
23
|
-
constructor(opts) {
|
|
24
|
-
super();
|
|
25
|
-
Object.defineProperty(this, "allowedOperators", {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
configurable: true,
|
|
28
|
-
writable: true,
|
|
29
|
-
value: void 0
|
|
30
|
-
});
|
|
31
|
-
Object.defineProperty(this, "allowedComparators", {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
configurable: true,
|
|
34
|
-
writable: true,
|
|
35
|
-
value: void 0
|
|
36
|
-
});
|
|
37
|
-
this.allowedOperators = opts?.allowedOperators ?? [
|
|
38
|
-
ir_js_1.Operators.and,
|
|
39
|
-
ir_js_1.Operators.or,
|
|
40
|
-
];
|
|
41
|
-
this.allowedComparators = opts?.allowedComparators ?? [
|
|
42
|
-
ir_js_1.Comparators.eq,
|
|
43
|
-
ir_js_1.Comparators.ne,
|
|
44
|
-
ir_js_1.Comparators.gt,
|
|
45
|
-
ir_js_1.Comparators.gte,
|
|
46
|
-
ir_js_1.Comparators.lt,
|
|
47
|
-
ir_js_1.Comparators.lte,
|
|
48
|
-
];
|
|
49
|
-
}
|
|
50
|
-
formatFunction(func) {
|
|
51
|
-
if (func in ir_js_1.Comparators) {
|
|
52
|
-
if (this.allowedComparators.length > 0 &&
|
|
53
|
-
this.allowedComparators.indexOf(func) === -1) {
|
|
54
|
-
throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else if (func in ir_js_1.Operators) {
|
|
58
|
-
if (this.allowedOperators.length > 0 &&
|
|
59
|
-
this.allowedOperators.indexOf(func) === -1) {
|
|
60
|
-
throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
throw new Error("Unknown comparator or operator");
|
|
65
|
-
}
|
|
66
|
-
return `$${func}`;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Visits an operation and returns a result.
|
|
70
|
-
* @param operation The operation to visit.
|
|
71
|
-
* @returns The result of visiting the operation.
|
|
72
|
-
*/
|
|
73
|
-
visitOperation(operation) {
|
|
74
|
-
const args = operation.args?.map((arg) => arg.accept(this));
|
|
75
|
-
return {
|
|
76
|
-
[this.formatFunction(operation.operator)]: args,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Visits a comparison and returns a result.
|
|
81
|
-
* @param comparison The comparison to visit.
|
|
82
|
-
* @returns The result of visiting the comparison.
|
|
83
|
-
*/
|
|
84
|
-
visitComparison(comparison) {
|
|
85
|
-
return {
|
|
86
|
-
[comparison.attribute]: {
|
|
87
|
-
[this.formatFunction(comparison.comparator)]: (0, utils_js_1.castValue)(comparison.value),
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Visits a structured query and returns a result.
|
|
93
|
-
* @param query The structured query to visit.
|
|
94
|
-
* @returns The result of visiting the structured query.
|
|
95
|
-
*/
|
|
96
|
-
visitStructuredQuery(query) {
|
|
97
|
-
let nextArg = {};
|
|
98
|
-
if (query.filter) {
|
|
99
|
-
nextArg = {
|
|
100
|
-
filter: query.filter.accept(this),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
return nextArg;
|
|
104
|
-
}
|
|
105
|
-
mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
|
|
106
|
-
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) && (0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) || mergeType === "replace") {
|
|
110
|
-
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
111
|
-
return undefined;
|
|
112
|
-
}
|
|
113
|
-
return generatedFilter;
|
|
114
|
-
}
|
|
115
|
-
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
116
|
-
if (forceDefaultFilter) {
|
|
117
|
-
return defaultFilter;
|
|
118
|
-
}
|
|
119
|
-
if (mergeType === "and") {
|
|
120
|
-
return undefined;
|
|
121
|
-
}
|
|
122
|
-
return defaultFilter;
|
|
123
|
-
}
|
|
124
|
-
if (mergeType === "and") {
|
|
125
|
-
return {
|
|
126
|
-
$and: [defaultFilter, generatedFilter],
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
else if (mergeType === "or") {
|
|
130
|
-
return {
|
|
131
|
-
$or: [defaultFilter, generatedFilter],
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
throw new Error("Unknown merge type");
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
exports.BasicTranslator = BasicTranslator;
|
|
4
|
+
var structured_query_1 = require("@langchain/core/structured_query");
|
|
5
|
+
Object.defineProperty(exports, "BaseTranslator", { enumerable: true, get: function () { return structured_query_1.BaseTranslator; } });
|
|
6
|
+
Object.defineProperty(exports, "BasicTranslator", { enumerable: true, get: function () { return structured_query_1.BasicTranslator; } });
|
|
@@ -1,69 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Comparator, Comparison, Operation, Operator, StructuredQuery, Visitor, VisitorComparisonResult, VisitorOperationResult, VisitorStructuredQueryResult } from "../../chains/query_constructor/ir.js";
|
|
3
|
-
/**
|
|
4
|
-
* Options object for the BasicTranslator class. Specifies the allowed
|
|
5
|
-
* operators and comparators.
|
|
6
|
-
*/
|
|
7
|
-
export type TranslatorOpts = {
|
|
8
|
-
allowedOperators: Operator[];
|
|
9
|
-
allowedComparators: Comparator[];
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* Abstract class that provides a blueprint for creating specific
|
|
13
|
-
* translator classes. Defines two abstract methods: formatFunction and
|
|
14
|
-
* mergeFilters.
|
|
15
|
-
*/
|
|
16
|
-
export declare abstract class BaseTranslator<T extends VectorStore = VectorStore> extends Visitor<T> {
|
|
17
|
-
/**
|
|
18
|
-
* Formats a given function (either an operator or a comparator) into a
|
|
19
|
-
* string.
|
|
20
|
-
* @param func The function to format.
|
|
21
|
-
* @returns Formatted string representation of the function.
|
|
22
|
-
*/
|
|
23
|
-
abstract formatFunction(func: Operator | Comparator): string;
|
|
24
|
-
/**
|
|
25
|
-
* Merges two filters into one, using a specified merge type.
|
|
26
|
-
* @param defaultFilter The default filter.
|
|
27
|
-
* @param generatedFilter The generated filter.
|
|
28
|
-
* @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'.
|
|
29
|
-
* @param forceDefaultFilter If true, the default filter will be used even if the generated filter is not empty.
|
|
30
|
-
* @returns The merged filter, or undefined if both filters are empty.
|
|
31
|
-
*/
|
|
32
|
-
abstract mergeFilters(defaultFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, generatedFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, mergeType?: "and" | "or" | "replace", forceDefaultFilter?: boolean): this["VisitStructuredQueryOutput"]["filter"] | undefined;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Class that extends the BaseTranslator class and provides concrete
|
|
36
|
-
* implementations for the abstract methods. Also declares three types:
|
|
37
|
-
* VisitOperationOutput, VisitComparisonOutput, and
|
|
38
|
-
* VisitStructuredQueryOutput, which are used as the return types for the
|
|
39
|
-
* visitOperation, visitComparison, and visitStructuredQuery methods
|
|
40
|
-
* respectively.
|
|
41
|
-
*/
|
|
42
|
-
export declare class BasicTranslator<T extends VectorStore = VectorStore> extends BaseTranslator<T> {
|
|
43
|
-
VisitOperationOutput: VisitorOperationResult;
|
|
44
|
-
VisitComparisonOutput: VisitorComparisonResult;
|
|
45
|
-
VisitStructuredQueryOutput: VisitorStructuredQueryResult;
|
|
46
|
-
allowedOperators: Operator[];
|
|
47
|
-
allowedComparators: Comparator[];
|
|
48
|
-
constructor(opts?: TranslatorOpts);
|
|
49
|
-
formatFunction(func: Operator | Comparator): string;
|
|
50
|
-
/**
|
|
51
|
-
* Visits an operation and returns a result.
|
|
52
|
-
* @param operation The operation to visit.
|
|
53
|
-
* @returns The result of visiting the operation.
|
|
54
|
-
*/
|
|
55
|
-
visitOperation(operation: Operation): this["VisitOperationOutput"];
|
|
56
|
-
/**
|
|
57
|
-
* Visits a comparison and returns a result.
|
|
58
|
-
* @param comparison The comparison to visit.
|
|
59
|
-
* @returns The result of visiting the comparison.
|
|
60
|
-
*/
|
|
61
|
-
visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
|
|
62
|
-
/**
|
|
63
|
-
* Visits a structured query and returns a result.
|
|
64
|
-
* @param query The structured query to visit.
|
|
65
|
-
* @returns The result of visiting the structured query.
|
|
66
|
-
*/
|
|
67
|
-
visitStructuredQuery(query: StructuredQuery): this["VisitStructuredQueryOutput"];
|
|
68
|
-
mergeFilters(defaultFilter: VisitorStructuredQueryResult["filter"] | undefined, generatedFilter: VisitorStructuredQueryResult["filter"] | undefined, mergeType?: string, forceDefaultFilter?: boolean): VisitorStructuredQueryResult["filter"] | undefined;
|
|
69
|
-
}
|
|
1
|
+
export { type TranslatorOpts, BaseTranslator, BasicTranslator, } from "@langchain/core/structured_query";
|
|
@@ -1,134 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { isFilterEmpty, castValue } from "./utils.js";
|
|
3
|
-
/**
|
|
4
|
-
* Abstract class that provides a blueprint for creating specific
|
|
5
|
-
* translator classes. Defines two abstract methods: formatFunction and
|
|
6
|
-
* mergeFilters.
|
|
7
|
-
*/
|
|
8
|
-
export class BaseTranslator extends Visitor {
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Class that extends the BaseTranslator class and provides concrete
|
|
12
|
-
* implementations for the abstract methods. Also declares three types:
|
|
13
|
-
* VisitOperationOutput, VisitComparisonOutput, and
|
|
14
|
-
* VisitStructuredQueryOutput, which are used as the return types for the
|
|
15
|
-
* visitOperation, visitComparison, and visitStructuredQuery methods
|
|
16
|
-
* respectively.
|
|
17
|
-
*/
|
|
18
|
-
export class BasicTranslator extends BaseTranslator {
|
|
19
|
-
constructor(opts) {
|
|
20
|
-
super();
|
|
21
|
-
Object.defineProperty(this, "allowedOperators", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true,
|
|
25
|
-
value: void 0
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "allowedComparators", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: void 0
|
|
32
|
-
});
|
|
33
|
-
this.allowedOperators = opts?.allowedOperators ?? [
|
|
34
|
-
Operators.and,
|
|
35
|
-
Operators.or,
|
|
36
|
-
];
|
|
37
|
-
this.allowedComparators = opts?.allowedComparators ?? [
|
|
38
|
-
Comparators.eq,
|
|
39
|
-
Comparators.ne,
|
|
40
|
-
Comparators.gt,
|
|
41
|
-
Comparators.gte,
|
|
42
|
-
Comparators.lt,
|
|
43
|
-
Comparators.lte,
|
|
44
|
-
];
|
|
45
|
-
}
|
|
46
|
-
formatFunction(func) {
|
|
47
|
-
if (func in Comparators) {
|
|
48
|
-
if (this.allowedComparators.length > 0 &&
|
|
49
|
-
this.allowedComparators.indexOf(func) === -1) {
|
|
50
|
-
throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
else if (func in Operators) {
|
|
54
|
-
if (this.allowedOperators.length > 0 &&
|
|
55
|
-
this.allowedOperators.indexOf(func) === -1) {
|
|
56
|
-
throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
throw new Error("Unknown comparator or operator");
|
|
61
|
-
}
|
|
62
|
-
return `$${func}`;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Visits an operation and returns a result.
|
|
66
|
-
* @param operation The operation to visit.
|
|
67
|
-
* @returns The result of visiting the operation.
|
|
68
|
-
*/
|
|
69
|
-
visitOperation(operation) {
|
|
70
|
-
const args = operation.args?.map((arg) => arg.accept(this));
|
|
71
|
-
return {
|
|
72
|
-
[this.formatFunction(operation.operator)]: args,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Visits a comparison and returns a result.
|
|
77
|
-
* @param comparison The comparison to visit.
|
|
78
|
-
* @returns The result of visiting the comparison.
|
|
79
|
-
*/
|
|
80
|
-
visitComparison(comparison) {
|
|
81
|
-
return {
|
|
82
|
-
[comparison.attribute]: {
|
|
83
|
-
[this.formatFunction(comparison.comparator)]: castValue(comparison.value),
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Visits a structured query and returns a result.
|
|
89
|
-
* @param query The structured query to visit.
|
|
90
|
-
* @returns The result of visiting the structured query.
|
|
91
|
-
*/
|
|
92
|
-
visitStructuredQuery(query) {
|
|
93
|
-
let nextArg = {};
|
|
94
|
-
if (query.filter) {
|
|
95
|
-
nextArg = {
|
|
96
|
-
filter: query.filter.accept(this),
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
return nextArg;
|
|
100
|
-
}
|
|
101
|
-
mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
|
|
102
|
-
if (isFilterEmpty(defaultFilter) && isFilterEmpty(generatedFilter)) {
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
if (isFilterEmpty(defaultFilter) || mergeType === "replace") {
|
|
106
|
-
if (isFilterEmpty(generatedFilter)) {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
return generatedFilter;
|
|
110
|
-
}
|
|
111
|
-
if (isFilterEmpty(generatedFilter)) {
|
|
112
|
-
if (forceDefaultFilter) {
|
|
113
|
-
return defaultFilter;
|
|
114
|
-
}
|
|
115
|
-
if (mergeType === "and") {
|
|
116
|
-
return undefined;
|
|
117
|
-
}
|
|
118
|
-
return defaultFilter;
|
|
119
|
-
}
|
|
120
|
-
if (mergeType === "and") {
|
|
121
|
-
return {
|
|
122
|
-
$and: [defaultFilter, generatedFilter],
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
else if (mergeType === "or") {
|
|
126
|
-
return {
|
|
127
|
-
$or: [defaultFilter, generatedFilter],
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
throw new Error("Unknown merge type");
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
1
|
+
export { BaseTranslator, BasicTranslator, } from "@langchain/core/structured_query";
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ChromaTranslator = void 0;
|
|
4
|
-
const
|
|
5
|
-
const base_js_1 = require("./base.cjs");
|
|
4
|
+
const structured_query_1 = require("@langchain/core/structured_query");
|
|
6
5
|
/**
|
|
7
6
|
* Specialized translator for the Chroma vector database. It extends the
|
|
8
7
|
* BasicTranslator class and translates internal query language elements
|
|
@@ -24,17 +23,17 @@ const base_js_1 = require("./base.cjs");
|
|
|
24
23
|
* );
|
|
25
24
|
* ```
|
|
26
25
|
*/
|
|
27
|
-
class ChromaTranslator extends
|
|
26
|
+
class ChromaTranslator extends structured_query_1.BasicTranslator {
|
|
28
27
|
constructor() {
|
|
29
28
|
super({
|
|
30
|
-
allowedOperators: [
|
|
29
|
+
allowedOperators: [structured_query_1.Operators.and, structured_query_1.Operators.or],
|
|
31
30
|
allowedComparators: [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
structured_query_1.Comparators.eq,
|
|
32
|
+
structured_query_1.Comparators.ne,
|
|
33
|
+
structured_query_1.Comparators.gt,
|
|
34
|
+
structured_query_1.Comparators.gte,
|
|
35
|
+
structured_query_1.Comparators.lt,
|
|
36
|
+
structured_query_1.Comparators.lte,
|
|
38
37
|
],
|
|
39
38
|
});
|
|
40
39
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Chroma } from "@langchain/community/vectorstores/chroma";
|
|
2
|
-
import { BasicTranslator } from "
|
|
2
|
+
import { BasicTranslator } from "@langchain/core/structured_query";
|
|
3
3
|
/**
|
|
4
4
|
* Specialized translator for the Chroma vector database. It extends the
|
|
5
5
|
* BasicTranslator class and translates internal query language elements
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Comparators, Operators } from "
|
|
2
|
-
import { BasicTranslator } from "./base.js";
|
|
1
|
+
import { BasicTranslator, Comparators, Operators, } from "@langchain/core/structured_query";
|
|
3
2
|
/**
|
|
4
3
|
* Specialized translator for the Chroma vector database. It extends the
|
|
5
4
|
* BasicTranslator class and translates internal query language elements
|