Hi, can anyone help me with a simple code snippet to get CrewAI’s ExaSearchTool to work. I am really struggling use the BaseModel and the decorator approach and can’t figure it out.
Btw. I have tried this code snippet but the Crew agent keeps gettng the structure of the call to Exa_search wrong: from crewai_tools import tool
from exa_py import Exa
import os
from typing import Union, Dict, Any
@tool
def exa_search(query: str) → str:
“”"
Search the web using EXA API with optimized search parameters.
“”"
try:
# Execute search
api_key = os.getenv(“EXA_API_KEY”)
if not api_key:
return “Error: EXA API key not found in environment variables”
exa = Exa(api_key=api_key)
response = exa.search_and_contents(
query=query,
num_results=5,
use_autoprompt=True,
type="auto",
text=True,
highlights=True,
summary=True
)
# Format results
formatted_results = []
for result in response.results:
sections = [
f"Title: {result.title or 'No title available'}",
f"Link: {result.url}",
"Snippet:"
]
if hasattr(result, 'summary') and result.summary:
sections.append("\nSummary:")
sections.append(f" {result.summary}")
if hasattr(result, 'highlights') and result.highlights:
sections.append("\nKey Points:")
for highlight in result.highlights:
sections.append(f" • {highlight}")
formatted_results.append("\n".join(sections) + "\n\n---\n")
return "\n".join(formatted_results)
except Exception as e:
return f"Error: {str(e)}"
exa didnt work for me too it doesnt even recognize the exa_py in the import
Try closing and reopening your IDE. This often resolves errors with unrecognized installed packages.
Try this way
pip install exa_py crewai
import os
from crewai import Agent, Task, Crew
from exa_py import Exa
Initialize Exa client
exa_client = Exa(api_key=os.environ.get(“EXA_API_KEY”))
Create a researcher agent that uses Exa
researcher = Agent(
role=“Research Analyst”,
goal=“Find accurate and up-to-date information”,
backstory=“Expert at gathering information from the web”,
tools=[
{
“name”: “exa_search”,
“description”: “Search the web for information”,
“func”: lambda query: exa_search(query, exa_client)
}
]
)
Define the search function
def exa_search(query, client):
results = client.search_and_contents(
query=query,
num_results=5,
text={“max_characters”: 3000}
)
formatted_results = []
for result in results.results:
formatted_results.append({
"title": result.title,
"url": result.url,
"text": result.text[:3000] if result.text else "No text available"
})
return formatted_results
Create tasks and crew as usual in CrewAI
task = Task(
description=“Research recent developments in quantum computing”,
agent=researcher
)
crew = Crew(
agents=[researcher],
tasks=[task]
)
result = crew.kickoff()