langchain 0.1.33 → 0.1.34

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.
@@ -262,12 +262,17 @@ class GithubRepoLoader extends base_js_1.BaseDocumentLoader {
262
262
  * @param gitmodulesContent the content of a .gitmodules file
263
263
  */
264
264
  async parseGitmodules(gitmodulesContent) {
265
+ let validGitmodulesContent = gitmodulesContent;
266
+ // in case the .gitmodules file does not end with a newline, we add one to make the regex work
267
+ if (!validGitmodulesContent.endsWith("\n")) {
268
+ validGitmodulesContent += "\n";
269
+ }
265
270
  // catches the initial line of submodule entries
266
271
  const submodulePattern = /\[submodule "(.*?)"]\n((\s+.*?\s*=\s*.*?\n)*)/g;
267
272
  // catches the properties of a submodule
268
273
  const keyValuePattern = /\s+(.*?)\s*=\s*(.*?)\s/g;
269
274
  const submoduleInfos = [];
270
- for (const [, name, propertyLines] of gitmodulesContent.matchAll(submodulePattern)) {
275
+ for (const [, name, propertyLines] of validGitmodulesContent.matchAll(submodulePattern)) {
271
276
  if (!name || !propertyLines) {
272
277
  throw new Error("Could not parse submodule entry");
273
278
  }
@@ -256,12 +256,17 @@ export class GithubRepoLoader extends BaseDocumentLoader {
256
256
  * @param gitmodulesContent the content of a .gitmodules file
257
257
  */
258
258
  async parseGitmodules(gitmodulesContent) {
259
+ let validGitmodulesContent = gitmodulesContent;
260
+ // in case the .gitmodules file does not end with a newline, we add one to make the regex work
261
+ if (!validGitmodulesContent.endsWith("\n")) {
262
+ validGitmodulesContent += "\n";
263
+ }
259
264
  // catches the initial line of submodule entries
260
265
  const submodulePattern = /\[submodule "(.*?)"]\n((\s+.*?\s*=\s*.*?\n)*)/g;
261
266
  // catches the properties of a submodule
262
267
  const keyValuePattern = /\s+(.*?)\s*=\s*(.*?)\s/g;
263
268
  const submoduleInfos = [];
264
- for (const [, name, propertyLines] of gitmodulesContent.matchAll(submodulePattern)) {
269
+ for (const [, name, propertyLines] of validGitmodulesContent.matchAll(submodulePattern)) {
265
270
  if (!name || !propertyLines) {
266
271
  throw new Error("Could not parse submodule entry");
267
272
  }
@@ -210,7 +210,7 @@ class OpenAIAssistantRunnable extends runnables_1.Runnable {
210
210
  const run = await this._waitForRun(runId, threadId);
211
211
  if (run.status === "completed") {
212
212
  const messages = await this.client.beta.threads.messages.list(threadId, {
213
- order: "asc",
213
+ order: "desc",
214
214
  });
215
215
  const newMessages = messages.data.filter((msg) => msg.run_id === runId);
216
216
  if (!this.asAgent) {
@@ -207,7 +207,7 @@ export class OpenAIAssistantRunnable extends Runnable {
207
207
  const run = await this._waitForRun(runId, threadId);
208
208
  if (run.status === "completed") {
209
209
  const messages = await this.client.beta.threads.messages.list(threadId, {
210
- order: "asc",
210
+ order: "desc",
211
211
  });
212
212
  const newMessages = messages.data.filter((msg) => msg.run_id === runId);
213
213
  if (!this.asAgent) {
@@ -62,10 +62,17 @@ class VectorStoreRetrieverMemory extends memory_1.BaseMemory {
62
62
  writable: true,
63
63
  value: void 0
64
64
  });
65
+ Object.defineProperty(this, "metadata", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
65
71
  this.vectorStoreRetriever = fields.vectorStoreRetriever;
66
72
  this.inputKey = fields.inputKey;
67
73
  this.memoryKey = fields.memoryKey ?? "memory";
68
74
  this.returnDocs = fields.returnDocs ?? false;
75
+ this.metadata = fields.metadata;
69
76
  }
70
77
  get memoryKeys() {
71
78
  return [this.memoryKey];
@@ -95,13 +102,16 @@ class VectorStoreRetrieverMemory extends memory_1.BaseMemory {
95
102
  * @returns A Promise that resolves to void.
96
103
  */
97
104
  async saveContext(inputValues, outputValues) {
105
+ const metadata = typeof this.metadata === "function"
106
+ ? this.metadata(inputValues, outputValues)
107
+ : this.metadata;
98
108
  const text = Object.entries(inputValues)
99
109
  .filter(([k]) => k !== this.memoryKey)
100
110
  .concat(Object.entries(outputValues))
101
111
  .map(([k, v]) => `${k}: ${v}`)
102
112
  .join("\n");
103
113
  await this.vectorStoreRetriever.addDocuments([
104
- new documents_1.Document({ pageContent: text }),
114
+ new documents_1.Document({ pageContent: text, metadata }),
105
115
  ]);
106
116
  }
107
117
  }
@@ -1,5 +1,7 @@
1
1
  import type { VectorStoreRetrieverInterface } from "@langchain/core/vectorstores";
2
2
  import { BaseMemory, InputValues, MemoryVariables, OutputValues } from "@langchain/core/memory";
3
+ type Metadata = Record<string, unknown>;
4
+ type MetadataFunction = (inputValues?: InputValues, outputValues?: OutputValues) => Metadata;
3
5
  /**
4
6
  * Interface for the parameters required to initialize a
5
7
  * VectorStoreRetrieverMemory instance.
@@ -10,6 +12,10 @@ export interface VectorStoreRetrieverMemoryParams {
10
12
  outputKey?: string;
11
13
  memoryKey?: string;
12
14
  returnDocs?: boolean;
15
+ /**
16
+ * Metadata to be added to the document when saving context.
17
+ */
18
+ metadata?: Metadata | MetadataFunction;
13
19
  }
14
20
  /**
15
21
  * Class for managing long-term memory in Large Language Model (LLM)
@@ -47,6 +53,7 @@ export declare class VectorStoreRetrieverMemory extends BaseMemory implements Ve
47
53
  inputKey?: string;
48
54
  memoryKey: string;
49
55
  returnDocs: boolean;
56
+ metadata?: Metadata | MetadataFunction;
50
57
  constructor(fields: VectorStoreRetrieverMemoryParams);
51
58
  get memoryKeys(): string[];
52
59
  /**
@@ -67,3 +74,4 @@ export declare class VectorStoreRetrieverMemory extends BaseMemory implements Ve
67
74
  */
68
75
  saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
69
76
  }
77
+ export {};
@@ -59,10 +59,17 @@ export class VectorStoreRetrieverMemory extends BaseMemory {
59
59
  writable: true,
60
60
  value: void 0
61
61
  });
62
+ Object.defineProperty(this, "metadata", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
62
68
  this.vectorStoreRetriever = fields.vectorStoreRetriever;
63
69
  this.inputKey = fields.inputKey;
64
70
  this.memoryKey = fields.memoryKey ?? "memory";
65
71
  this.returnDocs = fields.returnDocs ?? false;
72
+ this.metadata = fields.metadata;
66
73
  }
67
74
  get memoryKeys() {
68
75
  return [this.memoryKey];
@@ -92,13 +99,16 @@ export class VectorStoreRetrieverMemory extends BaseMemory {
92
99
  * @returns A Promise that resolves to void.
93
100
  */
94
101
  async saveContext(inputValues, outputValues) {
102
+ const metadata = typeof this.metadata === "function"
103
+ ? this.metadata(inputValues, outputValues)
104
+ : this.metadata;
95
105
  const text = Object.entries(inputValues)
96
106
  .filter(([k]) => k !== this.memoryKey)
97
107
  .concat(Object.entries(outputValues))
98
108
  .map(([k, v]) => `${k}: ${v}`)
99
109
  .join("\n");
100
110
  await this.vectorStoreRetriever.addDocuments([
101
- new Document({ pageContent: text }),
111
+ new Document({ pageContent: text, metadata }),
102
112
  ]);
103
113
  }
104
114
  }
@@ -14,7 +14,7 @@ Object.defineProperty(exports, "BasicTranslator", { enumerable: true, get: funct
14
14
  * implements the SelfQueryRetrieverArgs interface.
15
15
  * @example
16
16
  * ```typescript
17
- * const selfQueryRetriever = await SelfQueryRetriever.fromLLM({
17
+ * const selfQueryRetriever = SelfQueryRetriever.fromLLM({
18
18
  * llm: new ChatOpenAI(),
19
19
  * vectorStore: await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings()),
20
20
  * documentContents: "Brief summary of a movie",
@@ -30,7 +30,7 @@ export interface SelfQueryRetrieverArgs<T extends VectorStore> extends BaseRetri
30
30
  * implements the SelfQueryRetrieverArgs interface.
31
31
  * @example
32
32
  * ```typescript
33
- * const selfQueryRetriever = await SelfQueryRetriever.fromLLM({
33
+ * const selfQueryRetriever = SelfQueryRetriever.fromLLM({
34
34
  * llm: new ChatOpenAI(),
35
35
  * vectorStore: await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings()),
36
36
  * documentContents: "Brief summary of a movie",
@@ -9,7 +9,7 @@ export { BaseTranslator, BasicTranslator, FunctionalTranslator };
9
9
  * implements the SelfQueryRetrieverArgs interface.
10
10
  * @example
11
11
  * ```typescript
12
- * const selfQueryRetriever = await SelfQueryRetriever.fromLLM({
12
+ * const selfQueryRetriever = SelfQueryRetriever.fromLLM({
13
13
  * llm: new ChatOpenAI(),
14
14
  * vectorStore: await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings()),
15
15
  * documentContents: "Brief summary of a movie",
@@ -11,7 +11,7 @@ const base_js_1 = require("./base.cjs");
11
11
  * queries and compare results.
12
12
  * @example
13
13
  * ```typescript
14
- * const selfQueryRetriever = await SelfQueryRetriever.fromLLM({
14
+ * const selfQueryRetriever = SelfQueryRetriever.fromLLM({
15
15
  * llm: new ChatOpenAI(),
16
16
  * vectorStore: new PineconeStore(),
17
17
  * documentContents: "Brief summary of a movie",
@@ -8,7 +8,7 @@ import { BasicTranslator } from "./base.js";
8
8
  * queries and compare results.
9
9
  * @example
10
10
  * ```typescript
11
- * const selfQueryRetriever = await SelfQueryRetriever.fromLLM({
11
+ * const selfQueryRetriever = SelfQueryRetriever.fromLLM({
12
12
  * llm: new ChatOpenAI(),
13
13
  * vectorStore: new PineconeStore(),
14
14
  * documentContents: "Brief summary of a movie",
@@ -8,7 +8,7 @@ import { BasicTranslator } from "./base.js";
8
8
  * queries and compare results.
9
9
  * @example
10
10
  * ```typescript
11
- * const selfQueryRetriever = await SelfQueryRetriever.fromLLM({
11
+ * const selfQueryRetriever = SelfQueryRetriever.fromLLM({
12
12
  * llm: new ChatOpenAI(),
13
13
  * vectorStore: new PineconeStore(),
14
14
  * documentContents: "Brief summary of a movie",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.1.33",
3
+ "version": "0.1.34",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {