Using Exa with OpenAI's Response API
Exa’s web search can now be used with OpenAI’s new Response API. This lets you add live web search to your AI app using OpenAI’s function calling feature.
Get Started
First, you’ll need API keys from both OpenAI and Exa:
- Get your Exa API key from the Exa Dashboard
- Get your OpenAI API key from the OpenAI Dashboard
Python Example
Here’s a complete example using Python:
JavaScript Example
Here’s the same example using JavaScript:
Both examples show how to:
- Set up the OpenAI Response API with Exa as a tool
- Make a request to OpenAI
- Handle the search function call
- Send the search results back to OpenAI
- Get the final response
Remember to replace the empty API key strings with your actual API keys when trying these examples.
How Tool Calling Works
Let’s break down how the Exa web search tool works with OpenAI’s Response API:
-
Tool Definition: First, we define our Exa search as a tool that OpenAI can use:
-
Initial Request: When you send a message to OpenAI, the API looks at your message and decides if it needs to search the web. If it does, instead of giving a direct answer, it will return a “function call” in its output.
-
Function Call: If OpenAI decides to search, it returns something like:
-
Search Execution: Your code then:
- Takes this search query
- Calls Exa’s API to perform the actual web search
- Gets real web results back
-
Final Response: You send these web results back to OpenAI, and it gives you a final answer using the fresh information from the web.
This back-and-forth process happens automatically in the code above, letting OpenAI use Exa’s web search when it needs to find current information.