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.
Files changed (117) hide show
  1. package/chains/query_constructor/ir.cjs +1 -0
  2. package/chains/query_constructor/ir.d.ts +1 -0
  3. package/chains/query_constructor/ir.js +1 -0
  4. package/chains/query_constructor.cjs +1 -0
  5. package/chains/query_constructor.d.ts +1 -0
  6. package/chains/query_constructor.js +1 -0
  7. package/dist/agents/chat_convo/index.cjs +27 -11
  8. package/dist/agents/chat_convo/index.d.ts +4 -1
  9. package/dist/agents/chat_convo/index.js +28 -12
  10. package/dist/agents/chat_convo/outputParser.cjs +79 -7
  11. package/dist/agents/chat_convo/outputParser.d.ts +25 -13
  12. package/dist/agents/chat_convo/outputParser.js +77 -6
  13. package/dist/agents/chat_convo/prompt.cjs +11 -8
  14. package/dist/agents/chat_convo/prompt.d.ts +2 -2
  15. package/dist/agents/chat_convo/prompt.js +11 -8
  16. package/dist/callbacks/handlers/tracer_langchain.cjs +12 -4
  17. package/dist/callbacks/handlers/tracer_langchain.d.ts +4 -1
  18. package/dist/callbacks/handlers/tracer_langchain.js +12 -4
  19. package/dist/callbacks/manager.cjs +6 -2
  20. package/dist/callbacks/manager.js +6 -2
  21. package/dist/chains/query_constructor/index.cjs +105 -0
  22. package/dist/chains/query_constructor/index.d.ts +37 -0
  23. package/dist/chains/query_constructor/index.js +95 -0
  24. package/dist/chains/query_constructor/ir.cjs +116 -0
  25. package/dist/chains/query_constructor/ir.d.ts +60 -0
  26. package/dist/chains/query_constructor/ir.js +107 -0
  27. package/dist/chains/query_constructor/parser.cjs +103 -0
  28. package/dist/chains/query_constructor/parser.d.ts +12 -0
  29. package/dist/chains/query_constructor/parser.js +99 -0
  30. package/dist/chains/query_constructor/prompt.cjs +127 -0
  31. package/dist/chains/query_constructor/prompt.d.ts +15 -0
  32. package/dist/chains/query_constructor/prompt.js +124 -0
  33. package/dist/chains/sql_db/sql_db_chain.cjs +13 -0
  34. package/dist/chains/sql_db/sql_db_chain.d.ts +2 -0
  35. package/dist/chains/sql_db/sql_db_chain.js +13 -0
  36. package/dist/client/langchainplus.cjs +21 -15
  37. package/dist/client/langchainplus.d.ts +4 -2
  38. package/dist/client/langchainplus.js +21 -15
  39. package/dist/llms/sagemaker_endpoint.cjs +123 -0
  40. package/dist/llms/sagemaker_endpoint.d.ts +82 -0
  41. package/dist/llms/sagemaker_endpoint.js +118 -0
  42. package/dist/memory/buffer_memory.cjs +1 -1
  43. package/dist/memory/buffer_memory.js +1 -1
  44. package/dist/memory/buffer_window_memory.cjs +1 -1
  45. package/dist/memory/buffer_window_memory.js +1 -1
  46. package/dist/output_parsers/expression.cjs +68 -0
  47. package/dist/output_parsers/expression.d.ts +25 -0
  48. package/dist/output_parsers/expression.js +49 -0
  49. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.cjs +26 -0
  50. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +7 -0
  51. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.js +22 -0
  52. package/dist/output_parsers/expression_type_handlers/base.cjs +67 -0
  53. package/dist/output_parsers/expression_type_handlers/base.d.ts +23 -0
  54. package/dist/output_parsers/expression_type_handlers/base.js +62 -0
  55. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.cjs +24 -0
  56. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +7 -0
  57. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.js +20 -0
  58. package/dist/output_parsers/expression_type_handlers/call_expression_handler.cjs +52 -0
  59. package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +7 -0
  60. package/dist/output_parsers/expression_type_handlers/call_expression_handler.js +48 -0
  61. package/dist/output_parsers/expression_type_handlers/factory.cjs +56 -0
  62. package/dist/output_parsers/expression_type_handlers/factory.d.ts +9 -0
  63. package/dist/output_parsers/expression_type_handlers/factory.js +52 -0
  64. package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs +22 -0
  65. package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +7 -0
  66. package/dist/output_parsers/expression_type_handlers/identifier_handler.js +18 -0
  67. package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +45 -0
  68. package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +7 -0
  69. package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +41 -0
  70. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.cjs +24 -0
  71. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +7 -0
  72. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.js +20 -0
  73. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +29 -0
  74. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +7 -0
  75. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +25 -0
  76. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +36 -0
  77. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +7 -0
  78. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +32 -0
  79. package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs +22 -0
  80. package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +7 -0
  81. package/dist/output_parsers/expression_type_handlers/string_literal_handler.js +18 -0
  82. package/dist/output_parsers/expression_type_handlers/types.cjs +2 -0
  83. package/dist/output_parsers/expression_type_handlers/types.d.ts +41 -0
  84. package/dist/output_parsers/expression_type_handlers/types.js +1 -0
  85. package/dist/output_parsers/index.cjs +2 -1
  86. package/dist/output_parsers/index.d.ts +1 -1
  87. package/dist/output_parsers/index.js +1 -1
  88. package/dist/output_parsers/structured.cjs +81 -23
  89. package/dist/output_parsers/structured.d.ts +18 -0
  90. package/dist/output_parsers/structured.js +79 -22
  91. package/dist/retrievers/self_query/index.cjs +79 -0
  92. package/dist/retrievers/self_query/index.d.ts +33 -0
  93. package/dist/retrievers/self_query/index.js +74 -0
  94. package/dist/retrievers/self_query/translator.cjs +72 -0
  95. package/dist/retrievers/self_query/translator.d.ts +14 -0
  96. package/dist/retrievers/self_query/translator.js +67 -0
  97. package/dist/schema/query_constructor.cjs +26 -0
  98. package/dist/schema/query_constructor.d.ts +6 -0
  99. package/dist/schema/query_constructor.js +22 -0
  100. package/dist/tools/json.cjs +3 -1
  101. package/dist/tools/json.js +3 -1
  102. package/dist/util/event-source-parse.cjs +31 -5
  103. package/dist/util/event-source-parse.d.ts +3 -3
  104. package/dist/util/event-source-parse.js +31 -5
  105. package/llms/sagemaker_endpoint.cjs +1 -0
  106. package/llms/sagemaker_endpoint.d.ts +1 -0
  107. package/llms/sagemaker_endpoint.js +1 -0
  108. package/output_parsers/expression.cjs +1 -0
  109. package/output_parsers/expression.d.ts +1 -0
  110. package/output_parsers/expression.js +1 -0
  111. package/package.json +61 -3
  112. package/retrievers/self_query.cjs +1 -0
  113. package/retrievers/self_query.d.ts +1 -0
  114. package/retrievers/self_query.js +1 -0
  115. package/schema/query_constructor.cjs +1 -0
  116. package/schema/query_constructor.d.ts +1 -0
  117. package/schema/query_constructor.js +1 -0
