-
Notifications
You must be signed in to change notification settings - Fork 7
Add JavaScript/TypeScript integration to LangChain documentation #298
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
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/add-falkordb-langchain-js-docs
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3ab2416
Initial plan
Copilot 7d57744
Add JavaScript/TypeScript integration section to LangChain docs
Copilot a16e2ff
Add cross-references and update index pages for LangChain JS/TS integ…
Copilot ab2a491
Simplify installation command in JS/TS integration section
Copilot a0808ea
Merge branch 'main' into copilot/add-falkordb-langchain-js-docs
gkorland a878203
Merge branch 'main' into copilot/add-falkordb-langchain-js-docs
gkorland 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 hidden or 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 hidden or 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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,22 +9,30 @@ parent: "GenAI Tools" | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| FalkorDB is integrated with [LangChain](https://www.langchain.com/), bringing powerful graph database capabilities to AI-driven applications. This integration enables the creation of AI agents with memory, enhancing their ability to retain state and context across interactions. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The FalkorDB LangChain integration is available for both **Python** and **JavaScript/TypeScript** environments, making it easy to build intelligent applications in your preferred language. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Resources | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - 🔗 [FalkorDBQAChain Documentation](https://python.langchain.com/docs/use_cases/more/graph/graph_falkordb_qa) | ||||||||||||||||||||||
| - 🔗 [FalkorDBQAChain Documentation (Python)](https://python.langchain.com/docs/use_cases/more/graph/graph_falkordb_qa) | ||||||||||||||||||||||
| - 📦 [@falkordb/langchain-ts Package (JavaScript/TypeScript)](https://www.npmjs.com/package/@falkordb/langchain-ts) | ||||||||||||||||||||||
| - 💻 [FalkorDB-Langchain-js Repository](https://github.com/FalkorDB/FalkorDB-Langchain-js) | ||||||||||||||||||||||
| - 📓 [Blog: Build AI Agents with Memory – LangChain + FalkorDB](https://www.falkordb.com/blog/building-ai-agents-with-memory-langchain/) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Installation | ||||||||||||||||||||||
| --- | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Python Integration | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Installation | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Install LangChain with FalkorDB support: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```bash | ||||||||||||||||||||||
| pip install langchain langchain-community falkordb | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Quick Start | ||||||||||||||||||||||
| ### Quick Start | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 1. Connect to FalkorDB | ||||||||||||||||||||||
| #### 1. Connect to FalkorDB | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| from langchain_community.graphs import FalkorDBGraph | ||||||||||||||||||||||
|
|
@@ -39,7 +47,7 @@ graph = FalkorDBGraph( | |||||||||||||||||||||
| ) | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 2. Create a Knowledge Graph from Text | ||||||||||||||||||||||
| #### 2. Create a Knowledge Graph from Text | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| from langchain.chains import GraphCypherQAChain | ||||||||||||||||||||||
|
|
@@ -56,7 +64,7 @@ chain = GraphCypherQAChain.from_llm( | |||||||||||||||||||||
| ) | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 3. Query the Graph | ||||||||||||||||||||||
| #### 3. Query the Graph | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| # Ask natural language questions | ||||||||||||||||||||||
|
|
@@ -68,9 +76,9 @@ response = chain.run("What other movies did they act in?") | |||||||||||||||||||||
| print(response) | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Advanced Usage | ||||||||||||||||||||||
| ### Advanced Usage | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Using Graph Memory for Conversational AI | ||||||||||||||||||||||
| #### Using Graph Memory for Conversational AI | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| from langchain.memory import ConversationGraphMemory | ||||||||||||||||||||||
|
|
@@ -96,7 +104,7 @@ conversation.predict(input="I work as a software engineer") | |||||||||||||||||||||
| conversation.predict(input="What do you know about me?") | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Custom Cypher Generation | ||||||||||||||||||||||
| #### Custom Cypher Generation | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| from langchain.chains.graph_qa.cypher import GraphCypherQAChain | ||||||||||||||||||||||
|
|
@@ -131,7 +139,7 @@ chain = GraphCypherQAChain.from_llm( | |||||||||||||||||||||
| response = chain.run("Find all products in the electronics category") | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Loading Data into the Graph | ||||||||||||||||||||||
| #### Loading Data into the Graph | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| from langchain_community.document_loaders import TextLoader | ||||||||||||||||||||||
|
|
@@ -156,7 +164,7 @@ vector_store = FalkorDBVector.from_documents( | |||||||||||||||||||||
| ) | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Graph RAG Pattern | ||||||||||||||||||||||
| #### Graph RAG Pattern | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| from langchain.chains import RetrievalQA | ||||||||||||||||||||||
|
|
@@ -183,6 +191,101 @@ response = qa_chain.run("What are the key features of our product?") | |||||||||||||||||||||
| print(response) | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| --- | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## JavaScript/TypeScript Integration | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| FalkorDB also provides a JavaScript/TypeScript integration for LangChain applications through the [@falkordb/langchain-ts](https://www.npmjs.com/package/@falkordb/langchain-ts) package. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Installation | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```bash | ||||||||||||||||||||||
| npm install @falkordb/langchain-ts falkordb langchain @langchain/openai | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### Quick Start (JS/TS) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```typescript | ||||||||||||||||||||||
| import { FalkorDBGraph } from "@falkordb/langchain-ts"; | ||||||||||||||||||||||
| import { ChatOpenAI } from "@langchain/openai"; | ||||||||||||||||||||||
| import { GraphCypherQAChain } from "@langchain/community/chains/graph_qa/cypher"; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Initialize FalkorDB connection | ||||||||||||||||||||||
| const graph = await FalkorDBGraph.initialize({ | ||||||||||||||||||||||
| host: "localhost", | ||||||||||||||||||||||
| port: 6379, | ||||||||||||||||||||||
| graph: "movies" | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Set up the language model | ||||||||||||||||||||||
| const model = new ChatOpenAI({ temperature: 0 }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Create and populate the graph | ||||||||||||||||||||||
| await graph.query( | ||||||||||||||||||||||
| "CREATE (a:Actor {name:'Bruce Willis'})" + | ||||||||||||||||||||||
| "-[:ACTED_IN]->(:Movie {title: 'Pulp Fiction'})" | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Refresh the graph schema | ||||||||||||||||||||||
| await graph.refreshSchema(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Create a graph QA chain | ||||||||||||||||||||||
| const chain = GraphCypherQAChain.fromLLM({ | ||||||||||||||||||||||
| llm: model, | ||||||||||||||||||||||
| graph: graph as any, | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
Comment on lines
+234
to
+236
|
||||||||||||||||||||||
| llm: model, | |
| graph: graph as any, | |
| }); | |
| // If needed, define a minimal type for the graph to avoid 'as any'. | |
| // For example, if GraphCypherQAChain expects a BaseGraph type: | |
| // import type { BaseGraph } from "@langchain/community/graphs/base"; | |
| // (Uncomment the import above and use the correct type if available) | |
| const chain = GraphCypherQAChain.fromLLM({ | |
| llm: model, | |
| graph: graph, // graph should already be of the correct type |
This file contains hidden or 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 hidden or 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.
The code example uses 'graph' as the parameter name, but according to the API Reference section below (line 263), this parameter should be documented consistently. Consider clarifying whether the parameter is 'graph' or 'graphName' to avoid confusion.