Skip to content
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

feat(community): Neo4j chat message history #7331

Merged
merged 7 commits into from
Dec 14, 2024

Conversation

BernardFaucher
Copy link
Contributor

What? Why?

This PR adds the Neo4jChatMessageHistory class to @langchain/community, in furtherance of reaching functional parity with the Python version of the package. PTAL 👀

Tests?

Yes! A new integration test is added to the corresponding tests folder, covering all the new functionality in well-scoped describe/it blocks, including set-up and tear-down. To facilitate testing, the neo4j service has also been added to test-int-deps-docker-compose.yml, pinned to the latest image. To start the test container, run: docker-compose -f test-int-deps-docker-compose.yml up -d from the project root, then run the test with: yarn run test:single src/stores/tests/neo4j.int.test.ts from the libs/langchain-community workspace. Alternatively, you can test against a neo4j server running locally with Neo4j Desktop, or in the cloud using https://sandbox.neo4j.com/ - all three options are working.

Demo?

Yes! Here's a gif of the test successfully passing:
neo4jChatMessageHistoryTest

And here's a png of what a chat message history persisted with Neo4jChatMessageHistory looks like in neo4j:
persistedChatHistory

Considerations?

Where sensible, the defaults and conventions of the python code are maintained. Any divergences are configurable, and represent a holistic view of the js/node ecosystem and coding conventions. Raw Cypher passed to the neo4j driver via the new class is tightened up and idiomatic, without hurting query functionality, and uses the params object over string interpolation as much as possible as a security consideration.

Appreciation?

Twitter is not the place to be, but I'd love a friendly shout out on Mastodon at @[email protected] or mention on my LinkedIn 🙏

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Dec 8, 2024
Copy link

vercel bot commented Dec 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Dec 14, 2024 4:05am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Dec 14, 2024 4:05am

@dosubot dosubot bot added the auto:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features label Dec 8, 2024
Copy link
Collaborator

@jacoblee93 jacoblee93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok overall, will tag in @tomasonjo for a look too though!

libs/langchain-community/src/stores/message/neo4j.ts Outdated Show resolved Hide resolved
libs/langchain-community/src/stores/message/neo4j.ts Outdated Show resolved Hide resolved
@jacoblee93 jacoblee93 added hold On hold close PRs that need one or two touch-ups to be ready labels Dec 10, 2024
@BernardFaucher
Copy link
Contributor Author

Thanks for the feedback, @jacoblee93 ! I'll run yarn format and switch from the # to the private syntax.
The call to neo4j.driver is, indeed, sync - async initialization is deferred to first driver call. I'm happy to turn it into an IIAFE, exposed from a new class method init if that's what consumers expect. I'll make those changes, and give you and Tomaz a ping when they're in 👍

@BernardFaucher
Copy link
Contributor Author

Thanks for the review - your requested changes are in, @jacoblee93 🚀
From a Neo4j perspective, what do you think @tomasonjo ? Are there further optimizations I can add?

@BernardFaucher
Copy link
Contributor Author

cc: @adam-cowley

@tomasonjo
Copy link
Contributor

Can you copy some connection validation and query execution from: https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/src/graphs/neo4j_graph.ts#L312

@BernardFaucher
Copy link
Contributor Author

Can you copy some connection validation and query execution from: https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/src/graphs/neo4j_graph.ts#L312

Thanks for reviewing @tomasonjo 🙌

To your first ask, I do have a verifyConnection class method that's public, called as part of the initialize factory method, and included in the test suite. Did you have something else in mind for connection validation?

To your second ask, I'd like to make sure I'm understanding you correctly: You'd like the Neo4jChatMessageHistory class to be able to execute arbitrary Cypher queries on the underlying database? It's totally doable, it just seems like something better left to the Neo4jGraph class. If the intention is to avoid two expensive calls to neo4j.driver, an option might be to take an existing instance of a Neo4jGraph as an optional prop in the constructor, adjusting some of the surrounding logic accordingly. I'd considered going that route when first planning the code, but decided against it because of the APOC dependency. Does that sound preferable, or is giving Neo4jChatMessageHistory a query method what you're looking for?

@tomasonjo
Copy link
Contributor

tomasonjo commented Dec 13, 2024 via email

@BernardFaucher
Copy link
Contributor Author

I understand - thanks for clarifying! I'll get right on it 💨

@BernardFaucher
Copy link
Contributor Author

@tomasonjo Your requested changes are in & all tests are green ✅
@jacoblee93 I'm satisfied with this feature - would you like anything else before 🚢?

Copy link
Collaborator

@jacoblee93 jacoblee93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dosubot dosubot bot added the lgtm PRs that are ready to be merged as-is label Dec 14, 2024
@jacoblee93 jacoblee93 changed the title Neo4j chat message history feat(community): Neo4j chat message history Dec 14, 2024
@jacoblee93
Copy link
Collaborator

Neo4jError seems to take an additional argument for code, just switched to native error

@jacoblee93 jacoblee93 removed hold On hold close PRs that need one or two touch-ups to be ready labels Dec 14, 2024
@jacoblee93 jacoblee93 merged commit b2afdf1 into langchain-ai:main Dec 14, 2024
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features lgtm PRs that are ready to be merged as-is size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants