Install the AgentOps SDK
pip install agentops
Install the Exa SDK
pip install exa_py
Set Up Environment Variables
.env
file to store your API keys:AGENTOPS_API_KEY=your_agentops_api_key_here
EXA_API_KEY=your_exa_api_key_here
Initialize the Clients
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'))
Create Your Search Tool
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
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')