langchain 0.0.173 → 0.0.175

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 (43) hide show
  1. package/dist/callbacks/manager.cjs +2 -1
  2. package/dist/callbacks/manager.js +2 -1
  3. package/dist/load/import_constants.cjs +4 -0
  4. package/dist/load/import_constants.js +4 -0
  5. package/dist/memory/index.cjs +2 -1
  6. package/dist/memory/index.d.ts +1 -1
  7. package/dist/memory/index.js +1 -1
  8. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -18
  9. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +12 -6
  10. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +15 -18
  11. package/dist/storage/convex.cjs +145 -0
  12. package/dist/storage/convex.d.ts +85 -0
  13. package/dist/storage/convex.js +141 -0
  14. package/dist/stores/message/convex.cjs +120 -0
  15. package/dist/stores/message/convex.d.ts +60 -0
  16. package/dist/stores/message/convex.js +116 -0
  17. package/dist/util/convex.cjs +77 -0
  18. package/dist/util/convex.d.ts +26 -0
  19. package/dist/util/convex.js +74 -0
  20. package/dist/vectorstores/convex.cjs +177 -0
  21. package/dist/vectorstores/convex.d.ts +113 -0
  22. package/dist/vectorstores/convex.js +173 -0
  23. package/dist/vectorstores/milvus.cjs +4 -2
  24. package/dist/vectorstores/milvus.js +4 -2
  25. package/dist/vectorstores/pinecone.cjs +46 -9
  26. package/dist/vectorstores/pinecone.d.ts +20 -2
  27. package/dist/vectorstores/pinecone.js +46 -9
  28. package/dist/vectorstores/vercel_postgres.cjs +29 -7
  29. package/dist/vectorstores/vercel_postgres.d.ts +1 -1
  30. package/dist/vectorstores/vercel_postgres.js +29 -7
  31. package/package.json +38 -1
  32. package/storage/convex.cjs +1 -0
  33. package/storage/convex.d.ts +1 -0
  34. package/storage/convex.js +1 -0
  35. package/stores/message/convex.cjs +1 -0
  36. package/stores/message/convex.d.ts +1 -0
  37. package/stores/message/convex.js +1 -0
  38. package/util/convex.cjs +1 -0
  39. package/util/convex.d.ts +1 -0
  40. package/util/convex.js +1 -0
  41. package/vectorstores/convex.cjs +1 -0
  42. package/vectorstores/convex.d.ts +1 -0
  43. package/vectorstores/convex.js +1 -0
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ConvexChatMessageHistory = void 0;
5
+ // eslint-disable-next-line import/no-extraneous-dependencies
6
+ const server_1 = require("convex/server");
7
+ const index_js_1 = require("../../schema/index.cjs");
8
+ const utils_js_1 = require("./utils.cjs");
9
+ class ConvexChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
10
+ constructor(config) {
11
+ super();
12
+ Object.defineProperty(this, "lc_namespace", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: ["langchain", "stores", "message", "convex"]
17
+ });
18
+ Object.defineProperty(this, "ctx", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "sessionId", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "table", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "index", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "sessionIdField", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "messageTextFieldName", {
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, "lookup", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
66
+ Object.defineProperty(this, "deleteMany", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: void 0
71
+ });
72
+ this.ctx = config.ctx;
73
+ this.sessionId = config.sessionId;
74
+ this.table = config.table ?? "messages";
75
+ this.index = config.index ?? "bySessionId";
76
+ this.sessionIdField =
77
+ config.sessionIdField ?? "sessionId";
78
+ this.messageTextFieldName =
79
+ config.messageTextFieldName ?? "message";
80
+ this.insert =
81
+ config.insert ?? (0, server_1.makeFunctionReference)("langchain/db:insert");
82
+ this.lookup =
83
+ config.lookup ?? (0, server_1.makeFunctionReference)("langchain/db:lookup");
84
+ this.deleteMany =
85
+ config.deleteMany ??
86
+ (0, server_1.makeFunctionReference)("langchain/db:deleteMany");
87
+ }
88
+ async getMessages() {
89
+ const convexDocuments = await this.ctx.runQuery(this.lookup, {
90
+ table: this.table,
91
+ index: this.index,
92
+ keyField: this.sessionIdField,
93
+ key: this.sessionId,
94
+ });
95
+ return (0, utils_js_1.mapStoredMessagesToChatMessages)(convexDocuments.map((doc) => doc[this.messageTextFieldName]));
96
+ }
97
+ async addMessage(message) {
98
+ const messages = (0, utils_js_1.mapChatMessagesToStoredMessages)([message]);
99
+ // TODO: Remove chunking when Convex handles the concurrent requests correctly
100
+ const PAGE_SIZE = 16;
101
+ for (let i = 0; i < messages.length; i += PAGE_SIZE) {
102
+ await Promise.all(messages.slice(i, i + PAGE_SIZE).map((message) => this.ctx.runMutation(this.insert, {
103
+ table: this.table,
104
+ document: {
105
+ [this.sessionIdField]: this.sessionId,
106
+ [this.messageTextFieldName]: message,
107
+ },
108
+ })));
109
+ }
110
+ }
111
+ async clear() {
112
+ await this.ctx.runMutation(this.deleteMany, {
113
+ table: this.table,
114
+ index: this.index,
115
+ keyField: this.sessionIdField,
116
+ key: this.sessionId,
117
+ });
118
+ }
119
+ }
120
+ exports.ConvexChatMessageHistory = ConvexChatMessageHistory;
@@ -0,0 +1,60 @@
1
+ import { DocumentByName, FieldPaths, FunctionReference, GenericActionCtx, GenericDataModel, NamedTableInfo, TableNamesInDataModel, VectorIndexNames } from "convex/server";
2
+ import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js";
3
+ /**
4
+ * Type that defines the config required to initialize the
5
+ * ConvexChatMessageHistory class. At minimum it needs a sessionId
6
+ * and an ActionCtx.
7
+ */
8
+ export type ConvexChatMessageHistoryInput<DataModel extends GenericDataModel, TableName extends TableNamesInDataModel<DataModel> = "messages", IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>> = "bySessionId", SessionIdFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "sessionId", MessageTextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "message", InsertMutation extends FunctionReference<"mutation", "internal", {
9
+ table: string;
10
+ document: object;
11
+ }> = any, LookupQuery extends FunctionReference<"query", "internal", {
12
+ table: string;
13
+ index: string;
14
+ keyField: string;
15
+ key: string;
16
+ }, object[]> = any, DeleteManyMutation extends FunctionReference<"mutation", "internal", {
17
+ table: string;
18
+ index: string;
19
+ keyField: string;
20
+ key: string;
21
+ }> = any> = {
22
+ readonly ctx: GenericActionCtx<DataModel>;
23
+ readonly sessionId: DocumentByName<DataModel, TableName>[SessionIdFieldName];
24
+ readonly table?: TableName;
25
+ readonly index?: IndexName;
26
+ readonly sessionIdField?: SessionIdFieldName;
27
+ readonly messageTextFieldName?: MessageTextFieldName;
28
+ readonly insert?: InsertMutation;
29
+ readonly lookup?: LookupQuery;
30
+ readonly deleteMany?: DeleteManyMutation;
31
+ };
32
+ export declare class ConvexChatMessageHistory<DataModel extends GenericDataModel, SessionIdFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "sessionId", TableName extends TableNamesInDataModel<DataModel> = "messages", IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>> = "bySessionId", MessageTextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "message", InsertMutation extends FunctionReference<"mutation", "internal", {
33
+ table: string;
34
+ document: object;
35
+ }> = any, LookupQuery extends FunctionReference<"query", "internal", {
36
+ table: string;
37
+ index: string;
38
+ keyField: string;
39
+ key: string;
40
+ }, object[]> = any, DeleteManyMutation extends FunctionReference<"mutation", "internal", {
41
+ table: string;
42
+ index: string;
43
+ keyField: string;
44
+ key: string;
45
+ }> = any> extends BaseListChatMessageHistory {
46
+ lc_namespace: string[];
47
+ private readonly ctx;
48
+ private readonly sessionId;
49
+ private readonly table;
50
+ private readonly index;
51
+ private readonly sessionIdField;
52
+ private readonly messageTextFieldName;
53
+ private readonly insert;
54
+ private readonly lookup;
55
+ private readonly deleteMany;
56
+ constructor(config: ConvexChatMessageHistoryInput<DataModel, TableName, IndexName, SessionIdFieldName, MessageTextFieldName, InsertMutation, LookupQuery, DeleteManyMutation>);
57
+ getMessages(): Promise<BaseMessage[]>;
58
+ addMessage(message: BaseMessage): Promise<void>;
59
+ clear(): Promise<void>;
60
+ }
@@ -0,0 +1,116 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
+ import { makeFunctionReference, } from "convex/server";
4
+ import { BaseListChatMessageHistory } from "../../schema/index.js";
5
+ import { mapChatMessagesToStoredMessages, mapStoredMessagesToChatMessages, } from "./utils.js";
6
+ export class ConvexChatMessageHistory extends BaseListChatMessageHistory {
7
+ constructor(config) {
8
+ super();
9
+ Object.defineProperty(this, "lc_namespace", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: ["langchain", "stores", "message", "convex"]
14
+ });
15
+ Object.defineProperty(this, "ctx", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "sessionId", {
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, "sessionIdField", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "messageTextFieldName", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "insert", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "lookup", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ Object.defineProperty(this, "deleteMany", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
69
+ this.ctx = config.ctx;
70
+ this.sessionId = config.sessionId;
71
+ this.table = config.table ?? "messages";
72
+ this.index = config.index ?? "bySessionId";
73
+ this.sessionIdField =
74
+ config.sessionIdField ?? "sessionId";
75
+ this.messageTextFieldName =
76
+ config.messageTextFieldName ?? "message";
77
+ this.insert =
78
+ config.insert ?? makeFunctionReference("langchain/db:insert");
79
+ this.lookup =
80
+ config.lookup ?? makeFunctionReference("langchain/db:lookup");
81
+ this.deleteMany =
82
+ config.deleteMany ??
83
+ makeFunctionReference("langchain/db:deleteMany");
84
+ }
85
+ async getMessages() {
86
+ const convexDocuments = await this.ctx.runQuery(this.lookup, {
87
+ table: this.table,
88
+ index: this.index,
89
+ keyField: this.sessionIdField,
90
+ key: this.sessionId,
91
+ });
92
+ return mapStoredMessagesToChatMessages(convexDocuments.map((doc) => doc[this.messageTextFieldName]));
93
+ }
94
+ async addMessage(message) {
95
+ const messages = mapChatMessagesToStoredMessages([message]);
96
+ // TODO: Remove chunking when Convex handles the concurrent requests correctly
97
+ const PAGE_SIZE = 16;
98
+ for (let i = 0; i < messages.length; i += PAGE_SIZE) {
99
+ await Promise.all(messages.slice(i, i + PAGE_SIZE).map((message) => this.ctx.runMutation(this.insert, {
100
+ table: this.table,
101
+ document: {
102
+ [this.sessionIdField]: this.sessionId,
103
+ [this.messageTextFieldName]: message,
104
+ },
105
+ })));
106
+ }
107
+ }
108
+ async clear() {
109
+ await this.ctx.runMutation(this.deleteMany, {
110
+ table: this.table,
111
+ index: this.index,
112
+ keyField: this.sessionIdField,
113
+ key: this.sessionId,
114
+ });
115
+ }
116
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ /* eslint-disable spaced-comment */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.deleteMany = exports.upsert = exports.lookup = exports.insert = exports.get = void 0;
5
+ // eslint-disable-next-line import/no-extraneous-dependencies
6
+ const server_1 = require("convex/server");
7
+ // eslint-disable-next-line import/no-extraneous-dependencies
8
+ const values_1 = require("convex/values");
9
+ exports.get = (0, server_1.internalQueryGeneric)({
10
+ args: {
11
+ id: /*#__PURE__*/ values_1.v.string(),
12
+ },
13
+ handler: async (ctx, args) => {
14
+ const result = await ctx.db.get(args.id);
15
+ return result;
16
+ },
17
+ });
18
+ exports.insert = (0, server_1.internalMutationGeneric)({
19
+ args: {
20
+ table: /*#__PURE__*/ values_1.v.string(),
21
+ document: /*#__PURE__*/ values_1.v.any(),
22
+ },
23
+ handler: async (ctx, args) => {
24
+ await ctx.db.insert(args.table, args.document);
25
+ },
26
+ });
27
+ exports.lookup = (0, server_1.internalQueryGeneric)({
28
+ args: {
29
+ table: /*#__PURE__*/ values_1.v.string(),
30
+ index: /*#__PURE__*/ values_1.v.string(),
31
+ keyField: /*#__PURE__*/ values_1.v.string(),
32
+ key: /*#__PURE__*/ values_1.v.string(),
33
+ },
34
+ handler: async (ctx, args) => {
35
+ const result = await ctx.db
36
+ .query(args.table)
37
+ .withIndex(args.index, (q) => q.eq(args.keyField, args.key))
38
+ .collect();
39
+ return result;
40
+ },
41
+ });
42
+ exports.upsert = (0, server_1.internalMutationGeneric)({
43
+ args: {
44
+ table: /*#__PURE__*/ values_1.v.string(),
45
+ index: /*#__PURE__*/ values_1.v.string(),
46
+ keyField: /*#__PURE__*/ values_1.v.string(),
47
+ key: /*#__PURE__*/ values_1.v.string(),
48
+ document: /*#__PURE__*/ values_1.v.any(),
49
+ },
50
+ handler: async (ctx, args) => {
51
+ const existing = await ctx.db
52
+ .query(args.table)
53
+ .withIndex(args.index, (q) => q.eq(args.keyField, args.key))
54
+ .unique();
55
+ if (existing !== null) {
56
+ await ctx.db.replace(existing._id, args.document);
57
+ }
58
+ else {
59
+ await ctx.db.insert(args.table, args.document);
60
+ }
61
+ },
62
+ });
63
+ exports.deleteMany = (0, server_1.internalMutationGeneric)({
64
+ args: {
65
+ table: /*#__PURE__*/ values_1.v.string(),
66
+ index: /*#__PURE__*/ values_1.v.string(),
67
+ keyField: /*#__PURE__*/ values_1.v.string(),
68
+ key: /*#__PURE__*/ values_1.v.string(),
69
+ },
70
+ handler: async (ctx, args) => {
71
+ const existing = await ctx.db
72
+ .query(args.table)
73
+ .withIndex(args.index, (q) => q.eq(args.keyField, args.key))
74
+ .collect();
75
+ await Promise.all(existing.map((doc) => ctx.db.delete(doc._id)));
76
+ },
77
+ });
@@ -0,0 +1,26 @@
1
+ export declare const get: import("convex/server").RegisteredQuery<"internal", {
2
+ id: string;
3
+ }, Promise<any>>;
4
+ export declare const insert: import("convex/server").RegisteredMutation<"internal", {
5
+ document: any;
6
+ table: string;
7
+ }, Promise<void>>;
8
+ export declare const lookup: import("convex/server").RegisteredQuery<"internal", {
9
+ key: string;
10
+ index: string;
11
+ table: string;
12
+ keyField: string;
13
+ }, Promise<any[]>>;
14
+ export declare const upsert: import("convex/server").RegisteredMutation<"internal", {
15
+ key: string;
16
+ index: string;
17
+ document: any;
18
+ table: string;
19
+ keyField: string;
20
+ }, Promise<void>>;
21
+ export declare const deleteMany: import("convex/server").RegisteredMutation<"internal", {
22
+ key: string;
23
+ index: string;
24
+ table: string;
25
+ keyField: string;
26
+ }, Promise<void>>;
@@ -0,0 +1,74 @@
1
+ /* eslint-disable spaced-comment */
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
+ import { internalQueryGeneric as internalQuery, internalMutationGeneric as internalMutation, } from "convex/server";
4
+ // eslint-disable-next-line import/no-extraneous-dependencies
5
+ import { v } from "convex/values";
6
+ export const get = /*#__PURE__*/ internalQuery({
7
+ args: {
8
+ id: /*#__PURE__*/ v.string(),
9
+ },
10
+ handler: async (ctx, args) => {
11
+ const result = await ctx.db.get(args.id);
12
+ return result;
13
+ },
14
+ });
15
+ export const insert = /*#__PURE__*/ internalMutation({
16
+ args: {
17
+ table: /*#__PURE__*/ v.string(),
18
+ document: /*#__PURE__*/ v.any(),
19
+ },
20
+ handler: async (ctx, args) => {
21
+ await ctx.db.insert(args.table, args.document);
22
+ },
23
+ });
24
+ export const lookup = /*#__PURE__*/ internalQuery({
25
+ args: {
26
+ table: /*#__PURE__*/ v.string(),
27
+ index: /*#__PURE__*/ v.string(),
28
+ keyField: /*#__PURE__*/ v.string(),
29
+ key: /*#__PURE__*/ v.string(),
30
+ },
31
+ handler: async (ctx, args) => {
32
+ const result = await ctx.db
33
+ .query(args.table)
34
+ .withIndex(args.index, (q) => q.eq(args.keyField, args.key))
35
+ .collect();
36
+ return result;
37
+ },
38
+ });
39
+ export const upsert = /*#__PURE__*/ internalMutation({
40
+ args: {
41
+ table: /*#__PURE__*/ v.string(),
42
+ index: /*#__PURE__*/ v.string(),
43
+ keyField: /*#__PURE__*/ v.string(),
44
+ key: /*#__PURE__*/ v.string(),
45
+ document: /*#__PURE__*/ v.any(),
46
+ },
47
+ handler: async (ctx, args) => {
48
+ const existing = await ctx.db
49
+ .query(args.table)
50
+ .withIndex(args.index, (q) => q.eq(args.keyField, args.key))
51
+ .unique();
52
+ if (existing !== null) {
53
+ await ctx.db.replace(existing._id, args.document);
54
+ }
55
+ else {
56
+ await ctx.db.insert(args.table, args.document);
57
+ }
58
+ },
59
+ });
60
+ export const deleteMany = /*#__PURE__*/ internalMutation({
61
+ args: {
62
+ table: /*#__PURE__*/ v.string(),
63
+ index: /*#__PURE__*/ v.string(),
64
+ keyField: /*#__PURE__*/ v.string(),
65
+ key: /*#__PURE__*/ v.string(),
66
+ },
67
+ handler: async (ctx, args) => {
68
+ const existing = await ctx.db
69
+ .query(args.table)
70
+ .withIndex(args.index, (q) => q.eq(args.keyField, args.key))
71
+ .collect();
72
+ await Promise.all(existing.map((doc) => ctx.db.delete(doc._id)));
73
+ },
74
+ });
@@ -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;