Entrypoint into langchain-community.
.. warning:: Beta Feature!
Cache provides an optional caching layer for LLMs.
Cache is useful for two reasons:
Cache directly competes with Memory. See documentation for Pros and Cons.
Class hierarchy:
.. code-block::
BaseCache --> <name>Cache # Examples: InMemoryCache, RedisCache, GPTCache
Document Loaders are classes to load Documents.
Document Loaders are usually used to load a lot of Documents in a single run.
Class hierarchy:
.. code-block::
BaseLoader --> <name>Loader # Examples: TextLoader, UnstructuredFileLoader
Main helpers:
.. code-block::
Document, <name>TextSplitter
langchain-community utilities.
Embedding models are wrappers around embedding models from different APIs and services.
Embedding models can be LLMs or not.
Class hierarchy:
.. code-block::
Embeddings --> <name>Embeddings # Examples: OpenAIEmbeddings, HuggingFaceEmbeddings
Storage is an implementation of key-value store.
Storage module provides implementations of various key-value stores that conform to a simple key-value interface.
The primary goal of these storages is to support caching.
Class hierarchy:
.. code-block::
BaseStore --> <name>Store # Examples: MongoDBStore, RedisStore
Retriever class returns Documents given a text query.
It is more general than a vector store. A retriever does not need to be able to store documents, only to return (or retrieve) it. Vector stores can be used as the backbone of a retriever, but there are other types of retrievers as well.
Class hierarchy:
.. code-block::
BaseRetriever --> <name>Retriever # Examples: ArxivRetriever, MergerRetriever
Main helpers:
.. code-block::
Document, Serializable, Callbacks,
CallbackManagerForRetrieverRun, AsyncCallbackManagerForRetrieverRun
Cross encoders are wrappers around cross encoder models from different APIs and services.
Cross encoder models can be LLMs or not.
Class hierarchy:
.. code-block::
BaseCrossEncoder --> <name>CrossEncoder # Examples: SagemakerEndpointCrossEncoder
Chat Loaders load chat messages from common communications platforms.
Load chat messages from various communications platforms such as Facebook Messenger, Telegram, and WhatsApp. The loaded chat messages can be used for fine-tuning models.
Class hierarchy:
.. code-block::
BaseChatLoader --> <name>ChatLoader # Examples: WhatsAppChatLoader, IMessageChatLoader
Main helpers:
.. code-block::
ChatSession
Chat Models are a variation on language models.
While Chat Models use language models under the hood, the interface they expose is a bit different. Rather than expose a "text in, text out" API, they expose an interface where "chat messages" are the inputs and outputs.
OutputParser classes parse the output of an LLM call.
Class hierarchy:
.. code-block::
BaseLLMOutputParser --> BaseOutputParser --> <name>OutputParser # GuardrailsOutputParser
Main helpers:
.. code-block::
Serializable, Generation, PromptValue
Tools are classes that an Agent uses to interact with the world.
Each tool has a description. Agent uses the description to choose the right tool for the job.
Class hierarchy:
.. code-block::
ToolMetaclass --> BaseTool --> <name>Tool # Examples: AIPluginTool, BaseGraphQLTool
<name> # Examples: BraveSearch, HumanInputRun
Main helpers:
.. code-block::
CallbackManagerForToolRun, AsyncCallbackManagerForToolRun
Utilities are the integrations with third-party systems and packages.
Other LangChain classes use Utilities to interact with third-party systems and packages.
Toolkits are sets of tools that can be used to interact with various services and APIs.
Graphs provide a natural language interface to graph databases.
Docstores are classes to store and load Documents.
The Docstore is a simplified version of the Document Loader.
Class hierarchy:
.. code-block::
Docstore --> <name> # Examples: InMemoryDocstore, Wikipedia
Main helpers:
.. code-block::
Document, AddableMixin
Example selector implements logic for selecting examples to include them in prompts.
This allows us to select examples that are most relevant to the input.
There could be multiple strategies for selecting examples.
For example, one could select examples based on the similarity of the input to the examples. Another strategy could be to select examples based on the diversity of the examples.
Document Transformers are classes to transform Documents.
Document Transformers usually used to transform a lot of Documents in a single run.
Class hierarchy:
.. code-block::
BaseDocumentTransformer --> <name> # Examples: DoctranQATransformer, DoctranTextTranslator
Main helpers:
.. code-block::
Document
Chains module for langchain_community
This module contains the community chains.
Chat message history stores a history of the message interactions in a chat.
Class hierarchy:
.. code-block::
BaseChatMessageHistory --> <name>ChatMessageHistory # Examples: FileChatMessageHistory, PostgresChatMessageHistory
Main helpers:
.. code-block::
AIMessage, HumanMessage, BaseMessage
Callback handlers allow listening to events in LangChain.
Class hierarchy:
.. code-block::
BaseCallbackHandler --> <name>CallbackHandler # Example: AimCallbackHandler
Vector store stores embedded data and performs vector search.
One of the most common ways to store and search over unstructured data is to embed it and store the resulting embedding vectors, and then query the store and retrieve the data that are 'most similar' to the embedded query.
Adapters are used to adapt LangChain models to other APIs.
LangChain integrates with many model providers.
While LangChain has its own message and model APIs, LangChain has also made it as easy as possible to explore other models by exposing an adapter to adapt LangChain models to the other APIs, such as to the OpenAI API.
.. title:: Graph Vector Store
Sometimes embedding models don't capture all the important relationships between documents. Graph Vector Stores are an extension to both vector stores and retrievers that allow documents to be explicitly connected to each other.
Graph vector store retrievers use both vector similarity and links to find documents related to an unstructured query.
Graphs allow linking between documents. 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 it's content and linked from the URL(s) it is published at.
Link extractors <langchain_community.graph_vectorstores.extractors.link_extractor.LinkExtractor>
can be used to extract links from documents.
Example::
graph_vector_store = CassandraGraphVectorStore()
link_extractor = HtmlLinkExtractor()
links = link_extractor.extract_one(HtmlInput(document.page_content, "http://mysite"))
add_links(document, links)
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>`
- `langchain-jieba: link extraction tailored for Chinese language <https://github.com/cqzyys/langchain-jieba>`_
We chunk the State of the Union text and split it into documents::
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
raw_documents = TextLoader("state_of_the_union.txt").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(raw_documents)
Links can be added to documents manually but it's easier to use a
:class:~langchain_community.graph_vectorstores.extractors.link_extractor.LinkExtractor.
Several common link extractors are available and you can build your own.
For this guide, we'll use the
: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
create links between documents::
from langchain_community.graph_vectorstores.extractors import KeybertLinkExtractor
from langchain_community.graph_vectorstores.links import add_links
extractor = KeybertLinkExtractor()
for doc in documents:
add_links(doc, extractor.extract_one(doc))
We'll use an Apache Cassandra or Astra DB database as an example.
We create a
:class:~langchain_community.graph_vectorstores.cassandra.CassandraGraphVectorStore
from the documents and an :class:~langchain_openai.embeddings.base.OpenAIEmbeddings
model::
import cassio
from langchain_community.graph_vectorstores import CassandraGraphVectorStore
from langchain_openai import OpenAIEmbeddings
# Initialize cassio and the Cassandra session from the environment variables
cassio.init(auto=True)
store = CassandraGraphVectorStore.from_documents(
embedding=OpenAIEmbeddings(),
documents=documents,
)
If we don't traverse the graph, a graph vector store behaves like a regular 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
method returns documents similar to a query without considering
the links between documents::
docs = store.similarity_search(
"What did the president say about Ketanji Brown Jackson?"
)
The :meth:~langchain_community.graph_vectorstores.base.GraphVectorStore.traversal_search
method returns documents similar to a query considering the links
between documents. It first does a similarity search and then traverses the graph to
find linked documents::
docs = list(
store.traversal_search("What did the president say about Ketanji Brown Jackson?")
)
The graph vector store has async versions of the methods prefixed with a::
docs = [
doc
async for doc in store.atraversal_search(
"What did the president say about Ketanji Brown Jackson?"
)
]
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
such as traversal and mmr_traversal::
retriever = store.as_retriever(search_type="mmr_traversal")
docs = retriever.invoke("What did the president say about Ketanji Brown Jackson?")
Index is used to avoid writing duplicated content into the vectostore and to avoid over-writing content if it's unchanged.
Indexes also :
Create knowledge graphs from data.
Support indexing workflows from LangChain data loaders to vectorstores.
Importantly, Index keeps on working even if the content being written is derived via a set of transformations from some source content (e.g., indexing children documents that were derived from parent documents by chunking.)
LLM classes provide access to the large language model (LLM) APIs and services.
Class hierarchy:
.. code-block::
BaseLanguageModel --> BaseLLM --> LLM --> <name> # Examples: AI21, HuggingFaceHub, OpenAI
Main helpers:
.. code-block::
LLMResult, PromptValue,
CallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun,
CallbackManager, AsyncCallbackManager,
AIMessage, BaseMessage