OpenAI Exa Wrapper
Enhance your OpenAI chat completetions with a simple Exa wrapper that handles search, chunking and prompting.
Exa is designed from the ground up to enable seamless, accurate, and performant RAG (Retrieval-Augmented Generation). Exa provides factual, up to date information needed to ground LLM generations.
But good RAG requires more than just great search. The client needs to decide when to use RAG, with what queries. They need to handle chunking, prompting, and chaining LLM calls. We provide the Exa OpenAI wrapper that, with one line of code, does all that and turns any OpenAI chat completion into an Exa-powered RAG system.
Get Started
First, create an account and grab a free API key.
Get your Exa API key
Install the Exa and OpenAI python libraries
Instantiate Clients
Import and instantiate the Exa and OpenAI clients.
OPENAI_API_KEY
and EXA_API_KEY
variables respectively.Wrap the OpenAI client
The Exa.wrap
method takes your existing OpenAI client and wraps it with Exa-powered RAG capabilities.
Call the wrapped client
The wrapped client works exactly like the native OpenAI client, except that it automatically improves your completions with relevant search results.
Example output
End-to-end code example
Below is a code block that puts together all of the above. You can copy it into any Python script or Jupyter notebook to test out a complete RAG example.
Exmple with multiple questions
Here is a slightly more advanced example that shows how to use the wrapper to answer multiple questions.
Further configuration options and advanced usage
While the default settings work well for most use cases, the Exa OpenAI wrapper’s chat.completions.create()
method allows you to fine-tune the following parameters.
Option to include Exa results
use_exa
specifies whether to include Exa results for a given request:
auto
Exa will intelligently determine whether to include resultsrequired
Exa results will always be includednone
Exa results will never be included
Number of results
num_results
specifies how many search results Exa should retrieve (defaults to 3 results).
Maximum result length
result_max_len
specifies the maximum length of each Exa result (defaults to 2048 characters).
Search parameters
The Exa OpenAI wrapper supports any parameters that the exa.search()
function accepts. You can find a list of all the parameters here.