Bugfix/closing mongodb connections (#2085)

* mongodb connection issue

* updating and pinning mongodb version

* removing pnpm-lock.yaml

* converting to a singleton

* Update pnpm lock file

* Downgrade mongodb

---------

Co-authored-by: vinodkiran <vinodkiran@usa.net>
Co-authored-by: Vinod Paidimarry <vinodkiran@outlook.in>
This commit is contained in:
Octavian FlowiseAI 2024-04-02 14:57:20 +02:00 committed by GitHub
parent b9b0c9d227
commit ea255db15d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 191 additions and 334 deletions

View File

@ -1,5 +1,5 @@
import { MongoClient, Collection, Document } from 'mongodb' 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 { BufferMemory, BufferMemoryInput } from 'langchain/memory'
import { mapStoredMessageToChatMessage, AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages' import { mapStoredMessageToChatMessage, AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages'
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
@ -10,7 +10,7 @@ let mongoUrl: string
const getMongoClient = async (newMongoUrl: string) => { const getMongoClient = async (newMongoUrl: string) => {
if (!mongoClientSingleton) { if (!mongoClientSingleton) {
// if client doesn't exists // if client does not exist
mongoClientSingleton = new MongoClient(newMongoUrl) mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl mongoUrl = newMongoUrl
return mongoClientSingleton return mongoClientSingleton

View File

@ -1,6 +1,6 @@
import { flatten } from 'lodash' import { flatten } from 'lodash'
import { MongoClient } from 'mongodb' import { MongoClient } from 'mongodb'
import { MongoDBAtlasVectorSearch } from '@langchain/community/vectorstores/mongodb_atlas' import { MongoDBAtlasVectorSearch } from '@langchain/mongodb'
import { Embeddings } from '@langchain/core/embeddings' import { Embeddings } from '@langchain/core/embeddings'
import { Document } from '@langchain/core/documents' import { Document } from '@langchain/core/documents'
import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
@ -135,7 +135,8 @@ class MongoDBAtlas_VectorStores implements INode {
} }
} }
const mongoClient = new MongoClient(mongoDBConnectUrl) const mongoClient = await getMongoClient(mongoDBConnectUrl)
try {
const collection = mongoClient.db(databaseName).collection(collectionName) const collection = mongoClient.db(databaseName).collection(collectionName)
if (!textKey || textKey === '') textKey = 'text' if (!textKey || textKey === '') textKey = 'text'
@ -147,8 +148,6 @@ class MongoDBAtlas_VectorStores implements INode {
textKey, textKey,
embeddingKey embeddingKey
}) })
try {
await mongoDBAtlasVectorSearch.addDocuments(finalDocs) await mongoDBAtlasVectorSearch.addDocuments(finalDocs)
} catch (e) { } catch (e) {
throw new Error(e) throw new Error(e)
@ -167,7 +166,8 @@ class MongoDBAtlas_VectorStores implements INode {
let mongoDBConnectUrl = getCredentialParam('mongoDBConnectUrl', credentialData, nodeData) let mongoDBConnectUrl = getCredentialParam('mongoDBConnectUrl', credentialData, nodeData)
const mongoClient = new MongoClient(mongoDBConnectUrl) const mongoClient = await getMongoClient(mongoDBConnectUrl)
try {
const collection = mongoClient.db(databaseName).collection(collectionName) const collection = mongoClient.db(databaseName).collection(collectionName)
if (!textKey || textKey === '') textKey = 'text' if (!textKey || textKey === '') textKey = 'text'
@ -181,7 +181,28 @@ class MongoDBAtlas_VectorStores implements INode {
}) })
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 } module.exports = { nodeClass: MongoDBAtlas_VectorStores }

View File

@ -39,6 +39,7 @@
"@langchain/google-genai": "^0.0.10", "@langchain/google-genai": "^0.0.10",
"@langchain/groq": "^0.0.2", "@langchain/groq": "^0.0.2",
"@langchain/mistralai": "^0.0.11", "@langchain/mistralai": "^0.0.11",
"@langchain/mongodb": "^0.0.1",
"@langchain/openai": "^0.0.14", "@langchain/openai": "^0.0.14",
"@langchain/pinecone": "^0.0.3", "@langchain/pinecone": "^0.0.3",
"@mistralai/mistralai": "0.1.3", "@mistralai/mistralai": "0.1.3",
@ -81,7 +82,7 @@
"lunary": "^0.6.16", "lunary": "^0.6.16",
"mammoth": "^1.5.1", "mammoth": "^1.5.1",
"moment": "^2.29.3", "moment": "^2.29.3",
"mongodb": "6.2.0", "mongodb": "6.3.0",
"mysql2": "^3.9.2", "mysql2": "^3.9.2",
"node-fetch": "^2.6.11", "node-fetch": "^2.6.11",
"node-html-markdown": "^1.3.0", "node-html-markdown": "^1.3.0",

File diff suppressed because it is too large Load Diff