langchain 0.0.149 → 0.0.151
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/experimental/chat_models/bittensor.cjs +141 -0
- package/dist/experimental/chat_models/bittensor.d.ts +36 -0
- package/dist/experimental/chat_models/bittensor.js +137 -0
- package/dist/llms/openai.cjs +3 -2
- package/dist/llms/openai.js +3 -2
- package/dist/llms/replicate.cjs +28 -2
- package/dist/llms/replicate.d.ts +3 -0
- package/dist/llms/replicate.js +28 -2
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/load/import_map.cjs +2 -1
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/prompts/prompt.cjs +2 -0
- package/dist/prompts/prompt.d.ts +1 -1
- package/dist/prompts/prompt.js +2 -0
- package/dist/schema/runnable/base.cjs +11 -2
- package/dist/schema/runnable/base.d.ts +3 -1
- package/dist/schema/runnable/base.js +10 -2
- package/dist/schema/runnable/branch.cjs +106 -0
- package/dist/schema/runnable/branch.d.ts +66 -0
- package/dist/schema/runnable/branch.js +102 -0
- package/dist/schema/runnable/index.cjs +12 -16
- package/dist/schema/runnable/index.d.ts +2 -1
- package/dist/schema/runnable/index.js +2 -1
- package/dist/vectorstores/pgvector.cjs +277 -0
- package/dist/vectorstores/pgvector.d.ts +132 -0
- package/dist/vectorstores/pgvector.js +270 -0
- package/experimental/chat_models/bittensor.cjs +1 -0
- package/experimental/chat_models/bittensor.d.ts +1 -0
- package/experimental/chat_models/bittensor.js +1 -0
- package/package.json +19 -3
- package/vectorstores/pgvector.cjs +1 -0
- package/vectorstores/pgvector.d.ts +1 -0
- package/vectorstores/pgvector.js +1 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RunnableBranch = void 0;
|
|
4
|
+
const base_js_1 = require("./base.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Class that represents a runnable branch. The RunnableBranch is
|
|
7
|
+
* initialized with an array of branches and a default branch. When invoked,
|
|
8
|
+
* it evaluates the condition of each branch in order and executes the
|
|
9
|
+
* corresponding branch if the condition is true. If none of the conditions
|
|
10
|
+
* are true, it executes the default branch.
|
|
11
|
+
*/
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
+
class RunnableBranch extends base_js_1.Runnable {
|
|
14
|
+
static lc_name() {
|
|
15
|
+
return "RunnableBranch";
|
|
16
|
+
}
|
|
17
|
+
constructor(fields) {
|
|
18
|
+
super(fields);
|
|
19
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: ["langchain", "runnable", "branch"]
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: true
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "default", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: void 0
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(this, "branches", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: void 0
|
|
42
|
+
});
|
|
43
|
+
this.branches = fields.branches;
|
|
44
|
+
this.default = fields.default;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Convenience method for instantiating a RunnableBranch from
|
|
48
|
+
* RunnableLikes (objects, functions, or Runnables).
|
|
49
|
+
*
|
|
50
|
+
* Each item in the input except for the last one should be a
|
|
51
|
+
* tuple with two items. The first is a "condition" RunnableLike that
|
|
52
|
+
* returns "true" if the second RunnableLike in the tuple should run.
|
|
53
|
+
*
|
|
54
|
+
* The final item in the input should be a RunnableLike that acts as a
|
|
55
|
+
* default branch if no other branches match.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { RunnableBranch } from "langchain/schema/runnable";
|
|
60
|
+
*
|
|
61
|
+
* const branch = RunnableBranch.from([
|
|
62
|
+
* [(x: number) => x > 0, (x: number) => x + 1],
|
|
63
|
+
* [(x: number) => x < 0, (x: number) => x - 1],
|
|
64
|
+
* (x: number) => x
|
|
65
|
+
* ]);
|
|
66
|
+
* ```
|
|
67
|
+
* @param branches An array where the every item except the last is a tuple of [condition, runnable]
|
|
68
|
+
* pairs. The last item is a default runnable which is invoked if no other condition matches.
|
|
69
|
+
* @returns A new RunnableBranch.
|
|
70
|
+
*/
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
static from(branches) {
|
|
73
|
+
if (branches.length < 1) {
|
|
74
|
+
throw new Error("RunnableBranch requires at least one branch");
|
|
75
|
+
}
|
|
76
|
+
const branchLikes = branches.slice(0, -1);
|
|
77
|
+
const coercedBranches = branchLikes.map(([condition, runnable]) => [
|
|
78
|
+
(0, base_js_1._coerceToRunnable)(condition),
|
|
79
|
+
(0, base_js_1._coerceToRunnable)(runnable),
|
|
80
|
+
]);
|
|
81
|
+
const defaultBranch = (0, base_js_1._coerceToRunnable)(branches[branches.length - 1]);
|
|
82
|
+
return new this({
|
|
83
|
+
branches: coercedBranches,
|
|
84
|
+
default: defaultBranch,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async _invoke(input, config, runManager) {
|
|
88
|
+
let result;
|
|
89
|
+
for (let i = 0; i < this.branches.length; i += 1) {
|
|
90
|
+
const [condition, branchRunnable] = this.branches[i];
|
|
91
|
+
const conditionValue = await condition.invoke(input, this._patchConfig(config, runManager?.getChild(`condition:${i + 1}`)));
|
|
92
|
+
if (conditionValue) {
|
|
93
|
+
result = await branchRunnable.invoke(input, this._patchConfig(config, runManager?.getChild(`branch:${i + 1}`)));
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!result) {
|
|
98
|
+
result = await this.default.invoke(input, this._patchConfig(config, runManager?.getChild("default")));
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
async invoke(input, config = {}) {
|
|
103
|
+
return this._callWithConfig(this._invoke, input, config);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.RunnableBranch = RunnableBranch;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Runnable, RunnableLike } from "./base.js";
|
|
2
|
+
import { RunnableConfig } from "./config.js";
|
|
3
|
+
import { CallbackManagerForChainRun } from "../../callbacks/manager.js";
|
|
4
|
+
/**
|
|
5
|
+
* Type for a branch in the RunnableBranch. It consists of a condition
|
|
6
|
+
* runnable and a branch runnable. The condition runnable is used to
|
|
7
|
+
* determine whether the branch should be executed, and the branch runnable
|
|
8
|
+
* is executed if the condition is true.
|
|
9
|
+
*/
|
|
10
|
+
export type Branch<RunInput, RunOutput> = [
|
|
11
|
+
Runnable<RunInput, boolean>,
|
|
12
|
+
Runnable<RunInput, RunOutput>
|
|
13
|
+
];
|
|
14
|
+
export type BranchLike<RunInput, RunOutput> = [
|
|
15
|
+
RunnableLike<RunInput, boolean>,
|
|
16
|
+
RunnableLike<RunInput, RunOutput>
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Class that represents a runnable branch. The RunnableBranch is
|
|
20
|
+
* initialized with an array of branches and a default branch. When invoked,
|
|
21
|
+
* it evaluates the condition of each branch in order and executes the
|
|
22
|
+
* corresponding branch if the condition is true. If none of the conditions
|
|
23
|
+
* are true, it executes the default branch.
|
|
24
|
+
*/
|
|
25
|
+
export declare class RunnableBranch<RunInput = any, RunOutput = any> extends Runnable<RunInput, RunOutput> {
|
|
26
|
+
static lc_name(): string;
|
|
27
|
+
lc_namespace: string[];
|
|
28
|
+
lc_serializable: boolean;
|
|
29
|
+
default: Runnable<RunInput, RunOutput>;
|
|
30
|
+
branches: Branch<RunInput, RunOutput>[];
|
|
31
|
+
constructor(fields: {
|
|
32
|
+
branches: Branch<RunInput, RunOutput>[];
|
|
33
|
+
default: Runnable<RunInput, RunOutput>;
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* Convenience method for instantiating a RunnableBranch from
|
|
37
|
+
* RunnableLikes (objects, functions, or Runnables).
|
|
38
|
+
*
|
|
39
|
+
* Each item in the input except for the last one should be a
|
|
40
|
+
* tuple with two items. The first is a "condition" RunnableLike that
|
|
41
|
+
* returns "true" if the second RunnableLike in the tuple should run.
|
|
42
|
+
*
|
|
43
|
+
* The final item in the input should be a RunnableLike that acts as a
|
|
44
|
+
* default branch if no other branches match.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { RunnableBranch } from "langchain/schema/runnable";
|
|
49
|
+
*
|
|
50
|
+
* const branch = RunnableBranch.from([
|
|
51
|
+
* [(x: number) => x > 0, (x: number) => x + 1],
|
|
52
|
+
* [(x: number) => x < 0, (x: number) => x - 1],
|
|
53
|
+
* (x: number) => x
|
|
54
|
+
* ]);
|
|
55
|
+
* ```
|
|
56
|
+
* @param branches An array where the every item except the last is a tuple of [condition, runnable]
|
|
57
|
+
* pairs. The last item is a default runnable which is invoked if no other condition matches.
|
|
58
|
+
* @returns A new RunnableBranch.
|
|
59
|
+
*/
|
|
60
|
+
static from<RunInput = any, RunOutput = any>(branches: [
|
|
61
|
+
...BranchLike<RunInput, RunOutput>[],
|
|
62
|
+
RunnableLike<RunInput, RunOutput>
|
|
63
|
+
]): RunnableBranch<RunInput, RunOutput>;
|
|
64
|
+
_invoke(input: RunInput, config?: Partial<RunnableConfig>, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;
|
|
65
|
+
invoke(input: RunInput, config?: RunnableConfig): Promise<RunOutput>;
|
|
66
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Runnable, _coerceToRunnable } from "./base.js";
|
|
2
|
+
/**
|
|
3
|
+
* Class that represents a runnable branch. The RunnableBranch is
|
|
4
|
+
* initialized with an array of branches and a default branch. When invoked,
|
|
5
|
+
* it evaluates the condition of each branch in order and executes the
|
|
6
|
+
* corresponding branch if the condition is true. If none of the conditions
|
|
7
|
+
* are true, it executes the default branch.
|
|
8
|
+
*/
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
export class RunnableBranch extends Runnable {
|
|
11
|
+
static lc_name() {
|
|
12
|
+
return "RunnableBranch";
|
|
13
|
+
}
|
|
14
|
+
constructor(fields) {
|
|
15
|
+
super(fields);
|
|
16
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: ["langchain", "runnable", "branch"]
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: true
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "default", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(this, "branches", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: void 0
|
|
39
|
+
});
|
|
40
|
+
this.branches = fields.branches;
|
|
41
|
+
this.default = fields.default;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Convenience method for instantiating a RunnableBranch from
|
|
45
|
+
* RunnableLikes (objects, functions, or Runnables).
|
|
46
|
+
*
|
|
47
|
+
* Each item in the input except for the last one should be a
|
|
48
|
+
* tuple with two items. The first is a "condition" RunnableLike that
|
|
49
|
+
* returns "true" if the second RunnableLike in the tuple should run.
|
|
50
|
+
*
|
|
51
|
+
* The final item in the input should be a RunnableLike that acts as a
|
|
52
|
+
* default branch if no other branches match.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { RunnableBranch } from "langchain/schema/runnable";
|
|
57
|
+
*
|
|
58
|
+
* const branch = RunnableBranch.from([
|
|
59
|
+
* [(x: number) => x > 0, (x: number) => x + 1],
|
|
60
|
+
* [(x: number) => x < 0, (x: number) => x - 1],
|
|
61
|
+
* (x: number) => x
|
|
62
|
+
* ]);
|
|
63
|
+
* ```
|
|
64
|
+
* @param branches An array where the every item except the last is a tuple of [condition, runnable]
|
|
65
|
+
* pairs. The last item is a default runnable which is invoked if no other condition matches.
|
|
66
|
+
* @returns A new RunnableBranch.
|
|
67
|
+
*/
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
69
|
+
static from(branches) {
|
|
70
|
+
if (branches.length < 1) {
|
|
71
|
+
throw new Error("RunnableBranch requires at least one branch");
|
|
72
|
+
}
|
|
73
|
+
const branchLikes = branches.slice(0, -1);
|
|
74
|
+
const coercedBranches = branchLikes.map(([condition, runnable]) => [
|
|
75
|
+
_coerceToRunnable(condition),
|
|
76
|
+
_coerceToRunnable(runnable),
|
|
77
|
+
]);
|
|
78
|
+
const defaultBranch = _coerceToRunnable(branches[branches.length - 1]);
|
|
79
|
+
return new this({
|
|
80
|
+
branches: coercedBranches,
|
|
81
|
+
default: defaultBranch,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
async _invoke(input, config, runManager) {
|
|
85
|
+
let result;
|
|
86
|
+
for (let i = 0; i < this.branches.length; i += 1) {
|
|
87
|
+
const [condition, branchRunnable] = this.branches[i];
|
|
88
|
+
const conditionValue = await condition.invoke(input, this._patchConfig(config, runManager?.getChild(`condition:${i + 1}`)));
|
|
89
|
+
if (conditionValue) {
|
|
90
|
+
result = await branchRunnable.invoke(input, this._patchConfig(config, runManager?.getChild(`branch:${i + 1}`)));
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (!result) {
|
|
95
|
+
result = await this.default.invoke(input, this._patchConfig(config, runManager?.getChild("default")));
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
async invoke(input, config = {}) {
|
|
100
|
+
return this._callWithConfig(this._invoke, input, config);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -1,22 +1,18 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.RouterRunnable = exports.RunnablePassthrough = void 0;
|
|
18
|
-
|
|
3
|
+
exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
|
|
4
|
+
var base_js_1 = require("./base.cjs");
|
|
5
|
+
Object.defineProperty(exports, "Runnable", { enumerable: true, get: function () { return base_js_1.Runnable; } });
|
|
6
|
+
Object.defineProperty(exports, "RunnableBinding", { enumerable: true, get: function () { return base_js_1.RunnableBinding; } });
|
|
7
|
+
Object.defineProperty(exports, "RunnableEach", { enumerable: true, get: function () { return base_js_1.RunnableEach; } });
|
|
8
|
+
Object.defineProperty(exports, "RunnableRetry", { enumerable: true, get: function () { return base_js_1.RunnableRetry; } });
|
|
9
|
+
Object.defineProperty(exports, "RunnableSequence", { enumerable: true, get: function () { return base_js_1.RunnableSequence; } });
|
|
10
|
+
Object.defineProperty(exports, "RunnableMap", { enumerable: true, get: function () { return base_js_1.RunnableMap; } });
|
|
11
|
+
Object.defineProperty(exports, "RunnableLambda", { enumerable: true, get: function () { return base_js_1.RunnableLambda; } });
|
|
12
|
+
Object.defineProperty(exports, "RunnableWithFallbacks", { enumerable: true, get: function () { return base_js_1.RunnableWithFallbacks; } });
|
|
19
13
|
var passthrough_js_1 = require("./passthrough.cjs");
|
|
20
14
|
Object.defineProperty(exports, "RunnablePassthrough", { enumerable: true, get: function () { return passthrough_js_1.RunnablePassthrough; } });
|
|
21
15
|
var router_js_1 = require("./router.cjs");
|
|
22
16
|
Object.defineProperty(exports, "RouterRunnable", { enumerable: true, get: function () { return router_js_1.RouterRunnable; } });
|
|
17
|
+
var branch_js_1 = require("./branch.cjs");
|
|
18
|
+
Object.defineProperty(exports, "RunnableBranch", { enumerable: true, get: function () { return branch_js_1.RunnableBranch; } });
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { RunnableFunc, RunnableLike, RunnableBatchOptions, RunnableRetryFailedAttemptHandler, Runnable, RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableLambda, RunnableWithFallbacks, } from "./base.js";
|
|
2
2
|
export { RunnableConfig } from "./config.js";
|
|
3
3
|
export { RunnablePassthrough } from "./passthrough.js";
|
|
4
4
|
export { RouterRunnable } from "./router.js";
|
|
5
|
+
export { RunnableBranch, Branch, BranchLike } from "./branch.js";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableLambda, RunnableWithFallbacks, } from "./base.js";
|
|
2
2
|
export { RunnablePassthrough } from "./passthrough.js";
|
|
3
3
|
export { RouterRunnable } from "./router.js";
|
|
4
|
+
export { RunnableBranch } from "./branch.js";
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PGVectorStore = void 0;
|
|
7
|
+
const pg_1 = __importDefault(require("pg"));
|
|
8
|
+
const base_js_1 = require("./base.cjs");
|
|
9
|
+
const document_js_1 = require("../document.cjs");
|
|
10
|
+
const env_js_1 = require("../util/env.cjs");
|
|
11
|
+
/**
|
|
12
|
+
* Class that provides an interface to a Postgres vector database. It
|
|
13
|
+
* extends the `VectorStore` base class and implements methods for adding
|
|
14
|
+
* documents and vectors, performing similarity searches, and ensuring the
|
|
15
|
+
* existence of a table in the database.
|
|
16
|
+
*/
|
|
17
|
+
class PGVectorStore extends base_js_1.VectorStore {
|
|
18
|
+
_vectorstoreType() {
|
|
19
|
+
return "pgvector";
|
|
20
|
+
}
|
|
21
|
+
constructor(embeddings, config) {
|
|
22
|
+
super(embeddings, config);
|
|
23
|
+
Object.defineProperty(this, "tableName", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "idColumnName", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: void 0
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "vectorColumnName", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: void 0
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "contentColumnName", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: void 0
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(this, "metadataColumnName", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(this, "filter", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: void 0
|
|
58
|
+
});
|
|
59
|
+
Object.defineProperty(this, "_verbose", {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: true,
|
|
62
|
+
writable: true,
|
|
63
|
+
value: void 0
|
|
64
|
+
});
|
|
65
|
+
Object.defineProperty(this, "pool", {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
value: void 0
|
|
70
|
+
});
|
|
71
|
+
Object.defineProperty(this, "client", {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
configurable: true,
|
|
74
|
+
writable: true,
|
|
75
|
+
value: void 0
|
|
76
|
+
});
|
|
77
|
+
this.tableName = config.tableName;
|
|
78
|
+
this.filter = config.filter;
|
|
79
|
+
this.vectorColumnName = config.columns?.vectorColumnName ?? "embedding";
|
|
80
|
+
this.contentColumnName = config.columns?.contentColumnName ?? "text";
|
|
81
|
+
this.idColumnName = config.columns?.idColumnName ?? "id";
|
|
82
|
+
this.metadataColumnName = config.columns?.metadataColumnName ?? "metadata";
|
|
83
|
+
const pool = new pg_1.default.Pool(config.postgresConnectionOptions);
|
|
84
|
+
this.pool = pool;
|
|
85
|
+
this._verbose =
|
|
86
|
+
(0, env_js_1.getEnvironmentVariable)("LANGCHAIN_VERBOSE") === "true" ??
|
|
87
|
+
!!config.verbose;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Static method to create a new `PGVectorStore` instance from a
|
|
91
|
+
* connection. It creates a table if one does not exist, and calls
|
|
92
|
+
* `connect` to return a new instance of `PGVectorStore`.
|
|
93
|
+
*
|
|
94
|
+
* @param embeddings - Embeddings instance.
|
|
95
|
+
* @param fields - `PGVectorStoreArgs` instance.
|
|
96
|
+
* @returns A new instance of `PGVectorStore`.
|
|
97
|
+
*/
|
|
98
|
+
static async initialize(embeddings, config) {
|
|
99
|
+
const postgresqlVectorStore = new PGVectorStore(embeddings, config);
|
|
100
|
+
await postgresqlVectorStore._initializeClient();
|
|
101
|
+
await postgresqlVectorStore.ensureTableInDatabase();
|
|
102
|
+
return postgresqlVectorStore;
|
|
103
|
+
}
|
|
104
|
+
async _initializeClient() {
|
|
105
|
+
this.client = await this.pool.connect();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Method to add documents to the vector store. It converts the documents into
|
|
109
|
+
* vectors, and adds them to the store.
|
|
110
|
+
*
|
|
111
|
+
* @param documents - Array of `Document` instances.
|
|
112
|
+
* @returns Promise that resolves when the documents have been added.
|
|
113
|
+
*/
|
|
114
|
+
async addDocuments(documents) {
|
|
115
|
+
const texts = documents.map(({ pageContent }) => pageContent);
|
|
116
|
+
return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Generates the SQL placeholders for a specific row at the provided index.
|
|
120
|
+
*
|
|
121
|
+
* @param index - The index of the row for which placeholders need to be generated.
|
|
122
|
+
* @returns The SQL placeholders for the row values.
|
|
123
|
+
*/
|
|
124
|
+
generatePlaceholderForRowAt(index) {
|
|
125
|
+
const base = index * 3;
|
|
126
|
+
return `($${base + 1}, $${base + 2}, $${base + 3})`;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Constructs the SQL query for inserting rows into the specified table.
|
|
130
|
+
*
|
|
131
|
+
* @param rows - The rows of data to be inserted, consisting of values and records.
|
|
132
|
+
* @param chunkIndex - The starting index for generating query placeholders based on chunk positioning.
|
|
133
|
+
* @returns The complete SQL INSERT INTO query string.
|
|
134
|
+
*/
|
|
135
|
+
buildInsertQuery(rows, chunkIndex) {
|
|
136
|
+
const valuesPlaceholders = rows
|
|
137
|
+
.map((_, j) => this.generatePlaceholderForRowAt(chunkIndex + j))
|
|
138
|
+
.join(", ");
|
|
139
|
+
const text = `
|
|
140
|
+
INSERT INTO ${this.tableName}(
|
|
141
|
+
${this.contentColumnName},
|
|
142
|
+
${this.vectorColumnName},
|
|
143
|
+
${this.metadataColumnName}
|
|
144
|
+
)
|
|
145
|
+
VALUES ${valuesPlaceholders}
|
|
146
|
+
`;
|
|
147
|
+
return text;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Method to add vectors to the vector store. It converts the vectors into
|
|
151
|
+
* rows and inserts them into the database.
|
|
152
|
+
*
|
|
153
|
+
* @param vectors - Array of vectors.
|
|
154
|
+
* @param documents - Array of `Document` instances.
|
|
155
|
+
* @returns Promise that resolves when the vectors have been added.
|
|
156
|
+
*/
|
|
157
|
+
async addVectors(vectors, documents) {
|
|
158
|
+
const rows = vectors.map((embedding, idx) => {
|
|
159
|
+
const embeddingString = `[${embedding.join(",")}]`;
|
|
160
|
+
return [
|
|
161
|
+
documents[idx].pageContent,
|
|
162
|
+
embeddingString,
|
|
163
|
+
documents[idx].metadata,
|
|
164
|
+
];
|
|
165
|
+
});
|
|
166
|
+
const chunkSize = 500;
|
|
167
|
+
for (let i = 0; i < rows.length; i += chunkSize) {
|
|
168
|
+
const chunk = rows.slice(i, i + chunkSize);
|
|
169
|
+
const insertQuery = this.buildInsertQuery(chunk, i);
|
|
170
|
+
const flatValues = chunk.flat();
|
|
171
|
+
try {
|
|
172
|
+
await this.pool.query(insertQuery, flatValues);
|
|
173
|
+
}
|
|
174
|
+
catch (e) {
|
|
175
|
+
console.error(e);
|
|
176
|
+
throw new Error(`Error inserting: ${chunk[1]}`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Method to perform a similarity search in the vector store. It returns
|
|
182
|
+
* the `k` most similar documents to the query vector, along with their
|
|
183
|
+
* similarity scores.
|
|
184
|
+
*
|
|
185
|
+
* @param query - Query vector.
|
|
186
|
+
* @param k - Number of most similar documents to return.
|
|
187
|
+
* @param filter - Optional filter to apply to the search.
|
|
188
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
189
|
+
*/
|
|
190
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
191
|
+
const embeddingString = `[${query.join(",")}]`;
|
|
192
|
+
const _filter = filter ?? "{}";
|
|
193
|
+
const queryString = `
|
|
194
|
+
SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
|
|
195
|
+
FROM ${this.tableName}
|
|
196
|
+
WHERE ${this.metadataColumnName} @> $2
|
|
197
|
+
ORDER BY "_distance" ASC
|
|
198
|
+
LIMIT $3;`;
|
|
199
|
+
const documents = (await this.pool.query(queryString, [embeddingString, _filter, k])).rows;
|
|
200
|
+
const results = [];
|
|
201
|
+
for (const doc of documents) {
|
|
202
|
+
if (doc._distance != null && doc[this.contentColumnName] != null) {
|
|
203
|
+
const document = new document_js_1.Document({
|
|
204
|
+
pageContent: doc[this.contentColumnName],
|
|
205
|
+
metadata: doc[this.metadataColumnName],
|
|
206
|
+
});
|
|
207
|
+
results.push([document, doc._distance]);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return results;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Method to ensure the existence of the table in the database. It creates
|
|
214
|
+
* the table if it does not already exist.
|
|
215
|
+
*
|
|
216
|
+
* @returns Promise that resolves when the table has been ensured.
|
|
217
|
+
*/
|
|
218
|
+
async ensureTableInDatabase() {
|
|
219
|
+
await this.pool.query("CREATE EXTENSION IF NOT EXISTS vector;");
|
|
220
|
+
await this.pool.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";');
|
|
221
|
+
await this.pool.query(`
|
|
222
|
+
CREATE TABLE IF NOT EXISTS ${this.tableName} (
|
|
223
|
+
"${this.idColumnName}" uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY,
|
|
224
|
+
"${this.contentColumnName}" text,
|
|
225
|
+
"${this.metadataColumnName}" jsonb,
|
|
226
|
+
"${this.vectorColumnName}" vector
|
|
227
|
+
);
|
|
228
|
+
`);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Static method to create a new `PGVectorStore` instance from an
|
|
232
|
+
* array of texts and their metadata. It converts the texts into
|
|
233
|
+
* `Document` instances and adds them to the store.
|
|
234
|
+
*
|
|
235
|
+
* @param texts - Array of texts.
|
|
236
|
+
* @param metadatas - Array of metadata objects or a single metadata object.
|
|
237
|
+
* @param embeddings - Embeddings instance.
|
|
238
|
+
* @param dbConfig - `PGVectorStoreArgs` instance.
|
|
239
|
+
* @returns Promise that resolves with a new instance of `PGVectorStore`.
|
|
240
|
+
*/
|
|
241
|
+
static async fromTexts(texts, metadatas, embeddings, dbConfig) {
|
|
242
|
+
const docs = [];
|
|
243
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
244
|
+
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
|
|
245
|
+
const newDoc = new document_js_1.Document({
|
|
246
|
+
pageContent: texts[i],
|
|
247
|
+
metadata,
|
|
248
|
+
});
|
|
249
|
+
docs.push(newDoc);
|
|
250
|
+
}
|
|
251
|
+
return PGVectorStore.fromDocuments(docs, embeddings, dbConfig);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Static method to create a new `PGVectorStore` instance from an
|
|
255
|
+
* array of `Document` instances. It adds the documents to the store.
|
|
256
|
+
*
|
|
257
|
+
* @param docs - Array of `Document` instances.
|
|
258
|
+
* @param embeddings - Embeddings instance.
|
|
259
|
+
* @param dbConfig - `PGVectorStoreArgs` instance.
|
|
260
|
+
* @returns Promise that resolves with a new instance of `PGVectorStore`.
|
|
261
|
+
*/
|
|
262
|
+
static async fromDocuments(docs, embeddings, dbConfig) {
|
|
263
|
+
const instance = await PGVectorStore.initialize(embeddings, dbConfig);
|
|
264
|
+
await instance.addDocuments(docs);
|
|
265
|
+
return instance;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Closes all the clients in the pool and terminates the pool.
|
|
269
|
+
*
|
|
270
|
+
* @returns Promise that resolves when all clients are closed and the pool is terminated.
|
|
271
|
+
*/
|
|
272
|
+
async end() {
|
|
273
|
+
await this.client?.release();
|
|
274
|
+
return this.pool.end();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
exports.PGVectorStore = PGVectorStore;
|