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

Updated various pages to use the langchain-neo4j package #343

Open
wants to merge 1 commit into
base: publish
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ This methodology is particularly useful in instances where vector search produce
+*In[1]:*+
[source, python]
----
from langchain.vectorstores.neo4j_vector import Neo4jVector
from langchain_neo4j import Neo4jVector
from langchain_openai import OpenAIEmbeddings
import pandas as pd

Expand Down
86 changes: 30 additions & 56 deletions modules/genai-ecosystem/pages/langchain.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It manages templates, composes components into chains and supports monitoring an

The broad and deep Neo4j integration allows for vector search, cypher generation and database querying and knowledge graph construction.

Here is an overview of the https://python.langchain.com/docs/use_cases/graph/quickstart[Graph Integrations^].
Here is an overview of the https://python.langchain.com/docs/tutorials/graph/[Graph Integrations^].

* https://towardsdatascience.com/integrating-neo4j-into-the-langchain-ecosystem-df0e988344d2[Integrating Neo4j into the LangChain Ecosystem^]

Expand All @@ -27,7 +27,7 @@ image::https://cdn.graphacademy.neo4j.com/assets/img/courses/banners/llm-chatbot

[source,shell]
----
pip install langchain langchain-community
pip install langchain langchain-community langchain-neo4j
# pip install langchain-openai tiktoken
# pip install neo4j
----
Expand All @@ -50,7 +50,7 @@ The Neo4j Vector integration supports a number of operations
from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import Neo4jVector
from langchain_neo4j import Neo4jVector
from langchain_openai import OpenAIEmbeddings

loader = TextLoader("../../modules/state_of_the_union.txt")
Expand All @@ -64,7 +64,7 @@ embeddings = OpenAIEmbeddings()
# The Neo4jVector Module will connect to Neo4j and create a vector index if needed.

db = Neo4jVector.from_documents(
docs, OpenAIEmbeddings(), url=url, username=username, password=password
docs, embeddings, url=url, username=username, password=password
)

query = "What did the president say about Ketanji Brown Jackson"
Expand All @@ -85,7 +85,7 @@ Hybrid search combines vector search with fulltext search with re-ranking and de
from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import Neo4jVector
from langchain_neo4j import Neo4jVector
from langchain_openai import OpenAIEmbeddings

loader = TextLoader("../../modules/state_of_the_union.txt")
Expand All @@ -99,8 +99,8 @@ embeddings = OpenAIEmbeddings()
# The Neo4jVector Module will connect to Neo4j and create a vector index if needed.

db = Neo4jVector.from_documents(
docs, OpenAIEmbeddings(), url=url, username=username, password=password,
search_type: 'hybrid'
docs, embeddings, url=url, username=username, password=password,
search_type="hybrid"
)

query = "What did the president say about Ketanji Brown Jackson"
Expand Down Expand Up @@ -135,17 +135,17 @@ Many integrations allow you to use the Neo4j Graph as a source of data for LangC

[source,python]
----
from langchain_community.graphs import Neo4jGraph
from langchain_neo4j import Neo4jGraph

graph = Neo4jGraph(url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD)

QUERY = """
"MATCH (m:Movie)-[:IN_GENRE]->(:Genre {name:$genre})
MATCH (m:Movie)-[:IN_GENRE]->(:Genre {name: $genre})
RETURN m.title, m.plot
ORDER BY m.imdbRating DESC LIMIT 5"
ORDER BY m.imdbRating DESC LIMIT 5
"""

graph.query(QUERY, genre="action")
graph.query(QUERY, params={"genre": "action"})
----

=== CypherQAChain
Expand All @@ -159,28 +159,28 @@ Using an LLM and the graph schema it translates the user question into a Cypher
# pip install --upgrade --quiet langchain
# pip install --upgrade --quiet langchain-openai

from langchain.chains import GraphCypherQAChain
from langchain_community.graphs import Neo4jGraph
from langchain_neo4j import GraphCypherQAChain, Neo4jGraph
from langchain_openai import ChatOpenAI

graph = Neo4jGraph(url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD)