@@ -1,7 +1,8 @@
1
+ import { AsyncCaller } from "../../util/async_caller.js";
1
2
  import { getRuntimeEnvironment } from "../../util/env.js";
2
3
  import { BaseTracer } from "./tracer.js";
3
4
  export class LangChainTracer extends BaseTracer {
4
- constructor({ exampleId, tenantId, sessionName, sessionExtra, } = {}) {
5
+ constructor({ exampleId, tenantId, sessionName, sessionExtra, callerParams, } = {}) {
5
6
  super();
6
7
  Object.defineProperty(this, "name", {
7
8
  enumerable: true,
@@ -56,6 +57,12 @@ export class LangChainTracer extends BaseTracer {
56
57
  writable: true,
57
58
  value: void 0
58
59
  });
60
+ Object.defineProperty(this, "caller", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
59
66
  // eslint-disable-next-line no-process-env
60
67
  if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
61
68
  // eslint-disable-next-line no-process-env
@@ -76,6 +83,7 @@ export class LangChainTracer extends BaseTracer {
76
83
  "default";
77
84
  this.sessionExtra = sessionExtra;
78
85
  this.exampleId = exampleId;
86
+ this.caller = new AsyncCaller(callerParams ?? {});
79
87
  }
80
88
  async ensureSession() {
81
89
  if (this.session) {
@@ -83,7 +91,7 @@ export class LangChainTracer extends BaseTracer {
83
91
  }
84
92
  const tenantId = await this.ensureTenantId();
85
93
  const endpoint = `${this.endpoint}/sessions?upsert=true`;
86
- const res = await fetch(endpoint, {
94
+ const res = await this.caller.call(fetch, endpoint, {
87
95
  method: "POST",
88
96
  headers: this.headers,
89
97
  body: JSON.stringify({
@@ -105,7 +113,7 @@ export class LangChainTracer extends BaseTracer {
105
113
  return this.tenantId;
106
114
  }
107
115
  const endpoint = `${this.endpoint}/tenants`;
108
- const response = await fetch(endpoint, {
116
+ const response = await this.caller.call(fetch, endpoint, {
109
117
  method: "GET",
110
118
  headers: this.headers,
111
119
  });
@@ -146,7 +154,7 @@ export class LangChainTracer extends BaseTracer {
146
154
  async persistRun(run) {
147
155
  const persistedRun = await this._convertToCreate(run, this.exampleId);
148
156
  const endpoint = `${this.endpoint}/runs`;
149
- const response = await fetch(endpoint, {
157
+ const response = await this.caller.call(fetch, endpoint, {
150
158
  method: "POST",
151
159
  headers: this.headers,
152
160
  body: JSON.stringify(persistedRun),
@@ -334,6 +334,10 @@ class CallbackManager extends BaseCallbackManager {
334
334
  ? localHandlers.map(ensureHandler)
335
335
  : localHandlers?.handlers, false);
336
336
  }
337
+ const verboseEnabled = (typeof process !== "undefined"
338
+ ? // eslint-disable-next-line no-process-env
339
+ process.env?.LANGCHAIN_VERBOSE !== undefined
340
+ : false) || options?.verbose;
337
341
  const tracingV2Enabled = typeof process !== "undefined"
338
342
  ? // eslint-disable-next-line no-process-env
339
343
  process.env?.LANGCHAIN_TRACING_V2 !== undefined
@@ -343,11 +347,11 @@ class CallbackManager extends BaseCallbackManager {
343
347
  ? // eslint-disable-next-line no-process-env
344
348
  process.env?.LANGCHAIN_TRACING !== undefined
345
349
  : false);
346
- if (options?.verbose || tracingEnabled) {
350
+ if (verboseEnabled || tracingEnabled) {
347
351
  if (!callbackManager) {
348
352
  callbackManager = new CallbackManager();
349
353
  }
350
- if (options?.verbose &&
354
+ if (verboseEnabled &&
351
355
  !callbackManager.handlers.some((handler) => handler.name === console_js_1.ConsoleCallbackHandler.prototype.name)) {
352
356
  const consoleHandler = new console_js_1.ConsoleCallbackHandler();
353
357
  callbackManager.addHandler(consoleHandler, true);
@@ -327,6 +327,10 @@ export class CallbackManager extends BaseCallbackManager {
327
327
  ? localHandlers.map(ensureHandler)
328
328
  : localHandlers?.handlers, false);
329
329
  }
330
+ const verboseEnabled = (typeof process !== "undefined"
331
+ ? // eslint-disable-next-line no-process-env
332
+ process.env?.LANGCHAIN_VERBOSE !== undefined
333
+ : false) || options?.verbose;
330
334
  const tracingV2Enabled = typeof process !== "undefined"
331
335
  ? // eslint-disable-next-line no-process-env
332
336
  process.env?.LANGCHAIN_TRACING_V2 !== undefined
@@ -336,11 +340,11 @@ export class CallbackManager extends BaseCallbackManager {
336
340
  ? // eslint-disable-next-line no-process-env
337
341
  process.env?.LANGCHAIN_TRACING !== undefined
338
342
  : false);
339
- if (options?.verbose || tracingEnabled) {
343
+ if (verboseEnabled || tracingEnabled) {
340
344
  if (!callbackManager) {
341
345
  callbackManager = new CallbackManager();
342
346
  }
343
- if (options?.verbose &&
347
+ if (verboseEnabled &&
344
348
  !callbackManager.handlers.some((handler) => handler.name === ConsoleCallbackHandler.prototype.name)) {
345
349
  const consoleHandler = new ConsoleCallbackHandler();
346
350
  callbackManager.addHandler(consoleHandler, true);
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadQueryContstructorChain = exports.formatAttributeInfo = exports.StructuredQueryOutputParser = exports.EXAMPLE_PROMPT = exports.DEFAULT_SUFFIX = exports.DEFAULT_SCHEMA = exports.DEFAULT_PREFIX = exports.DEFAULT_EXAMPLES = exports.QueryTransformer = void 0;
4
+ const zod_1 = require("zod");
5
+ const parser_js_1 = require("./parser.cjs");
6
+ Object.defineProperty(exports, "QueryTransformer", { enumerable: true, get: function () { return parser_js_1.QueryTransformer; } });
7
+ const ir_js_1 = require("./ir.cjs");
8
+ const prompt_js_1 = require("./prompt.cjs");
9
+ Object.defineProperty(exports, "DEFAULT_EXAMPLES", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_EXAMPLES; } });
10
+ Object.defineProperty(exports, "DEFAULT_PREFIX", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_PREFIX; } });
11
+ Object.defineProperty(exports, "DEFAULT_SCHEMA", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_SCHEMA; } });
12
+ Object.defineProperty(exports, "DEFAULT_SUFFIX", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_SUFFIX; } });
13
+ Object.defineProperty(exports, "EXAMPLE_PROMPT", { enumerable: true, get: function () { return prompt_js_1.EXAMPLE_PROMPT; } });
14
+ const template_js_1 = require("../../prompts/template.cjs");
15
+ const llm_chain_js_1 = require("../llm_chain.cjs");
16
+ const few_shot_js_1 = require("../../prompts/few_shot.cjs");
17
+ const structured_js_1 = require("../../output_parsers/structured.cjs");
18
+ const queryInputSchema = /* #__PURE__ */ zod_1.z.object({
19
+ query: /* #__PURE__ */ zod_1.z
20
+ .string()
21
+ .describe("text string to compare to document contents"),
22
+ filter: /* #__PURE__ */ zod_1.z
23
+ .string()
24
+ .optional()
25
+ .describe("logical condition statement for filtering documents"),
26
+ });
27
+ class StructuredQueryOutputParser extends structured_js_1.AsymmetricStructuredOutputParser {
28
+ constructor(parserFunction) {
29
+ super(queryInputSchema);
30
+ Object.defineProperty(this, "parserFunction", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: parserFunction
35
+ });
36
+ }
37
+ async outputProcessor(input) {
38
+ return this.parserFunction(input.query, input.filter);
39
+ }
40
+ static fromComponents(allowedComparators = [], allowedOperators = []) {
41
+ const queryTransformer = new parser_js_1.QueryTransformer(allowedComparators, allowedOperators);
42
+ return new StructuredQueryOutputParser(async (query, filter) => {
43
+ let myQuery = query;
44
+ if (myQuery.length === 0) {
45
+ myQuery = " ";
46
+ }
47
+ if (filter === "NO_FILTER" || filter === undefined) {
48
+ return new ir_js_1.StructuredQuery(query);
49
+ }
50
+ else {
51
+ const parsedFilter = await queryTransformer.parse(filter);
52
+ return new ir_js_1.StructuredQuery(query, parsedFilter);
53
+ }
54
+ });
55
+ }
56
+ }
57
+ exports.StructuredQueryOutputParser = StructuredQueryOutputParser;
58
+ function formatAttributeInfo(info) {
59
+ const infoObj = info.reduce((acc, attr) => {
60
+ acc[attr.name] = {
61
+ type: attr.type,
62
+ description: attr.description,
63
+ };
64
+ return acc;
65
+ }, {});
66
+ return JSON.stringify(infoObj, null, 2)
67
+ .replaceAll("{", "{{")
68
+ .replaceAll("}", "}}");
69
+ }
70
+ exports.formatAttributeInfo = formatAttributeInfo;
71
+ const defaultExample = prompt_js_1.DEFAULT_EXAMPLES.map((EXAMPLE) => EXAMPLE);
72
+ function _getPrompt(documentContents, attributeInfo, allowedComparators, allowedOperators, examples = defaultExample) {
73
+ const myAllowedComparators = allowedComparators ?? Object.values(ir_js_1.Comparators);
74
+ const myAllowedOperators = allowedOperators ?? Object.values(ir_js_1.Operators);
75
+ const attributeJSON = formatAttributeInfo(attributeInfo);
76
+ const schema = (0, template_js_1.interpolateFString)(prompt_js_1.DEFAULT_SCHEMA, {
77
+ allowed_comparators: myAllowedComparators.join(" | "),
78
+ allowed_operators: myAllowedOperators.join(" | "),
79
+ });
80
+ const prefix = (0, template_js_1.interpolateFString)(prompt_js_1.DEFAULT_PREFIX, {
81
+ schema,
82
+ });
83
+ const suffix = (0, template_js_1.interpolateFString)(prompt_js_1.DEFAULT_SUFFIX, {
84
+ i: examples.length + 1,
85
+ content: documentContents,
86
+ attributes: attributeJSON,
87
+ });
88
+ const outputParser = StructuredQueryOutputParser.fromComponents(allowedComparators, allowedOperators);
89
+ return new few_shot_js_1.FewShotPromptTemplate({
90
+ examples,
91
+ examplePrompt: prompt_js_1.EXAMPLE_PROMPT,
92
+ inputVariables: ["query"],
93
+ suffix,
94
+ prefix,
95
+ outputParser,
96
+ });
97
+ }
98
+ function loadQueryContstructorChain(opts) {
99
+ const prompt = _getPrompt(opts.documentContents, opts.attributeInfo, opts.allowedComparators, opts.allowedOperators, opts.examples);
100
+ return new llm_chain_js_1.LLMChain({
101
+ llm: opts.llm,
102
+ prompt,
103
+ });
104
+ }
105
+ exports.loadQueryContstructorChain = loadQueryContstructorChain;
@@ -0,0 +1,37 @@
1
+ import { z } from "zod";
2
+ import { QueryTransformer, TraverseType } from "./parser.js";
3
+ import { Comparator, Operator, StructuredQuery } from "./ir.js";
4
+ import { InputValues } from "../../schema/index.js";
5
+ import { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT } from "./prompt.js";
6
+ import { LLMChain } from "../llm_chain.js";
7
+ import { BaseLanguageModel } from "../../base_language/index.js";
8
+ import { AsymmetricStructuredOutputParser } from "../../output_parsers/structured.js";
9
+ import { AttributeInfo } from "../../schema/query_constructor.js";
10
+ export { QueryTransformer, TraverseType };
11
+ export { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT, };
12
+ declare const queryInputSchema: z.ZodObject<{
13
+ query: z.ZodString;
14
+ filter: z.ZodOptional<z.ZodString>;
15
+ }, "strip", z.ZodTypeAny, {
16
+ query: string;
17
+ filter?: string | undefined;
18
+ }, {
19
+ query: string;
20
+ filter?: string | undefined;
21
+ }>;
22
+ export declare class StructuredQueryOutputParser extends AsymmetricStructuredOutputParser<typeof queryInputSchema, StructuredQuery> {
23
+ private parserFunction;
24
+ constructor(parserFunction: (query: string, filter?: string) => Promise<StructuredQuery>);
25
+ outputProcessor(input: z.infer<typeof queryInputSchema>): Promise<StructuredQuery>;
26
+ static fromComponents(allowedComparators?: Comparator[], allowedOperators?: Operator[]): StructuredQueryOutputParser;
27
+ }
28
+ export declare function formatAttributeInfo(info: AttributeInfo[]): string;
29
+ export type QueryConstructorChainOptions = {
30
+ llm: BaseLanguageModel;
31
+ documentContents: string;
32
+ attributeInfo: AttributeInfo[];
33
+ examples?: InputValues[];
34
+ allowedComparators?: Comparator[];
35
+ allowedOperators?: Operator[];
36
+ };
37
+ export declare function loadQueryContstructorChain(opts: QueryConstructorChainOptions): LLMChain<string>;
@@ -0,0 +1,95 @@
1
+ import { z } from "zod";
2
+ import { QueryTransformer } from "./parser.js";
3
+ import { Comparators, Operators, StructuredQuery, } from "./ir.js";
4
+ import { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT, } from "./prompt.js";
5
+ import { interpolateFString } from "../../prompts/template.js";
6
+ import { LLMChain } from "../llm_chain.js";
7
+ import { FewShotPromptTemplate } from "../../prompts/few_shot.js";
8
+ import { AsymmetricStructuredOutputParser } from "../../output_parsers/structured.js";
9
+ export { QueryTransformer };
10
+ export { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT, };
11
+ const queryInputSchema = /* #__PURE__ */ z.object({
12
+ query: /* #__PURE__ */ z
13
+ .string()
14
+ .describe("text string to compare to document contents"),
15
+ filter: /* #__PURE__ */ z
16
+ .string()
17
+ .optional()
18
+ .describe("logical condition statement for filtering documents"),
19
+ });
20
+ export class StructuredQueryOutputParser extends AsymmetricStructuredOutputParser {
21
+ constructor(parserFunction) {
22
+ super(queryInputSchema);
23
+ Object.defineProperty(this, "parserFunction", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: parserFunction
28
+ });
29
+ }
30
+ async outputProcessor(input) {
31
+ return this.parserFunction(input.query, input.filter);
32
+ }
33
+ static fromComponents(allowedComparators = [], allowedOperators = []) {
34
+ const queryTransformer = new QueryTransformer(allowedComparators, allowedOperators);
35
+ return new StructuredQueryOutputParser(async (query, filter) => {
36
+ let myQuery = query;
37
+ if (myQuery.length === 0) {
38
+ myQuery = " ";
39
+ }
40
+ if (filter === "NO_FILTER" || filter === undefined) {
41
+ return new StructuredQuery(query);
42
+ }
43
+ else {
44
+ const parsedFilter = await queryTransformer.parse(filter);
45
+ return new StructuredQuery(query, parsedFilter);
46
+ }
47
+ });
48
+ }
49
+ }
50
+ export function formatAttributeInfo(info) {
51
+ const infoObj = info.reduce((acc, attr) => {
52
+ acc[attr.name] = {
53
+ type: attr.type,
54
+ description: attr.description,
55
+ };
56
+ return acc;
57
+ }, {});
58
+ return JSON.stringify(infoObj, null, 2)
59
+ .replaceAll("{", "{{")
60
+ .replaceAll("}", "}}");
61
+ }
62
+ const defaultExample = DEFAULT_EXAMPLES.map((EXAMPLE) => EXAMPLE);
63
+ function _getPrompt(documentContents, attributeInfo, allowedComparators, allowedOperators, examples = defaultExample) {
64
+ const myAllowedComparators = allowedComparators ?? Object.values(Comparators);
65
+ const myAllowedOperators = allowedOperators ?? Object.values(Operators);
66
+ const attributeJSON = formatAttributeInfo(attributeInfo);
67
+ const schema = interpolateFString(DEFAULT_SCHEMA, {
68
+ allowed_comparators: myAllowedComparators.join(" | "),
69
+ allowed_operators: myAllowedOperators.join(" | "),
70
+ });
71
+ const prefix = interpolateFString(DEFAULT_PREFIX, {
72
+ schema,
73
+ });
74
+ const suffix = interpolateFString(DEFAULT_SUFFIX, {
75
+ i: examples.length + 1,
76
+ content: documentContents,
77
+ attributes: attributeJSON,
78
+ });
79
+ const outputParser = StructuredQueryOutputParser.fromComponents(allowedComparators, allowedOperators);
80
+ return new FewShotPromptTemplate({
81
+ examples,
82
+ examplePrompt: EXAMPLE_PROMPT,
83
+ inputVariables: ["query"],
84
+ suffix,
85
+ prefix,
86
+ outputParser,
87
+ });
88
+ }
89
+ export function loadQueryContstructorChain(opts) {
90
+ const prompt = _getPrompt(opts.documentContents, opts.attributeInfo, opts.allowedComparators, opts.allowedOperators, opts.examples);
91
+ return new LLMChain({
92
+ llm: opts.llm,
93
+ prompt,
94
+ });
95
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StructuredQuery = exports.Operation = exports.Comparison = exports.FilterDirective = exports.Expression = exports.Visitor = exports.Comparators = exports.Operators = void 0;
4
+ exports.Operators = {
5
+ and: "and",
6
+ or: "or",
7
+ not: "not",
8
+ };
9
+ exports.Comparators = {
10
+ eq: "eq",
11
+ lt: "lt",
12
+ gt: "gt",
13
+ lte: "lte",
14
+ gte: "gte",
15
+ };
16
+ class Visitor {
17
+ }
18
+ exports.Visitor = Visitor;
19
+ class Expression {
20
+ accept(visitor) {
21
+ if (this.exprName === "Operation") {
22
+ return visitor.visitOperation(this);
23
+ }
24
+ else if (this.exprName === "Comparison") {
25
+ return visitor.visitComparison(this);
26
+ }
27
+ else if (this.exprName === "StructuredQuery") {
28
+ return visitor.visitStructuredQuery(this);
29
+ }
30
+ else {
31
+ throw new Error("Unknown Expression type");
32
+ }
33
+ }
34
+ }
35
+ exports.Expression = Expression;
36
+ class FilterDirective extends Expression {
37
+ }
38
+ exports.FilterDirective = FilterDirective;
39
+ class Comparison extends FilterDirective {
40
+ constructor(comparator, attribute, value) {
41
+ super();
42
+ Object.defineProperty(this, "comparator", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: comparator
47
+ });
48
+ Object.defineProperty(this, "attribute", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: attribute
53
+ });
54
+ Object.defineProperty(this, "value", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: value
59
+ });
60
+ Object.defineProperty(this, "exprName", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: "Comparison"
65
+ });
66
+ }
67
+ }
68
+ exports.Comparison = Comparison;
69
+ class Operation extends FilterDirective {
70
+ constructor(operator, args) {
71
+ super();
72
+ Object.defineProperty(this, "operator", {
73
+ enumerable: true,
74
+ configurable: true,
75
+ writable: true,
76
+ value: operator
77
+ });
78
+ Object.defineProperty(this, "args", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: args
83
+ });
84
+ Object.defineProperty(this, "exprName", {
85
+ enumerable: true,
86
+ configurable: true,
87
+ writable: true,
88
+ value: "Operation"
89
+ });
90
+ }
91
+ }
92
+ exports.Operation = Operation;
93
+ class StructuredQuery extends Expression {
94
+ constructor(query, filter) {
95
+ super();
96
+ Object.defineProperty(this, "query", {
97
+ enumerable: true,
98
+ configurable: true,
99
+ writable: true,
100
+ value: query
101
+ });
102
+ Object.defineProperty(this, "filter", {
103
+ enumerable: true,
104
+ configurable: true,
105
+ writable: true,
106
+ value: filter
107
+ });
108
+ Object.defineProperty(this, "exprName", {
109
+ enumerable: true,
110
+ configurable: true,
111
+ writable: true,
112
+ value: "StructuredQuery"
113
+ });
114
+ }
115
+ }
116
+ exports.StructuredQuery = StructuredQuery;
@@ -0,0 +1,60 @@
1
+ export type AND = "and";
2
+ export type OR = "or";
3
+ export type NOT = "not";
4
+ export type Operator = AND | OR | NOT;
5
+ export type EQ = "eq";
6
+ export type LT = "lt";
7
+ export type GT = "gt";
8
+ export type LTE = "lte";
9
+ export type GTE = "gte";
10
+ export type Comparator = EQ | LT | GT | LTE | GTE;
11
+ export declare const Operators: {
12
+ [key: string]: Operator;
13
+ };
14
+ export declare const Comparators: {
15
+ [key: string]: Comparator;
16
+ };
17
+ export type VisitorResult = VisitorOperationResult | VisitorComparisonResult | VisitorStructuredQueryResult;
18
+ export type VisitorOperationResult = {
19
+ [operator: string]: VisitorResult[];
20
+ };
21
+ export type VisitorComparisonResult = {
22
+ [attr: string]: {
23
+ [comparator: string]: string | number;
24
+ };
25
+ };
26
+ export type VisitorStructuredQueryResult = {
27
+ filter?: VisitorStructuredQueryResult | VisitorComparisonResult | VisitorOperationResult;
28
+ };
29
+ export declare abstract class Visitor {
30
+ abstract allowedOperators: Operator[];
31
+ abstract allowedComparators: Comparator[];
32
+ abstract visitOperation(operation: Operation): VisitorOperationResult;
33
+ abstract visitComparison(comparison: Comparison): VisitorComparisonResult;
34
+ abstract visitStructuredQuery(structuredQuery: StructuredQuery): VisitorStructuredQueryResult;
35
+ }
36
+ export declare abstract class Expression {
37
+ abstract exprName: "Operation" | "Comparison" | "StructuredQuery";
38
+ accept(visitor: Visitor): VisitorOperationResult | VisitorComparisonResult | VisitorStructuredQueryResult;
39
+ }
40
+ export declare abstract class FilterDirective extends Expression {
41
+ }
42
+ export declare class Comparison extends FilterDirective {
43
+ comparator: Comparator;
44
+ attribute: string;
45
+ value: string | number;
46
+ exprName: "Comparison";
47
+ constructor(comparator: Comparator, attribute: string, value: string | number);
48
+ }
49
+ export declare class Operation extends FilterDirective {
50
+ operator: Operator;
51
+ args?: FilterDirective[] | undefined;
52
+ exprName: "Operation";
53
+ constructor(operator: Operator, args?: FilterDirective[] | undefined);
54
+ }
55
+ export declare class StructuredQuery extends Expression {
56
+ query: string;
57
+ filter?: FilterDirective | undefined;
58
+ exprName: "StructuredQuery";
59
+ constructor(query: string, filter?: FilterDirective | undefined);
60
+ }
@@ -0,0 +1,107 @@
1
+ export const Operators = {
2
+ and: "and",
3
+ or: "or",
4
+ not: "not",
5
+ };
6
+ export const Comparators = {
7
+ eq: "eq",
8
+ lt: "lt",
9
+ gt: "gt",
10
+ lte: "lte",
11
+ gte: "gte",
12
+ };
13
+ export class Visitor {
14
+ }
15
+ export class Expression {
16
+ accept(visitor) {
17
+ if (this.exprName === "Operation") {
18
+ return visitor.visitOperation(this);
19
+ }
20
+ else if (this.exprName === "Comparison") {
21
+ return visitor.visitComparison(this);
22
+ }
23
+ else if (this.exprName === "StructuredQuery") {
24
+ return visitor.visitStructuredQuery(this);
25
+ }
26
+ else {
27
+ throw new Error("Unknown Expression type");
28
+ }
29
+ }
30
+ }
31
+ export class FilterDirective extends Expression {
32
+ }
33
+ export class Comparison extends FilterDirective {
34
+ constructor(comparator, attribute, value) {
35
+ super();
36
+ Object.defineProperty(this, "comparator", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: comparator
41
+ });
42
+ Object.defineProperty(this, "attribute", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: attribute
47
+ });
48
+ Object.defineProperty(this, "value", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: value
53
+ });
54
+ Object.defineProperty(this, "exprName", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: "Comparison"
59
+ });
60
+ }
61
+ }
62
+ export class Operation extends FilterDirective {
63
+ constructor(operator, args) {
64
+ super();
65
+ Object.defineProperty(this, "operator", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: operator
70
+ });
71
+ Object.defineProperty(this, "args", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: args
76
+ });
77
+ Object.defineProperty(this, "exprName", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: "Operation"
82
+ });
83
+ }
84
+ }
85
+ export class StructuredQuery extends Expression {
86
+ constructor(query, filter) {
87
+ super();
88
+ Object.defineProperty(this, "query", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: query
93
+ });
94
+ Object.defineProperty(this, "filter", {
95
+ enumerable: true,
96
+ configurable: true,
97
+ writable: true,
98
+ value: filter
99
+ });
100
+ Object.defineProperty(this, "exprName", {
101
+ enumerable: true,
102
+ configurable: true,
103
+ writable: true,
104
+ value: "StructuredQuery"
105
+ });
106
+ }
107
+ }