Using Online Search
A guide on how to integrate and use tools in your realtime Voice Bot using OpenAI Realtime API.
Ensure you have completed the Overview before proceeding with this guide.
In this guide, we will extend the basic Voice Bot by integrating a Search Tool that allows the bot to perform online searches based on user queries. This enhancement enables your bot to provide more accurate and up-to-date information by fetching data from the web in real-time.
Integrating Tools into Your Realtime API Voice Bot
We will modify the existing Voice Bot to include a search functionality that interacts with the Exa API. This involves creating a new tool, updating the bot’s setup, and handling the search results.
- The full code is available here.
Creating Pydantic Models
To handle structured data within your tool, we utilize Pydantic models. These models provide type validation and ensure consistency across your application.
Define the Models
First, define the models for queries and search results.
Integrate the Search Tool
Ensure you’re adding this to voice_bot.py
created in Get Started guide.
Create the Search Tool
Create a new tool that interacts with the Exa API to perform searches.
Update the VoiceBot Setup
Modify the setup
method to include the newly created SearchTool
.
Setup API Keys (if running locally)
You will need the following environment variables:
EXA_API_KEY
- Sign up and navigate to Exa API to get the API key.DEEPGRAM_API_KEY
- Sign up and navigate to Deepgram to get the API key.CARTESIA_API_KEY
- Sign up and navigate to Cartesia to get the API key.OPENAI_API_KEY
- Sign up and navigate to OpenAI to get the API key.
Once you have your keys, create a .env
file in the same directory as voice_bot_with_online_search.py
and add the following:
Now you can run the following command to start the server:
Understanding the Integration
SearchTool Class
The SearchTool
class extends sp.Tool
and is responsible for interacting with the Exa API to perform web searches based on user queries.
- run Method: This asynchronous method takes a
Query
object, sends a POST request to the Exa API with the query parameters, and returns aSearchResult
object containing the search outcome.
VoiceBot Setup
In the setup
method of the VoiceBot
class, we initialize the llm_node
with the SearchTool
. This setup allows the bot to utilize the search functionality alongside other AI services.
Support
For any assistance or questions, feel free to join our Discord community. We’re excited to see what you build!
Was this page helpful?