-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
authored and
root
committed
Dec 11, 2024
1 parent
1305e9d
commit 0d8f922
Showing
6 changed files
with
188 additions
and
77 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
libs/langchain-azure-cosmosdb/src/chat_histories/mongodb.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { | ||
Collection, | ||
Document as AzureCosmosMongoDBDocument, | ||
PushOperator, | ||
Db, | ||
MongoClient, | ||
} from "mongodb"; | ||
import { BaseListChatMessageHistory } from "@langchain/core/chat_history"; | ||
import { | ||
BaseMessage, | ||
mapChatMessagesToStoredMessages, | ||
mapStoredMessagesToChatMessages, | ||
} from "@langchain/core/messages"; | ||
import { getEnvironmentVariable } from "@langchain/core/utils/env"; | ||
|
||
export interface AzureCosmosDBMongoChatHistoryDBConfig { | ||
readonly client?: MongoClient; | ||
readonly connectionString?: string; | ||
readonly databaseName?: string; | ||
readonly collectionName?: string; | ||
} | ||
|
||
export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHistory { | ||
lc_namespace = ["langchain", "stores", "message", "azurecosmosdb"]; | ||
|
||
get lc_secrets(): { [key: string]: string } { | ||
return { | ||
connectionString: "AZURE_COSMOSDB_MONGODB_CONNECTION_STRING", | ||
}; | ||
} | ||
|
||
private initPromise?: Promise<void>; | ||
|
||
private readonly client: MongoClient | undefined; | ||
|
||
private database: Db; | ||
|
||
private collection: Collection<AzureCosmosMongoDBDocument>; | ||
|
||
private sessionId: string; | ||
|
||
private idKey = "sessionId"; | ||
|
||
initialize: () => Promise<void>; | ||
|
||
constructor( | ||
dbConfig: AzureCosmosDBMongoChatHistoryDBConfig, | ||
sessionId: string | ||
) { | ||
super(); | ||
|
||
const connectionString = | ||
dbConfig.connectionString ?? | ||
getEnvironmentVariable("AZURE_COSMOSDB_MONGODB_CONNECTION_STRING"); | ||
|
||
if (!dbConfig.client && !connectionString) { | ||
throw new Error( | ||
"Mongo client or connection string must be set." | ||
); | ||
} | ||
|
||
if (!dbConfig.client) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.client = new MongoClient(connectionString!, { | ||
appName: "langchainjs", | ||
}); | ||
} | ||
|
||
// 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"; | ||
|
||
this.sessionId = sessionId; | ||
|
||
// Deferring initialization to the first call to `initialize` | ||
this.initialize = () => { | ||
if (this.initPromise === undefined) { | ||
this.initPromise = this.init( | ||
client, | ||
databaseName, | ||
collectionName | ||
).catch((error) => { | ||
console.error( | ||
"Error during AzureCosmosDBMongoChatMessageHistory initialization: ", | ||
error | ||
); | ||
}); | ||
} | ||
|
||
return this.initPromise; | ||
}; | ||
} | ||
|
||
/** | ||
* Initializes the AzureCosmosDBMongoChatMessageHistory by connecting to the database. | ||
* @param client The MongoClient to use for connecting to the database. | ||
* @param databaseName The name of the database to use. | ||
* @param collectionName The name of the collection to use. | ||
* @returns A promise that resolves when the AzureCosmosDBMongoChatMessageHistory has been initialized. | ||
*/ | ||
private async init( | ||
client: MongoClient, | ||
databaseName: string, | ||
collectionName: string | ||
): Promise<void> { | ||
this.initPromise = (async () => { | ||
await client.connect(); | ||
this.database = client.db(databaseName); | ||
this.collection = this.database.collection(collectionName); | ||
})(); | ||
|
||
return this.initPromise; | ||
} | ||
|
||
/** | ||
* Retrieves the messages stored in the history. | ||
* @returns A promise that resolves with the messages stored in the history. | ||
*/ | ||
async getMessages(): Promise<BaseMessage[]> { | ||
await this.initialize(); | ||
|
||
const document = await this.collection.findOne({ | ||
[this.idKey]: this.sessionId, | ||
}); | ||
const messages = document?.messages || []; | ||
return mapStoredMessagesToChatMessages(messages); | ||
} | ||
|
||
/** | ||
* Adds a message to the history. | ||
* @param message The message to add to the history. | ||
* @returns A promise that resolves when the message has been added to the history. | ||
*/ | ||
async addMessage(message: BaseMessage): Promise<void> { | ||
await this.initialize(); | ||
|
||
const messages = mapChatMessagesToStoredMessages([message]); | ||
await this.collection.updateOne( | ||
{ [this.idKey]: this.sessionId }, | ||
{ | ||
$push: { messages: { $each: messages } } as PushOperator<Document>, | ||
}, | ||
{ upsert: true } | ||
); | ||
} | ||
|
||
/** | ||
* Clear the history. | ||
* @returns A promise that resolves when the history has been cleared. | ||
*/ | ||
async clear(): Promise<void> { | ||
await this.initialize(); | ||
|
||
await this.collection.deleteOne({ [this.idKey]: this.sessionId }); | ||
} | ||
} |
File renamed without changes.
58 changes: 0 additions & 58 deletions
58
libs/langchain-azure-cosmosdb/src/chat_histories_azure_cosmosdb_mongodb.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./azure_cosmosdb_mongodb.js"; | ||
export * from "./azure_cosmosdb_nosql.js"; | ||
export * from "./caches.js"; | ||
export * from "./chat_histories.js"; | ||
export * from "./chat_histories/nosql.js"; | ||
export * from "./chat_histories/mongodb.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters