
LANGCHAIN — What Is Retrieval?
Social media is not about the exploitation of technology but service to community. — Simon Mainwaring
Retrieval in the context of LangChain involves the process of retrieving relevant documents to be used in answering user queries. In this tutorial, we will explore the recent changes made to LangChain to facilitate the use of alternative retrieval methods, beyond the traditional VectorDB object. We will also discuss the problems faced and the solutions provided by LangChain to enable the integration of custom retrievers. Let's delve into the details.
Introduction
The retrieval process in LangChain involves retrieving documents relevant to user queries, which are then passed to the language model for further processing. The traditional method of retrieval involves semantic search, where documents are represented as numerical vectors and stored in a vector database. Incoming queries are also vectorized, and relevant documents are retrieved based on their proximity in the embedding space.
Problems
Two main issues were identified with the traditional retrieval process:
- Variations in Retrieval Methods: There are different variations in how the retrieval step is performed, and users often want to go beyond semantic search. This includes using different query methods and specifying metadata filters.
- Integration of External Retrievers: LangChain realized the need to integrate retrievers constructed outside the LangChain ecosystem, such as the OpenAI ChatGPT Retrieval Plugin.
Solution
To address these issues, LangChain introduced the concept of a Retriever and made changes to existing chains to use the Retriever interface. The new approach aims to make it easier for alternative retrievers to be used in chains and agents, fostering innovation in retrieval methods.
Code Examples
Let’s take a look at some code examples to understand how to work with the new Retriever interface in Python and TypeScript.
Python
from langchain.retrievers import Retriever
class CustomRetriever(Retriever):
def get_relevant_documents(self, query: str):
# Custom logic to retrieve relevant documents
return relevant_documentsTypeScript
import { Retriever } from 'langchain';
class CustomRetriever implements Retriever {
get_relevant_documents(query: string) {
// Custom logic to retrieve relevant documents
return relevantDocuments;
}
}In the above examples, we define a custom retriever by implementing the Retriever interface and providing the logic to retrieve relevant documents based on the user query.
Q&A
Here are some common questions and answers related to the recent changes in LangChain:
Q: What’s the difference between an index and a retriever?
A: An index is a data structure that supports efficient searching, and a retriever is the component that uses the index to find and return relevant documents in response to a user’s query.
Q: If I was using a VectorStore before in VectorDBQA chain, what do I now use in RetrievalQA chain?
A: You can use a VectorStoreRetriever, which can be created from an existing vector store by using the as_retriever() method.
Q: Can I contribute a new retrieval method to the library?
A: Yes! LangChain has started a new langchain/retrievers module for this purpose.
Conclusion
LangChain’s recent changes to accommodate alternative retrieval methods through the Retriever interface provide users with the flexibility to experiment with different approaches to retrieval. By integrating external retrievers and encouraging innovation, LangChain aims to enhance the retrieval process and provide better support for custom retrieval methods.
In this tutorial, we explored the recent changes in LangChain related to retrieval and the introduction of the Retriever interface. We also provided code examples to demonstrate the implementation of custom retrievers in Python and TypeScript. These changes mark a significant step towards enabling innovative retrieval methods within LangChain.
