What this doc covers
- Setting up LangChain with Exa using Exa’s search integration
- Connecting this to a toolchain that uses OpenAI for generation
Guide
1. Pre-requisites and installation
Install the core, OpenAI and Exa LangChain librariesPython
OPENAI_API_KEY and EXA_API_KEY for OpenAI and Exa keys respectively.
Get Exa API Key
Follow this link to get your API key
2. Use Exa Search to power a LangChain Tool
Set up a Retriever tool usingExaSearchRetriever
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 above5. 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 librariesPython
Python
7. Running the full RAG toolchain
Let’s invoke the chain:Python
Stdout
8. Optionally, stream the output of the chain
Optionally, you mayPython
Streamed text output

