langchain 0.0.62 → 0.0.63
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/chains/base.cjs +3 -2
- package/dist/chains/base.js +3 -2
- package/dist/chat_models/base.cjs +1 -0
- package/dist/chat_models/base.js +1 -0
- package/dist/llms/base.cjs +1 -0
- package/dist/llms/base.js +1 -0
- package/dist/vectorstores/chroma.cjs +25 -27
- package/dist/vectorstores/chroma.d.ts +8 -4
- package/dist/vectorstores/chroma.js +25 -27
- package/package.json +3 -2
package/dist/chains/base.cjs
CHANGED
|
@@ -21,11 +21,11 @@ class BaseChain extends index_js_2.BaseLangChain {
|
|
|
21
21
|
async run(
|
|
22
22
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
23
|
input, callbacks) {
|
|
24
|
-
const isKeylessInput = this.inputKeys.length
|
|
24
|
+
const isKeylessInput = this.inputKeys.length <= 1;
|
|
25
25
|
if (!isKeylessInput) {
|
|
26
26
|
throw new Error(`Chain ${this._chainType()} expects multiple inputs, cannot use 'run' `);
|
|
27
27
|
}
|
|
28
|
-
const values = { [this.inputKeys[0]]: input };
|
|
28
|
+
const values = this.inputKeys.length ? { [this.inputKeys[0]]: input } : {};
|
|
29
29
|
const returnValues = await this.call(values, callbacks);
|
|
30
30
|
const keys = Object.keys(returnValues);
|
|
31
31
|
if (keys.length === 1) {
|
|
@@ -63,6 +63,7 @@ class BaseChain extends index_js_2.BaseLangChain {
|
|
|
63
63
|
// add the runManager's currentRunId to the outputValues
|
|
64
64
|
Object.defineProperty(outputValues, index_js_1.RUN_KEY, {
|
|
65
65
|
value: runManager ? { runId: runManager?.runId } : undefined,
|
|
66
|
+
configurable: true,
|
|
66
67
|
});
|
|
67
68
|
return outputValues;
|
|
68
69
|
}
|
package/dist/chains/base.js
CHANGED
|
@@ -18,11 +18,11 @@ export class BaseChain extends BaseLangChain {
|
|
|
18
18
|
async run(
|
|
19
19
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
20
|
input, callbacks) {
|
|
21
|
-
const isKeylessInput = this.inputKeys.length
|
|
21
|
+
const isKeylessInput = this.inputKeys.length <= 1;
|
|
22
22
|
if (!isKeylessInput) {
|
|
23
23
|
throw new Error(`Chain ${this._chainType()} expects multiple inputs, cannot use 'run' `);
|
|
24
24
|
}
|
|
25
|
-
const values = { [this.inputKeys[0]]: input };
|
|
25
|
+
const values = this.inputKeys.length ? { [this.inputKeys[0]]: input } : {};
|
|
26
26
|
const returnValues = await this.call(values, callbacks);
|
|
27
27
|
const keys = Object.keys(returnValues);
|
|
28
28
|
if (keys.length === 1) {
|
|
@@ -60,6 +60,7 @@ export class BaseChain extends BaseLangChain {
|
|
|
60
60
|
// add the runManager's currentRunId to the outputValues
|
|
61
61
|
Object.defineProperty(outputValues, RUN_KEY, {
|
|
62
62
|
value: runManager ? { runId: runManager?.runId } : undefined,
|
|
63
|
+
configurable: true,
|
|
63
64
|
});
|
|
64
65
|
return outputValues;
|
|
65
66
|
}
|
|
@@ -37,6 +37,7 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
|
|
|
37
37
|
await runManager?.handleLLMEnd(output);
|
|
38
38
|
Object.defineProperty(output, index_js_1.RUN_KEY, {
|
|
39
39
|
value: runManager ? { runId: runManager?.runId } : undefined,
|
|
40
|
+
configurable: true,
|
|
40
41
|
});
|
|
41
42
|
return output;
|
|
42
43
|
}
|
package/dist/chat_models/base.js
CHANGED
package/dist/llms/base.cjs
CHANGED
|
@@ -58,6 +58,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
|
|
|
58
58
|
// it isnt included when listing the keys of the output object.
|
|
59
59
|
Object.defineProperty(output, index_js_2.RUN_KEY, {
|
|
60
60
|
value: runManager ? { runId: runManager?.runId } : undefined,
|
|
61
|
+
configurable: true,
|
|
61
62
|
});
|
|
62
63
|
return output;
|
|
63
64
|
}
|
package/dist/llms/base.js
CHANGED
|
@@ -55,6 +55,7 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
55
55
|
// it isnt included when listing the keys of the output object.
|
|
56
56
|
Object.defineProperty(output, RUN_KEY, {
|
|
57
57
|
value: runManager ? { runId: runManager?.runId } : undefined,
|
|
58
|
+
configurable: true,
|
|
58
59
|
});
|
|
59
60
|
return output;
|
|
60
61
|
}
|
|
@@ -13,6 +13,12 @@ class Chroma extends base_js_1.VectorStore {
|
|
|
13
13
|
writable: true,
|
|
14
14
|
value: void 0
|
|
15
15
|
});
|
|
16
|
+
Object.defineProperty(this, "collection", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
16
22
|
Object.defineProperty(this, "collectionName", {
|
|
17
23
|
enumerable: true,
|
|
18
24
|
configurable: true,
|
|
@@ -31,44 +37,36 @@ class Chroma extends base_js_1.VectorStore {
|
|
|
31
37
|
writable: true,
|
|
32
38
|
value: void 0
|
|
33
39
|
});
|
|
34
|
-
this.index = args.index;
|
|
35
40
|
this.numDimensions = args.numDimensions;
|
|
36
41
|
this.embeddings = embeddings;
|
|
37
42
|
this.collectionName = ensureCollectionName(args.collectionName);
|
|
38
|
-
|
|
43
|
+
if ("index" in args) {
|
|
44
|
+
this.index = args.index;
|
|
45
|
+
}
|
|
46
|
+
else if ("url" in args) {
|
|
47
|
+
this.url = args.url || "http://localhost:8000";
|
|
48
|
+
}
|
|
39
49
|
}
|
|
40
50
|
async addDocuments(documents) {
|
|
41
51
|
const texts = documents.map(({ pageContent }) => pageContent);
|
|
42
52
|
await this.addVectors(await this.embeddings.embedDocuments(texts), documents);
|
|
43
53
|
}
|
|
44
54
|
async ensureCollection() {
|
|
45
|
-
if (!this.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
await this.index.createCollection(this.collectionName);
|
|
50
|
-
}
|
|
51
|
-
catch {
|
|
52
|
-
// ignore error
|
|
55
|
+
if (!this.collection) {
|
|
56
|
+
if (!this.index) {
|
|
57
|
+
const { ChromaClient } = await Chroma.imports();
|
|
58
|
+
this.index = new ChromaClient(this.url);
|
|
53
59
|
}
|
|
60
|
+
this.collection = await this.index.getOrCreateCollection(this.collectionName);
|
|
54
61
|
}
|
|
62
|
+
return this.collection;
|
|
55
63
|
}
|
|
56
64
|
async addVectors(vectors, documents) {
|
|
57
65
|
if (vectors.length === 0) {
|
|
58
66
|
return;
|
|
59
67
|
}
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
this.numDimensions = vectors[0].length;
|
|
63
|
-
}
|
|
64
|
-
const { ChromaClient } = await Chroma.imports();
|
|
65
|
-
this.index = new ChromaClient(this.url);
|
|
66
|
-
try {
|
|
67
|
-
await this.index.createCollection(this.collectionName);
|
|
68
|
-
}
|
|
69
|
-
catch {
|
|
70
|
-
// ignore error
|
|
71
|
-
}
|
|
68
|
+
if (this.numDimensions === undefined) {
|
|
69
|
+
this.numDimensions = vectors[0].length;
|
|
72
70
|
}
|
|
73
71
|
if (vectors.length !== documents.length) {
|
|
74
72
|
throw new Error(`Vectors and metadatas must have the same length`);
|
|
@@ -76,19 +74,19 @@ class Chroma extends base_js_1.VectorStore {
|
|
|
76
74
|
if (vectors[0].length !== this.numDimensions) {
|
|
77
75
|
throw new Error(`Vectors must have the same length as the number of dimensions (${this.numDimensions})`);
|
|
78
76
|
}
|
|
79
|
-
const collection = await this.
|
|
77
|
+
const collection = await this.ensureCollection();
|
|
80
78
|
const docstoreSize = await collection.count();
|
|
81
79
|
await collection.add(Array.from({ length: vectors.length }, (_, i) => (docstoreSize + i).toString()), vectors, documents.map(({ metadata }) => metadata), documents.map(({ pageContent }) => pageContent));
|
|
82
80
|
}
|
|
83
81
|
async similaritySearchVectorWithScore(query, k) {
|
|
84
|
-
|
|
85
|
-
throw new Error("Vector store not initialised yet. Try calling `addTexts` first.");
|
|
86
|
-
}
|
|
87
|
-
const collection = await this.index.getCollection(this.collectionName);
|
|
82
|
+
const collection = await this.ensureCollection();
|
|
88
83
|
// similaritySearchVectorWithScore supports one query vector at a time
|
|
89
84
|
// chroma supports multiple query vectors at a time
|
|
90
85
|
const result = await collection.query(query, k);
|
|
91
86
|
const { ids, distances, documents, metadatas } = result;
|
|
87
|
+
if (!ids || !distances || !documents || !metadatas) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
92
90
|
// get the result data from the first and only query vector
|
|
93
91
|
const [firstIds] = ids;
|
|
94
92
|
const [firstDistances] = distances;
|
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import type { ChromaClient as ChromaClientT } from "chromadb";
|
|
1
|
+
import type { ChromaClient as ChromaClientT, Collection } from "chromadb";
|
|
2
2
|
import { Embeddings } from "../embeddings/base.js";
|
|
3
3
|
import { VectorStore } from "./base.js";
|
|
4
4
|
import { Document } from "../document.js";
|
|
5
|
-
export
|
|
5
|
+
export type ChromaLibArgs = {
|
|
6
6
|
url?: string;
|
|
7
7
|
numDimensions?: number;
|
|
8
8
|
collectionName?: string;
|
|
9
|
+
} | {
|
|
9
10
|
index?: ChromaClientT;
|
|
10
|
-
|
|
11
|
+
numDimensions?: number;
|
|
12
|
+
collectionName?: string;
|
|
13
|
+
};
|
|
11
14
|
export declare class Chroma extends VectorStore {
|
|
12
15
|
index?: ChromaClientT;
|
|
16
|
+
collection?: Collection;
|
|
13
17
|
collectionName: string;
|
|
14
18
|
numDimensions?: number;
|
|
15
19
|
url: string;
|
|
16
20
|
constructor(embeddings: Embeddings, args: ChromaLibArgs);
|
|
17
21
|
addDocuments(documents: Document[]): Promise<void>;
|
|
18
|
-
ensureCollection(): Promise<
|
|
22
|
+
ensureCollection(): Promise<Collection>;
|
|
19
23
|
addVectors(vectors: number[][], documents: Document[]): Promise<void>;
|
|
20
24
|
similaritySearchVectorWithScore(query: number[], k: number): Promise<[Document<Record<string, any>>, number][]>;
|
|
21
25
|
static fromTexts(texts: string[], metadatas: object[] | object, embeddings: Embeddings, dbConfig: {
|
|
@@ -10,6 +10,12 @@ export class Chroma extends VectorStore {
|
|
|
10
10
|
writable: true,
|
|
11
11
|
value: void 0
|
|
12
12
|
});
|
|
13
|
+
Object.defineProperty(this, "collection", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
13
19
|
Object.defineProperty(this, "collectionName", {
|
|
14
20
|
enumerable: true,
|
|
15
21
|
configurable: true,
|
|
@@ -28,44 +34,36 @@ export class Chroma extends VectorStore {
|
|
|
28
34
|
writable: true,
|
|
29
35
|
value: void 0
|
|
30
36
|
});
|
|
31
|
-
this.index = args.index;
|
|
32
37
|
this.numDimensions = args.numDimensions;
|
|
33
38
|
this.embeddings = embeddings;
|
|
34
39
|
this.collectionName = ensureCollectionName(args.collectionName);
|
|
35
|
-
|
|
40
|
+
if ("index" in args) {
|
|
41
|
+
this.index = args.index;
|
|
42
|
+
}
|
|
43
|
+
else if ("url" in args) {
|
|
44
|
+
this.url = args.url || "http://localhost:8000";
|
|
45
|
+
}
|
|
36
46
|
}
|
|
37
47
|
async addDocuments(documents) {
|
|
38
48
|
const texts = documents.map(({ pageContent }) => pageContent);
|
|
39
49
|
await this.addVectors(await this.embeddings.embedDocuments(texts), documents);
|
|
40
50
|
}
|
|
41
51
|
async ensureCollection() {
|
|
42
|
-
if (!this.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
await this.index.createCollection(this.collectionName);
|
|
47
|
-
}
|
|
48
|
-
catch {
|
|
49
|
-
// ignore error
|
|
52
|
+
if (!this.collection) {
|
|
53
|
+
if (!this.index) {
|
|
54
|
+
const { ChromaClient } = await Chroma.imports();
|
|
55
|
+
this.index = new ChromaClient(this.url);
|
|
50
56
|
}
|
|
57
|
+
this.collection = await this.index.getOrCreateCollection(this.collectionName);
|
|
51
58
|
}
|
|
59
|
+
return this.collection;
|
|
52
60
|
}
|
|
53
61
|
async addVectors(vectors, documents) {
|
|
54
62
|
if (vectors.length === 0) {
|
|
55
63
|
return;
|
|
56
64
|
}
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
this.numDimensions = vectors[0].length;
|
|
60
|
-
}
|
|
61
|
-
const { ChromaClient } = await Chroma.imports();
|
|
62
|
-
this.index = new ChromaClient(this.url);
|
|
63
|
-
try {
|
|
64
|
-
await this.index.createCollection(this.collectionName);
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
// ignore error
|
|
68
|
-
}
|
|
65
|
+
if (this.numDimensions === undefined) {
|
|
66
|
+
this.numDimensions = vectors[0].length;
|
|
69
67
|
}
|
|
70
68
|
if (vectors.length !== documents.length) {
|
|
71
69
|
throw new Error(`Vectors and metadatas must have the same length`);
|
|
@@ -73,19 +71,19 @@ export class Chroma extends VectorStore {
|
|
|
73
71
|
if (vectors[0].length !== this.numDimensions) {
|
|
74
72
|
throw new Error(`Vectors must have the same length as the number of dimensions (${this.numDimensions})`);
|
|
75
73
|
}
|
|
76
|
-
const collection = await this.
|
|
74
|
+
const collection = await this.ensureCollection();
|
|
77
75
|
const docstoreSize = await collection.count();
|
|
78
76
|
await collection.add(Array.from({ length: vectors.length }, (_, i) => (docstoreSize + i).toString()), vectors, documents.map(({ metadata }) => metadata), documents.map(({ pageContent }) => pageContent));
|
|
79
77
|
}
|
|
80
78
|
async similaritySearchVectorWithScore(query, k) {
|
|
81
|
-
|
|
82
|
-
throw new Error("Vector store not initialised yet. Try calling `addTexts` first.");
|
|
83
|
-
}
|
|
84
|
-
const collection = await this.index.getCollection(this.collectionName);
|
|
79
|
+
const collection = await this.ensureCollection();
|
|
85
80
|
// similaritySearchVectorWithScore supports one query vector at a time
|
|
86
81
|
// chroma supports multiple query vectors at a time
|
|
87
82
|
const result = await collection.query(query, k);
|
|
88
83
|
const { ids, distances, documents, metadatas } = result;
|
|
84
|
+
if (!ids || !distances || !documents || !metadatas) {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
89
87
|
// get the result data from the first and only query vector
|
|
90
88
|
const [firstIds] = ids;
|
|
91
89
|
const [firstDistances] = distances;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langchain",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.63",
|
|
4
4
|
"description": "Typescript bindings for langchain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -302,6 +302,7 @@
|
|
|
302
302
|
"@typescript-eslint/parser": "^5.51.0",
|
|
303
303
|
"axios": "^0.26.0",
|
|
304
304
|
"cheerio": "^1.0.0-rc.12",
|
|
305
|
+
"chromadb": "^1.4.0",
|
|
305
306
|
"cohere-ai": "^5.0.2",
|
|
306
307
|
"d3-dsv": "^2.0.0",
|
|
307
308
|
"dotenv": "^16.0.3",
|
|
@@ -346,7 +347,7 @@
|
|
|
346
347
|
"@zilliz/milvus2-sdk-node": "^2.2.0",
|
|
347
348
|
"axios": "^0.26.0",
|
|
348
349
|
"cheerio": "^1.0.0-rc.12",
|
|
349
|
-
"chromadb": "^1.
|
|
350
|
+
"chromadb": "^1.4.0",
|
|
350
351
|
"cohere-ai": "^5.0.2",
|
|
351
352
|
"d3-dsv": "^2.0.0",
|
|
352
353
|
"epub2": "^3.0.1",
|