langchain 0.0.69 → 0.0.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/mrkl/prompt.cjs +1 -1
- package/dist/agents/mrkl/prompt.d.ts +1 -1
- package/dist/agents/mrkl/prompt.js +1 -1
- package/dist/callbacks/base.cjs +25 -2
- package/dist/callbacks/base.js +2 -2
- package/dist/chains/llm_chain.d.ts +7 -7
- package/dist/chains/question_answering/load.cjs +14 -40
- package/dist/chains/question_answering/load.d.ts +3 -3
- package/dist/chains/question_answering/load.js +16 -42
- package/dist/chains/sql_db/sql_db_chain.cjs +4 -0
- package/dist/chains/sql_db/sql_db_chain.d.ts +3 -1
- package/dist/chains/sql_db/sql_db_chain.js +4 -0
- package/dist/chains/sql_db/sql_db_prompt.cjs +58 -1
- package/dist/chains/sql_db/sql_db_prompt.d.ts +3 -0
- package/dist/chains/sql_db/sql_db_prompt.js +57 -0
- package/dist/chat_models/openai.cjs +89 -4
- package/dist/chat_models/openai.d.ts +18 -53
- package/dist/chat_models/openai.js +90 -5
- package/dist/document_loaders/fs/directory.d.ts +5 -6
- package/dist/document_loaders/fs/pdf.d.ts +1 -1
- package/dist/document_loaders/fs/unstructured.cjs +68 -49
- package/dist/document_loaders/fs/unstructured.d.ts +16 -11
- package/dist/document_loaders/fs/unstructured.js +69 -50
- package/dist/document_loaders/web/s3.cjs +2 -1
- package/dist/document_loaders/web/s3.js +2 -1
- package/dist/embeddings/openai.cjs +86 -6
- package/dist/embeddings/openai.d.ts +7 -2
- package/dist/embeddings/openai.js +86 -6
- package/dist/llms/openai-chat.cjs +97 -6
- package/dist/llms/openai-chat.d.ts +22 -58
- package/dist/llms/openai-chat.js +97 -6
- package/dist/llms/openai.cjs +94 -5
- package/dist/llms/openai.d.ts +18 -59
- package/dist/llms/openai.js +95 -6
- package/dist/memory/base.cjs +1 -1
- package/dist/memory/base.js +1 -1
- package/dist/memory/index.cjs +3 -1
- package/dist/memory/index.d.ts +1 -0
- package/dist/memory/index.js +1 -0
- package/dist/memory/vector_store.cjs +61 -0
- package/dist/memory/vector_store.d.ts +19 -0
- package/dist/memory/vector_store.js +57 -0
- package/dist/output_parsers/combining.cjs +15 -4
- package/dist/output_parsers/combining.d.ts +1 -0
- package/dist/output_parsers/combining.js +15 -4
- package/dist/output_parsers/structured.cjs +10 -5
- package/dist/output_parsers/structured.js +10 -5
- package/dist/retrievers/time_weighted.d.ts +1 -1
- package/dist/tools/webbrowser.cjs +1 -1
- package/dist/tools/webbrowser.js +1 -1
- package/dist/util/sql_utils.cjs +15 -1
- package/dist/util/sql_utils.d.ts +2 -0
- package/dist/util/sql_utils.js +13 -0
- package/dist/vectorstores/chroma.cjs +25 -2
- package/dist/vectorstores/chroma.js +2 -2
- package/dist/vectorstores/milvus.cjs +25 -2
- package/dist/vectorstores/milvus.js +2 -2
- package/dist/vectorstores/myscale.cjs +26 -3
- package/dist/vectorstores/myscale.js +3 -3
- package/dist/vectorstores/opensearch.cjs +25 -2
- package/dist/vectorstores/opensearch.js +2 -2
- package/dist/vectorstores/pinecone.cjs +25 -2
- package/dist/vectorstores/pinecone.js +2 -2
- package/dist/vectorstores/prisma.cjs +1 -1
- package/dist/vectorstores/prisma.js +2 -1
- package/dist/vectorstores/supabase.cjs +13 -1
- package/dist/vectorstores/supabase.d.ts +6 -1
- package/dist/vectorstores/supabase.js +13 -1
- package/dist/vectorstores/weaviate.cjs +25 -2
- package/dist/vectorstores/weaviate.js +2 -2
- package/package.json +5 -5
package/dist/llms/openai.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isNode } from "browser-or-node";
|
|
1
2
|
import { Configuration, OpenAIApi, } from "openai";
|
|
2
3
|
import fetchAdapter from "../util/axios-fetch-adapter.js";
|
|
3
4
|
import { chunkArray } from "../util/chunk.js";
|
|
@@ -10,6 +11,12 @@ import { OpenAIChat } from "./openai-chat.js";
|
|
|
10
11
|
* To use you should have the `openai` package installed, with the
|
|
11
12
|
* `OPENAI_API_KEY` environment variable set.
|
|
12
13
|
*
|
|
14
|
+
* To use with Azure you should have the `openai` package installed, with the
|
|
15
|
+
* `AZURE_OPENAI_API_KEY`,
|
|
16
|
+
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
17
|
+
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
18
|
+
* and `AZURE_OPENAI_API_VERSION` environment variable set.
|
|
19
|
+
*
|
|
13
20
|
* @remarks
|
|
14
21
|
* Any parameters that are valid to be passed to {@link
|
|
15
22
|
* https://platform.openai.com/docs/api-reference/completions/create |
|
|
@@ -19,7 +26,8 @@ import { OpenAIChat } from "./openai-chat.js";
|
|
|
19
26
|
export class OpenAI extends BaseLLM {
|
|
20
27
|
constructor(fields, configuration) {
|
|
21
28
|
if (fields?.modelName?.startsWith("gpt-3.5-turbo") ||
|
|
22
|
-
fields?.modelName?.startsWith("gpt-4")
|
|
29
|
+
fields?.modelName?.startsWith("gpt-4") ||
|
|
30
|
+
fields?.modelName?.startsWith("gpt-4-32k")) {
|
|
23
31
|
// eslint-disable-next-line no-constructor-return, @typescript-eslint/no-explicit-any
|
|
24
32
|
return new OpenAIChat(fields, configuration);
|
|
25
33
|
}
|
|
@@ -108,6 +116,30 @@ export class OpenAI extends BaseLLM {
|
|
|
108
116
|
writable: true,
|
|
109
117
|
value: false
|
|
110
118
|
});
|
|
119
|
+
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
writable: true,
|
|
123
|
+
value: void 0
|
|
124
|
+
});
|
|
125
|
+
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: true,
|
|
128
|
+
writable: true,
|
|
129
|
+
value: void 0
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: true,
|
|
134
|
+
writable: true,
|
|
135
|
+
value: void 0
|
|
136
|
+
});
|
|
137
|
+
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
configurable: true,
|
|
140
|
+
writable: true,
|
|
141
|
+
value: void 0
|
|
142
|
+
});
|
|
111
143
|
Object.defineProperty(this, "client", {
|
|
112
144
|
enumerable: true,
|
|
113
145
|
configurable: true,
|
|
@@ -125,9 +157,32 @@ export class OpenAI extends BaseLLM {
|
|
|
125
157
|
? // eslint-disable-next-line no-process-env
|
|
126
158
|
process.env?.OPENAI_API_KEY
|
|
127
159
|
: undefined);
|
|
128
|
-
|
|
129
|
-
|
|
160
|
+
const azureApiKey = fields?.azureOpenAIApiKey ??
|
|
161
|
+
(typeof process !== "undefined"
|
|
162
|
+
? // eslint-disable-next-line no-process-env
|
|
163
|
+
process.env?.AZURE_OPENAI_API_KEY
|
|
164
|
+
: undefined);
|
|
165
|
+
if (!azureApiKey && !apiKey) {
|
|
166
|
+
throw new Error("(Azure) OpenAI API key not found");
|
|
130
167
|
}
|
|
168
|
+
const azureApiInstanceName = fields?.azureOpenAIApiInstanceName ??
|
|
169
|
+
(typeof process !== "undefined"
|
|
170
|
+
? // eslint-disable-next-line no-process-env
|
|
171
|
+
process.env?.AZURE_OPENAI_API_INSTANCE_NAME
|
|
172
|
+
: undefined);
|
|
173
|
+
const azureApiDeploymentName = (fields?.azureOpenAIApiCompletionsDeploymentName ||
|
|
174
|
+
fields?.azureOpenAIApiDeploymentName) ??
|
|
175
|
+
(typeof process !== "undefined"
|
|
176
|
+
? // eslint-disable-next-line no-process-env
|
|
177
|
+
process.env?.AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME ||
|
|
178
|
+
// eslint-disable-next-line no-process-env
|
|
179
|
+
process.env?.AZURE_OPENAI_API_DEPLOYMENT_NAME
|
|
180
|
+
: undefined);
|
|
181
|
+
const azureApiVersion = fields?.azureOpenAIApiVersion ??
|
|
182
|
+
(typeof process !== "undefined"
|
|
183
|
+
? // eslint-disable-next-line no-process-env
|
|
184
|
+
process.env?.AZURE_OPENAI_API_VERSION
|
|
185
|
+
: undefined);
|
|
131
186
|
this.modelName = fields?.modelName ?? this.modelName;
|
|
132
187
|
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
133
188
|
this.batchSize = fields?.batchSize ?? this.batchSize;
|
|
@@ -142,12 +197,27 @@ export class OpenAI extends BaseLLM {
|
|
|
142
197
|
this.logitBias = fields?.logitBias;
|
|
143
198
|
this.stop = fields?.stop;
|
|
144
199
|
this.streaming = fields?.streaming ?? false;
|
|
200
|
+
this.azureOpenAIApiVersion = azureApiVersion;
|
|
201
|
+
this.azureOpenAIApiKey = azureApiKey;
|
|
202
|
+
this.azureOpenAIApiInstanceName = azureApiInstanceName;
|
|
203
|
+
this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
|
|
145
204
|
if (this.streaming && this.n > 1) {
|
|
146
205
|
throw new Error("Cannot stream results when n > 1");
|
|
147
206
|
}
|
|
148
207
|
if (this.streaming && this.bestOf > 1) {
|
|
149
208
|
throw new Error("Cannot stream results when bestOf > 1");
|
|
150
209
|
}
|
|
210
|
+
if (this.azureOpenAIApiKey) {
|
|
211
|
+
if (!this.azureOpenAIApiInstanceName) {
|
|
212
|
+
throw new Error("Azure OpenAI API instance name not found");
|
|
213
|
+
}
|
|
214
|
+
if (!this.azureOpenAIApiDeploymentName) {
|
|
215
|
+
throw new Error("Azure OpenAI API deployment name not found");
|
|
216
|
+
}
|
|
217
|
+
if (!this.azureOpenAIApiVersion) {
|
|
218
|
+
throw new Error("Azure OpenAI API version not found");
|
|
219
|
+
}
|
|
220
|
+
}
|
|
151
221
|
this.clientConfig = {
|
|
152
222
|
apiKey,
|
|
153
223
|
...configuration,
|
|
@@ -237,6 +307,7 @@ export class OpenAI extends BaseLLM {
|
|
|
237
307
|
prompt: subPrompts[i],
|
|
238
308
|
}, {
|
|
239
309
|
...options,
|
|
310
|
+
adapter: fetchAdapter,
|
|
240
311
|
responseType: "stream",
|
|
241
312
|
onmessage: (event) => {
|
|
242
313
|
if (event.data?.trim?.() === "[DONE]") {
|
|
@@ -306,18 +377,36 @@ export class OpenAI extends BaseLLM {
|
|
|
306
377
|
/** @ignore */
|
|
307
378
|
async completionWithRetry(request, options) {
|
|
308
379
|
if (!this.client) {
|
|
380
|
+
const endpoint = this.azureOpenAIApiKey
|
|
381
|
+
? `https://${this.azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${this.azureOpenAIApiDeploymentName}`
|
|
382
|
+
: this.clientConfig.basePath;
|
|
309
383
|
const clientConfig = new Configuration({
|
|
310
384
|
...this.clientConfig,
|
|
385
|
+
basePath: endpoint,
|
|
311
386
|
baseOptions: {
|
|
312
387
|
timeout: this.timeout,
|
|
313
|
-
adapter: fetchAdapter,
|
|
314
388
|
...this.clientConfig.baseOptions,
|
|
315
389
|
},
|
|
316
390
|
});
|
|
317
391
|
this.client = new OpenAIApi(clientConfig);
|
|
318
392
|
}
|
|
393
|
+
const axiosOptions = {
|
|
394
|
+
adapter: isNode ? undefined : fetchAdapter,
|
|
395
|
+
...this.clientConfig.baseOptions,
|
|
396
|
+
...options,
|
|
397
|
+
};
|
|
398
|
+
if (this.azureOpenAIApiKey) {
|
|
399
|
+
axiosOptions.headers = {
|
|
400
|
+
"api-key": this.azureOpenAIApiKey,
|
|
401
|
+
...axiosOptions.headers,
|
|
402
|
+
};
|
|
403
|
+
axiosOptions.params = {
|
|
404
|
+
"api-version": this.azureOpenAIApiVersion,
|
|
405
|
+
...axiosOptions.params,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
319
408
|
return this.caller
|
|
320
|
-
.call(this.client.createCompletion.bind(this.client), request,
|
|
409
|
+
.call(this.client.createCompletion.bind(this.client), request, axiosOptions)
|
|
321
410
|
.then((res) => res.data);
|
|
322
411
|
}
|
|
323
412
|
_llmType() {
|
|
@@ -382,4 +471,4 @@ export class PromptLayerOpenAI extends OpenAI {
|
|
|
382
471
|
return response;
|
|
383
472
|
}
|
|
384
473
|
}
|
|
385
|
-
export { OpenAIChat, PromptLayerOpenAIChat
|
|
474
|
+
export { OpenAIChat, PromptLayerOpenAIChat } from "./openai-chat.js";
|
package/dist/memory/base.cjs
CHANGED
|
@@ -17,7 +17,7 @@ const getInputValue = (inputValues, inputKey) => {
|
|
|
17
17
|
if (keys.length === 1) {
|
|
18
18
|
return inputValues[keys[0]];
|
|
19
19
|
}
|
|
20
|
-
throw new Error(`input values have
|
|
20
|
+
throw new Error(`input values have ${keys.length} keys, you must specify an input key or pass only 1 key as input`);
|
|
21
21
|
};
|
|
22
22
|
exports.getInputValue = getInputValue;
|
|
23
23
|
/**
|
package/dist/memory/base.js
CHANGED
|
@@ -13,7 +13,7 @@ export const getInputValue = (inputValues, inputKey) => {
|
|
|
13
13
|
if (keys.length === 1) {
|
|
14
14
|
return inputValues[keys[0]];
|
|
15
15
|
}
|
|
16
|
-
throw new Error(`input values have
|
|
16
|
+
throw new Error(`input values have ${keys.length} keys, you must specify an input key or pass only 1 key as input`);
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* This function is used by memory classes to get a string representation
|
package/dist/memory/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MotorheadMemory = exports.ChatMessageHistory = exports.BaseChatMemory = exports.BufferWindowMemory = exports.ConversationSummaryMemory = exports.getBufferString = exports.getInputValue = exports.BaseMemory = exports.BufferMemory = void 0;
|
|
3
|
+
exports.VectorStoreRetrieverMemory = exports.MotorheadMemory = exports.ChatMessageHistory = exports.BaseChatMemory = exports.BufferWindowMemory = exports.ConversationSummaryMemory = exports.getBufferString = exports.getInputValue = exports.BaseMemory = exports.BufferMemory = void 0;
|
|
4
4
|
var buffer_memory_js_1 = require("./buffer_memory.cjs");
|
|
5
5
|
Object.defineProperty(exports, "BufferMemory", { enumerable: true, get: function () { return buffer_memory_js_1.BufferMemory; } });
|
|
6
6
|
var base_js_1 = require("./base.cjs");
|
|
@@ -17,3 +17,5 @@ var in_memory_js_1 = require("../stores/message/in_memory.cjs");
|
|
|
17
17
|
Object.defineProperty(exports, "ChatMessageHistory", { enumerable: true, get: function () { return in_memory_js_1.ChatMessageHistory; } });
|
|
18
18
|
var motorhead_memory_js_1 = require("./motorhead_memory.cjs");
|
|
19
19
|
Object.defineProperty(exports, "MotorheadMemory", { enumerable: true, get: function () { return motorhead_memory_js_1.MotorheadMemory; } });
|
|
20
|
+
var vector_store_js_1 = require("./vector_store.cjs");
|
|
21
|
+
Object.defineProperty(exports, "VectorStoreRetrieverMemory", { enumerable: true, get: function () { return vector_store_js_1.VectorStoreRetrieverMemory; } });
|
package/dist/memory/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export { BufferWindowMemory, BufferWindowMemoryInput, } from "./buffer_window_me
|
|
|
5
5
|
export { BaseChatMemory, BaseChatMemoryInput } from "./chat_memory.js";
|
|
6
6
|
export { ChatMessageHistory } from "../stores/message/in_memory.js";
|
|
7
7
|
export { MotorheadMemory, MotorheadMemoryInput } from "./motorhead_memory.js";
|
|
8
|
+
export { VectorStoreRetrieverMemory, VectorStoreRetrieverMemoryParams, } from "./vector_store.js";
|
package/dist/memory/index.js
CHANGED
|
@@ -5,3 +5,4 @@ export { BufferWindowMemory, } from "./buffer_window_memory.js";
|
|
|
5
5
|
export { BaseChatMemory } from "./chat_memory.js";
|
|
6
6
|
export { ChatMessageHistory } from "../stores/message/in_memory.js";
|
|
7
7
|
export { MotorheadMemory } from "./motorhead_memory.js";
|
|
8
|
+
export { VectorStoreRetrieverMemory, } from "./vector_store.js";
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VectorStoreRetrieverMemory = void 0;
|
|
4
|
+
const document_js_1 = require("../document.cjs");
|
|
5
|
+
const base_js_1 = require("./base.cjs");
|
|
6
|
+
class VectorStoreRetrieverMemory extends base_js_1.BaseMemory {
|
|
7
|
+
constructor(fields) {
|
|
8
|
+
super();
|
|
9
|
+
Object.defineProperty(this, "vectorStoreRetriever", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: void 0
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "inputKey", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "memoryKey", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "returnDocs", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
this.vectorStoreRetriever = fields.vectorStoreRetriever;
|
|
34
|
+
this.inputKey = fields.inputKey;
|
|
35
|
+
this.memoryKey = fields.memoryKey ?? "memory";
|
|
36
|
+
this.returnDocs = fields.returnDocs ?? false;
|
|
37
|
+
}
|
|
38
|
+
get memoryKeys() {
|
|
39
|
+
return [this.memoryKey];
|
|
40
|
+
}
|
|
41
|
+
async loadMemoryVariables(values) {
|
|
42
|
+
const query = (0, base_js_1.getInputValue)(values, this.inputKey);
|
|
43
|
+
const results = await this.vectorStoreRetriever.getRelevantDocuments(query);
|
|
44
|
+
return {
|
|
45
|
+
[this.memoryKey]: this.returnDocs
|
|
46
|
+
? results
|
|
47
|
+
: results.map((r) => r.pageContent).join("\n"),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async saveContext(inputValues, outputValues) {
|
|
51
|
+
const text = Object.entries(inputValues)
|
|
52
|
+
.filter(([k]) => k !== this.memoryKey)
|
|
53
|
+
.concat(Object.entries(outputValues))
|
|
54
|
+
.map(([k, v]) => `${k}: ${v}`)
|
|
55
|
+
.join("\n");
|
|
56
|
+
await this.vectorStoreRetriever.addDocuments([
|
|
57
|
+
new document_js_1.Document({ pageContent: text }),
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.VectorStoreRetrieverMemory = VectorStoreRetrieverMemory;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { VectorStoreRetriever } from "../vectorstores/base.js";
|
|
2
|
+
import { BaseMemory, InputValues, MemoryVariables, OutputValues } from "./base.js";
|
|
3
|
+
export interface VectorStoreRetrieverMemoryParams {
|
|
4
|
+
vectorStoreRetriever: VectorStoreRetriever;
|
|
5
|
+
inputKey?: string;
|
|
6
|
+
outputKey?: string;
|
|
7
|
+
memoryKey?: string;
|
|
8
|
+
returnDocs?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare class VectorStoreRetrieverMemory extends BaseMemory implements VectorStoreRetrieverMemoryParams {
|
|
11
|
+
vectorStoreRetriever: VectorStoreRetriever;
|
|
12
|
+
inputKey?: string;
|
|
13
|
+
memoryKey: string;
|
|
14
|
+
returnDocs: boolean;
|
|
15
|
+
constructor(fields: VectorStoreRetrieverMemoryParams);
|
|
16
|
+
get memoryKeys(): string[];
|
|
17
|
+
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
|
|
18
|
+
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Document } from "../document.js";
|
|
2
|
+
import { BaseMemory, getInputValue, } from "./base.js";
|
|
3
|
+
export class VectorStoreRetrieverMemory extends BaseMemory {
|
|
4
|
+
constructor(fields) {
|
|
5
|
+
super();
|
|
6
|
+
Object.defineProperty(this, "vectorStoreRetriever", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
writable: true,
|
|
10
|
+
value: void 0
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(this, "inputKey", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "memoryKey", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "returnDocs", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
this.vectorStoreRetriever = fields.vectorStoreRetriever;
|
|
31
|
+
this.inputKey = fields.inputKey;
|
|
32
|
+
this.memoryKey = fields.memoryKey ?? "memory";
|
|
33
|
+
this.returnDocs = fields.returnDocs ?? false;
|
|
34
|
+
}
|
|
35
|
+
get memoryKeys() {
|
|
36
|
+
return [this.memoryKey];
|
|
37
|
+
}
|
|
38
|
+
async loadMemoryVariables(values) {
|
|
39
|
+
const query = getInputValue(values, this.inputKey);
|
|
40
|
+
const results = await this.vectorStoreRetriever.getRelevantDocuments(query);
|
|
41
|
+
return {
|
|
42
|
+
[this.memoryKey]: this.returnDocs
|
|
43
|
+
? results
|
|
44
|
+
: results.map((r) => r.pageContent).join("\n"),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async saveContext(inputValues, outputValues) {
|
|
48
|
+
const text = Object.entries(inputValues)
|
|
49
|
+
.filter(([k]) => k !== this.memoryKey)
|
|
50
|
+
.concat(Object.entries(outputValues))
|
|
51
|
+
.map(([k, v]) => `${k}: ${v}`)
|
|
52
|
+
.join("\n");
|
|
53
|
+
await this.vectorStoreRetriever.addDocuments([
|
|
54
|
+
new Document({ pageContent: text }),
|
|
55
|
+
]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -15,20 +15,29 @@ class CombiningOutputParser extends output_parser_js_1.BaseOutputParser {
|
|
|
15
15
|
writable: true,
|
|
16
16
|
value: void 0
|
|
17
17
|
});
|
|
18
|
+
Object.defineProperty(this, "outputDelimiter", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: "-----"
|
|
23
|
+
});
|
|
18
24
|
this.parsers = parsers;
|
|
19
25
|
}
|
|
20
26
|
async parse(input, callbacks) {
|
|
21
27
|
const inputs = input
|
|
22
28
|
.trim()
|
|
23
|
-
.split(
|
|
29
|
+
.split(new RegExp(`${this.outputDelimiter}Output \\d+${this.outputDelimiter}`))
|
|
24
30
|
.slice(1);
|
|
25
31
|
const ret = {};
|
|
26
32
|
for (const [i, p] of this.parsers.entries()) {
|
|
27
33
|
let parsed;
|
|
28
34
|
try {
|
|
29
|
-
|
|
35
|
+
let extracted = inputs[i].includes("```")
|
|
30
36
|
? inputs[i].trim().split(/```/)[1]
|
|
31
37
|
: inputs[i].trim();
|
|
38
|
+
if (extracted.endsWith(this.outputDelimiter)) {
|
|
39
|
+
extracted = extracted.slice(0, -this.outputDelimiter.length);
|
|
40
|
+
}
|
|
32
41
|
parsed = await p.parse(extracted, callbacks);
|
|
33
42
|
}
|
|
34
43
|
catch (e) {
|
|
@@ -40,8 +49,10 @@ class CombiningOutputParser extends output_parser_js_1.BaseOutputParser {
|
|
|
40
49
|
}
|
|
41
50
|
getFormatInstructions() {
|
|
42
51
|
return `${[
|
|
43
|
-
`Return the following ${this.parsers.length} outputs, each formatted as described below:`,
|
|
44
|
-
...this.parsers.map((p, i) =>
|
|
52
|
+
`Return the following ${this.parsers.length} outputs, each formatted as described below. Include the delimiter characters "${this.outputDelimiter}" in your response:`,
|
|
53
|
+
...this.parsers.map((p, i) => `${this.outputDelimiter}Output ${i + 1}${this.outputDelimiter}\n${p
|
|
54
|
+
.getFormatInstructions()
|
|
55
|
+
.trim()}\n${this.outputDelimiter}`),
|
|
45
56
|
].join("\n\n")}\n`;
|
|
46
57
|
}
|
|
47
58
|
}
|
|
@@ -7,6 +7,7 @@ export type CombinedOutput = Record<string, any>;
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class CombiningOutputParser extends BaseOutputParser {
|
|
9
9
|
parsers: BaseOutputParser[];
|
|
10
|
+
outputDelimiter: string;
|
|
10
11
|
constructor(...parsers: BaseOutputParser[]);
|
|
11
12
|
parse(input: string, callbacks?: Callbacks): Promise<CombinedOutput>;
|
|
12
13
|
getFormatInstructions(): string;
|
|
@@ -12,20 +12,29 @@ export class CombiningOutputParser extends BaseOutputParser {
|
|
|
12
12
|
writable: true,
|
|
13
13
|
value: void 0
|
|
14
14
|
});
|
|
15
|
+
Object.defineProperty(this, "outputDelimiter", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: "-----"
|
|
20
|
+
});
|
|
15
21
|
this.parsers = parsers;
|
|
16
22
|
}
|
|
17
23
|
async parse(input, callbacks) {
|
|
18
24
|
const inputs = input
|
|
19
25
|
.trim()
|
|
20
|
-
.split(
|
|
26
|
+
.split(new RegExp(`${this.outputDelimiter}Output \\d+${this.outputDelimiter}`))
|
|
21
27
|
.slice(1);
|
|
22
28
|
const ret = {};
|
|
23
29
|
for (const [i, p] of this.parsers.entries()) {
|
|
24
30
|
let parsed;
|
|
25
31
|
try {
|
|
26
|
-
|
|
32
|
+
let extracted = inputs[i].includes("```")
|
|
27
33
|
? inputs[i].trim().split(/```/)[1]
|
|
28
34
|
: inputs[i].trim();
|
|
35
|
+
if (extracted.endsWith(this.outputDelimiter)) {
|
|
36
|
+
extracted = extracted.slice(0, -this.outputDelimiter.length);
|
|
37
|
+
}
|
|
29
38
|
parsed = await p.parse(extracted, callbacks);
|
|
30
39
|
}
|
|
31
40
|
catch (e) {
|
|
@@ -37,8 +46,10 @@ export class CombiningOutputParser extends BaseOutputParser {
|
|
|
37
46
|
}
|
|
38
47
|
getFormatInstructions() {
|
|
39
48
|
return `${[
|
|
40
|
-
`Return the following ${this.parsers.length} outputs, each formatted as described below:`,
|
|
41
|
-
...this.parsers.map((p, i) =>
|
|
49
|
+
`Return the following ${this.parsers.length} outputs, each formatted as described below. Include the delimiter characters "${this.outputDelimiter}" in your response:`,
|
|
50
|
+
...this.parsers.map((p, i) => `${this.outputDelimiter}Output ${i + 1}${this.outputDelimiter}\n${p
|
|
51
|
+
.getFormatInstructions()
|
|
52
|
+
.trim()}\n${this.outputDelimiter}`),
|
|
42
53
|
].join("\n\n")}\n`;
|
|
43
54
|
}
|
|
44
55
|
}
|
|
@@ -22,13 +22,18 @@ class StructuredOutputParser extends output_parser_js_1.BaseOutputParser {
|
|
|
22
22
|
return new this(zodSchema);
|
|
23
23
|
}
|
|
24
24
|
getFormatInstructions() {
|
|
25
|
-
return `
|
|
25
|
+
return `You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema. The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
27
|
+
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
|
|
30
|
+
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
|
|
31
|
+
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
32
|
+
|
|
33
|
+
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match exactly!
|
|
34
|
+
|
|
35
|
+
Here is the JSON Schema instance your output must adhere to:
|
|
36
|
+
\`\`\`json
|
|
32
37
|
${JSON.stringify((0, zod_to_json_schema_1.zodToJsonSchema)(this.schema))}
|
|
33
38
|
\`\`\`
|
|
34
39
|
`;
|
|
@@ -19,13 +19,18 @@ export class StructuredOutputParser extends BaseOutputParser {
|
|
|
19
19
|
return new this(zodSchema);
|
|
20
20
|
}
|
|
21
21
|
getFormatInstructions() {
|
|
22
|
-
return `
|
|
22
|
+
return `You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema. The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
24
|
+
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
|
|
27
|
+
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
|
|
28
|
+
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
29
|
+
|
|
30
|
+
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match exactly!
|
|
31
|
+
|
|
32
|
+
Here is the JSON Schema instance your output must adhere to:
|
|
33
|
+
\`\`\`json
|
|
29
34
|
${JSON.stringify(zodToJsonSchema(this.schema))}
|
|
30
35
|
\`\`\`
|
|
31
36
|
`;
|
|
@@ -171,7 +171,7 @@ class WebBrowser extends base_js_2.Tool {
|
|
|
171
171
|
enumerable: true,
|
|
172
172
|
configurable: true,
|
|
173
173
|
writable: true,
|
|
174
|
-
value: `useful for when you need to find something on or summarize a webpage. input should be a comma
|
|
174
|
+
value: `useful for when you need to find something on or summarize a webpage. input should be a comma separated list of "ONE valid http URL including protocol","what you want to find on the page or empty string for a summary".`
|
|
175
175
|
});
|
|
176
176
|
this.model = model;
|
|
177
177
|
this.embeddings = embeddings;
|
package/dist/tools/webbrowser.js
CHANGED
|
@@ -140,7 +140,7 @@ export class WebBrowser extends Tool {
|
|
|
140
140
|
enumerable: true,
|
|
141
141
|
configurable: true,
|
|
142
142
|
writable: true,
|
|
143
|
-
value: `useful for when you need to find something on or summarize a webpage. input should be a comma
|
|
143
|
+
value: `useful for when you need to find something on or summarize a webpage. input should be a comma separated list of "ONE valid http URL including protocol","what you want to find on the page or empty string for a summary".`
|
|
144
144
|
});
|
|
145
145
|
this.model = model;
|
|
146
146
|
this.embeddings = embeddings;
|
package/dist/util/sql_utils.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateTableInfoFromTables = exports.getTableAndColumnsName = exports.verifyIgnoreTablesExistInDatabase = exports.verifyIncludeTablesExistInDatabase = exports.verifyListTablesExistInDatabase = void 0;
|
|
3
|
+
exports.getPromptTemplateFromDataSource = exports.generateTableInfoFromTables = exports.getTableAndColumnsName = exports.verifyIgnoreTablesExistInDatabase = exports.verifyIncludeTablesExistInDatabase = exports.verifyListTablesExistInDatabase = void 0;
|
|
4
|
+
const sql_db_prompt_js_1 = require("../chains/sql_db/sql_db_prompt.cjs");
|
|
4
5
|
const verifyListTablesExistInDatabase = (tablesFromDatabase, listTables, errorPrefixMsg) => {
|
|
5
6
|
const onlyTableNames = tablesFromDatabase.map((table) => table.tableName);
|
|
6
7
|
if (listTables.length > 0) {
|
|
@@ -159,3 +160,16 @@ const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow) =
|
|
|
159
160
|
return globalString;
|
|
160
161
|
};
|
|
161
162
|
exports.generateTableInfoFromTables = generateTableInfoFromTables;
|
|
163
|
+
const getPromptTemplateFromDataSource = (appDataSource) => {
|
|
164
|
+
if (appDataSource.options.type === "postgres") {
|
|
165
|
+
return sql_db_prompt_js_1.SQL_POSTGRES_PROMPT;
|
|
166
|
+
}
|
|
167
|
+
if (appDataSource.options.type === "sqlite") {
|
|
168
|
+
return sql_db_prompt_js_1.SQL_SQLITE_PROMPT;
|
|
169
|
+
}
|
|
170
|
+
if (appDataSource.options.type === "mysql") {
|
|
171
|
+
return sql_db_prompt_js_1.SQL_MYSQL_PROMPT;
|
|
172
|
+
}
|
|
173
|
+
return sql_db_prompt_js_1.DEFAULT_SQL_DATABASE_PROMPT;
|
|
174
|
+
};
|
|
175
|
+
exports.getPromptTemplateFromDataSource = getPromptTemplateFromDataSource;
|
package/dist/util/sql_utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { DataSource, DataSourceOptions } from "typeorm";
|
|
2
|
+
import { PromptTemplate } from "../prompts/index.js";
|
|
2
3
|
export interface SqlDatabaseParams {
|
|
3
4
|
includesTables?: Array<string>;
|
|
4
5
|
ignoreTables?: Array<string>;
|
|
@@ -27,3 +28,4 @@ export declare const verifyIncludeTablesExistInDatabase: (tablesFromDatabase: Ar
|
|
|
27
28
|
export declare const verifyIgnoreTablesExistInDatabase: (tablesFromDatabase: Array<SqlTable>, ignoreTables: Array<string>) => void;
|
|
28
29
|
export declare const getTableAndColumnsName: (appDataSource: DataSource) => Promise<Array<SqlTable>>;
|
|
29
30
|
export declare const generateTableInfoFromTables: (tables: Array<SqlTable> | undefined, appDataSource: DataSource, nbSampleRow: number) => Promise<string>;
|
|
31
|
+
export declare const getPromptTemplateFromDataSource: (appDataSource: DataSource) => PromptTemplate;
|
package/dist/util/sql_utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DEFAULT_SQL_DATABASE_PROMPT, SQL_MYSQL_PROMPT, SQL_POSTGRES_PROMPT, SQL_SQLITE_PROMPT, } from "../chains/sql_db/sql_db_prompt.js";
|
|
1
2
|
export const verifyListTablesExistInDatabase = (tablesFromDatabase, listTables, errorPrefixMsg) => {
|
|
2
3
|
const onlyTableNames = tablesFromDatabase.map((table) => table.tableName);
|
|
3
4
|
if (listTables.length > 0) {
|
|
@@ -151,3 +152,15 @@ export const generateTableInfoFromTables = async (tables, appDataSource, nbSampl
|
|
|
151
152
|
}
|
|
152
153
|
return globalString;
|
|
153
154
|
};
|
|
155
|
+
export const getPromptTemplateFromDataSource = (appDataSource) => {
|
|
156
|
+
if (appDataSource.options.type === "postgres") {
|
|
157
|
+
return SQL_POSTGRES_PROMPT;
|
|
158
|
+
}
|
|
159
|
+
if (appDataSource.options.type === "sqlite") {
|
|
160
|
+
return SQL_SQLITE_PROMPT;
|
|
161
|
+
}
|
|
162
|
+
if (appDataSource.options.type === "mysql") {
|
|
163
|
+
return SQL_MYSQL_PROMPT;
|
|
164
|
+
}
|
|
165
|
+
return DEFAULT_SQL_DATABASE_PROMPT;
|
|
166
|
+
};
|
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.Chroma = void 0;
|
|
4
|
-
const
|
|
27
|
+
const uuid = __importStar(require("uuid"));
|
|
5
28
|
const base_js_1 = require("./base.cjs");
|
|
6
29
|
const document_js_1 = require("../document.cjs");
|
|
7
30
|
class Chroma extends base_js_1.VectorStore {
|
|
@@ -139,7 +162,7 @@ class Chroma extends base_js_1.VectorStore {
|
|
|
139
162
|
exports.Chroma = Chroma;
|
|
140
163
|
function ensureCollectionName(collectionName) {
|
|
141
164
|
if (!collectionName) {
|
|
142
|
-
return `langchain-${
|
|
165
|
+
return `langchain-${uuid.v4()}`;
|
|
143
166
|
}
|
|
144
167
|
return collectionName;
|
|
145
168
|
}
|