Use Exa’s semantic search and contents endpoints to give your agents access to up-to-date, relevant information on the web.
1

Install the AgentOps SDK

pip install agentops
2

Install the Exa SDK

pip install exa_py
3

Set Up Environment Variables

Create a .env file to store your API keys:
AGENTOPS_API_KEY=your_agentops_api_key_here
EXA_API_KEY=your_exa_api_key_here
4

Initialize the Clients

Set up both AgentOps and Exa in your code:
import agentops
from exa_py import Exa
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Initialize AgentOps
agentops.init(os.getenv('AGENTOPS_API_KEY'))

# Initialize Exa client
exa = Exa(api_key=os.getenv('EXA_API_KEY'))
5

Create Your Search Tool

Create a tool that uses Exa’s search capabilities:
from crewai_tools import tool
from exa_py import Exa
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

@tool("Exa search and get contents")
def search_and_contents(question: str) -> str:
    """
    Args: The search query or question to find information about
    Returns: Formatted string containing titles, URLs, and highlights from the search results
    """
    exa = Exa(api_key=os.getenv('EXA_API_KEY'))

    response = exa.search_and_contents(
        query,
        type="auto",
        num_results=10,
        highlights=True
    )

    parsedResult = ''.join([
        f'<Title id={idx}>{eachResult.title}</Title>'
        f'<URL id={idx}>{eachResult.url}</URL>'
        f'<Highlight id={idx}>{"".join(eachResult.highlights)}</Highlight>' 
        for (idx, eachResult) in enumerate(response.results)
    ])

    return parsedResult

Full Example

import agentops
from crewai_tools import tool
from exa_py import Exa
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

agentops.init(os.getenv('AGENTOPS_API_KEY'))

@tool("Exa search and get contents")
def search_and_contents(question: str) -> str:
    """
    Tool using Exa's Python SDK to run semantic search and return result highlights.
    """
    exa = Exa(api_key=os.getenv('EXA_API_KEY'))

    response = exa.search_and_contents(
        query,
        type="auto",
        num_results=3,
        highlights=True
    )

    parsedResult = ''.join([
        f'<Title id={idx}>{eachResult.title}</Title>'
        f'<URL id={idx}>{eachResult.url}</URL>'
        f'<Highlight id={idx}>{"".join(eachResult.highlights)}</Highlight>' 
        for (idx, eachResult) in enumerate(response.results)
    ])

    return parsedResult

# Example usage
results = search_and_contents("Latest advancements in AI")
print(results)

agentops.end_session('Success')