langchain 0.0.76 → 0.0.77
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/chains/query_constructor/ir.cjs +1 -0
- package/chains/query_constructor/ir.d.ts +1 -0
- package/chains/query_constructor/ir.js +1 -0
- package/chains/query_constructor.cjs +1 -0
- package/chains/query_constructor.d.ts +1 -0
- package/chains/query_constructor.js +1 -0
- package/dist/agents/chat_convo/index.cjs +27 -11
- package/dist/agents/chat_convo/index.d.ts +4 -1
- package/dist/agents/chat_convo/index.js +28 -12
- package/dist/agents/chat_convo/outputParser.cjs +79 -7
- package/dist/agents/chat_convo/outputParser.d.ts +25 -13
- package/dist/agents/chat_convo/outputParser.js +77 -6
- package/dist/agents/chat_convo/prompt.cjs +11 -8
- package/dist/agents/chat_convo/prompt.d.ts +2 -2
- package/dist/agents/chat_convo/prompt.js +11 -8
- package/dist/callbacks/handlers/tracer_langchain.cjs +12 -4
- package/dist/callbacks/handlers/tracer_langchain.d.ts +4 -1
- package/dist/callbacks/handlers/tracer_langchain.js +12 -4
- package/dist/callbacks/manager.cjs +6 -2
- package/dist/callbacks/manager.js +6 -2
- package/dist/chains/query_constructor/index.cjs +105 -0
- package/dist/chains/query_constructor/index.d.ts +37 -0
- package/dist/chains/query_constructor/index.js +95 -0
- package/dist/chains/query_constructor/ir.cjs +116 -0
- package/dist/chains/query_constructor/ir.d.ts +60 -0
- package/dist/chains/query_constructor/ir.js +107 -0
- package/dist/chains/query_constructor/parser.cjs +103 -0
- package/dist/chains/query_constructor/parser.d.ts +12 -0
- package/dist/chains/query_constructor/parser.js +99 -0
- package/dist/chains/query_constructor/prompt.cjs +127 -0
- package/dist/chains/query_constructor/prompt.d.ts +15 -0
- package/dist/chains/query_constructor/prompt.js +124 -0
- package/dist/chains/sql_db/sql_db_chain.cjs +13 -0
- package/dist/chains/sql_db/sql_db_chain.d.ts +2 -0
- package/dist/chains/sql_db/sql_db_chain.js +13 -0
- package/dist/client/langchainplus.cjs +21 -15
- package/dist/client/langchainplus.d.ts +4 -2
- package/dist/client/langchainplus.js +21 -15
- package/dist/llms/sagemaker_endpoint.cjs +123 -0
- package/dist/llms/sagemaker_endpoint.d.ts +82 -0
- package/dist/llms/sagemaker_endpoint.js +118 -0
- package/dist/memory/buffer_memory.cjs +1 -1
- package/dist/memory/buffer_memory.js +1 -1
- package/dist/memory/buffer_window_memory.cjs +1 -1
- package/dist/memory/buffer_window_memory.js +1 -1
- package/dist/output_parsers/expression.cjs +68 -0
- package/dist/output_parsers/expression.d.ts +25 -0
- package/dist/output_parsers/expression.js +49 -0
- package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.cjs +26 -0
- package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.js +22 -0
- package/dist/output_parsers/expression_type_handlers/base.cjs +67 -0
- package/dist/output_parsers/expression_type_handlers/base.d.ts +23 -0
- package/dist/output_parsers/expression_type_handlers/base.js +62 -0
- package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.cjs +24 -0
- package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.js +20 -0
- package/dist/output_parsers/expression_type_handlers/call_expression_handler.cjs +52 -0
- package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/call_expression_handler.js +48 -0
- package/dist/output_parsers/expression_type_handlers/factory.cjs +56 -0
- package/dist/output_parsers/expression_type_handlers/factory.d.ts +9 -0
- package/dist/output_parsers/expression_type_handlers/factory.js +52 -0
- package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs +22 -0
- package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/identifier_handler.js +18 -0
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +45 -0
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +41 -0
- package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.cjs +24 -0
- package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.js +20 -0
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +29 -0
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +25 -0
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +36 -0
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +32 -0
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs +22 -0
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +7 -0
- package/dist/output_parsers/expression_type_handlers/string_literal_handler.js +18 -0
- package/dist/output_parsers/expression_type_handlers/types.cjs +2 -0
- package/dist/output_parsers/expression_type_handlers/types.d.ts +41 -0
- package/dist/output_parsers/expression_type_handlers/types.js +1 -0
- package/dist/output_parsers/index.cjs +2 -1
- package/dist/output_parsers/index.d.ts +1 -1
- package/dist/output_parsers/index.js +1 -1
- package/dist/output_parsers/structured.cjs +81 -23
- package/dist/output_parsers/structured.d.ts +18 -0
- package/dist/output_parsers/structured.js +79 -22
- package/dist/retrievers/self_query/index.cjs +79 -0
- package/dist/retrievers/self_query/index.d.ts +33 -0
- package/dist/retrievers/self_query/index.js +74 -0
- package/dist/retrievers/self_query/translator.cjs +72 -0
- package/dist/retrievers/self_query/translator.d.ts +14 -0
- package/dist/retrievers/self_query/translator.js +67 -0
- package/dist/schema/query_constructor.cjs +26 -0
- package/dist/schema/query_constructor.d.ts +6 -0
- package/dist/schema/query_constructor.js +22 -0
- package/dist/tools/json.cjs +3 -1
- package/dist/tools/json.js +3 -1
- package/dist/util/event-source-parse.cjs +31 -5
- package/dist/util/event-source-parse.d.ts +3 -3
- package/dist/util/event-source-parse.js +31 -5
- package/llms/sagemaker_endpoint.cjs +1 -0
- package/llms/sagemaker_endpoint.d.ts +1 -0
- package/llms/sagemaker_endpoint.js +1 -0
- package/output_parsers/expression.cjs +1 -0
- package/output_parsers/expression.d.ts +1 -0
- package/output_parsers/expression.js +1 -0
- package/package.json +61 -3
- package/retrievers/self_query.cjs +1 -0
- package/retrievers/self_query.d.ts +1 -0
- package/retrievers/self_query.js +1 -0
- package/schema/query_constructor.cjs +1 -0
- package/schema/query_constructor.d.ts +1 -0
- package/schema/query_constructor.js +1 -0
|
@@ -59,30 +59,87 @@ export class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
|
|
|
59
59
|
}
|
|
60
60
|
_schemaToInstruction(schemaInput, indent = 2) {
|
|
61
61
|
const schema = schemaInput;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
if ("type" in schema) {
|
|
63
|
+
let nullable = false;
|
|
64
|
+
let type;
|
|
65
|
+
if (Array.isArray(schema.type)) {
|
|
66
|
+
const nullIdx = schema.type.findIndex((type) => type === "null");
|
|
67
|
+
if (nullIdx !== -1) {
|
|
68
|
+
nullable = true;
|
|
69
|
+
schema.type.splice(nullIdx, 1);
|
|
70
|
+
}
|
|
71
|
+
type = schema.type.join(" | ");
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
type = schema.type;
|
|
75
|
+
}
|
|
76
|
+
if (schema.type === "object" && schema.properties) {
|
|
77
|
+
const description = schema.description
|
|
78
|
+
? ` // ${schema.description}`
|
|
79
|
+
: "";
|
|
80
|
+
const properties = Object.entries(schema.properties)
|
|
81
|
+
.map(([key, value]) => {
|
|
82
|
+
const isOptional = schema.required?.includes(key)
|
|
83
|
+
? ""
|
|
84
|
+
: " (optional)";
|
|
85
|
+
return `${" ".repeat(indent)}"${key}": ${this._schemaToInstruction(value, indent + 2)}${isOptional}`;
|
|
86
|
+
})
|
|
87
|
+
.join("\n");
|
|
88
|
+
return `{\n${properties}\n${" ".repeat(indent - 2)}}${description}`;
|
|
89
|
+
}
|
|
90
|
+
if (schema.type === "array" && schema.items) {
|
|
91
|
+
const description = schema.description
|
|
92
|
+
? ` // ${schema.description}`
|
|
93
|
+
: "";
|
|
94
|
+
return `array[\n${" ".repeat(indent)}${this._schemaToInstruction(schema.items, indent + 2)}\n${" ".repeat(indent - 2)}] ${description}`;
|
|
95
|
+
}
|
|
96
|
+
const isNullable = nullable ? " (nullable)" : "";
|
|
69
97
|
const description = schema.description ? ` // ${schema.description}` : "";
|
|
70
|
-
|
|
71
|
-
.map(([key, value]) => {
|
|
72
|
-
const isOptional = schema.required?.includes(key)
|
|
73
|
-
? ""
|
|
74
|
-
: " (optional)";
|
|
75
|
-
return `${" ".repeat(indent)}"${key}": ${this._schemaToInstruction(value, indent + 2)}${isOptional}`;
|
|
76
|
-
})
|
|
77
|
-
.join("\n");
|
|
78
|
-
return `{\n${properties}\n${" ".repeat(indent - 2)}}${description}`;
|
|
98
|
+
return `${type}${description}${isNullable}`;
|
|
79
99
|
}
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
|
|
100
|
+
if ("anyOf" in schema) {
|
|
101
|
+
return schema.anyOf
|
|
102
|
+
.map((s) => this._schemaToInstruction(s, indent))
|
|
103
|
+
.join(`\n${" ".repeat(indent - 2)}`);
|
|
83
104
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
105
|
+
throw new Error("unsupported schema type");
|
|
106
|
+
}
|
|
107
|
+
static fromZodSchema(schema) {
|
|
108
|
+
return new this(schema);
|
|
109
|
+
}
|
|
110
|
+
static fromNamesAndDescriptions(schemas) {
|
|
111
|
+
const zodSchema = z.object(Object.fromEntries(Object.entries(schemas).map(([name, description]) => [name, z.string().describe(description)])));
|
|
112
|
+
return new this(zodSchema);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
export class AsymmetricStructuredOutputParser extends BaseOutputParser {
|
|
116
|
+
constructor(inputSchema) {
|
|
117
|
+
super();
|
|
118
|
+
Object.defineProperty(this, "inputSchema", {
|
|
119
|
+
enumerable: true,
|
|
120
|
+
configurable: true,
|
|
121
|
+
writable: true,
|
|
122
|
+
value: inputSchema
|
|
123
|
+
});
|
|
124
|
+
Object.defineProperty(this, "structuredInputParser", {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
configurable: true,
|
|
127
|
+
writable: true,
|
|
128
|
+
value: void 0
|
|
129
|
+
});
|
|
130
|
+
this.structuredInputParser = new JsonMarkdownStructuredOutputParser(inputSchema);
|
|
131
|
+
}
|
|
132
|
+
async parse(text) {
|
|
133
|
+
let parsedInput;
|
|
134
|
+
try {
|
|
135
|
+
parsedInput = await this.structuredInputParser.parse(text);
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
|
139
|
+
}
|
|
140
|
+
return this.outputProcessor(parsedInput);
|
|
141
|
+
}
|
|
142
|
+
getFormatInstructions() {
|
|
143
|
+
return this.structuredInputParser.getFormatInstructions();
|
|
87
144
|
}
|
|
88
145
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SelfQueryRetriever = exports.BasicTranslator = exports.BaseTranslator = void 0;
|
|
4
|
+
const index_js_1 = require("../../chains/query_constructor/index.cjs");
|
|
5
|
+
const index_js_2 = require("../../schema/index.cjs");
|
|
6
|
+
const translator_js_1 = require("./translator.cjs");
|
|
7
|
+
Object.defineProperty(exports, "BaseTranslator", { enumerable: true, get: function () { return translator_js_1.BaseTranslator; } });
|
|
8
|
+
Object.defineProperty(exports, "BasicTranslator", { enumerable: true, get: function () { return translator_js_1.BasicTranslator; } });
|
|
9
|
+
class SelfQueryRetriever extends index_js_2.BaseRetriever {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super();
|
|
12
|
+
Object.defineProperty(this, "vectorStore", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "llmChain", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "verbose", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, "structuredQueryTranslator", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(this, "searchParams", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: { k: 4 }
|
|
41
|
+
});
|
|
42
|
+
this.vectorStore = options.vectorStore;
|
|
43
|
+
this.llmChain = options.llmChain;
|
|
44
|
+
this.verbose = options.verbose ?? false;
|
|
45
|
+
this.searchParams = options.searchParams ?? this.searchParams;
|
|
46
|
+
this.structuredQueryTranslator = options.structuredQueryTranslator;
|
|
47
|
+
}
|
|
48
|
+
async getRelevantDocuments(query) {
|
|
49
|
+
const { [this.llmChain.outputKey]: output } = await this.llmChain.call({
|
|
50
|
+
[this.llmChain.inputKeys[0]]: query,
|
|
51
|
+
});
|
|
52
|
+
const nextArg = this.structuredQueryTranslator.visitStructuredQuery(output);
|
|
53
|
+
if (nextArg.filter) {
|
|
54
|
+
return this.vectorStore.similaritySearch(query, this.searchParams?.k, nextArg.filter);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return this.vectorStore.similaritySearch(query, this.searchParams?.k, this.searchParams?.filter);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
static fromLLM(opts) {
|
|
61
|
+
const { structuredQueryTranslator } = opts;
|
|
62
|
+
const allowedComparators = opts.allowedComparators ?? structuredQueryTranslator.allowedComparators;
|
|
63
|
+
const allowedOperators = opts.allowedOperators ?? structuredQueryTranslator.allowedOperators;
|
|
64
|
+
const llmChain = (0, index_js_1.loadQueryContstructorChain)({
|
|
65
|
+
llm: opts.llm,
|
|
66
|
+
documentContents: opts.documentContents,
|
|
67
|
+
attributeInfo: opts.attributeInfo,
|
|
68
|
+
examples: opts.examples,
|
|
69
|
+
allowedComparators,
|
|
70
|
+
allowedOperators,
|
|
71
|
+
});
|
|
72
|
+
return new SelfQueryRetriever({
|
|
73
|
+
vectorStore: opts.vectorStore,
|
|
74
|
+
llmChain,
|
|
75
|
+
structuredQueryTranslator,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.SelfQueryRetriever = SelfQueryRetriever;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
|
+
import { QueryConstructorChainOptions } from "../../chains/query_constructor/index.js";
|
|
3
|
+
import { Document } from "../../document.js";
|
|
4
|
+
import { BaseRetriever } from "../../schema/index.js";
|
|
5
|
+
import { VectorStore } from "../../vectorstores/base.js";
|
|
6
|
+
import { BaseTranslator, BasicTranslator } from "./translator.js";
|
|
7
|
+
export { BaseTranslator, BasicTranslator };
|
|
8
|
+
export type SelfQueryRetrieverArgs = {
|
|
9
|
+
vectorStore: VectorStore;
|
|
10
|
+
llmChain: LLMChain;
|
|
11
|
+
structuredQueryTranslator: BaseTranslator;
|
|
12
|
+
verbose?: boolean;
|
|
13
|
+
searchParams?: {
|
|
14
|
+
k?: number;
|
|
15
|
+
filter?: VectorStore["FilterType"];
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare class SelfQueryRetriever extends BaseRetriever implements SelfQueryRetrieverArgs {
|
|
19
|
+
vectorStore: VectorStore;
|
|
20
|
+
llmChain: LLMChain;
|
|
21
|
+
verbose?: boolean;
|
|
22
|
+
structuredQueryTranslator: BaseTranslator;
|
|
23
|
+
searchParams?: {
|
|
24
|
+
k?: number;
|
|
25
|
+
filter?: VectorStore["FilterType"];
|
|
26
|
+
};
|
|
27
|
+
constructor(options: SelfQueryRetrieverArgs);
|
|
28
|
+
getRelevantDocuments(query: string): Promise<Document<Record<string, unknown>>[]>;
|
|
29
|
+
static fromLLM(opts: QueryConstructorChainOptions & {
|
|
30
|
+
vectorStore: VectorStore;
|
|
31
|
+
structuredQueryTranslator: BaseTranslator;
|
|
32
|
+
}): SelfQueryRetriever;
|
|
33
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { loadQueryContstructorChain, } from "../../chains/query_constructor/index.js";
|
|
2
|
+
import { BaseRetriever } from "../../schema/index.js";
|
|
3
|
+
import { BaseTranslator, BasicTranslator } from "./translator.js";
|
|
4
|
+
export { BaseTranslator, BasicTranslator };
|
|
5
|
+
export class SelfQueryRetriever extends BaseRetriever {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super();
|
|
8
|
+
Object.defineProperty(this, "vectorStore", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: void 0
|
|
13
|
+
});
|
|
14
|
+
Object.defineProperty(this, "llmChain", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: void 0
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "verbose", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(this, "structuredQueryTranslator", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "searchParams", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: { k: 4 }
|
|
37
|
+
});
|
|
38
|
+
this.vectorStore = options.vectorStore;
|
|
39
|
+
this.llmChain = options.llmChain;
|
|
40
|
+
this.verbose = options.verbose ?? false;
|
|
41
|
+
this.searchParams = options.searchParams ?? this.searchParams;
|
|
42
|
+
this.structuredQueryTranslator = options.structuredQueryTranslator;
|
|
43
|
+
}
|
|
44
|
+
async getRelevantDocuments(query) {
|
|
45
|
+
const { [this.llmChain.outputKey]: output } = await this.llmChain.call({
|
|
46
|
+
[this.llmChain.inputKeys[0]]: query,
|
|
47
|
+
});
|
|
48
|
+
const nextArg = this.structuredQueryTranslator.visitStructuredQuery(output);
|
|
49
|
+
if (nextArg.filter) {
|
|
50
|
+
return this.vectorStore.similaritySearch(query, this.searchParams?.k, nextArg.filter);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return this.vectorStore.similaritySearch(query, this.searchParams?.k, this.searchParams?.filter);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static fromLLM(opts) {
|
|
57
|
+
const { structuredQueryTranslator } = opts;
|
|
58
|
+
const allowedComparators = opts.allowedComparators ?? structuredQueryTranslator.allowedComparators;
|
|
59
|
+
const allowedOperators = opts.allowedOperators ?? structuredQueryTranslator.allowedOperators;
|
|
60
|
+
const llmChain = loadQueryContstructorChain({
|
|
61
|
+
llm: opts.llm,
|
|
62
|
+
documentContents: opts.documentContents,
|
|
63
|
+
attributeInfo: opts.attributeInfo,
|
|
64
|
+
examples: opts.examples,
|
|
65
|
+
allowedComparators,
|
|
66
|
+
allowedOperators,
|
|
67
|
+
});
|
|
68
|
+
return new SelfQueryRetriever({
|
|
69
|
+
vectorStore: opts.vectorStore,
|
|
70
|
+
llmChain,
|
|
71
|
+
structuredQueryTranslator,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BasicTranslator = exports.BaseTranslator = void 0;
|
|
4
|
+
const ir_js_1 = require("../../chains/query_constructor/ir.cjs");
|
|
5
|
+
class BaseTranslator extends ir_js_1.Visitor {
|
|
6
|
+
}
|
|
7
|
+
exports.BaseTranslator = BaseTranslator;
|
|
8
|
+
class BasicTranslator extends BaseTranslator {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
Object.defineProperty(this, "allowedOperators", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: [ir_js_1.Operators.and, ir_js_1.Operators.or]
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "allowedComparators", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: [
|
|
22
|
+
ir_js_1.Comparators.eq,
|
|
23
|
+
ir_js_1.Comparators.neq,
|
|
24
|
+
ir_js_1.Comparators.gt,
|
|
25
|
+
ir_js_1.Comparators.gte,
|
|
26
|
+
ir_js_1.Comparators.lt,
|
|
27
|
+
ir_js_1.Comparators.lte,
|
|
28
|
+
]
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
formatFunction(func) {
|
|
32
|
+
if (func in ir_js_1.Comparators) {
|
|
33
|
+
if (this.allowedComparators.length > 0 &&
|
|
34
|
+
this.allowedComparators.indexOf(func) === -1) {
|
|
35
|
+
throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (func in ir_js_1.Operators) {
|
|
39
|
+
if (this.allowedOperators.length > 0 &&
|
|
40
|
+
this.allowedOperators.indexOf(func) === -1) {
|
|
41
|
+
throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
throw new Error("Unknown comparator or operator");
|
|
46
|
+
}
|
|
47
|
+
return `$${func}`;
|
|
48
|
+
}
|
|
49
|
+
visitOperation(operation) {
|
|
50
|
+
const args = operation.args?.map((arg) => arg.accept(this));
|
|
51
|
+
return {
|
|
52
|
+
[this.formatFunction(operation.operator)]: args,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
visitComparison(comparison) {
|
|
56
|
+
return {
|
|
57
|
+
[comparison.attribute]: {
|
|
58
|
+
[this.formatFunction(comparison.comparator)]: comparison.value,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
visitStructuredQuery(query) {
|
|
63
|
+
let nextArg = {};
|
|
64
|
+
if (query.filter) {
|
|
65
|
+
nextArg = {
|
|
66
|
+
filter: query.filter.accept(this),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return nextArg;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.BasicTranslator = BasicTranslator;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Comparator, Comparison, Operation, Operator, StructuredQuery, Visitor, VisitorComparisonResult, VisitorOperationResult, VisitorStructuredQueryResult } from "../../chains/query_constructor/ir.js";
|
|
2
|
+
export declare abstract class BaseTranslator extends Visitor {
|
|
3
|
+
abstract allowedOperators: Operator[];
|
|
4
|
+
abstract allowedComparators: Comparator[];
|
|
5
|
+
abstract formatFunction(func: Operator | Comparator): string;
|
|
6
|
+
}
|
|
7
|
+
export declare class BasicTranslator extends BaseTranslator {
|
|
8
|
+
allowedOperators: Operator[];
|
|
9
|
+
allowedComparators: Comparator[];
|
|
10
|
+
formatFunction(func: Operator | Comparator): string;
|
|
11
|
+
visitOperation(operation: Operation): VisitorOperationResult;
|
|
12
|
+
visitComparison(comparison: Comparison): VisitorComparisonResult;
|
|
13
|
+
visitStructuredQuery(query: StructuredQuery): VisitorStructuredQueryResult;
|
|
14
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Comparators, Operators, Visitor, } from "../../chains/query_constructor/ir.js";
|
|
2
|
+
export class BaseTranslator extends Visitor {
|
|
3
|
+
}
|
|
4
|
+
export class BasicTranslator extends BaseTranslator {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
Object.defineProperty(this, "allowedOperators", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: [Operators.and, Operators.or]
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(this, "allowedComparators", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: [
|
|
18
|
+
Comparators.eq,
|
|
19
|
+
Comparators.neq,
|
|
20
|
+
Comparators.gt,
|
|
21
|
+
Comparators.gte,
|
|
22
|
+
Comparators.lt,
|
|
23
|
+
Comparators.lte,
|
|
24
|
+
]
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
formatFunction(func) {
|
|
28
|
+
if (func in Comparators) {
|
|
29
|
+
if (this.allowedComparators.length > 0 &&
|
|
30
|
+
this.allowedComparators.indexOf(func) === -1) {
|
|
31
|
+
throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else if (func in Operators) {
|
|
35
|
+
if (this.allowedOperators.length > 0 &&
|
|
36
|
+
this.allowedOperators.indexOf(func) === -1) {
|
|
37
|
+
throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
throw new Error("Unknown comparator or operator");
|
|
42
|
+
}
|
|
43
|
+
return `$${func}`;
|
|
44
|
+
}
|
|
45
|
+
visitOperation(operation) {
|
|
46
|
+
const args = operation.args?.map((arg) => arg.accept(this));
|
|
47
|
+
return {
|
|
48
|
+
[this.formatFunction(operation.operator)]: args,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
visitComparison(comparison) {
|
|
52
|
+
return {
|
|
53
|
+
[comparison.attribute]: {
|
|
54
|
+
[this.formatFunction(comparison.comparator)]: comparison.value,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
visitStructuredQuery(query) {
|
|
59
|
+
let nextArg = {};
|
|
60
|
+
if (query.filter) {
|
|
61
|
+
nextArg = {
|
|
62
|
+
filter: query.filter.accept(this),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return nextArg;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AttributeInfo = void 0;
|
|
4
|
+
class AttributeInfo {
|
|
5
|
+
constructor(name, type, description) {
|
|
6
|
+
Object.defineProperty(this, "name", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
writable: true,
|
|
10
|
+
value: name
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(this, "type", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: type
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "description", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: description
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.AttributeInfo = AttributeInfo;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export class AttributeInfo {
|
|
2
|
+
constructor(name, type, description) {
|
|
3
|
+
Object.defineProperty(this, "name", {
|
|
4
|
+
enumerable: true,
|
|
5
|
+
configurable: true,
|
|
6
|
+
writable: true,
|
|
7
|
+
value: name
|
|
8
|
+
});
|
|
9
|
+
Object.defineProperty(this, "type", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: type
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "description", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: description
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
package/dist/tools/json.cjs
CHANGED
|
@@ -27,7 +27,9 @@ class JsonSpec {
|
|
|
27
27
|
const pointer = jsonpointer_1.default.compile(input);
|
|
28
28
|
const res = pointer.get(this.obj);
|
|
29
29
|
if (typeof res === "object" && !Array.isArray(res) && res !== null) {
|
|
30
|
-
return Object.keys(res)
|
|
30
|
+
return Object.keys(res)
|
|
31
|
+
.map((i) => i.replaceAll("~", "~0").replaceAll("/", "~1"))
|
|
32
|
+
.join(", ");
|
|
31
33
|
}
|
|
32
34
|
throw new Error(`Value at ${input} is not a dictionary, get the value directly instead.`);
|
|
33
35
|
}
|
package/dist/tools/json.js
CHANGED
|
@@ -21,7 +21,9 @@ export class JsonSpec {
|
|
|
21
21
|
const pointer = jsonpointer.compile(input);
|
|
22
22
|
const res = pointer.get(this.obj);
|
|
23
23
|
if (typeof res === "object" && !Array.isArray(res) && res !== null) {
|
|
24
|
-
return Object.keys(res)
|
|
24
|
+
return Object.keys(res)
|
|
25
|
+
.map((i) => i.replaceAll("~", "~0").replaceAll("/", "~1"))
|
|
26
|
+
.join(", ");
|
|
25
27
|
}
|
|
26
28
|
throw new Error(`Value at ${input} is not a dictionary, get the value directly instead.`);
|
|
27
29
|
}
|
|
@@ -16,9 +16,18 @@ exports.EventStreamContentType = "text/event-stream";
|
|
|
16
16
|
*/
|
|
17
17
|
async function getBytes(stream, onChunk) {
|
|
18
18
|
const reader = stream.getReader();
|
|
19
|
-
|
|
20
|
-
//
|
|
21
|
-
|
|
19
|
+
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
|
|
20
|
+
// This change is essential to ensure that we capture every last piece of information from streams,
|
|
21
|
+
// such as those from Azure OpenAI, which may not terminate with a blank line. Without this
|
|
22
|
+
// mechanism, we risk ignoring a possibly significant last message.
|
|
23
|
+
// See https://github.com/hwchase17/langchainjs/issues/1299 for details.
|
|
24
|
+
// eslint-disable-next-line no-constant-condition
|
|
25
|
+
while (true) {
|
|
26
|
+
const result = await reader.read();
|
|
27
|
+
if (result.done) {
|
|
28
|
+
onChunk(new Uint8Array(), true);
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
22
31
|
onChunk(result.value);
|
|
23
32
|
}
|
|
24
33
|
}
|
|
@@ -35,7 +44,11 @@ function getLines(onLine) {
|
|
|
35
44
|
let fieldLength; // length of the `field` portion of the line
|
|
36
45
|
let discardTrailingNewline = false;
|
|
37
46
|
// return a function that can process each incoming byte chunk:
|
|
38
|
-
return function onChunk(arr) {
|
|
47
|
+
return function onChunk(arr, flush) {
|
|
48
|
+
if (flush) {
|
|
49
|
+
onLine(arr, 0, true);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
39
52
|
if (buffer === undefined) {
|
|
40
53
|
buffer = arr;
|
|
41
54
|
position = 0;
|
|
@@ -107,7 +120,14 @@ function getMessages(onMessage, onId, onRetry) {
|
|
|
107
120
|
let message = newMessage();
|
|
108
121
|
const decoder = new TextDecoder();
|
|
109
122
|
// return a function that can process each incoming line buffer:
|
|
110
|
-
return function onLine(line, fieldLength) {
|
|
123
|
+
return function onLine(line, fieldLength, flush) {
|
|
124
|
+
if (flush) {
|
|
125
|
+
if (!isEmpty(message)) {
|
|
126
|
+
onMessage?.(message);
|
|
127
|
+
message = newMessage();
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
111
131
|
if (line.length === 0) {
|
|
112
132
|
// empty line denotes end of message. Trigger the callback and start a new message:
|
|
113
133
|
onMessage?.(message);
|
|
@@ -163,3 +183,9 @@ function newMessage() {
|
|
|
163
183
|
retry: undefined,
|
|
164
184
|
};
|
|
165
185
|
}
|
|
186
|
+
function isEmpty(message) {
|
|
187
|
+
return (message.data === "" &&
|
|
188
|
+
message.event === "" &&
|
|
189
|
+
message.id === "" &&
|
|
190
|
+
message.retry === undefined);
|
|
191
|
+
}
|
|
@@ -19,14 +19,14 @@ export interface EventSourceMessage {
|
|
|
19
19
|
* @param onChunk A function that will be called on each new byte chunk in the stream.
|
|
20
20
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
21
21
|
*/
|
|
22
|
-
export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array) => void): Promise<void>;
|
|
22
|
+
export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array, flush?: boolean) => void): Promise<void>;
|
|
23
23
|
/**
|
|
24
24
|
* Parses arbitary byte chunks into EventSource line buffers.
|
|
25
25
|
* Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
|
|
26
26
|
* @param onLine A function that will be called on each new EventSource line.
|
|
27
27
|
* @returns A function that should be called for each incoming byte chunk.
|
|
28
28
|
*/
|
|
29
|
-
export declare function getLines(onLine: (line: Uint8Array, fieldLength: number) => void): (arr: Uint8Array) => void;
|
|
29
|
+
export declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void): (arr: Uint8Array, flush?: boolean) => void;
|
|
30
30
|
/**
|
|
31
31
|
* Parses line buffers into EventSourceMessages.
|
|
32
32
|
* @param onId A function that will be called on each `id` field.
|
|
@@ -34,4 +34,4 @@ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number)
|
|
|
34
34
|
* @param onMessage A function that will be called on each message.
|
|
35
35
|
* @returns A function that should be called for each incoming line buffer.
|
|
36
36
|
*/
|
|
37
|
-
export declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number) => void;
|
|
37
|
+
export declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number, flush?: boolean) => void;
|