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:
parent
b9b0c9d227
commit
ea255db15d
|
|
@ -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'
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
@ -135,20 +135,19 @@ class MongoDBAtlas_VectorStores implements INode {
|
|||
}
|
||||
}
|
||||
|
||||
const mongoClient = new MongoClient(mongoDBConnectUrl)
|
||||
const collection = mongoClient.db(databaseName).collection(collectionName)
|
||||
|
||||
if (!textKey || textKey === '') textKey = 'text'
|
||||
if (!embeddingKey || embeddingKey === '') embeddingKey = 'embedding'
|
||||
|
||||
const mongoDBAtlasVectorSearch = new MongoDBAtlasVectorSearch(embeddings, {
|
||||
collection,
|
||||
indexName,
|
||||
textKey,
|
||||
embeddingKey
|
||||
})
|
||||
|
||||
const mongoClient = await getMongoClient(mongoDBConnectUrl)
|
||||
try {
|
||||
const collection = mongoClient.db(databaseName).collection(collectionName)
|
||||
|
||||
if (!textKey || textKey === '') textKey = 'text'
|
||||
if (!embeddingKey || embeddingKey === '') embeddingKey = 'embedding'
|
||||
|
||||
const mongoDBAtlasVectorSearch = new MongoDBAtlasVectorSearch(embeddings, {
|
||||
collection,
|
||||
indexName,
|
||||
textKey,
|
||||
embeddingKey
|
||||
})
|
||||
await mongoDBAtlasVectorSearch.addDocuments(finalDocs)
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
447
pnpm-lock.yaml
447
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue