-
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
Open
crisjy
wants to merge
6
commits into
langchain-ai:main
Choose a base branch
from
crisjy:chathistory_session
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
511f3e1
add session context for a user mongodb
crisjy 2ae8412
add session context for a user mongodb
crisjy c823f68
Merge branch 'langchain-ai:main' into chathistory_session
crisjy c2a6775
format
crisjy d48e3fd
Merge branch 'main' into chathistory_session
crisjy 46c2ab3
Merge branch 'langchain-ai:main' into chathistory_session
crisjy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -20,6 +20,11 @@ export interface AzureCosmosDBMongoChatHistoryDBConfig { | |
readonly collectionName?: string; | ||
} | ||
|
||
export type ChatSession = { | ||
id: string; | ||
context: Record<string, unknown>; | ||
}; | ||
|
||
const ID_KEY = "sessionId"; | ||
|
||
export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHistory { | ||
|
@@ -33,6 +38,8 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
|
||
private initPromise?: Promise<void>; | ||
|
||
private context: Record<string, unknown> = {}; | ||
|
||
private readonly client: MongoClient | undefined; | ||
|
||
private database: Db; | ||
|
@@ -134,10 +141,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 }, | ||
{ | ||
$push: { messages: { $each: messages } } as PushOperator<Document>, | ||
$set: { context }, | ||
}, | ||
{ upsert: true } | ||
); | ||
|
@@ -152,4 +161,51 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis | |
|
||
await this.collection.deleteOne({ [ID_KEY]: this.sessionId }); | ||
} | ||
|
||
async getAllSessions(): Promise<ChatSession[]> { | ||
await this.initialize(); | ||
const documents = await this.collection.find().toArray(); | ||
|
||
const chatSessions: ChatSession[] = documents.map((doc) => ({ | ||
id: doc[ID_KEY], | ||
context: doc.context || {}, | ||
})); | ||
|
||
return chatSessions; | ||
} | ||
|
||
async clearAllSessions() { | ||
await this.initialize(); | ||
try { | ||
await this.collection.deleteMany({}); | ||
} catch (error) { | ||
console.log("Error clearing sessions:", error); | ||
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. Would be better to throw so these errors can be identified |
||
} | ||
} | ||
|
||
async getContext(): Promise<Record<string, unknown>> { | ||
await this.initialize(); | ||
|
||
const document = await this.collection.findOne({ | ||
[ID_KEY]: this.sessionId, | ||
}); | ||
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.log("Error setting context", error); | ||
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. Would be better to throw so these errors can be identified |
||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This would delete all sessions for all users, which might not be the intended behavior... I think you missed the user handling here