Exa Researcher - Python
What this doc covers
- Using Exa’s Auto Search to pick the best search setting for each query (keyword or neural)
- Using search_and_contents() through Exa’s Python SDK
In this example, we will build Exa Researcher, a Python 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.
To run this code, first we need a Exa API key and an OpenAI API key.
If you would like to se the full code for this tutorial as a Colab notebook, click here
Setup
Let’s import the Exa and OpenAI SDKs and set up our API keys to create client objects for each. We’ll use environment variables to securely store our 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 start 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 auto search 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-3.5 Turbo
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.
This Python implementation of Exa Researcher demonstrates how to leverage Exa’s Auto Search feature and the OpenAI API to create an automated research tool. By combining Exa’s powerful search capabilities with GPT-3.5 Turbo’s language understanding and generation, we’ve created a system that can quickly gather and synthesize information on any given topic.