mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-17 10:13:29 +00:00
community: Fix links in GraphVectorStore pydoc (#25959)
Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
parent
e49c413977
commit
58f339a67c
@ -1,6 +1,9 @@
|
|||||||
"""**Graph Vector Store**
|
""".. title:: Graph Vector Store
|
||||||
|
|
||||||
Sometimes embedding models don’t capture all the important relationships between
|
Graph Vector Store
|
||||||
|
==================
|
||||||
|
|
||||||
|
Sometimes embedding models don't capture all the important relationships between
|
||||||
documents.
|
documents.
|
||||||
Graph Vector Stores are an extension to both vector stores and retrievers that allow
|
Graph Vector Stores are an extension to both vector stores and retrievers that allow
|
||||||
documents to be explicitly connected to each other.
|
documents to be explicitly connected to each other.
|
||||||
@ -13,11 +16,10 @@ Each document identifies tags that link to and from it.
|
|||||||
For example, a paragraph of text may be linked to URLs based on the anchor tags in
|
For example, a paragraph of text may be linked to URLs based on the anchor tags in
|
||||||
it's content and linked from the URL(s) it is published at.
|
it's content and linked from the URL(s) it is published at.
|
||||||
|
|
||||||
Link extractors can be used to extract links from documents.
|
`Link extractors <langchain_community.graph_vectorstores.extractors.link_extractor.LinkExtractor>`
|
||||||
|
can be used to extract links from documents.
|
||||||
|
|
||||||
Example:
|
Example::
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
graph_vector_store = CassandraGraphVectorStore()
|
graph_vector_store = CassandraGraphVectorStore()
|
||||||
link_extractor = HtmlLinkExtractor()
|
link_extractor = HtmlLinkExtractor()
|
||||||
@ -25,13 +27,18 @@ Example:
|
|||||||
add_links(document, links)
|
add_links(document, links)
|
||||||
graph_vector_store.add_document(document)
|
graph_vector_store.add_document(document)
|
||||||
|
|
||||||
***********
|
.. seealso::
|
||||||
|
|
||||||
|
- :class:`How to use a graph vector store as a retriever <langchain_community.graph_vectorstores.base.GraphVectorStoreRetriever>`
|
||||||
|
- :class:`How to create links between documents <langchain_community.graph_vectorstores.links.Link>`
|
||||||
|
- :class:`How to link Documents on hyperlinks in HTML <langchain_community.graph_vectorstores.extractors.html_link_extractor.HtmlLinkExtractor>`
|
||||||
|
- :class:`How to link Documents on common keywords (using KeyBERT) <langchain_community.graph_vectorstores.extractors.keybert_link_extractor.KeybertLinkExtractor>`
|
||||||
|
- :class:`How to link Documents on common named entities (using GliNER) <langchain_community.graph_vectorstores.extractors.gliner_link_extractor.GLiNERLinkExtractor>`
|
||||||
|
|
||||||
Get started
|
Get started
|
||||||
***********
|
-----------
|
||||||
|
|
||||||
We chunk the State of the Union text and split it into documents.
|
We chunk the State of the Union text and split it into documents::
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from langchain_community.document_loaders import TextLoader
|
from langchain_community.document_loaders import TextLoader
|
||||||
from langchain_text_splitters import CharacterTextSplitter
|
from langchain_text_splitters import CharacterTextSplitter
|
||||||
@ -41,14 +48,12 @@ We chunk the State of the Union text and split it into documents.
|
|||||||
documents = text_splitter.split_documents(raw_documents)
|
documents = text_splitter.split_documents(raw_documents)
|
||||||
|
|
||||||
Links can be added to documents manually but it's easier to use a
|
Links can be added to documents manually but it's easier to use a
|
||||||
:class:`~langchain_community.graph_vectorstores.extractors.LinkExtractor`.
|
:class:`~langchain_community.graph_vectorstores.extractors.link_extractor.LinkExtractor`.
|
||||||
Several common link extractors are available and you can build your own.
|
Several common link extractors are available and you can build your own.
|
||||||
For this guide, we'll use the
|
For this guide, we'll use the
|
||||||
:class:`~langchain_community.graph_vectorstores.extractors.KeybertLinkExtractor`
|
:class:`~langchain_community.graph_vectorstores.extractors.keybert_link_extractor.KeybertLinkExtractor`
|
||||||
which uses the KeyBERT model to tag documents with keywords and uses these keywords to
|
which uses the KeyBERT model to tag documents with keywords and uses these keywords to
|
||||||
create links between documents.
|
create links between documents::
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from langchain_community.graph_vectorstores.extractors import KeybertLinkExtractor
|
from langchain_community.graph_vectorstores.extractors import KeybertLinkExtractor
|
||||||
from langchain_community.graph_vectorstores.links import add_links
|
from langchain_community.graph_vectorstores.links import add_links
|
||||||
@ -58,15 +63,14 @@ create links between documents.
|
|||||||
for doc in documents:
|
for doc in documents:
|
||||||
add_links(doc, extractor.extract_one(doc))
|
add_links(doc, extractor.extract_one(doc))
|
||||||
|
|
||||||
***********************************************
|
|
||||||
Create the graph vector store and add documents
|
Create the graph vector store and add documents
|
||||||
***********************************************
|
-----------------------------------------------
|
||||||
|
|
||||||
We'll use an Apache Cassandra or Astra DB database as an example.
|
We'll use an Apache Cassandra or Astra DB database as an example.
|
||||||
We create a :class:`~langchain_community.graph_vectorstores.CassandraGraphVectorStore`
|
We create a
|
||||||
from the documents and an :class:`~langchain_openai.OpenAIEmbeddings` model.
|
:class:`~langchain_community.graph_vectorstores.cassandra.CassandraGraphVectorStore`
|
||||||
|
from the documents and an :class:`~langchain_openai.embeddings.base.OpenAIEmbeddings`
|
||||||
.. code-block:: python
|
model::
|
||||||
|
|
||||||
import cassio
|
import cassio
|
||||||
from langchain_community.graph_vectorstores import CassandraGraphVectorStore
|
from langchain_community.graph_vectorstores import CassandraGraphVectorStore
|
||||||
@ -80,45 +84,37 @@ from the documents and an :class:`~langchain_openai.OpenAIEmbeddings` model.
|
|||||||
documents=documents,
|
documents=documents,
|
||||||
)
|
)
|
||||||
|
|
||||||
*****************
|
|
||||||
Similarity search
|
Similarity search
|
||||||
*****************
|
-----------------
|
||||||
|
|
||||||
If we don't traverse the graph, a graph vector store behaves like a regular vector
|
If we don't traverse the graph, a graph vector store behaves like a regular vector
|
||||||
store.
|
store.
|
||||||
So all methods available in a vector store are also available in a graph vector store.
|
So all methods available in a vector store are also available in a graph vector store.
|
||||||
The :meth:`~langchain_community.graph_vectorstores.base.GraphVectorStore.similarity_search`
|
The :meth:`~langchain_community.graph_vectorstores.base.GraphVectorStore.similarity_search`
|
||||||
method returns documents similar to a query without considering
|
method returns documents similar to a query without considering
|
||||||
the links between documents.
|
the links between documents::
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
docs = store.similarity_search(
|
docs = store.similarity_search(
|
||||||
"What did the president say about Ketanji Brown Jackson?"
|
"What did the president say about Ketanji Brown Jackson?"
|
||||||
)
|
)
|
||||||
|
|
||||||
****************
|
|
||||||
Traversal search
|
Traversal search
|
||||||
****************
|
----------------
|
||||||
|
|
||||||
The :meth:`~langchain_community.graph_vectorstores.base.GraphVectorStore.traversal_search`
|
The :meth:`~langchain_community.graph_vectorstores.base.GraphVectorStore.traversal_search`
|
||||||
method returns documents similar to a query considering the links
|
method returns documents similar to a query considering the links
|
||||||
between documents. It first does a similarity search and then traverses the graph to
|
between documents. It first does a similarity search and then traverses the graph to
|
||||||
find linked documents.
|
find linked documents::
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
docs = list(
|
docs = list(
|
||||||
store.traversal_search("What did the president say about Ketanji Brown Jackson?")
|
store.traversal_search("What did the president say about Ketanji Brown Jackson?")
|
||||||
)
|
)
|
||||||
|
|
||||||
*************
|
|
||||||
Async methods
|
Async methods
|
||||||
*************
|
-------------
|
||||||
|
|
||||||
The graph vector store has async versions of the methods prefixed with ``a``.
|
The graph vector store has async versions of the methods prefixed with ``a``::
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
docs = [
|
docs = [
|
||||||
doc
|
doc
|
||||||
@ -127,15 +123,12 @@ The graph vector store has async versions of the methods prefixed with ``a``.
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
****************************
|
|
||||||
Graph vector store retriever
|
Graph vector store retriever
|
||||||
****************************
|
----------------------------
|
||||||
|
|
||||||
The graph vector store can be converted to a retriever.
|
The graph vector store can be converted to a retriever.
|
||||||
It is similar to the vector store retriever but it also has traversal search methods
|
It is similar to the vector store retriever but it also has traversal search methods
|
||||||
such as ``traversal`` and ``mmr_traversal``.
|
such as ``traversal`` and ``mmr_traversal``::
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
retriever = store.as_retriever(search_type="mmr_traversal")
|
retriever = store.as_retriever(search_type="mmr_traversal")
|
||||||
docs = retriever.invoke("What did the president say about Ketanji Brown Jackson?")
|
docs = retriever.invoke("What did the president say about Ketanji Brown Jackson?")
|
||||||
|
Loading…
Reference in New Issue
Block a user