LangChain is a framework for building applications that combine LLMs with data, APIs and other tools. In this guide, we’ll go over how to use Exa’s LangChain integration to perform RAG with the following steps:
- Set up Exa’s LangChain integration and use Exa to retrieve relevant content
- Connect this content to a toolchain that uses OpenAI’s LLM for generation
See a YouTube tutorial of a very similar setup by the LangChain team here.
See the full reference from LangChain here.
Get Started
1
Pre-requisites and installation
Install the core OpenAI and Exa LangChain libraries
Bash
Ensure API keys are initialized properly. For LangChain libraries, the environment variable names are
OPENAI_API_KEY
and EXA_API_KEY
for OpenAI and Exa keys respectively. Get your Exa API key
2
Use Exa Search to power a LangChain Tool
Set up a Retriever tool using
ExaSearchRetriever
. This is a retriever that connects to Exa Search to find relevant documents via semantic search. First import the relevant libraries and instantiate the ExaSearchRetriever.Python
3
Create a prompt template (optional)
We use a LangChain PromptTemplate to define a template of placeholder to parse out URLs and Highlights from the Exa retriever.
Python
4
Parse the URL and content from Exa results
We use a Runnable Lambda to parse out the URL and Highlights attributes from the Exa Search results then pass this to the prompt template above
Python
5
Join Exa results and content for retrieval
Complete the retrieval chain by stitching together the Exa retriever, the parser and a short lambda function - this is crucial for passing the result as a single string as context for the LLM in the next step.
Python
6
Set up the rest of the toolchain including OpenAI for generation
In this step, we define the system prompt with Query and Context template inputs to be grabbed from the user and Exa Search respectively. First, once again import the relevant libraries and components from LangChains librariesThen we define a generation prompt - the prompt template that is used with context from Exa to perform RAG.We set the generation LLM to OpenAI, then connect everything with a RunnableParallel parallel connection. The generation prompt, containing the query and context, is then passed to the LLM and parsed for better output representation.
Python
Python
Python
7
Running the full RAG toolchain
8
Optionally, stream the output of the chain
Optionally, you mayOutputs, in a stream - click here to learn more about the .stream method and other options, including handling of chunks and how to think about further parsing outputs:As you can see, the output generation is enriched with the context of our Exa Search query result!
Python
Streamed text output