Exa Researcher - JavaScript
Example project using the Exa JS SDK.
What this doc covers
- Using Exa’s Auto Search to pick the best search setting for each query (keyword or neural)
- Using searchAndContents() through Exa’s JavaScript SDK
In this example, we will build Exa Researcher, a JavaScript app that, given a research topic, automatically searches for relevant sources with Exa’s Auto Search and synthesizes the information into a reliable research report.
Fastest setup: Interact with the code in your browser with this Replit template.
Alternatively, this interactive notebook was made with the Deno Javascript kernel for Jupyter so you can easily run it locally. Check out the plain JS version if you prefer a regular Javascript file you can run with NodeJS, or want to skip to the final result. If you’d like to run this notebook locally, Installing Deno and connecting Deno to Jupyter is fast and easy.
To play with this code, first we need a Exa API key and an OpenAI API key.
Setup
Let’s import the Exa and OpenAI SDKs and put in our API keys to create a client object for each. Make sure to pick the right imports for your runtime and paste or load your API keys.
Since we’ll be making several calls to the OpenAI API to get a completion from GPT-3.5 Turbo, let’s make a simple utility function so we can pass in the system and user messages directly, and get the LLM’s response back as a string.
Okay, great! Now let’s starting building Exa Researcher.
Exa Auto Search
The researcher should be able to automatically generate research reports for all kinds of different topics. Here’s two to start:
The first thing our researcher has to do is decide what kind of search to do for the given topic.
Exa offers two kinds of search: neural and keyword search. Here’s how we decide:
- Neural search is preferred when the query is broad and complex because it lets us retrieve high quality, semantically relevant data. Neural search is especially suitable when a topic is well-known and popularly discussed on the Internet, allowing the machine learning model to retrieve contents which are more likely recommended by real humans.
- Keyword search is useful when the topic is specific, local or obscure. If the query is a specific person’s name, and identifier, or acronym, such that relevant results will contain the query itself, keyword search may do well. And if the machine learning model doesn’t know about the topic, but relevant documents can be found by directly matching the search query, keyword search may be necessary.
Conveniently, Exa’s autosearch feature (on by default) will automatically decide whether to use keyword
or neural
search for each query. For example, if a query is a specific person’s name, Exa would decide to use keyword search.
Now, we’ll create a helper function to generate search queries for our topic.
Next, let’s write another function that actually calls the Exa API to perform searches using Auto Search.
Writing a report with GPT-4
The final step is to instruct the LLM to synthesize the content into a research report, including citations of the original links. We can do that by pairing the content and the URLs and writing them into the prompt.
All Together Now
Now, let’s just wrap everything into one Researcher function that strings together all the functions we’ve written. Given a user’s research topic, the Researcher will generate search queries, feed those queries to Exa Auto Search, and finally use an LLM to synthesize the retrieved information. Three simple steps!
In just a couple lines of code, we’ve used Exa to go from a research topic to a valuable essay with up-to-date sources.
For a link to a complete, cleaned up version of this project that you can execute in your NodeJS environment, check out the alternative JS-only version.