langchain 0.0.168 → 0.0.170

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 (107) hide show
  1. package/README.md +2 -2
  2. package/agents/format_scratchpad.cjs +1 -0
  3. package/agents/format_scratchpad.d.ts +1 -0
  4. package/agents/format_scratchpad.js +1 -0
  5. package/chat_models/yandex.cjs +1 -0
  6. package/chat_models/yandex.d.ts +1 -0
  7. package/chat_models/yandex.js +1 -0
  8. package/dist/agents/agent.cjs +49 -1
  9. package/dist/agents/agent.d.ts +19 -1
  10. package/dist/agents/agent.js +47 -0
  11. package/dist/agents/executor.cjs +10 -1
  12. package/dist/agents/executor.d.ts +22 -8
  13. package/dist/agents/executor.js +11 -2
  14. package/dist/agents/format_scratchpad.cjs +25 -0
  15. package/dist/agents/format_scratchpad.d.ts +10 -0
  16. package/dist/agents/format_scratchpad.js +21 -0
  17. package/dist/agents/toolkits/aws_sfn.d.ts +4 -1
  18. package/dist/agents/toolkits/conversational_retrieval/openai_functions.d.ts +1 -1
  19. package/dist/agents/toolkits/json/json.d.ts +4 -1
  20. package/dist/agents/toolkits/openapi/openapi.cjs +8 -0
  21. package/dist/agents/toolkits/openapi/openapi.d.ts +12 -1
  22. package/dist/agents/toolkits/openapi/openapi.js +8 -0
  23. package/dist/agents/toolkits/sql/sql.d.ts +4 -1
  24. package/dist/agents/toolkits/vectorstore/vectorstore.d.ts +8 -2
  25. package/dist/agents/types.d.ts +13 -1
  26. package/dist/callbacks/handlers/llmonitor.cjs +21 -17
  27. package/dist/callbacks/handlers/llmonitor.js +21 -17
  28. package/dist/chains/sql_db/sql_db_chain.cjs +9 -0
  29. package/dist/chains/sql_db/sql_db_chain.d.ts +9 -0
  30. package/dist/chains/sql_db/sql_db_chain.js +9 -0
  31. package/dist/chat_models/baiduwenxin.cjs +12 -1
  32. package/dist/chat_models/baiduwenxin.d.ts +3 -1
  33. package/dist/chat_models/baiduwenxin.js +12 -1
  34. package/dist/chat_models/cloudflare_workersai.cjs +7 -2
  35. package/dist/chat_models/cloudflare_workersai.d.ts +1 -1
  36. package/dist/chat_models/cloudflare_workersai.js +7 -2
  37. package/dist/chat_models/yandex.cjs +117 -0
  38. package/dist/chat_models/yandex.d.ts +16 -0
  39. package/dist/chat_models/yandex.js +113 -0
  40. package/dist/document_loaders/web/assemblyai.cjs +63 -114
  41. package/dist/document_loaders/web/assemblyai.d.ts +38 -57
  42. package/dist/document_loaders/web/assemblyai.js +63 -100
  43. package/dist/evaluation/comparison/prompt.d.ts +2 -2
  44. package/dist/experimental/chains/violation_of_expectations/index.cjs +5 -0
  45. package/dist/experimental/chains/violation_of_expectations/index.d.ts +1 -0
  46. package/dist/experimental/chains/violation_of_expectations/index.js +1 -0
  47. package/dist/experimental/chains/violation_of_expectations/types.cjs +49 -0
  48. package/dist/experimental/chains/violation_of_expectations/types.d.ts +69 -0
  49. package/dist/experimental/chains/violation_of_expectations/types.js +46 -0
  50. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.cjs +328 -0
  51. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.d.ts +148 -0
  52. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.js +324 -0
  53. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.cjs +49 -0
  54. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.d.ts +5 -0
  55. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.js +46 -0
  56. package/dist/llms/cloudflare_workersai.cjs +14 -7
  57. package/dist/llms/cloudflare_workersai.d.ts +1 -1
  58. package/dist/llms/cloudflare_workersai.js +14 -7
  59. package/dist/load/import_constants.cjs +1 -0
  60. package/dist/load/import_constants.js +1 -0
  61. package/dist/load/import_map.cjs +5 -2
  62. package/dist/load/import_map.d.ts +3 -0
  63. package/dist/load/import_map.js +3 -0
  64. package/dist/memory/index.d.ts +1 -1
  65. package/dist/memory/index.js +1 -1
  66. package/dist/retrievers/time_weighted.cjs +1 -1
  67. package/dist/retrievers/time_weighted.d.ts +1 -1
  68. package/dist/retrievers/time_weighted.js +1 -1
  69. package/dist/retrievers/zep.cjs +29 -3
  70. package/dist/retrievers/zep.d.ts +14 -0
  71. package/dist/retrievers/zep.js +29 -3
  72. package/dist/schema/runnable/base.cjs +4 -1
  73. package/dist/schema/runnable/base.d.ts +1 -0
  74. package/dist/schema/runnable/base.js +4 -1
  75. package/dist/schema/runnable/passthrough.cjs +33 -1
  76. package/dist/schema/runnable/passthrough.d.ts +11 -1
  77. package/dist/schema/runnable/passthrough.js +32 -1
  78. package/dist/sql_db.cjs +12 -0
  79. package/dist/sql_db.d.ts +12 -0
  80. package/dist/sql_db.js +12 -0
  81. package/dist/storage/ioredis.cjs +2 -1
  82. package/dist/storage/ioredis.js +2 -1
  83. package/dist/storage/upstash_redis.cjs +155 -0
  84. package/dist/storage/upstash_redis.d.ts +59 -0
  85. package/dist/storage/upstash_redis.js +151 -0
  86. package/dist/storage/vercel_kv.cjs +2 -1
  87. package/dist/storage/vercel_kv.js +2 -1
  88. package/dist/types/assemblyai-types.cjs +0 -150
  89. package/dist/types/assemblyai-types.d.ts +4 -670
  90. package/dist/types/assemblyai-types.js +1 -149
  91. package/dist/vectorstores/faiss.cjs +38 -6
  92. package/dist/vectorstores/faiss.d.ts +14 -2
  93. package/dist/vectorstores/faiss.js +38 -6
  94. package/dist/vectorstores/pgvector.cjs +1 -1
  95. package/dist/vectorstores/pgvector.js +1 -1
  96. package/dist/vectorstores/weaviate.cjs +13 -2
  97. package/dist/vectorstores/weaviate.js +13 -2
  98. package/experimental/chains/violation_of_expectations.cjs +1 -0
  99. package/experimental/chains/violation_of_expectations.d.ts +1 -0
  100. package/experimental/chains/violation_of_expectations.js +1 -0
  101. package/package.json +47 -10
  102. package/storage/upstash_redis.cjs +1 -0
  103. package/storage/upstash_redis.d.ts +1 -0
  104. package/storage/upstash_redis.js +1 -0
  105. package/dist/util/assemblyai-client.cjs +0 -173
  106. package/dist/util/assemblyai-client.d.ts +0 -63
  107. package/dist/util/assemblyai-client.js +0 -170
