Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/closing mongodb connections #2085

Merged
merged 6 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MongoClient, Collection, Document } from 'mongodb'
import { MongoDBChatMessageHistory } from '@langchain/community/stores/message/mongodb'
import { MongoDBChatMessageHistory } from '@langchain/mongodb'
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
import { mapStoredMessageToChatMessage, AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages'
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
Expand All @@ -10,7 +10,7 @@ let mongoUrl: string

const getMongoClient = async (newMongoUrl: string) => {
if (!mongoClientSingleton) {
// if client doesn't exists
// if client does not exist
mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl
return mongoClientSingleton
Expand Down
69 changes: 45 additions & 24 deletions packages/components/nodes/vectorstores/MongoDBAtlas/MongoDBAtlas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { flatten } from 'lodash'
import { MongoClient } from 'mongodb'
import { MongoDBAtlasVectorSearch } from '@langchain/community/vectorstores/mongodb_atlas'
import { MongoDBAtlasVectorSearch } from '@langchain/mongodb'
import { Embeddings } from '@langchain/core/embeddings'
import { Document } from '@langchain/core/documents'
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
Expand Down Expand Up @@ -135,20 +135,19 @@ class MongoDBAtlas_VectorStores implements INode {
}
}

const mongoClient = new MongoClient(mongoDBConnectUrl)
const collection = mongoClient.db(databaseName).collection(collectionName)
const mongoClient = await getMongoClient(mongoDBConnectUrl)
try {
const collection = mongoClient.db(databaseName).collection(collectionName)

if (!textKey || textKey === '') textKey = 'text'
if (!embeddingKey || embeddingKey === '') embeddingKey = 'embedding'
if (!textKey || textKey === '') textKey = 'text'
if (!embeddingKey || embeddingKey === '') embeddingKey = 'embedding'

const mongoDBAtlasVectorSearch = new MongoDBAtlasVectorSearch(embeddings, {
collection,
indexName,
textKey,
embeddingKey
})

try {
const mongoDBAtlasVectorSearch = new MongoDBAtlasVectorSearch(embeddings, {
collection,
indexName,
textKey,
embeddingKey
})
await mongoDBAtlasVectorSearch.addDocuments(finalDocs)
} catch (e) {
throw new Error(e)
Expand All @@ -167,21 +166,43 @@ class MongoDBAtlas_VectorStores implements INode {

let mongoDBConnectUrl = getCredentialParam('mongoDBConnectUrl', credentialData, nodeData)

const mongoClient = new MongoClient(mongoDBConnectUrl)
const collection = mongoClient.db(databaseName).collection(collectionName)
const mongoClient = await getMongoClient(mongoDBConnectUrl)
try {
const collection = mongoClient.db(databaseName).collection(collectionName)

if (!textKey || textKey === '') textKey = 'text'
if (!embeddingKey || embeddingKey === '') embeddingKey = 'embedding'
if (!textKey || textKey === '') textKey = 'text'
if (!embeddingKey || embeddingKey === '') embeddingKey = 'embedding'

const vectorStore = new MongoDBAtlasVectorSearch(embeddings, {
collection,
indexName,
textKey,
embeddingKey
})
const vectorStore = new MongoDBAtlasVectorSearch(embeddings, {
collection,
indexName,
textKey,
embeddingKey
})

return resolveVectorStoreOrRetriever(nodeData, vectorStore)
return resolveVectorStoreOrRetriever(nodeData, vectorStore)
} catch (e) {
throw new Error(e)
}
}
}

let mongoClientSingleton: MongoClient
let mongoUrl: string

const getMongoClient = async (newMongoUrl: string) => {
if (!mongoClientSingleton) {
// if client does not exist
mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl
return mongoClientSingleton
} else if (mongoClientSingleton && newMongoUrl !== mongoUrl) {
// if client exists but url changed
mongoClientSingleton.close()
mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl
return mongoClientSingleton
}
return mongoClientSingleton
}
module.exports = { nodeClass: MongoDBAtlas_VectorStores }
3 changes: 2 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@langchain/google-genai": "^0.0.10",
"@langchain/groq": "^0.0.2",
"@langchain/mistralai": "^0.0.11",
"@langchain/mongodb": "^0.0.1",
"@langchain/openai": "^0.0.14",
"@langchain/pinecone": "^0.0.3",
"@mistralai/mistralai": "0.1.3",
Expand Down Expand Up @@ -81,7 +82,7 @@
"lunary": "^0.6.16",
"mammoth": "^1.5.1",
"moment": "^2.29.3",
"mongodb": "6.2.0",
"mongodb": "6.3.0",
"mysql2": "^3.9.2",
"node-fetch": "^2.6.11",
"node-html-markdown": "^1.3.0",
Expand Down
Loading
Loading