diff --git a/libs/langchain-azure-cosmosdb/src/chat_histories/mongodb.ts b/libs/langchain-azure-cosmosdb/src/chat_histories/mongodb.ts index e7db4cb0be72..53104c198d71 100644 --- a/libs/langchain-azure-cosmosdb/src/chat_histories/mongodb.ts +++ b/libs/langchain-azure-cosmosdb/src/chat_histories/mongodb.ts @@ -20,6 +20,8 @@ export interface AzureCosmosDBMongoChatHistoryDBConfig { readonly collectionName?: string; } +const ID_KEY = "sessionId"; + export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHistory { lc_namespace = ["langchain", "stores", "message", "azurecosmosdb"]; @@ -39,8 +41,6 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis private sessionId: string; - private idKey = "sessionId"; - initialize: () => Promise; constructor( @@ -54,9 +54,7 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis getEnvironmentVariable("AZURE_COSMOSDB_MONGODB_CONNECTION_STRING"); if (!dbConfig.client && !connectionString) { - throw new Error( - "Mongo client or connection string must be set." - ); + throw new Error("Mongo client or connection string must be set."); } if (!dbConfig.client) { @@ -68,8 +66,8 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const client = dbConfig.client || this.client!; - const databaseName = dbConfig.databaseName ?? "documentsDB"; - const collectionName = dbConfig.collectionName ?? "documents"; + const databaseName = dbConfig.databaseName ?? "chatHistoryDB"; + const collectionName = dbConfig.collectionName ?? "chatHistory"; this.sessionId = sessionId; @@ -121,7 +119,7 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis await this.initialize(); const document = await this.collection.findOne({ - [this.idKey]: this.sessionId, + [ID_KEY]: this.sessionId, }); const messages = document?.messages || []; return mapStoredMessagesToChatMessages(messages); @@ -137,7 +135,7 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis const messages = mapChatMessagesToStoredMessages([message]); await this.collection.updateOne( - { [this.idKey]: this.sessionId }, + { [ID_KEY]: this.sessionId }, { $push: { messages: { $each: messages } } as PushOperator, }, @@ -152,6 +150,6 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis async clear(): Promise { await this.initialize(); - await this.collection.deleteOne({ [this.idKey]: this.sessionId }); + await this.collection.deleteOne({ [ID_KEY]: this.sessionId }); } } diff --git a/libs/langchain-azure-cosmosdb/src/index.ts b/libs/langchain-azure-cosmosdb/src/index.ts index 32e989fe55c0..883710c842ff 100644 --- a/libs/langchain-azure-cosmosdb/src/index.ts +++ b/libs/langchain-azure-cosmosdb/src/index.ts @@ -2,4 +2,4 @@ export * from "./azure_cosmosdb_mongodb.js"; export * from "./azure_cosmosdb_nosql.js"; export * from "./caches.js"; export * from "./chat_histories/nosql.js"; -export * from "./chat_histories/mongodb.js"; \ No newline at end of file +export * from "./chat_histories/mongodb.js"; diff --git a/libs/langchain-azure-cosmosdb/src/tests/chat_histories_azure_cosmosdb_mongodb.int.test.ts b/libs/langchain-azure-cosmosdb/src/tests/chat_histories/mongodb.int.test.ts similarity index 98% rename from libs/langchain-azure-cosmosdb/src/tests/chat_histories_azure_cosmosdb_mongodb.int.test.ts rename to libs/langchain-azure-cosmosdb/src/tests/chat_histories/mongodb.int.test.ts index e3137ef2857a..35c4a2cf0311 100644 --- a/libs/langchain-azure-cosmosdb/src/tests/chat_histories_azure_cosmosdb_mongodb.int.test.ts +++ b/libs/langchain-azure-cosmosdb/src/tests/chat_histories/mongodb.int.test.ts @@ -5,7 +5,7 @@ import { AIMessage, HumanMessage } from "@langchain/core/messages"; import { AzureCosmosDBMongoChatMessageHistory, AzureCosmosDBMongoChatHistoryDBConfig, -} from "../chat_histories/mongodb.js"; +} from "../../chat_histories/mongodb.js"; afterAll(async () => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/libs/langchain-azure-cosmosdb/src/tests/chat_histories.int.test.ts b/libs/langchain-azure-cosmosdb/src/tests/chat_histories/nosql.int.test.ts similarity index 98% rename from libs/langchain-azure-cosmosdb/src/tests/chat_histories.int.test.ts rename to libs/langchain-azure-cosmosdb/src/tests/chat_histories/nosql.int.test.ts index 85c57cb64860..9a6b12b125b6 100644 --- a/libs/langchain-azure-cosmosdb/src/tests/chat_histories.int.test.ts +++ b/libs/langchain-azure-cosmosdb/src/tests/chat_histories/nosql.int.test.ts @@ -6,7 +6,7 @@ import { HumanMessage, AIMessage } from "@langchain/core/messages"; import { CosmosClient } from "@azure/cosmos"; import { DefaultAzureCredential } from "@azure/identity"; import { ObjectId } from "mongodb"; -import { AzureCosmsosDBNoSQLChatMessageHistory } from "../chat_histories/nosql.js"; +import { AzureCosmsosDBNoSQLChatMessageHistory } from "../../chat_histories/nosql.js"; const DATABASE_NAME = "langchainTestDB"; const CONTAINER_NAME = "testContainer";