# Insert some movie data
graph.query(
"""
MERGE (m:Movie {title:"Top Gun"})
"""
MERGE (m:Movie {title:'Top Gun'})
WITH m
UNWIND ["Tom Cruise", "Val Kilmer", "Anthony Edwards", "Meg Ryan"] AS actor
UNWIND ['Tom Cruise', 'Val Kilmer', 'Anthony Edwards', 'Meg Ryan'] AS actor
MERGE (a:Actor {name:actor})
MERGE (a)-[:ACTED_IN]->(m)
"""
)

chain = GraphCypherQAChain.from_llm(
ChatOpenAI(temperature=0), graph=graph, verbose=True
ChatOpenAI(temperature=0), graph=graph, verbose=True,
allow_dangerous_requests=True
)

chain.run("Who played in Top Gun?")
chain.run("Who acted in Top Gun?")
----

=== Advanced RAG Strategies
Expand All @@ -195,8 +195,6 @@ These are also available as LangChain Templates.

* https://blog.langchain.dev/implementing-advanced-retrieval-rag-strategies-with-neo4j/[Implementing Advanced Retrieval RAG Strategies with Neo4j^]

* https://python.langchain.com/docs/templates/neo4j-advanced-rag

[source,shell]
----
pip install -U "langchain-cli[serve]"
Expand Down Expand Up @@ -227,33 +225,6 @@ langchain serve

image::https://lh7-us.googleusercontent.com/jfDNiPa5ccefX6h0HiVzJbqnlgAZgfPda90truHSfbwSs3JkfxZ-xbA9mZE8y2fNf_3n5cgVhbdhN0ryuMoK2JNbMgTe1OLJMA6CQRhWBxzdKRLVurUFDndT7ki4vMh-cdv3SAn040HTpab9XkzGj5Q[]

=== LangChain Templates

https://blog.langchain.dev/langchain-templates/[Langchain Templates^] are a set of preconfigured chains and components that can be used to build GenAI workflows and applications.
You can test them interactively on the LangChain Playground and run them with https://github.com/langchain-ai/langserve[LangServe^] to run as REST APIs, they also integrate with [LangSmith] for monitoring and observability.

By creating an application from templates, their source code is added to your application and you can modify them to fit your needs.

==== List of Templates

This https://python.langchain.com/docs/templates/neo4j-cypher[Cypher template] allows you to interact with a Neo4j graph database in natural language, using an OpenAI LLM.

It transforms a natural language question into a Cypher query (used to fetch data from Neo4j databases), executes the query, and provides a natural language response based on the query results.

The https://python.langchain.com/docs/templates/neo4j-cypher-ft[Cypher-FT Template^] additionally utilizes a full-text index for efficient mapping of text values to database entries, thereby enhancing the generation of accurate Cypher statements.

The https://python.langchain.com/docs/templates/neo4j-cypher-memory[Cypher Memory Template^] also features a conversational memory module that stores the dialogue history in the Neo4j graph database. The conversation memory is uniquely maintained for each user session, ensuring personalized interactions.

The https://python.langchain.com/docs/templates/neo4j-generation[Neo4j generation Template^] pairs LLM-based knowledge graph extraction using OpenAI functions, with Neo4j AuraDB, a fully managed cloud graph database.

This https://python.langchain.com/docs/templates/neo4j-vector-memory[Neo4j Vector Memory Template^] allows you to integrate an LLM with a vector-based retrieval system using Neo4j as the vector store. Additionally, it uses the graph capabilities of the Neo4j database to store and retrieve the dialogue history of a specific user's session. Having the dialogue history stored as a graph allows for seamless conversational flows but also gives you the ability to analyze user behavior and text chunk retrieval through graph analytics.

The https://python.langchain.com/docs/templates/neo4j-parent[Parent-Child Retriever Template^] allows you to balance precise embeddings and context retention by splitting documents into smaller chunks and retrieving their original or larger text information.

Using a Neo4j vector index, the package queries child nodes using vector similarity search and retrieves the corresponding parent's text.

The https://python.langchain.com/docs/templates/neo4j-semantic-layer[Neo4j Semantic Layer Template^] is designed to implement an agent capable of interacting with a graph database like Neo4j through a semantic layer using OpenAI function calling. The semantic layer equips the agent with a suite of robust tools, allowing it to interact with the graph databas based on the user's intent.

=== Semantic Layer

A semantic layer on top of a (graph) database doesn't rely on automatic query generation but offers a number of APIs and tools to give the LLM access to the database and it's structures.
Expand Down Expand Up @@ -281,14 +252,14 @@ You can index embeddings for and link questions and answers back to the retrieve

Creating a Knowledge Graph from unstructured data like PDF documents used to be a complex and time-consuming task that required training and using dedicated, large NLP models.

The https://python.langchain.com/docs/use_cases/graph/constructing[Graph Transformers^] are tools that allows you to extract structured data from unstructured documents and transform it into a Knowledge Graph.
The https://python.langchain.com/v0.1/docs/use_cases/graph/constructing[Graph Transformers^] are tools that allows you to extract structured data from unstructured documents and transform it into a Knowledge Graph.

NOTE: You can see a practical application, code and demo for extracting knowledge graphs from PDFs, YouTube transcripts, wikpedia articles and more with the xref:llm-graph-builder.adoc[LLM Graph Builder].

image::

* https://python.langchain.com/docs/use_cases/graph/integrations/diffbot_graphtransformer[Diffbot Graph Transformer^]
* https://python.langchain.com/docs/use_cases/graph/constructing#llm-graph-transformer[LLM Graph Transformer^]
* https://python.langchain.com/docs/integrations/graphs/diffbot/[Diffbot Graph Transformer^]
* https://python.langchain.com/v0.1/docs/use_cases/graph/constructing/#llm-graph-transformer[LLM Graph Transformer^]

* https://neo4j.com/developer-blog/knowledge-graph-based-chatbot-with-gpt-3-and-neo4j/[Knowledge Graph-based Chatbot with GPT-3 and Neo4j^]
* https://blog.langchain.dev/constructing-knowledge-graphs-from-text-using-openai-functions/[Constructing Knowledge Graphs from Text using OpenAI Functions^]
Expand All @@ -298,13 +269,13 @@ image::
== Documentation

* https://python.langchain.com/docs/integrations/providers/neo4j/[Neo4j Integrations^]
* https://python.langchain.com/docs/use_cases/graph/graph_cypher_qa[Graph Cypher QA Chain^]
* https://python.langchain.com/docs/tutorials/graph/#chain[Graph Cypher QA Chain^]
* https://python.langchain.com/docs/integrations/vectorstores/neo4jvector[Neo4j Vector^]
* https://python.langchain.com/docs/use_cases/graph/constructing[Graph Transformers^]
* https://python.langchain.com/v0.1/docs/use_cases/graph/constructing[Graph Transformers^]

== Starter Kit

This https://github.com/neo4j-examples/langchain-starter-kit/blob/main/app/vector_chain.py[starter-kit] demonstrates how to run a FastAPI server using LangChain to answer queries on data stored in a Neo4j instance. The single endpoint can be used to retrieve answers using either a https://python.langchain.com/v0.1/docs/integrations/vectorstores/neo4jvector/[Vector index chain], https://python.langchain.com/v0.1/docs/integrations/graphs/neo4j_cypher/[GraphCypherQA Chain], or a composite answer of both. The https://github.com/neo4j-examples/langchain-starter-kit/tree/langserve[langserve] branch contains an example of the same service, using https://python.langchain.com/v0.1/docs/langserve/[LangServe]
This https://github.com/neo4j-examples/langchain-starter-kit/blob/main/app/vector_chain.py[starter-kit] demonstrates how to run a FastAPI server using LangChain to answer queries on data stored in a Neo4j instance. The single endpoint can be used to retrieve answers using either a https://python.langchain.com/docs/integrations/vectorstores/neo4jvector/[Vector index chain], https://python.langchain.com/docs/integrations/graphs/neo4j_cypher/[GraphCypherQA Chain], or a composite answer of both. The https://github.com/neo4j-examples/langchain-starter-kit/tree/langserve[langserve] branch contains an example of the same service, using https://python.langchain.com/v0.1/docs/langserve/[LangServe]

See this https://neo4j.com/developer-blog/langchain-neo4j-starter-kit/[Developer Blog Article] for additional details and instructions on working with the Starter Kit.

Expand All @@ -313,10 +284,13 @@ See this https://neo4j.com/developer-blog/langchain-neo4j-starter-kit/[Developer
|===
| icon:user[] Authors | https://github.com/tomasonjo[Tomaz Bratanic^]
| icon:comments[] Community Support | https://community.neo4j.com/[Neo4j Online Community^]
| icon:github[] Data Repository | https://github.com/langchain-ai/langchain[GitHub]
| icon:github[] Issues | https://github.com/langchain-ai/langchain/issues
| icon:github[] LangChain Repository | https://github.com/langchain-ai/langchain[GitHub]
| icon:github[] LangChain Issues | https://github.com/langchain-ai/langchain/issues[Issues]
| icon:github[] LangChain Neo4j Repository | https://github.com/langchain-ai/langchain-neo4j[GitHub]
| icon:book[] LangChain Neo4j API Docs | https://python.langchain.com/api_reference/neo4j/index.html[API Docs]
| icon:book[] LangChain Neo4j Issues | https://github.com/langchain-ai/langchain-neo4j/issues[Issues]
| icon:book[] Documentation | https://python.langchain.com/docs/integrations/providers/neo4j/[Neo4j Integrations^]
| icon:book[] Documentation | https://python.langchain.com/docs/use_cases/graph/quickstart[Graph Overview Docs^]
| icon:book[] Documentation | https://python.langchain.com/docs/tutorials/graph[Graph Overview Docs^]
| icon:github[] Starter Kit |https://github.com/neo4j-examples/langchain-starter-kit[LangChain Starter Kit^]
| icon:code[] Juypter | https://github.com/tomasonjo/blogs/tree/master/llm[Jupyter Notebooks^]
|===
Expand Down
2 changes: 1 addition & 1 deletion modules/genai-ecosystem/pages/llm-graph-builder.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Afterwards you can use different RAG approaches (GraphRAG, Vector, Text2Cypher)
====

The front-end is a React Application and the back-end a Python FastAPI application running on Google Cloud Run, but you can deploy it locally using docker compose.
It uses the https://python.langchain.com/docs/use_cases/graph/constructing[llm-graph-transformer module^] that Neo4j contributed to LangChain and other langchain integrations (e.g. for GraphRAG search).
It uses the https://python.langchain.com/v0.1/docs/use_cases/graph/constructing[llm-graph-transformer module^] that Neo4j contributed to LangChain and other langchain integrations (e.g. for GraphRAG search).

All Features are documented in detail here: xref::llm-graph-builder-features.adoc[]

Expand Down