tryAGI / LangChain

C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
https://tryagi.github.io/LangChain/
MIT License
507 stars 78 forks source link

Adding MongoDB Chat History Functionality #256

Closed vikhyat90 closed 4 months ago

vikhyat90 commented 4 months ago

Summary by CodeRabbit

CLAassistant commented 4 months ago

CLA assistant check
All committers have signed the CLA.

coderabbitai[bot] commented 4 months ago

Walkthrough

This update introduces several components for MongoDB integration within the LangChain framework. It includes interfaces and classes for database configuration, context management, and handling chat message histories. Additionally, it provides a base entity model and necessary project configurations for both unit and integration tests, enhancing the structure for MongoDB operations and testing.

Changes

File Path Change Summary
.../BsonCollectionAttribute.cs Introduces BsonCollectionAttribute for MongoDB collection naming.
.../Client/DatabaseConfiguration.cs, .../Client/IDatabaseConfiguration.cs Adds classes and interfaces for database configuration.
.../Client/IMongoContext.cs, .../Client/MongoContext.cs Defines and implements context for MongoDB interaction.
.../Client/IMongoDBClient.cs Interface for MongoDB database interactions.
.../Guard.cs Adds a class for argument validation.
.../IMongoChatMessageHistory.cs, .../MongoChatMessageHistory.cs Interfaces and classes for managing chat message history.
.../Model/BaseEntity.cs, .../Model/LangChainAISessionHistory.cs Base entity and AI session history models.
.../LangChain.Databases.Mongo.csproj, .../MongoIntegrationTests/..., .../MongoTests/... Project configurations for MongoDB and testing setups.
LangChain.sln Updates solution file with new project entries and workflows.

🐇✨ In the realm of code, where data streams flow, A rabbit hopped, with changes in tow. Collections named, connections defined, In MongoDB's maze, paths intertwined. Cheers to the craft, so deftly spun, A leap towards progress, brightly begun! 🌟📚


