Skip to content

Commit ac07cc7

Browse files
authored
feat(mongodb): auto append client metadata for mongodb (langchain-ai#8819)
1 parent 26a1ce6 commit ac07cc7

File tree

11 files changed

+102
-6
lines changed

11 files changed

+102
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/mongodb": minor
3+
---
4+
5+
Updates the mongodb vector search, memory, and chat history modules to append client metadata

β€Žexamples/src/memory/mongodb.tsβ€Ž

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { ChatOpenAI } from "@langchain/openai";
44
import { ConversationChain } from "langchain/chains";
55
import { MongoDBChatMessageHistory } from "@langchain/mongodb";
66

7-
const client = new MongoClient(process.env.MONGODB_ATLAS_URI || "", {
8-
driverInfo: { name: "langchainjs" },
9-
});
7+
const client = new MongoClient(process.env.MONGODB_ATLAS_URI || "");
108
await client.connect();
119
const collection = client.db("langchain").collection("memory");
1210

β€Žlibs/langchain-mongodb/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"author": "LangChain",
3333
"license": "MIT",
3434
"dependencies": {
35-
"mongodb": "^6.17.0"
35+
"mongodb": "^6.20.0"
3636
},
3737
"peerDependencies": {
3838
"@langchain/core": ">=0.2.21 <0.4.0"

β€Žlibs/langchain-mongodb/src/chat_history.tsβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export class MongoDBChatMessageHistory extends BaseListChatMessageHistory {
4040
super();
4141
this.collection = collection;
4242
this.sessionId = sessionId;
43+
this.collection.db.client.appendMetadata({
44+
name: "langchainjs_chat_history",
45+
});
4346
}
4447

4548
async getMessages(): Promise<BaseMessage[]> {

β€Žlibs/langchain-mongodb/src/storage.tsβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export class MongoDBStore extends BaseStore<string, Uint8Array> {
6565
this.yieldKeysScanBatchSize =
6666
fields.yieldKeysScanBatchSize ?? this.yieldKeysScanBatchSize;
6767
this.namespace = fields.namespace;
68+
this.collection.db.client.appendMetadata({
69+
name: "langchainjs_storage",
70+
});
6871
}
6972

7073
_getPrefixedKey(key: string) {

β€Žlibs/langchain-mongodb/src/tests/chat_history.int.test.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-process-env */
2-
2+
import { beforeAll, expect, test } from "@jest/globals";
33
import { Collection, MongoClient, ObjectId } from "mongodb";
44
import { AIMessage, HumanMessage } from "@langchain/core/messages";
55
import { MongoDBChatMessageHistory } from "../chat_history.js";

β€Žlibs/langchain-mongodb/src/tests/storage.int.test.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-process-env */
2+
import { beforeAll, expect, test } from "@jest/globals";
23
import { v4 as uuidv4 } from "uuid";
34
import { Collection, MongoClient, ServerApiVersion } from "mongodb";
45
import { MongoDBStore } from "../storage.js";

β€Žlibs/langchain-mongodb/src/tests/vectorstores.int.test.tsβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ function getEmbeddings() {
113113
return new OpenAIEmbeddings();
114114
}
115115

116+
test("MongoDBStore sets client metadata", () => {
117+
const spy = jest.spyOn(client, "appendMetadata");
118+
// eslint-disable-next-line no-new
119+
new PatchedVectorStore(getEmbeddings(), {
120+
collection,
121+
});
122+
expect(spy).toHaveBeenCalledWith({ name: "langchainjs_vector" });
123+
jest.clearAllMocks();
124+
});
125+
116126
test("MongoDBAtlasVectorSearch with external ids", async () => {
117127
const vectorStore = new PatchedVectorStore(getEmbeddings(), {
118128
collection,

β€Žlibs/langchain-mongodb/src/vectorstores.tsβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
7979
this.embeddingKey = args.embeddingKey ?? "embedding";
8080
this.primaryKey = args.primaryKey ?? "_id";
8181
this.caller = new AsyncCaller(args);
82+
this.collection.db.client.appendMetadata({
83+
name: "langchainjs_vector",
84+
});
8285
}
8386

8487
/**

β€Žlibs/langchain-mongodb/tsconfig.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"noUnusedParameters": true,
1616
"useDefineForClassFields": true,
1717
"strictPropertyInitialization": false,
18+
"resolveJsonModule": true,
1819
"allowJs": true,
1920
"strict": true
2021
},

0 commit comments

Comments
Β (0)