Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Dec 16, 2024
1 parent 0d8f922 commit 515461f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions libs/langchain-azure-cosmosdb/src/chat_histories/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand All @@ -39,8 +41,6 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis

private sessionId: string;

private idKey = "sessionId";

initialize: () => Promise<void>;

constructor(
Expand All @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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<Document>,
},
Expand All @@ -152,6 +150,6 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis
async clear(): Promise<void> {
await this.initialize();

await this.collection.deleteOne({ [this.idKey]: this.sessionId });
await this.collection.deleteOne({ [ID_KEY]: this.sessionId });
}
}
2 changes: 1 addition & 1 deletion libs/langchain-azure-cosmosdb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
export * from "./chat_histories/mongodb.js";
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 515461f

Please sign in to comment.