-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add session context for a user mongodb #7436
base: main
Are you sure you want to change the base?
Changes from all commits
511f3e1
2ae8412
c823f68
c2a6775
d48e3fd
46c2ab3
4fdbd6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,13 @@ export interface AzureCosmosDBMongoChatHistoryDBConfig { | |
readonly collectionName?: string; | ||
} | ||
|
||
export type ChatSessionMongo = { | ||
id: string; | ||
context: Record<string, unknown>; | ||
}; | ||
|
||
const ID_KEY = "sessionId"; | ||
const ID_USER = "userId"; | ||
|
||
export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHistory { | ||
lc_namespace = ["langchain", "stores", "message", "azurecosmosdb"]; | ||
|
@@ -33,6 +39,8 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
|
||
private initPromise?: Promise<void>; | ||
|
||
private context: Record<string, unknown> = {}; | ||
|
||
private readonly client: MongoClient | undefined; | ||
|
||
private database: Db; | ||
|
@@ -41,11 +49,14 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
|
||
private sessionId: string; | ||
|
||
private userId: string; | ||
|
||
initialize: () => Promise<void>; | ||
|
||
constructor( | ||
dbConfig: AzureCosmosDBMongoChatHistoryDBConfig, | ||
sessionId: string | ||
sessionId: string, | ||
userId: string | ||
) { | ||
super(); | ||
|
||
|
@@ -70,6 +81,7 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
const collectionName = dbConfig.collectionName ?? "chatHistory"; | ||
|
||
this.sessionId = sessionId; | ||
this.userId = userId ?? "anonymous"; | ||
|
||
// Deferring initialization to the first call to `initialize` | ||
this.initialize = () => { | ||
|
@@ -120,6 +132,7 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
|
||
const document = await this.collection.findOne({ | ||
[ID_KEY]: this.sessionId, | ||
[ID_USER]: this.userId, | ||
}); | ||
const messages = document?.messages || []; | ||
return mapStoredMessagesToChatMessages(messages); | ||
|
@@ -134,10 +147,12 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
await this.initialize(); | ||
|
||
const messages = mapChatMessagesToStoredMessages([message]); | ||
const context = await this.getContext(); | ||
await this.collection.updateOne( | ||
{ [ID_KEY]: this.sessionId }, | ||
{ [ID_KEY]: this.sessionId, [ID_USER]: this.userId }, | ||
{ | ||
$push: { messages: { $each: messages } } as PushOperator<Document>, | ||
$set: { context }, | ||
}, | ||
{ upsert: true } | ||
); | ||
|
@@ -150,6 +165,60 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
async clear(): Promise<void> { | ||
await this.initialize(); | ||
|
||
await this.collection.deleteOne({ [ID_KEY]: this.sessionId }); | ||
await this.collection.deleteOne({ | ||
[ID_KEY]: this.sessionId, | ||
[ID_USER]: this.userId, | ||
}); | ||
} | ||
|
||
async getAllSessions(): Promise<ChatSessionMongo[]> { | ||
await this.initialize(); | ||
const documents = await this.collection.find().toArray(); | ||
|
||
const chatSessions: ChatSessionMongo[] = documents.map((doc) => ({ | ||
id: doc[ID_KEY], | ||
user_id: doc[ID_USER], | ||
context: doc.context || {}, | ||
})); | ||
|
||
return chatSessions; | ||
} | ||
|
||
async clearAllSessions() { | ||
await this.initialize(); | ||
try { | ||
await this.collection.deleteMany({}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would delete all sessions for all users, which might not be the intended behavior... I think you missed the user handling here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, add user id as handle for sessions, thank you for your comment! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you forgot to update this call to filter it for the current user only |
||
} catch (error) { | ||
console.error("Error clearing chat history sessions:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
async getContext(): Promise<Record<string, unknown>> { | ||
await this.initialize(); | ||
|
||
const document = await this.collection.findOne({ | ||
[ID_KEY]: this.sessionId, | ||
[ID_USER]: this.userId, | ||
}); | ||
this.context = document?.context || this.context; | ||
return this.context; | ||
} | ||
|
||
async setContext(context: Record<string, unknown>): Promise<void> { | ||
await this.initialize(); | ||
|
||
try { | ||
await this.collection.updateOne( | ||
{ [ID_KEY]: this.sessionId }, | ||
{ | ||
$set: { context }, | ||
}, | ||
{ upsert: true } | ||
); | ||
} catch (error) { | ||
console.error("Error setting chat history context", error); | ||
throw error; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing user filter here