Recent Review Details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
Commits Files that changed from the base of the PR and between e52d6dea4b48e2fd63dfb569075f353ed006b991 and 6829d8d4994e6273626ef9348f07084e3d80b24b.
Files selected for processing (18) * LangChain.Databases.Mongo/Client/BsonCollectionAttribute.cs (1 hunks) * LangChain.Databases.Mongo/Client/DatabaseConfiguration.cs (1 hunks) * LangChain.Databases.Mongo/Client/IDatabaseConfiguration.cs (1 hunks) * LangChain.Databases.Mongo/Client/IMongoContext.cs (1 hunks) * LangChain.Databases.Mongo/Client/IMongoDBClient.cs (1 hunks) * LangChain.Databases.Mongo/Client/MongoContext.cs (1 hunks) * LangChain.Databases.Mongo/Client/MongoDBClient.cs (1 hunks) * LangChain.Databases.Mongo/Guard.cs (1 hunks) * LangChain.Databases.Mongo/IMongoChatMessageHistory.cs (1 hunks) * LangChain.Databases.Mongo/LangChain.Databases.Mongo.csproj (1 hunks) * LangChain.Databases.Mongo/Model/BaseEntity.cs (1 hunks) * LangChain.Databases.Mongo/Model/LangChainAISessionHistory.cs (1 hunks) * LangChain.Databases.Mongo/MongoChatMessageHistory.cs (1 hunks) * LangChain.Databases.MongoIntegrationTests/LangChain.Databases.MongoIntegrationTests.csproj (1 hunks) * LangChain.Databases.MongoIntegrationTests/MongoChatMessageHistoryTests.cs (1 hunks) * LangChain.Databases.MongoTests/LangChain.Databases.MongoTests.csproj (1 hunks) * LangChain.Databases.MongoTests/MongoChatMessageHistoryTests.cs (1 hunks) * LangChain.sln (4 hunks)
Files skipped from review due to trivial changes (2) * LangChain.Databases.Mongo/Client/DatabaseConfiguration.cs * LangChain.Databases.MongoTests/MongoChatMessageHistoryTests.cs
Additional comments not posted (41)
LangChain.Databases.Mongo/IMongoChatMessageHistory.cs (3)
`7-7`: Property `Messages` correctly provides read-only access to message history. --- `9-9`: Method `AddMessage` appropriately implements asynchronous addition of messages. --- `10-10`: Method `Clear` correctly provides asynchronous clearing of message history.
LangChain.Databases.Mongo/Client/IMongoContext.cs (1)
`12-12`: Method `GetCollection` correctly encapsulates functionality to access MongoDB collections by name.
LangChain.Databases.Mongo/Client/IDatabaseConfiguration.cs (2)
`11-11`: Property `ConnectionString` correctly allows for setting and getting the database connection string. --- `13-13`: Property `DatabaseName` correctly allows for setting and getting the name of the database.
LangChain.Databases.Mongo/Client/BsonCollectionAttribute.cs (2)
`11-14`: Constructor of `BsonCollectionAttribute` correctly initializes the `CollectionName` property with the provided collection name. --- `16-16`: Property `CollectionName` is appropriately read-only and encapsulates the MongoDB collection name.
LangChain.Databases.Mongo/LangChain.Databases.Mongo.csproj (2)
`3-7`: Configuration settings in the project file are appropriately set for a modern .NET project, including enabling implicit usings and nullable reference types. --- `8-14`: Package and project references are correctly included, ensuring necessary dependencies are met for the MongoDB integration.
LangChain.Databases.Mongo/Model/LangChainAISessionHistory.cs (2)
`11-12`: BSON attributes are correctly applied to `LangChainAISessionHistory`, specifying the collection name and instructing the serializer to ignore extra elements. --- `15-16`: Properties `SessionID` and `Message` are appropriately defined to capture essential information for AI session history.
LangChain.Databases.Mongo/Client/IMongoDBClient.cs (2)
`13-13`: Method `BatchDeactivate` correctly implements asynchronous deactivation of entities based on a filter, which is essential for managing entity states. --- `18-18`: Method `InsertAsync` correctly implements asynchronous insertion of entities, aligning with standard database interaction practices.
LangChain.Databases.Mongo/Model/BaseEntity.cs (1)
`14-22`: Properties in `BaseEntity` are well-defined with appropriate BSON attributes and sensible defaults, providing essential functionality for entity management.
LangChain.Databases.MongoTests/LangChain.Databases.MongoTests.csproj (3)
`4-4`: Ensure compatibility of the target framework version `net8.0` with other project components. --- `13-17`: Package versions specified are up-to-date and consistent with the project's requirements. --- `20-20`: Usage of `Microsoft.VisualStudio.TestTools.UnitTesting` namespace through `Using` directive ensures consistency in testing framework across the project.
LangChain.Databases.Mongo/Client/MongoContext.cs (2)
`14-18`: The constructor correctly initializes the MongoDB client and database using the provided configuration. This encapsulation enhances maintainability. --- `20-23`: Method `GetCollection` efficiently retrieves collections from the MongoDB database, which is crucial for performance in database operations.
LangChain.Databases.Mongo/Guard.cs (1)
`17-20`: The `AgainstNullArgument` method correctly throws an `ArgumentNullException` if the provided value is null, which is a good practice for defensive programming.
LangChain.Databases.MongoIntegrationTests/LangChain.Databases.MongoIntegrationTests.csproj (3)
`4-4`: Ensure compatibility of the target framework version `net8.0` with other project components. --- `13-17`: Package versions specified are up-to-date and consistent with the project's requirements. --- `20-20`: Proper project reference to `LangChain.Databases.Mongo.csproj` ensures that integration tests have access to the necessary MongoDB functionality.
LangChain.Databases.Mongo/MongoChatMessageHistory.cs (4)
`19-26`: Constructor correctly initializes the `_sessionId` and `_mongoRepository`, ensuring that each instance of `MongoChatMessageHistory` is properly configured for use. --- `28-31`: Method `Clear` uses the `BatchDeactivate` method from the MongoDB client to deactivate session histories, which is an efficient way to handle bulk updates. --- `33-41`: Method `AddMessage` serializes the message and stores it in the database. Ensure that serialization and deserialization processes are secure and handle potential exceptions. --- `43-55`: Property `Messages` retrieves and deserializes messages efficiently. However, consider handling potential exceptions during deserialization to improve robustness.
LangChain.Databases.Mongo/Client/MongoDBClient.cs (7)
`16-19`: Constructor correctly initializes the `_mongoContext`, ensuring that the MongoDB client is ready for database operations. --- `20-26`: Method `BatchDeactivate` efficiently handles batch updates by deactivating entities based on a filter. This method enhances performance for bulk operations. --- `28-35`: Method `Get` correctly retrieves data based on a filter and projection, which is essential for fetching specific data efficiently. --- `37-40`: Method `GetCollection` retrieves collections dynamically based on the type, which is a flexible approach to handling different data types. --- `42-50`: Method `GetCollectionName` uses reflection to fetch the collection name from attributes. Ensure that this reflection does not impact performance negatively. --- `52-59`: Method `GetSync` provides a synchronous way to fetch data, which might be necessary in certain contexts. Ensure that its usage does not lead to performance bottlenecks. --- `61-65`: Method `InsertAsync` correctly performs null checks before inserting data, which prevents errors related to null values.
LangChain.Databases.MongoIntegrationTests/MongoChatMessageHistoryTests.cs (3)
`30-42`: Test `GetMessages_EmptyHistory_Ok` correctly checks that the message history is empty when no messages have been added. This test validates the initial state of the message history. --- `44-69`: Test `AddMessage_Ok` correctly adds messages and verifies their content and roles. This test ensures that messages are stored and retrieved accurately. --- `72-84`: Test `Clear_Ok` ensures that the message history can be cleared, and it verifies that no messages exist after the clear operation. This test checks the functionality of the `Clear` method.
LangChain.sln (3)
`38-38`: Added `.github\workflows\auto-merge.yml` to the solution items under the `workflows` project. This addition aligns with the PR's objective to enhance GitHub workflows, potentially automating the merging process under certain conditions. --- `347-350`: Added `LangChain.Databases.Mongo` project to the solution under the new `Mongo` project group. This addition is crucial for the MongoDB integration, providing the necessary project structure for database-related operations within the LangChain framework. --- `351-352`: Added `LangChain.Databases.MongoIntegrationTests` project to the solution under the `Mongo` project group. This setup facilitates organized testing of the MongoDB integration, ensuring that the new database functionalities are robust and reliable.
--- Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
sweep-ai[bot] commented 4 months ago

Apply Sweep Rules to your PR?

This is an automated message generated by Sweep AI.

HavenDV commented 4 months ago

@all-contributors please add @vikhyat90 for infrastructure, tests and code

allcontributors[bot] commented 4 months ago

@HavenDV

I've put up a pull request to add @vikhyat90! :tada: