LangChain Elasticsearch Integration for RAG and Vector Stores

Author

Reads 1.1K

Clipboard with Statistical Data and Digital Tablet with Stock Market Display on Screen
Credit: pexels.com, Clipboard with Statistical Data and Digital Tablet with Stock Market Display on Screen

LangChain's integration with Elasticsearch allows for seamless interaction between the two technologies, enabling users to leverage the strengths of both. This integration is particularly useful for building Retrieval Augmented Generation (RAG) models.

With LangChain's Elasticsearch integration, users can easily query vector stores and retrieve relevant documents. This is made possible through the use of Elasticsearch's vector search capabilities.

By combining LangChain's capabilities with Elasticsearch's, users can build more effective RAG models that can retrieve and generate high-quality text. This is achieved by leveraging Elasticsearch's ability to index and search vector embeddings.

For another approach, see: Elasticsearch Vector

Setup and Configuration

To set up langchain elasticsearch, you'll need to install the langchain-elasticsearch package. This is the first step in using the Elasticsearch vector search.

There are two main ways to set up an Elasticsearch instance: using Elastic Cloud or running it locally. If you're using Elastic Cloud, you can sign up for a free trial. To connect to an Elasticsearch instance running on your local machine, simply pass the Elasticsearch URL and index name along with the embedding object to the constructor.

To run Elasticsearch locally, you can use the official Elasticsearch Docker image. This is the easiest way to get started with Elasticsearch.

Credentials

A laptop showing an analytics dashboard with charts and graphs, symbolizing modern data analysis tools.
Credit: pexels.com, A laptop showing an analytics dashboard with charts and graphs, symbolizing modern data analysis tools.

To set up an Elasticsearch instance, you have two main options: using Elastic Cloud or running it locally.

Elastic Cloud is a managed Elasticsearch service that offers a free trial.

To connect to an Elasticsearch instance without login credentials, you need to start the docker instance with security enabled and pass the Elasticsearch URL and index name along with the embedding object to the constructor.

For local installation, you can use the official Elasticsearch Docker image.

To obtain an API key, you'll need to follow the instructions provided by the Elasticsearch Docker documentation.

To connect with login credentials, use the parameters es_api_key, es_user, and es_password.

Here are the key parameters to remember:

Elastic Cloud

Connecting to an Elasticsearch instance on Elastic Cloud is straightforward. You can use either the es_cloud_id parameter or es_url to do so.

The es_cloud_id parameter is a convenient option, allowing you to connect to your instance with ease. To use it, simply specify the ID of your Elastic Cloud instance.

Alternatively, you can use the es_url parameter to connect to your instance. This option requires you to provide the URL of your Elastic Cloud instance.

Broaden your view: Elastic Search Cluster

Building a Simple RAG Application

Credit: youtube.com, Setting up Retrieval Augmented Generation (RAG) in 3 Steps

Building a simple RAG application is a great way to get started with integrating Elasticsearch and LangChain. To do this, you'll need to add some example documents to your Elasticsearch store.

You can use the default gpt-3.5-turbo model offered by OpenAI, which powers ChatGPT, as your LLM. This model is a good choice for a simple RAG application.

To plug together your RAG system, you'll need to chain together the retrieval step with adding the documents to the prompt and sending it to the LLM. This can be done with just a few lines of code.

Here are the basic steps to build a simple RAG application:

  • Add example documents to your Elasticsearch store.
  • Define your LLM, in this case, the gpt-3.5-turbo model.
  • Chain together the retrieval step with adding the documents to the prompt and sending it to the LLM.

With these steps, you'll have a basic RAG system up and running. You can then customize the document builder to suit your needs, especially if you have indices that weren't created using LangChain.

Vectorstores Store

You'll likely want to query your vector store during the running of your chain or agent, and you can use filtering when searching.

Credit: youtube.com, Ultimate Guide to Vector Stores in LangChain - Part 9

Once you've created your vector store and added relevant documents, you can start querying it. This is where the magic happens, and you can start getting meaningful results.

The query vector store is a crucial part of the process, and it's what allows you to search and retrieve specific information from your vector store.

You can use filtering when searching to narrow down your results and get more precise answers. This is especially useful when you have a large dataset and need to quickly find specific information.

If you're already using Elasticsearch in your project, you'll want to know about the new ElasticsearchStore implementation. It's more flexible and easier to use than the old implementations.

Upgrading to ElasticsearchStore is a relatively straightforward process, and it's definitely worth it if you're already using Elasticsearch. The new implementation is designed to be more efficient and easier to integrate with your existing code.

Additional reading: Elasticsearch Dense Vector

Query and Retrieval

You can query your vector store using filtering when searching. The Elasticsearch store offers common retrieval strategies out-of-the-box, including DenseVectorStrategy, which returns the top k most similar vectors to the query vector.

For more insights, see: Elasticsearch Vector Database

Credit: youtube.com, Implementing Agentic RAG using Elasticsearch & Langchain

To use BM25RetrievalStrategy, you need to specify it in the ElasticsearchStore constructor, and you can perform searches without using embeddings. Customizing the query is also possible using the custom_query parameter at search, which allows you to adjust the query that is used to retrieve documents from Elasticsearch.

Elasticsearch's Query DSL gives users full control over how to search their data, and in LangChain, the ElasticsearchRetriever enables this full flexibility directly.

For another approach, see: Elasticsearch Full Text Search

Query Vector Store

Once your vector store has been created and the relevant documents have been added, you can query it during the running of your chain or agent. This is where the magic happens, and you can start retrieving relevant information.

To query your vector store, you can use filtering when searching, as shown in the examples. This allows you to narrow down your search results and get more precise answers.

The ElasticsearchStore uses the DenseVectorStrategy by default, which returns the top k most similar vectors to the query vector. The k parameter is set when the ElasticsearchStore is initialized, and the default value is 10.

Credit: youtube.com, OpenAI Embeddings and Vector Databases Crash Course

You can also use other retrieval strategies, such as DenseVectorScriptScoreStrategy, which performs exact vector retrieval using Elasticsearch's script score query. This strategy was called ExactRetrievalStrategy prior to version 0.2.0.

Here are some common retrieval strategies supported by ElasticsearchStore:

  • DenseVectorStrategy (default)
  • DenseVectorScriptScoreStrategy
  • BM25RetrievalStrategy

Each strategy has its own strengths and weaknesses, and you can choose the one that best fits your use case. By default, the DenseVectorStrategy is used, but you can specify a different strategy when initializing the ElasticsearchStore.

Note that the similarity algorithm used by ElasticsearchStore cannot be changed at query time. It must be set when creating the index mapping for the field. If you need to change the similarity algorithm, you need to delete the index and recreate it with the correct distance strategy.

Consider reading: Elasticsearch _template

Datasets for Main Economic Indicators

Datasets for Main Economic Indicators are essential for querying and retrieval.

The World Bank's World Development Indicators dataset is one of the most comprehensive, covering over 1,000 indicators from 227 countries and territories.

Credit: youtube.com, Doing Economics Research? Try These Data Sets!

It includes data on GDP, inflation, unemployment, and more.

The International Monetary Fund's (IMF) International Financial Statistics dataset provides data on exchange rates, interest rates, and international trade.

IMF's dataset is available in multiple formats, including CSV and Excel.

The Organisation for Economic Co-operation and Development (OECD) provides datasets on GDP, inflation, and employment rates.

OECD's datasets are available in multiple languages, including English, French, and Spanish.

A unique perspective: Elasticsearch Spring Data

Retrieval Strategies

Elasticsearch has big advantages over other vector only databases, thanks to its ability to support a wide range of retrieval strategies.

By default, ElasticsearchStore uses the DenseVectorStrategy, which returns the top k most similar vectors to the query vector, with the k parameter set to 10.

You can also use the SparseVectorStrategy, which requires the ELSER model to be deployed and running in Elasticsearch ml node, and needs a model ID to be provided.

The DenseVectorScriptScoreStrategy uses Elasticsearch's script score query to perform exact vector retrieval, while the BM25Strategy allows for full-text keyword search.

Credit: youtube.com, ElasticON AI: Building LLM applications with LangChain

If you want to use pure BM25 without vector search, you can use the BM25RetrievalStrategy, which doesn't require embeddings.

With LangChain and the Elasticsearch retriever, you can use Elasticsearch's Query DSL to search your data in a more complex way, and even add semantic reranking capabilities to your retrieval step.

Retrieval Strategies

Retrieval strategies are a crucial part of Elasticsearch, allowing you to configure how your search results are retrieved. ElasticsearchStore uses the DenseVectorStrategy by default, which returns the top k most similar vectors to the query vector.

The k parameter is set when the ElasticsearchStore is initialized, with a default value of 10. You can also specify other retrieval strategies, such as DenseVectorScriptScoreStrategy, which uses Elasticsearch's script score query to perform exact vector retrieval.

If you want to perform a simple similarity search with filtering on metadata, you can use the following approach. This can be done by configuring ElasticsearchStore to support similarity search.

Credit: youtube.com, Advanced Retrieval Strategies: Deep Memory, Small to Big Retrieval, Structured and Unstructured Data

Elasticsearch supports the following vector distance similarity algorithms: cosine, euclidean, and dot_product. The cosine similarity algorithm is the default, but you can specify the similarity algorithm needed via the similarity parameter.

However, note that the similarity algorithm cannot be changed at query time. It's needed to be set when creating the index mapping for the field. If you need to change the similarity algorithm, you need to delete the index and recreate it with the correct distance_strategy.

Here are the supported retrieval strategies:

  • DenseVectorStrategy (default)
  • DenseVectorScriptScoreStrategy
  • BM25Strategy
  • BM25RetrievalStrategy
  • SparseVectorStrategy (ELSER)
  • Similarity search with filtering on metadata
  • Similarity search with score

Note that some retrieval strategies, such as SparseVectorStrategy, require specific conditions to be met, such as having the ELSER model deployed and running in Elasticsearch ml node.

Based on GPT-4

Based on GPT-4, we can see that it's being used in conjunction with other technologies like ElasticSearch and LangChain. We've also seen how these tools can be used to process specific data, like the CSV's related to CO2 production and gross national income.

The ElasticSearch agent is built on top of ElasticSearch, LangChain, and GPT-4, indicating a robust and integrated approach to information retrieval.

Customization and Upgrades

Credit: youtube.com, build elasticsearch vector index using langchain & gpt-4o|Tutorial:75

If you're already using Elasticsearch in your langchain project, you can upgrade to the new ElasticsearchStore implementation. This new implementation is more flexible and easier to use.

The old implementations, ElasticVectorSearch and ElasticKNNSearch, are now deprecated, which means they won't be supported in the future. You should consider upgrading to the new ElasticsearchStore to avoid any potential issues.

The new ElasticsearchStore can handle various types of vector retrieval, including approximate dense vector, exact dense vector, and sparse vector (ELSER), as well as BM25 retrieval and hybrid retrieval, via strategies.

Customize the Query

You can adjust the query used to retrieve documents from Elasticsearch with the custom_query parameter at search. This allows you to use a more complex query.

This is useful for supporting linear boosting of fields.

With this feature, you can tailor the search results to your specific needs.

Customize Builder

You can adjust how a Document is being built using data retrieved from Elasticsearch with the doc_builder parameter at search. This is especially useful if you have indices which were not created using Langchain.

Abstract visualization of data analytics with graphs and charts showing dynamic growth.
Credit: pexels.com, Abstract visualization of data analytics with graphs and charts showing dynamic growth.

The doc_builder parameter allows for customization of the Document Builder, making it a powerful tool for tailoring your search results to specific needs.

To use the doc_builder parameter, you'll need to have Elasticsearch set up and running. If you're already using Elasticsearch in your project, you may be using the old implementations: ElasticVectorSearch and ElasticKNNSearch, which are now deprecated.

The new implementation, ElasticsearchStore, is more flexible and easier to use, making it a great choice for customizing your Document Builder.

A fresh viewpoint: Elasticsearch Document

ChatGPT 4 - Main

ChatGPT 4 is a powerful tool that can be used to build custom applications, including RAG (Retrieve, Augment, Generate) systems.

You can start building a simple RAG application by adding example documents to your Elasticsearch store.

The default gpt-3.5-turbo model offered by OpenAI is a great choice for the LLM (Large Language Model) in your RAG system.

To plug together your RAG system, you'll need to transform your Elasticsearch store into a LangChain retriever and chain it together with the retrieval step and adding the documents to the prompt.

A business professional holds a printed chart in an outdoor setting, illustrating data insights and analysis.
Credit: pexels.com, A business professional holds a printed chart in an outdoor setting, illustrating data insights and analysis.

There are several tools available for building an Elasticsearch agent, including one based on Elasticsearch, LangChain, and ChatGPT 4.

The agent's UI is available on GitHub, and the code contains three logical units: configuration parameters, agent tools, and the agent factory.

The agent tools are all LangChain based and have input schemas specified using Pydantic's BaseModel class.

Here are the main tools included in the agent:

  • list indices tool: lists ElasticSearch indices
  • index details tool: lists aliases, mappings, and settings of a specific index
  • index data tool: gets a list of entries from an ElasticSearch index
  • index search tool: searches an ElasticSearch index and returns hits or aggregations

The agent factory is responsible for initializing the agent executor, and all tools are meant to use OpenAI functions.

Integration and Packages

To use the langchain-elasticsearch partner package, you first need to install it.

The langchain-elasticsearch integration package can be imported from the langchain_elasticsearch module, allowing you to use simple methods to index and search your data.

You can use Elastic's sparse vector model ELSER as your retrieval strategy, which has to be deployed first.

Integration Package

To install the langchain-elasticsearch integration package, you first need to install it. This is a crucial step to get started with using the package.

Hands exchanging a package with an electronic signature for contactless delivery.
Credit: pexels.com, Hands exchanging a package with an electronic signature for contactless delivery.

The package allows you to import classes from the langchain_elasticsearch module, such as the ElasticsearchStore, which provides simple methods to index and search your data.

You can use Elastic's sparse vector model ELSER as your retrieval strategy, but it needs to be deployed first. This model is used in the example to improve the RAG system's response.

Optimizing the retrieval strategy is one way to improve the system, but it's not the only way.

Simple UI on GitHub

The Simple UI on GitHub is a great example of how to integrate a tool with ElasticSearch. The UI is provided by the onepointconsulting/elasticsearch-agent-chainlit GitHub repository.

One interesting aspect of this code is the message handler for incoming messages. It's a simple yet effective approach to storing user questions in memory.

The prompt used in this code contains up to 5 previous questions from the user. This is a basic attempt to implement memory in the system, focusing solely on the user's questions.

The prompt also includes instructions to retrieve indices and their details from ElasticSearch for each question. This demonstrates how the code is designed to interact with the ElasticSearch database.

Check this out: Elasticsearch Ui

Tanya Hodkiewicz

Junior Assigning Editor

Tanya Hodkiewicz is a seasoned Assigning Editor with a keen eye for compelling content. With a proven track record of commissioning articles that captivate and inform, Tanya has established herself as a trusted voice in the industry. Her expertise spans a range of categories, including "Important" pieces that tackle complex, timely topics and "Decade in Review" features that offer insightful retrospectives on significant events.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.