@@ -6,7 +6,7 @@ exports.LAST_ACCESSED_AT_KEY = "last_accessed_at";
6
6
  exports.BUFFER_IDX = "buffer_idx";
7
7
  /**
8
8
  * TimeWeightedVectorStoreRetriever retrieves documents based on their time-weighted relevance.
9
- * ref: https://github.com/hwchase17/langchain/blob/master/langchain/retrievers/time_weighted_retriever.py
9
+ * ref: https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/retrievers/time_weighted_retriever.py
10
10
  */
11
11
  class TimeWeightedVectorStoreRetriever extends retriever_js_1.BaseRetriever {
12
12
  static lc_name() {
@@ -19,7 +19,7 @@ export declare const LAST_ACCESSED_AT_KEY = "last_accessed_at";
19
19
  export declare const BUFFER_IDX = "buffer_idx";
20
20
  /**
21
21
  * TimeWeightedVectorStoreRetriever retrieves documents based on their time-weighted relevance.
22
- * ref: https://github.com/hwchase17/langchain/blob/master/langchain/retrievers/time_weighted_retriever.py
22
+ * ref: https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/retrievers/time_weighted_retriever.py
23
23
  */
24
24
  export declare class TimeWeightedVectorStoreRetriever extends BaseRetriever {
25
25
  static lc_name(): string;
@@ -3,7 +3,7 @@ export const LAST_ACCESSED_AT_KEY = "last_accessed_at";
3
3
  export const BUFFER_IDX = "buffer_idx";
4
4
  /**
5
5
  * TimeWeightedVectorStoreRetriever retrieves documents based on their time-weighted relevance.
6
- * ref: https://github.com/hwchase17/langchain/blob/master/langchain/retrievers/time_weighted_retriever.py
6
+ * ref: https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/retrievers/time_weighted_retriever.py
7
7
  */
8
8
  export class TimeWeightedVectorStoreRetriever extends BaseRetriever {
9
9
  static lc_name() {
@@ -47,8 +47,29 @@ class ZepRetriever extends retriever_js_1.BaseRetriever {
47
47
  writable: true,
48
48
  value: void 0
49
49
  });
50
+ Object.defineProperty(this, "searchType", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ Object.defineProperty(this, "mmrLambda", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: void 0
61
+ });
62
+ Object.defineProperty(this, "filter", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
50
68
  this.sessionId = config.sessionId;
51
69
  this.topK = config.topK;
70
+ this.searchType = config.searchType;
71
+ this.mmrLambda = config.mmrLambda;
72
+ this.filter = config.filter;
52
73
  this.zepClientPromise = zep_js_1.ZepClient.init(config.url, config.apiKey);
53
74
  }
54
75
  /**
@@ -59,9 +80,9 @@ class ZepRetriever extends retriever_js_1.BaseRetriever {
59
80
  searchResultToDoc(results) {
60
81
  return results
61
82
  .filter((r) => r.message)
62
- .map(({ message: { content } = {}, ...metadata }, dist) => new document_js_1.Document({
83
+ .map(({ message: { content, metadata: messageMetadata } = {}, dist, ...rest }) => new document_js_1.Document({
63
84
  pageContent: content ?? "",
64
- metadata: { score: dist, ...metadata },
85
+ metadata: { score: dist, ...messageMetadata, ...rest },
65
86
  }));
66
87
  }
67
88
  /**
@@ -70,7 +91,12 @@ class ZepRetriever extends retriever_js_1.BaseRetriever {
70
91
  * @returns {Promise<Document[]>} A promise that resolves to an array of relevant Document objects.
71
92
  */
72
93
  async _getRelevantDocuments(query) {
73
- const payload = { text: query, metadata: {} };
94
+ const payload = {
95
+ text: query,
96
+ metadata: this.filter,
97
+ search_type: this.searchType,
98
+ mmr_lambda: this.mmrLambda,
99
+ };
74
100
  // Wait for ZepClient to be initialized
75
101
  const zepClient = await this.zepClientPromise;
76
102
  if (!zepClient) {
@@ -4,12 +4,23 @@ import { Document } from "../document.js";
4
4
  /**
5
5
  * Configuration interface for the ZepRetriever class. Extends the
6
6
  * BaseRetrieverInput interface.
7
+ *
8
+ * @argument {string} sessionId - The ID of the Zep session.
9
+ * @argument {string} url - The URL of the Zep API.
10
+ * @argument {number} [topK] - The number of results to return.
11
+ * @argument {string} [apiKey] - The API key for the Zep API.
12
+ * @argument [searchType] [searchType] - The type of search to perform: "similarity" or "mmr".
13
+ * @argument {number} [mmrLambda] - The lambda value for the MMR search.
14
+ * @argument {Record<string, unknown>} [filter] - The metadata filter to apply to the search.
7
15
  */
8
16
  export interface ZepRetrieverConfig extends BaseRetrieverInput {
9
17
  sessionId: string;
10
18
  url: string;
11
19
  topK?: number;
12
20
  apiKey?: string;
21
+ searchType?: "similarity" | "mmr";
22
+ mmrLambda?: number;
23
+ filter?: Record<string, unknown>;
13
24
  }
14
25
  /**
15
26
  * Class for retrieving information from a Zep long-term memory store.
@@ -27,6 +38,9 @@ export declare class ZepRetriever extends BaseRetriever {
27
38
  zepClientPromise: Promise<ZepClient>;
28
39
  private sessionId;
29
40
  private topK?;
41
+ private searchType?;
42
+ private mmrLambda?;
43
+ private filter?;
30
44
  constructor(config: ZepRetrieverConfig);
31
45
  /**
32
46
  * Converts an array of search results to an array of Document objects.
@@ -44,8 +44,29 @@ export class ZepRetriever extends BaseRetriever {
44
44
  writable: true,
45
45
  value: void 0
46
46
  });
47
+ Object.defineProperty(this, "searchType", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "mmrLambda", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "filter", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
47
65
  this.sessionId = config.sessionId;
48
66
  this.topK = config.topK;
67
+ this.searchType = config.searchType;
68
+ this.mmrLambda = config.mmrLambda;
69
+ this.filter = config.filter;
49
70
  this.zepClientPromise = ZepClient.init(config.url, config.apiKey);
50
71
  }
51
72
  /**
@@ -56,9 +77,9 @@ export class ZepRetriever extends BaseRetriever {
56
77
  searchResultToDoc(results) {
57
78
  return results
58
79
  .filter((r) => r.message)
59
- .map(({ message: { content } = {}, ...metadata }, dist) => new Document({
80
+ .map(({ message: { content, metadata: messageMetadata } = {}, dist, ...rest }) => new Document({
60
81
  pageContent: content ?? "",
61
- metadata: { score: dist, ...metadata },
82
+ metadata: { score: dist, ...messageMetadata, ...rest },
62
83
  }));
63
84
  }
64
85
  /**
@@ -67,7 +88,12 @@ export class ZepRetriever extends BaseRetriever {
67
88
  * @returns {Promise<Document[]>} A promise that resolves to an array of relevant Document objects.
68
89
  */
69
90
  async _getRelevantDocuments(query) {
70
- const payload = { text: query, metadata: {} };
91
+ const payload = {
92
+ text: query,
93
+ metadata: this.filter,
94
+ search_type: this.searchType,
95
+ mmr_lambda: this.mmrLambda,
96
+ };
71
97
  // Wait for ZepClient to be initialized
72
98
  const zepClient = await this.zepClientPromise;
73
99
  if (!zepClient) {
@@ -366,7 +366,7 @@ class Runnable extends serializable_js_1.Serializable {
366
366
  }
367
367
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
368
368
  static isRunnable(thing) {
369
- return thing.lc_runnable;
369
+ return thing ? thing.lc_runnable : false;
370
370
  }
371
371
  }
372
372
  exports.Runnable = Runnable;
@@ -844,6 +844,9 @@ class RunnableMap extends Runnable {
844
844
  static lc_name() {
845
845
  return "RunnableMap";
846
846
  }
847
+ getStepsKeys() {
848
+ return Object.keys(this.steps);
849
+ }
847
850
  constructor(fields) {
848
851
  super(fields);
849
852
  Object.defineProperty(this, "lc_namespace", {
@@ -289,6 +289,7 @@ export declare class RunnableMap<RunInput> extends Runnable<RunInput, Record<str
289
289
  lc_namespace: string[];
290
290
  lc_serializable: boolean;
291
291
  protected steps: Record<string, Runnable<RunInput>>;
292
+ getStepsKeys(): string[];
292
293
  constructor(fields: {
293
294
  steps: Record<string, RunnableLike<RunInput>>;
294
295
  });
@@ -360,7 +360,7 @@ export class Runnable extends Serializable {
360
360
  }
361
361
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
362
362
  static isRunnable(thing) {
363
- return thing.lc_runnable;
363
+ return thing ? thing.lc_runnable : false;
364
364
  }
365
365
  }
366
366
  /**
@@ -833,6 +833,9 @@ export class RunnableMap extends Runnable {
833
833
  static lc_name() {
834
834
  return "RunnableMap";
835
835
  }
836
+ getStepsKeys() {
837
+ return Object.keys(this.steps);
838
+ }
836
839
  constructor(fields) {
837
840
  super(fields);
838
841
  Object.defineProperty(this, "lc_namespace", {
@@ -1,7 +1,36 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RunnablePassthrough = void 0;
3
+ exports.RunnablePassthrough = exports.RunnableAssign = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
+ /**
6
+ * A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
7
+ */
8
+ class RunnableAssign extends base_js_1.Runnable {
9
+ constructor(mapper) {
10
+ super();
11
+ Object.defineProperty(this, "lc_namespace", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: ["langchain", "schema", "runnable"]
16
+ });
17
+ Object.defineProperty(this, "mapper", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ this.mapper = mapper;
24
+ }
25
+ async invoke(input, options) {
26
+ const mapperResult = await this.mapper.invoke(input, options);
27
+ return {
28
+ ...input,
29
+ ...mapperResult,
30
+ };
31
+ }
32
+ }
33
+ exports.RunnableAssign = RunnableAssign;
5
34
  /**
6
35
  * A runnable that passes through the input.
7
36
  */
@@ -27,5 +56,8 @@ class RunnablePassthrough extends base_js_1.Runnable {
27
56
  async invoke(input, options) {
28
57
  return this._callWithConfig((input) => Promise.resolve(input), input, options);
29
58
  }
59
+ static assign(mapping) {
60
+ return new RunnableAssign(new base_js_1.RunnableMap({ steps: mapping }));
61
+ }
30
62
  }
31
63
  exports.RunnablePassthrough = RunnablePassthrough;
@@ -1,5 +1,14 @@
1
- import { Runnable } from "./base.js";
1
+ import { Runnable, RunnableLike, RunnableMap } from "./base.js";
2
2
  import type { RunnableConfig } from "./config.js";
3
+ /**
4
+ * A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
5
+ */
6
+ export declare class RunnableAssign<RunInput extends Record<string, any> = any, RunOutput extends Record<string, any> = any, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput> {
7
+ lc_namespace: string[];
8
+ mapper: RunnableMap<RunInput>;
9
+ constructor(mapper: RunnableMap<RunInput>);
10
+ invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
11
+ }
3
12
  /**
4
13
  * A runnable that passes through the input.
5
14
  */
@@ -8,4 +17,5 @@ export declare class RunnablePassthrough<RunInput> extends Runnable<RunInput, Ru
8
17
  lc_namespace: string[];
9
18
  lc_serializable: boolean;
10
19
  invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunInput>;
20
+ static assign(mapping: Record<string, RunnableLike<Record<string, unknown>, any>>): RunnableAssign<Record<string, unknown>, Record<string, unknown>>;
11
21
  }
@@ -1,4 +1,32 @@
1
- import { Runnable } from "./base.js";
1
+ import { Runnable, RunnableMap } from "./base.js";
2
+ /**
3
+ * A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
4
+ */
5
+ export class RunnableAssign extends Runnable {
6
+ constructor(mapper) {
7
+ super();
8
+ Object.defineProperty(this, "lc_namespace", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: ["langchain", "schema", "runnable"]
13
+ });
14
+ Object.defineProperty(this, "mapper", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ this.mapper = mapper;
21
+ }
22
+ async invoke(input, options) {
23
+ const mapperResult = await this.mapper.invoke(input, options);
24
+ return {
25
+ ...input,
26
+ ...mapperResult,
27
+ };
28
+ }
29
+ }
2
30
  /**
3
31
  * A runnable that passes through the input.
4
32
  */
@@ -24,4 +52,7 @@ export class RunnablePassthrough extends Runnable {
24
52
  async invoke(input, options) {
25
53
  return this._callWithConfig((input) => Promise.resolve(input), input, options);
26
54
  }
55
+ static assign(mapping) {
56
+ return new RunnableAssign(new RunnableMap({ steps: mapping }));
57
+ }
27
58
  }
package/dist/sql_db.cjs CHANGED
@@ -3,6 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SqlDatabase = void 0;
4
4
  const sql_utils_js_1 = require("./util/sql_utils.cjs");
5
5
  const serializable_js_1 = require("./load/serializable.cjs");
6
+ /**
7
+ * Class that represents a SQL database in the LangChain framework.
8
+ *
9
+ * @security **Security Notice**
10
+ * This class generates SQL queries for the given database.
11
+ * The SQLDatabase class provides a getTableInfo method that can be used
12
+ * to get column information as well as sample data from the table.
13
+ * To mitigate risk of leaking sensitive data, limit permissions
14
+ * to read and scope to the tables that are needed.
15
+ * Optionally, use the includesTables or ignoreTables class parameters
16
+ * to limit which tables can/cannot be accessed.
17
+ */
6
18
  class SqlDatabase extends serializable_js_1.Serializable {
7
19
  toJSON() {
8
20
  return this.toJSONNotImplemented();
package/dist/sql_db.d.ts CHANGED
@@ -2,6 +2,18 @@ import type { DataSource as DataSourceT, DataSourceOptions } from "typeorm";
2
2
  import { SerializedSqlDatabase, SqlDatabaseDataSourceParams, SqlDatabaseOptionsParams, SqlTable } from "./util/sql_utils.js";
3
3
  import { Serializable } from "./load/serializable.js";
4
4
  export type { SqlDatabaseDataSourceParams, SqlDatabaseOptionsParams };
5
+ /**
6
+ * Class that represents a SQL database in the LangChain framework.
7
+ *
8
+ * @security **Security Notice**
9
+ * This class generates SQL queries for the given database.
10
+ * The SQLDatabase class provides a getTableInfo method that can be used
11
+ * to get column information as well as sample data from the table.
12
+ * To mitigate risk of leaking sensitive data, limit permissions
13
+ * to read and scope to the tables that are needed.
14
+ * Optionally, use the includesTables or ignoreTables class parameters
15
+ * to limit which tables can/cannot be accessed.
16
+ */
5
17
  export declare class SqlDatabase extends Serializable implements SqlDatabaseOptionsParams, SqlDatabaseDataSourceParams {
6
18
  lc_namespace: string[];
7
19
  toJSON(): import("./load/serializable.js").SerializedNotImplemented;
package/dist/sql_db.js CHANGED
@@ -1,5 +1,17 @@
1
1
  import { generateTableInfoFromTables, getTableAndColumnsName, verifyIgnoreTablesExistInDatabase, verifyIncludeTablesExistInDatabase, verifyListTablesExistInDatabase, } from "./util/sql_utils.js";
2
2
  import { Serializable } from "./load/serializable.js";
3
+ /**
4
+ * Class that represents a SQL database in the LangChain framework.
5
+ *
6
+ * @security **Security Notice**
7
+ * This class generates SQL queries for the given database.
8
+ * The SQLDatabase class provides a getTableInfo method that can be used
9
+ * to get column information as well as sample data from the table.
10
+ * To mitigate risk of leaking sensitive data, limit permissions
11
+ * to read and scope to the tables that are needed.
12
+ * Optionally, use the includesTables or ignoreTables class parameters
13
+ * to limit which tables can/cannot be accessed.
14
+ */
3
15
  export class SqlDatabase extends Serializable {
4
16
  toJSON() {
5
17
  return this.toJSONNotImplemented();
@@ -115,7 +115,8 @@ class RedisByteStore extends storage_js_1.BaseStore {
115
115
  async *yieldKeys(prefix) {
116
116
  let pattern;
117
117
  if (prefix) {
118
- pattern = this._getPrefixedKey(prefix);
118
+ const wildcardPrefix = prefix.endsWith("*") ? prefix : `${prefix}*`;
119
+ pattern = this._getPrefixedKey(wildcardPrefix);
119
120
  }
120
121
  else {
121
122
  pattern = this._getPrefixedKey("*");
@@ -112,7 +112,8 @@ export class RedisByteStore extends BaseStore {
112
112
  async *yieldKeys(prefix) {
113
113
  let pattern;
114
114
  if (prefix) {
115
- pattern = this._getPrefixedKey(prefix);
115
+ const wildcardPrefix = prefix.endsWith("*") ? prefix : `${prefix}*`;
116
+ pattern = this._getPrefixedKey(wildcardPrefix);
116
117
  }
117
118
  else {
118
119
  pattern = this._getPrefixedKey("*");
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UpstashRedisStore = void 0;
4
+ const redis_1 = require("@upstash/redis");
5
+ const storage_js_1 = require("../schema/storage.cjs");
6
+ /**
7
+ * Class that extends the BaseStore class to interact with an Upstash Redis
8
+ * database. It provides methods for getting, setting, and deleting data,
9
+ * as well as yielding keys from the database.
10
+ */
11
+ class UpstashRedisStore extends storage_js_1.BaseStore {
12
+ constructor(fields) {
13
+ super(fields);
14
+ Object.defineProperty(this, "lc_namespace", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: ["langchain", "storage"]
19
+ });
20
+ Object.defineProperty(this, "client", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "namespace", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "yieldKeysScanBatchSize", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: 1000
37
+ });
38
+ Object.defineProperty(this, "sessionTTL", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ if (fields.client) {
45
+ this.client = fields.client;
46
+ }
47
+ else if (fields.config) {
48
+ this.client = new redis_1.Redis(fields.config);
49
+ }
50
+ else {
51
+ throw new Error(`Upstash Redis store requires either a config object or a pre-configured client.`);
52
+ }
53
+ this.sessionTTL = fields.sessionTTL;
54
+ this.yieldKeysScanBatchSize =
55
+ fields.yieldKeysScanBatchSize ?? this.yieldKeysScanBatchSize;
56
+ this.namespace = fields.namespace;
57
+ }
58
+ _getPrefixedKey(key) {
59
+ if (this.namespace) {
60
+ const delimiter = "/";
61
+ return `${this.namespace}${delimiter}${key}`;
62
+ }
63
+ return key;
64
+ }
65
+ _getDeprefixedKey(key) {
66
+ if (this.namespace) {
67
+ const delimiter = "/";
68
+ return key.slice(this.namespace.length + delimiter.length);
69
+ }
70
+ return key;
71
+ }
72
+ /**
73
+ * Gets multiple keys from the Upstash Redis database.
74
+ * @param keys Array of keys to be retrieved.
75
+ * @returns An array of retrieved values.
76
+ */
77
+ async mget(keys) {
78
+ const encoder = new TextEncoder();
79
+ const prefixedKeys = keys.map(this._getPrefixedKey.bind(this));
80
+ const retrievedValues = await this.client.mget(...prefixedKeys);
81
+ return retrievedValues.map((value) => {
82
+ if (!value) {
83
+ return undefined;
84
+ }
85
+ else if (typeof value === "object") {
86
+ return encoder.encode(JSON.stringify(value));
87
+ }
88
+ else {
89
+ return encoder.encode(value);
90
+ }
91
+ });
92
+ }
93
+ /**
94
+ * Sets multiple keys in the Upstash Redis database.
95
+ * @param keyValuePairs Array of key-value pairs to be set.
96
+ * @returns Promise that resolves when all keys have been set.
97
+ */
98
+ async mset(keyValuePairs) {
99
+ const decoder = new TextDecoder();
100
+ const encodedKeyValuePairs = keyValuePairs.map(([key, value]) => [
101
+ this._getPrefixedKey(key),
102
+ decoder.decode(value),
103
+ ]);
104
+ const pipeline = this.client.pipeline();
105
+ for (const [key, value] of encodedKeyValuePairs) {
106
+ if (this.sessionTTL) {
107
+ pipeline.setex(key, this.sessionTTL, value);
108
+ }
109
+ else {
110
+ pipeline.set(key, value);
111
+ }
112
+ }
113
+ await pipeline.exec();
114
+ }
115
+ /**
116
+ * Deletes multiple keys from the Upstash Redis database.
117
+ * @param keys Array of keys to be deleted.
118
+ * @returns Promise that resolves when all keys have been deleted.
119
+ */
120
+ async mdelete(keys) {
121
+ await this.client.del(...keys.map(this._getPrefixedKey.bind(this)));
122
+ }
123
+ /**
124
+ * Yields keys from the Upstash Redis database.
125
+ * @param prefix Optional prefix to filter the keys. A wildcard (*) is always appended to the end.
126
+ * @returns An AsyncGenerator that yields keys from the Upstash Redis database.
127
+ */
128
+ async *yieldKeys(prefix) {
129
+ let pattern;
130
+ if (prefix) {
131
+ const wildcardPrefix = prefix.endsWith("*") ? prefix : `${prefix}*`;
132
+ pattern = `${this._getPrefixedKey(wildcardPrefix)}*`;
133
+ }
134
+ else {
135
+ pattern = this._getPrefixedKey("*");
136
+ }
137
+ let [cursor, batch] = await this.client.scan(0, {
138
+ match: pattern,
139
+ count: this.yieldKeysScanBatchSize,
140
+ });
141
+ for (const key of batch) {
142
+ yield this._getDeprefixedKey(key);
143
+ }
144
+ while (cursor !== 0) {
145
+ [cursor, batch] = await this.client.scan(cursor, {
146
+ match: pattern,
147
+ count: this.yieldKeysScanBatchSize,
148
+ });
149
+ for (const key of batch) {
150
+ yield this._getDeprefixedKey(key);
151
+ }
152
+ }
153
+ }
154
+ }
155
+ exports.UpstashRedisStore = UpstashRedisStore;
@@ -0,0 +1,59 @@
1
+ import { Redis as UpstashRedis, type RedisConfigNodejs } from "@upstash/redis";
2
+ import { BaseStore } from "../schema/storage.js";
3
+ /**
4
+ * Type definition for the input parameters required to initialize an
5
+ * instance of the UpstashStoreInput class.
6
+ */
7
+ export interface UpstashRedisStoreInput {
8
+ sessionTTL?: number;
9
+ config?: RedisConfigNodejs;
10
+ client?: UpstashRedis;
11
+ /**
12
+ * The amount of keys to retrieve per batch when yielding keys.
13
+ * @default 1000
14
+ */
15
+ yieldKeysScanBatchSize?: number;
16
+ /**
17
+ * The namespace to use for the keys in the database.
18
+ */
19
+ namespace?: string;
20
+ }
21
+ /**
22
+ * Class that extends the BaseStore class to interact with an Upstash Redis
23
+ * database. It provides methods for getting, setting, and deleting data,
24
+ * as well as yielding keys from the database.
25
+ */
26
+ export declare class UpstashRedisStore extends BaseStore<string, Uint8Array> {
27
+ lc_namespace: string[];
28
+ protected client: UpstashRedis;
29
+ protected namespace?: string;
30
+ protected yieldKeysScanBatchSize: number;
31
+ private sessionTTL?;
32
+ constructor(fields: UpstashRedisStoreInput);
33
+ _getPrefixedKey(key: string): string;
34
+ _getDeprefixedKey(key: string): string;
35
+ /**
36
+ * Gets multiple keys from the Upstash Redis database.
37
+ * @param keys Array of keys to be retrieved.
38
+ * @returns An array of retrieved values.
39
+ */
40
+ mget(keys: string[]): Promise<(Uint8Array | undefined)[]>;
41
+ /**
42
+ * Sets multiple keys in the Upstash Redis database.
43
+ * @param keyValuePairs Array of key-value pairs to be set.
44
+ * @returns Promise that resolves when all keys have been set.
45
+ */
46
+ mset(keyValuePairs: [string, Uint8Array][]): Promise<void>;
47
+ /**
48
+ * Deletes multiple keys from the Upstash Redis database.
49
+ * @param keys Array of keys to be deleted.
50
+ * @returns Promise that resolves when all keys have been deleted.
51
+ */
52
+ mdelete(keys: string[]): Promise<void>;
53
+ /**
54
+ * Yields keys from the Upstash Redis database.
55
+ * @param prefix Optional prefix to filter the keys. A wildcard (*) is always appended to the end.
56
+ * @returns An AsyncGenerator that yields keys from the Upstash Redis database.
57
+ */
58
+ yieldKeys(prefix?: string): AsyncGenerator<string>;
59
+ }