langchain 0.0.174 → 0.0.176

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 (69) hide show
  1. package/dist/chat_models/googlevertexai/common.cjs +46 -7
  2. package/dist/chat_models/googlevertexai/common.d.ts +7 -2
  3. package/dist/chat_models/googlevertexai/common.js +47 -8
  4. package/dist/chat_models/googlevertexai/index.cjs +4 -3
  5. package/dist/chat_models/googlevertexai/index.js +4 -3
  6. package/dist/chat_models/googlevertexai/web.cjs +2 -1
  7. package/dist/chat_models/googlevertexai/web.js +2 -1
  8. package/dist/embeddings/googlevertexai.cjs +1 -1
  9. package/dist/embeddings/googlevertexai.js +1 -1
  10. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +2 -2
  11. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +1 -1
  12. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  13. package/dist/experimental/multimodal_embeddings/googlevertexai.js +2 -2
  14. package/dist/experimental/plan_and_execute/agent_executor.cjs +7 -4
  15. package/dist/experimental/plan_and_execute/agent_executor.d.ts +4 -3
  16. package/dist/experimental/plan_and_execute/agent_executor.js +8 -5
  17. package/dist/experimental/plan_and_execute/prompt.cjs +25 -9
  18. package/dist/experimental/plan_and_execute/prompt.d.ts +9 -1
  19. package/dist/experimental/plan_and_execute/prompt.js +23 -8
  20. package/dist/llms/googlevertexai/common.cjs +46 -13
  21. package/dist/llms/googlevertexai/common.d.ts +8 -3
  22. package/dist/llms/googlevertexai/common.js +46 -13
  23. package/dist/llms/googlevertexai/index.cjs +4 -3
  24. package/dist/llms/googlevertexai/index.js +4 -3
  25. package/dist/llms/googlevertexai/web.cjs +2 -1
  26. package/dist/llms/googlevertexai/web.js +2 -1
  27. package/dist/load/import_constants.cjs +4 -0
  28. package/dist/load/import_constants.js +4 -0
  29. package/dist/storage/convex.cjs +145 -0
  30. package/dist/storage/convex.d.ts +85 -0
  31. package/dist/storage/convex.js +141 -0
  32. package/dist/stores/message/convex.cjs +120 -0
  33. package/dist/stores/message/convex.d.ts +60 -0
  34. package/dist/stores/message/convex.js +116 -0
  35. package/dist/types/googlevertexai-types.d.ts +12 -10
  36. package/dist/util/convex.cjs +77 -0
  37. package/dist/util/convex.d.ts +26 -0
  38. package/dist/util/convex.js +74 -0
  39. package/dist/util/googlevertexai-connection.cjs +298 -10
  40. package/dist/util/googlevertexai-connection.d.ts +76 -7
  41. package/dist/util/googlevertexai-connection.js +294 -9
  42. package/dist/util/googlevertexai-gauth.cjs +36 -0
  43. package/dist/util/googlevertexai-gauth.d.ts +8 -0
  44. package/dist/util/googlevertexai-gauth.js +32 -0
  45. package/dist/util/googlevertexai-webauth.cjs +38 -2
  46. package/dist/util/googlevertexai-webauth.d.ts +2 -6
  47. package/dist/util/googlevertexai-webauth.js +38 -2
  48. package/dist/vectorstores/convex.cjs +177 -0
  49. package/dist/vectorstores/convex.d.ts +113 -0
  50. package/dist/vectorstores/convex.js +173 -0
  51. package/dist/vectorstores/googlevertexai.d.ts +4 -4
  52. package/dist/vectorstores/milvus.cjs +4 -2
  53. package/dist/vectorstores/milvus.js +4 -2
  54. package/dist/vectorstores/vercel_postgres.cjs +29 -7
  55. package/dist/vectorstores/vercel_postgres.d.ts +1 -1
  56. package/dist/vectorstores/vercel_postgres.js +29 -7
  57. package/package.json +38 -1
  58. package/storage/convex.cjs +1 -0
  59. package/storage/convex.d.ts +1 -0
  60. package/storage/convex.js +1 -0
  61. package/stores/message/convex.cjs +1 -0
  62. package/stores/message/convex.d.ts +1 -0
  63. package/stores/message/convex.js +1 -0
  64. package/util/convex.cjs +1 -0
  65. package/util/convex.d.ts +1 -0
  66. package/util/convex.js +1 -0
  67. package/vectorstores/convex.cjs +1 -0
  68. package/vectorstores/convex.d.ts +1 -0
  69. package/vectorstores/convex.js +1 -0
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConvexVectorStore = void 0;
4
+ // eslint-disable-next-line import/no-extraneous-dependencies
5
+ const server_1 = require("convex/server");
6
+ const document_js_1 = require("../document.cjs");
7
+ const base_js_1 = require("./base.cjs");
8
+ /**
9
+ * Class that is a wrapper around Convex storage and vector search. It is used
10
+ * to insert embeddings in Convex documents with a vector search index,
11
+ * and perform a vector search on them.
12
+ *
13
+ * ConvexVectorStore does NOT implement maxMarginalRelevanceSearch.
14
+ */
15
+ class ConvexVectorStore extends base_js_1.VectorStore {
16
+ _vectorstoreType() {
17
+ return "convex";
18
+ }
19
+ constructor(embeddings, config) {
20
+ super(embeddings, config);
21
+ Object.defineProperty(this, "ctx", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "table", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "index", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "textField", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "embeddingField", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "metadataField", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "insert", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ Object.defineProperty(this, "get", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
69
+ this.ctx = config.ctx;
70
+ this.table = config.table ?? "documents";
71
+ this.index = config.index ?? "byEmbedding";
72
+ this.textField = config.textField ?? "text";
73
+ this.embeddingField =
74
+ config.embeddingField ?? "embedding";
75
+ this.metadataField =
76
+ config.metadataField ?? "metadata";
77
+ this.insert =
78
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
+ config.insert ?? (0, server_1.makeFunctionReference)("langchain/db:insert");
80
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
+ this.get = config.get ?? (0, server_1.makeFunctionReference)("langchain/db:get");
82
+ }
83
+ /**
84
+ * Add vectors and their corresponding documents to the Convex table.
85
+ * @param vectors Vectors to be added.
86
+ * @param documents Corresponding documents to be added.
87
+ * @returns Promise that resolves when the vectors and documents have been added.
88
+ */
89
+ async addVectors(vectors, documents) {
90
+ const convexDocuments = vectors.map((embedding, idx) => ({
91
+ [this.textField]: documents[idx].pageContent,
92
+ [this.embeddingField]: embedding,
93
+ [this.metadataField]: documents[idx].metadata,
94
+ }));
95
+ // TODO: Remove chunking when Convex handles the concurrent requests correctly
96
+ const PAGE_SIZE = 16;
97
+ for (let i = 0; i < convexDocuments.length; i += PAGE_SIZE) {
98
+ await Promise.all(convexDocuments.slice(i, i + PAGE_SIZE).map((document) => this.ctx.runMutation(this.insert, {
99
+ table: this.table,
100
+ document,
101
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
+ })));
103
+ }
104
+ }
105
+ /**
106
+ * Add documents to the Convex table. It first converts
107
+ * the documents to vectors using the embeddings and then calls the
108
+ * addVectors method.
109
+ * @param documents Documents to be added.
110
+ * @returns Promise that resolves when the documents have been added.
111
+ */
112
+ async addDocuments(documents) {
113
+ const texts = documents.map(({ pageContent }) => pageContent);
114
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
115
+ }
116
+ /**
117
+ * Similarity search on the vectors stored in the
118
+ * Convex table. It returns a list of documents and their
119
+ * corresponding similarity scores.
120
+ * @param query Query vector for the similarity search.
121
+ * @param k Number of nearest neighbors to return.
122
+ * @param filter Optional filter to be applied.
123
+ * @returns Promise that resolves to a list of documents and their corresponding similarity scores.
124
+ */
125
+ async similaritySearchVectorWithScore(query, k, filter) {
126
+ const idsAndScores = await this.ctx.vectorSearch(this.table, this.index, {
127
+ vector: query,
128
+ limit: k,
129
+ filter: filter?.filter,
130
+ });
131
+ const documents = await Promise.all(idsAndScores.map(({ _id }) =>
132
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
+ this.ctx.runQuery(this.get, { id: _id })));
134
+ return documents.map(({ [this.textField]: text, [this.embeddingField]: embedding, [this.metadataField]: metadata, }, idx) => [
135
+ new document_js_1.Document({
136
+ pageContent: text,
137
+ metadata: {
138
+ ...metadata,
139
+ ...(filter?.includeEmbeddings ? { embedding } : null),
140
+ },
141
+ }),
142
+ idsAndScores[idx]._score,
143
+ ]);
144
+ }
145
+ /**
146
+ * Static method to create an instance of ConvexVectorStore from a
147
+ * list of texts. It first converts the texts to vectors and then adds
148
+ * them to the Convex table.
149
+ * @param texts List of texts to be converted to vectors.
150
+ * @param metadatas Metadata for the texts.
151
+ * @param embeddings Embeddings to be used for conversion.
152
+ * @param dbConfig Database configuration for Convex.
153
+ * @returns Promise that resolves to a new instance of ConvexVectorStore.
154
+ */
155
+ static async fromTexts(texts, metadatas, embeddings, dbConfig) {
156
+ const docs = texts.map((text, i) => new document_js_1.Document({
157
+ pageContent: text,
158
+ metadata: Array.isArray(metadatas) ? metadatas[i] : metadatas,
159
+ }));
160
+ return ConvexVectorStore.fromDocuments(docs, embeddings, dbConfig);
161
+ }
162
+ /**
163
+ * Static method to create an instance of ConvexVectorStore from a
164
+ * list of documents. It first converts the documents to vectors and then
165
+ * adds them to the Convex table.
166
+ * @param docs List of documents to be converted to vectors.
167
+ * @param embeddings Embeddings to be used for conversion.
168
+ * @param dbConfig Database configuration for Convex.
169
+ * @returns Promise that resolves to a new instance of ConvexVectorStore.
170
+ */
171
+ static async fromDocuments(docs, embeddings, dbConfig) {
172
+ const instance = new this(embeddings, dbConfig);
173
+ await instance.addDocuments(docs);
174
+ return instance;
175
+ }
176
+ }
177
+ exports.ConvexVectorStore = ConvexVectorStore;
@@ -0,0 +1,113 @@
1
+ import { DocumentByInfo, FieldPaths, FilterExpression, FunctionReference, GenericActionCtx, GenericDataModel, GenericTableInfo, NamedTableInfo, NamedVectorIndex, TableNamesInDataModel, VectorFilterBuilder, VectorIndexNames } from "convex/server";
2
+ import { Document } from "../document.js";
3
+ import { Embeddings } from "../embeddings/base.js";
4
+ import { VectorStore } from "./base.js";
5
+ /**
6
+ * Type that defines the config required to initialize the
7
+ * ConvexVectorStore class. It includes the table name,
8
+ * index name, text field name, and embedding field name.
9
+ */
10
+ export type ConvexVectorStoreConfig<DataModel extends GenericDataModel, TableName extends TableNamesInDataModel<DataModel>, IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>, TextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, EmbeddingFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, MetadataFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, InsertMutation extends FunctionReference<"mutation", "internal", {
11
+ table: string;
12
+ document: object;
13
+ }>, GetQuery extends FunctionReference<"query", "internal", {
14
+ id: string;
15
+ }, object | null>> = {
16
+ readonly ctx: GenericActionCtx<DataModel>;
17
+ readonly table?: TableName;
18
+ readonly index?: IndexName;
19
+ readonly textField?: TextFieldName;
20
+ readonly embeddingField?: EmbeddingFieldName;
21
+ readonly metadataField?: MetadataFieldName;
22
+ readonly insert?: InsertMutation;
23
+ readonly get?: GetQuery;
24
+ };
25
+ /**
26
+ * Class that is a wrapper around Convex storage and vector search. It is used
27
+ * to insert embeddings in Convex documents with a vector search index,
28
+ * and perform a vector search on them.
29
+ *
30
+ * ConvexVectorStore does NOT implement maxMarginalRelevanceSearch.
31
+ */
32
+ export declare class ConvexVectorStore<DataModel extends GenericDataModel, TableName extends TableNamesInDataModel<DataModel>, IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>, TextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, EmbeddingFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, MetadataFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, InsertMutation extends FunctionReference<"mutation", "internal", {
33
+ table: string;
34
+ document: object;
35
+ }>, GetQuery extends FunctionReference<"query", "internal", {
36
+ id: string;
37
+ }, object | null>> extends VectorStore {
38
+ /**
39
+ * Type that defines the filter used in the
40
+ * similaritySearchVectorWithScore and maxMarginalRelevanceSearch methods.
41
+ * It includes limit, filter and a flag to include embeddings.
42
+ */
43
+ FilterType: {
44
+ filter?: (q: VectorFilterBuilder<DocumentByInfo<GenericTableInfo>, NamedVectorIndex<NamedTableInfo<DataModel, TableName>, IndexName>>) => FilterExpression<boolean>;
45
+ includeEmbeddings?: boolean;
46
+ };
47
+ private readonly ctx;
48
+ private readonly table;
49
+ private readonly index;
50
+ private readonly textField;
51
+ private readonly embeddingField;
52
+ private readonly metadataField;
53
+ private readonly insert;
54
+ private readonly get;
55
+ _vectorstoreType(): string;
56
+ constructor(embeddings: Embeddings, config: ConvexVectorStoreConfig<DataModel, TableName, IndexName, TextFieldName, EmbeddingFieldName, MetadataFieldName, InsertMutation, GetQuery>);
57
+ /**
58
+ * Add vectors and their corresponding documents to the Convex table.
59
+ * @param vectors Vectors to be added.
60
+ * @param documents Corresponding documents to be added.
61
+ * @returns Promise that resolves when the vectors and documents have been added.
62
+ */
63
+ addVectors(vectors: number[][], documents: Document[]): Promise<void>;
64
+ /**
65
+ * Add documents to the Convex table. It first converts
66
+ * the documents to vectors using the embeddings and then calls the
67
+ * addVectors method.
68
+ * @param documents Documents to be added.
69
+ * @returns Promise that resolves when the documents have been added.
70
+ */
71
+ addDocuments(documents: Document[]): Promise<void>;
72
+ /**
73
+ * Similarity search on the vectors stored in the
74
+ * Convex table. It returns a list of documents and their
75
+ * corresponding similarity scores.
76
+ * @param query Query vector for the similarity search.
77
+ * @param k Number of nearest neighbors to return.
78
+ * @param filter Optional filter to be applied.
79
+ * @returns Promise that resolves to a list of documents and their corresponding similarity scores.
80
+ */
81
+ similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[Document, number][]>;
82
+ /**
83
+ * Static method to create an instance of ConvexVectorStore from a
84
+ * list of texts. It first converts the texts to vectors and then adds
85
+ * them to the Convex table.
86
+ * @param texts List of texts to be converted to vectors.
87
+ * @param metadatas Metadata for the texts.
88
+ * @param embeddings Embeddings to be used for conversion.
89
+ * @param dbConfig Database configuration for Convex.
90
+ * @returns Promise that resolves to a new instance of ConvexVectorStore.
91
+ */
92
+ static fromTexts<DataModel extends GenericDataModel, TableName extends TableNamesInDataModel<DataModel>, IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>, TextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, EmbeddingFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, MetadataFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, InsertMutation extends FunctionReference<"mutation", "internal", {
93
+ table: string;
94
+ document: object;
95
+ }>, GetQuery extends FunctionReference<"query", "internal", {
96
+ id: string;
97
+ }, object | null>>(texts: string[], metadatas: object[] | object, embeddings: Embeddings, dbConfig: ConvexVectorStoreConfig<DataModel, TableName, IndexName, TextFieldName, EmbeddingFieldName, MetadataFieldName, InsertMutation, GetQuery>): Promise<ConvexVectorStore<DataModel, TableName, IndexName, TextFieldName, EmbeddingFieldName, MetadataFieldName, InsertMutation, GetQuery>>;
98
+ /**
99
+ * Static method to create an instance of ConvexVectorStore from a
100
+ * list of documents. It first converts the documents to vectors and then
101
+ * adds them to the Convex table.
102
+ * @param docs List of documents to be converted to vectors.
103
+ * @param embeddings Embeddings to be used for conversion.
104
+ * @param dbConfig Database configuration for Convex.
105
+ * @returns Promise that resolves to a new instance of ConvexVectorStore.
106
+ */
107
+ static fromDocuments<DataModel extends GenericDataModel, TableName extends TableNamesInDataModel<DataModel>, IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>>, TextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, EmbeddingFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, MetadataFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>>, InsertMutation extends FunctionReference<"mutation", "internal", {
108
+ table: string;
109
+ document: object;
110
+ }>, GetQuery extends FunctionReference<"query", "internal", {
111
+ id: string;
112
+ }, object | null>>(docs: Document[], embeddings: Embeddings, dbConfig: ConvexVectorStoreConfig<DataModel, TableName, IndexName, TextFieldName, EmbeddingFieldName, MetadataFieldName, InsertMutation, GetQuery>): Promise<ConvexVectorStore<DataModel, TableName, IndexName, TextFieldName, EmbeddingFieldName, MetadataFieldName, InsertMutation, GetQuery>>;
113
+ }
@@ -0,0 +1,173 @@
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+ import { makeFunctionReference, } from "convex/server";
3
+ import { Document } from "../document.js";
4
+ import { VectorStore } from "./base.js";
5
+ /**
6
+ * Class that is a wrapper around Convex storage and vector search. It is used
7
+ * to insert embeddings in Convex documents with a vector search index,
8
+ * and perform a vector search on them.
9
+ *
10
+ * ConvexVectorStore does NOT implement maxMarginalRelevanceSearch.
11
+ */
12
+ export class ConvexVectorStore extends VectorStore {
13
+ _vectorstoreType() {
14
+ return "convex";
15
+ }
16
+ constructor(embeddings, config) {
17
+ super(embeddings, config);
18
+ Object.defineProperty(this, "ctx", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "table", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "index", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "textField", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "embeddingField", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "metadataField", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ Object.defineProperty(this, "insert", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ Object.defineProperty(this, "get", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
66
+ this.ctx = config.ctx;
67
+ this.table = config.table ?? "documents";
68
+ this.index = config.index ?? "byEmbedding";
69
+ this.textField = config.textField ?? "text";
70
+ this.embeddingField =
71
+ config.embeddingField ?? "embedding";
72
+ this.metadataField =
73
+ config.metadataField ?? "metadata";
74
+ this.insert =
75
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
+ config.insert ?? makeFunctionReference("langchain/db:insert");
77
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
+ this.get = config.get ?? makeFunctionReference("langchain/db:get");
79
+ }
80
+ /**
81
+ * Add vectors and their corresponding documents to the Convex table.
82
+ * @param vectors Vectors to be added.
83
+ * @param documents Corresponding documents to be added.
84
+ * @returns Promise that resolves when the vectors and documents have been added.
85
+ */
86
+ async addVectors(vectors, documents) {
87
+ const convexDocuments = vectors.map((embedding, idx) => ({
88
+ [this.textField]: documents[idx].pageContent,
89
+ [this.embeddingField]: embedding,
90
+ [this.metadataField]: documents[idx].metadata,
91
+ }));
92
+ // TODO: Remove chunking when Convex handles the concurrent requests correctly
93
+ const PAGE_SIZE = 16;
94
+ for (let i = 0; i < convexDocuments.length; i += PAGE_SIZE) {
95
+ await Promise.all(convexDocuments.slice(i, i + PAGE_SIZE).map((document) => this.ctx.runMutation(this.insert, {
96
+ table: this.table,
97
+ document,
98
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
+ })));
100
+ }
101
+ }
102
+ /**
103
+ * Add documents to the Convex table. It first converts
104
+ * the documents to vectors using the embeddings and then calls the
105
+ * addVectors method.
106
+ * @param documents Documents to be added.
107
+ * @returns Promise that resolves when the documents have been added.
108
+ */
109
+ async addDocuments(documents) {
110
+ const texts = documents.map(({ pageContent }) => pageContent);
111
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
112
+ }
113
+ /**
114
+ * Similarity search on the vectors stored in the
115
+ * Convex table. It returns a list of documents and their
116
+ * corresponding similarity scores.
117
+ * @param query Query vector for the similarity search.
118
+ * @param k Number of nearest neighbors to return.
119
+ * @param filter Optional filter to be applied.
120
+ * @returns Promise that resolves to a list of documents and their corresponding similarity scores.
121
+ */
122
+ async similaritySearchVectorWithScore(query, k, filter) {
123
+ const idsAndScores = await this.ctx.vectorSearch(this.table, this.index, {
124
+ vector: query,
125
+ limit: k,
126
+ filter: filter?.filter,
127
+ });
128
+ const documents = await Promise.all(idsAndScores.map(({ _id }) =>
129
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
+ this.ctx.runQuery(this.get, { id: _id })));
131
+ return documents.map(({ [this.textField]: text, [this.embeddingField]: embedding, [this.metadataField]: metadata, }, idx) => [
132
+ new Document({
133
+ pageContent: text,
134
+ metadata: {
135
+ ...metadata,
136
+ ...(filter?.includeEmbeddings ? { embedding } : null),
137
+ },
138
+ }),
139
+ idsAndScores[idx]._score,
140
+ ]);
141
+ }
142
+ /**
143
+ * Static method to create an instance of ConvexVectorStore from a
144
+ * list of texts. It first converts the texts to vectors and then adds
145
+ * them to the Convex table.
146
+ * @param texts List of texts to be converted to vectors.
147
+ * @param metadatas Metadata for the texts.
148
+ * @param embeddings Embeddings to be used for conversion.
149
+ * @param dbConfig Database configuration for Convex.
150
+ * @returns Promise that resolves to a new instance of ConvexVectorStore.
151
+ */
152
+ static async fromTexts(texts, metadatas, embeddings, dbConfig) {
153
+ const docs = texts.map((text, i) => new Document({
154
+ pageContent: text,
155
+ metadata: Array.isArray(metadatas) ? metadatas[i] : metadatas,
156
+ }));
157
+ return ConvexVectorStore.fromDocuments(docs, embeddings, dbConfig);
158
+ }
159
+ /**
160
+ * Static method to create an instance of ConvexVectorStore from a
161
+ * list of documents. It first converts the documents to vectors and then
162
+ * adds them to the Convex table.
163
+ * @param docs List of documents to be converted to vectors.
164
+ * @param embeddings Embeddings to be used for conversion.
165
+ * @param dbConfig Database configuration for Convex.
166
+ * @returns Promise that resolves to a new instance of ConvexVectorStore.
167
+ */
168
+ static async fromDocuments(docs, embeddings, dbConfig) {
169
+ const instance = new this(embeddings, dbConfig);
170
+ await instance.addDocuments(docs);
171
+ return instance;
172
+ }
173
+ }
@@ -4,7 +4,7 @@ import { Embeddings } from "../embeddings/base.js";
4
4
  import { Document, DocumentInput } from "../document.js";
5
5
  import { GoogleVertexAIConnection } from "../util/googlevertexai-connection.js";
6
6
  import { AsyncCaller, AsyncCallerCallOptions, AsyncCallerParams } from "../util/async_caller.js";
7
- import { GoogleVertexAIConnectionParams, GoogleResponse } from "../types/googlevertexai-types.js";
7
+ import { GoogleVertexAIConnectionParams, GoogleResponse, GoogleAbstractedClientOpsMethod } from "../types/googlevertexai-types.js";
8
8
  import { Docstore } from "../schema/index.js";
9
9
  /**
10
10
  * Allows us to create IdDocument classes that contain the ID.
@@ -36,7 +36,7 @@ declare class IndexEndpointConnection extends GoogleVertexAIConnection<AsyncCall
36
36
  indexEndpoint: string;
37
37
  constructor(fields: IndexEndpointConnectionParams, caller: AsyncCaller);
38
38
  buildUrl(): Promise<string>;
39
- buildMethod(): string;
39
+ buildMethod(): GoogleAbstractedClientOpsMethod;
40
40
  request(options: AsyncCallerCallOptions): Promise<IndexEndpointResponse>;
41
41
  }
42
42
  /**
@@ -55,7 +55,7 @@ declare class RemoveDatapointConnection extends GoogleVertexAIConnection<AsyncCa
55
55
  index: string;
56
56
  constructor(fields: RemoveDatapointParams, caller: AsyncCaller);
57
57
  buildUrl(): Promise<string>;
58
- buildMethod(): string;
58
+ buildMethod(): GoogleAbstractedClientOpsMethod;
59
59
  request(datapointIds: string[], options: AsyncCallerCallOptions): Promise<RemoveDatapointResponse>;
60
60
  }
61
61
  interface UpsertDatapointParams extends GoogleVertexAIConnectionParams<GoogleAuthOptions> {
@@ -81,7 +81,7 @@ declare class UpsertDatapointConnection extends GoogleVertexAIConnection<AsyncCa
81
81
  index: string;
82
82
  constructor(fields: UpsertDatapointParams, caller: AsyncCaller);
83
83
  buildUrl(): Promise<string>;
84
- buildMethod(): string;
84
+ buildMethod(): GoogleAbstractedClientOpsMethod;
85
85
  request(datapoints: IndexDatapoint[], options: AsyncCallerCallOptions): Promise<UpsertDatapointResponse>;
86
86
  }
87
87
  /**
@@ -577,11 +577,13 @@ function genCollectionName() {
577
577
  }
578
578
  function getTextFieldMaxLength(documents) {
579
579
  let textMaxLength = 0;
580
+ const textEncoder = new TextEncoder();
580
581
  // eslint-disable-next-line no-plusplus
581
582
  for (let i = 0; i < documents.length; i++) {
582
583
  const text = documents[i].pageContent;
583
- if (text.length > textMaxLength) {
584
- textMaxLength = text.length;
584
+ const textLengthInBytes = textEncoder.encode(text).length;
585
+ if (textLengthInBytes > textMaxLength) {
586
+ textMaxLength = textLengthInBytes;
585
587
  }
586
588
  }
587
589
  return textMaxLength;
@@ -550,11 +550,13 @@ function genCollectionName() {
550
550
  }
551
551
  function getTextFieldMaxLength(documents) {
552
552
  let textMaxLength = 0;
553
+ const textEncoder = new TextEncoder();
553
554
  // eslint-disable-next-line no-plusplus
554
555
  for (let i = 0; i < documents.length; i++) {
555
556
  const text = documents[i].pageContent;
556
- if (text.length > textMaxLength) {
557
- textMaxLength = text.length;
557
+ const textLengthInBytes = textEncoder.encode(text).length;
558
+ if (textLengthInBytes > textMaxLength) {
559
+ textMaxLength = textLengthInBytes;
558
560
  }
559
561
  }
560
562
  return textMaxLength;
@@ -206,14 +206,36 @@ class VercelPostgres extends base_js_1.VectorStore {
206
206
  */
207
207
  async similaritySearchVectorWithScore(query, k, filter) {
208
208
  const embeddingString = `[${query.join(",")}]`;
209
- const _filter = filter ?? "{}";
209
+ const _filter = filter ?? {};
210
+ const whereClauses = [];
211
+ const values = [embeddingString, k];
212
+ let paramCount = values.length;
213
+ for (const [key, value] of Object.entries(_filter)) {
214
+ if (typeof value === "object" && value !== null) {
215
+ const currentParamCount = paramCount;
216
+ const placeholders = value.in
217
+ .map((_, index) => `$${currentParamCount + index + 1}`)
218
+ .join(",");
219
+ whereClauses.push(`${this.metadataColumnName}->>'${key}' IN (${placeholders})`);
220
+ values.push(...value.in);
221
+ paramCount += value.in.length;
222
+ }
223
+ else {
224
+ paramCount += 1;
225
+ whereClauses.push(`${this.metadataColumnName}->>'${key}' = $${paramCount}`);
226
+ values.push(value);
227
+ }
228
+ }
229
+ const whereClause = whereClauses.length
230
+ ? `WHERE ${whereClauses.join(" AND ")}`
231
+ : "";
210
232
  const queryString = `
211
- SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
212
- FROM ${this.tableName}
213
- WHERE ${this.metadataColumnName} @> $2
214
- ORDER BY "_distance" ASC
215
- LIMIT $3;`;
216
- const documents = (await this.client.query(queryString, [embeddingString, _filter, k])).rows;
233
+ SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
234
+ FROM ${this.tableName}
235
+ ${whereClause}
236
+ ORDER BY "_distance" ASC
237
+ LIMIT $2;`;
238
+ const documents = (await this.client.query(queryString, values)).rows;
217
239
  const results = [];
218
240
  for (const doc of documents) {
219
241
  if (doc._distance != null && doc[this.contentColumnName] != null) {
@@ -2,7 +2,7 @@ import { type VercelPool, type VercelPoolClient, type VercelPostgresPoolConfig }
2
2
  import { VectorStore } from "./base.js";
3
3
  import { Embeddings } from "../embeddings/base.js";
4
4
  import { Document } from "../document.js";
5
- type Metadata = Record<string, unknown>;
5
+ type Metadata = Record<string, string | number | Record<"in", string[]>>;
6
6
  /**
7
7
  * Interface that defines the arguments required to create a
8
8
  * `VercelPostgres` instance. It includes Postgres connection options,
@@ -203,14 +203,36 @@ export class VercelPostgres extends VectorStore {
203
203
  */
204
204
  async similaritySearchVectorWithScore(query, k, filter) {
205
205
  const embeddingString = `[${query.join(",")}]`;
206
- const _filter = filter ?? "{}";
206
+ const _filter = filter ?? {};
207
+ const whereClauses = [];
208
+ const values = [embeddingString, k];
209
+ let paramCount = values.length;
210
+ for (const [key, value] of Object.entries(_filter)) {
211
+ if (typeof value === "object" && value !== null) {
212
+ const currentParamCount = paramCount;
213
+ const placeholders = value.in
214
+ .map((_, index) => `$${currentParamCount + index + 1}`)
215
+ .join(",");
216
+ whereClauses.push(`${this.metadataColumnName}->>'${key}' IN (${placeholders})`);
217
+ values.push(...value.in);
218
+ paramCount += value.in.length;
219
+ }
220
+ else {
221
+ paramCount += 1;
222
+ whereClauses.push(`${this.metadataColumnName}->>'${key}' = $${paramCount}`);
223
+ values.push(value);
224
+ }
225
+ }
226
+ const whereClause = whereClauses.length
227
+ ? `WHERE ${whereClauses.join(" AND ")}`
228
+ : "";
207
229
  const queryString = `
208
- SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
209
- FROM ${this.tableName}
210
- WHERE ${this.metadataColumnName} @> $2
211
- ORDER BY "_distance" ASC
212
- LIMIT $3;`;
213
- const documents = (await this.client.query(queryString, [embeddingString, _filter, k])).rows;
230
+ SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
231
+ FROM ${this.tableName}
232
+ ${whereClause}
233
+ ORDER BY "_distance" ASC
234
+ LIMIT $2;`;
235
+ const documents = (await this.client.query(queryString, values)).rows;
214
236
  const results = [];
215
237
  for (const doc of documents) {
216
238
  if (doc._distance != null && doc[this.contentColumnName] != null) {