Skip to main content

Exa Web Search Tool

Exa is a powerful semantic web search API that provides intelligent search capabilities for AI agents. It supports neural search, content filtering, and real-time web crawling.

Installation

npm install @exalabs/ai-sdk

Environment Variables

EXA_API_KEY=your-exa-api-key
Get your API key from the Exa Dashboard.

Quick Start

import { Agent } from 'praisonai';
import { exaSearch } from 'praisonai/tools';

const agent = new Agent({
  name: 'WebResearcher',
  instructions: 'You are a research assistant that searches the web for information.',
  tools: [exaSearch()],
});

const result = await agent.run('Find the latest AI developments in Europe');
console.log(result.text);

Configuration Options

import { exaSearch } from 'praisonai/tools';

const searchTool = exaSearch({
  // Search type: auto, neural, fast, deep
  type: 'auto',
  
  // Number of results (default: 10)
  numResults: 10,
  
  // Category filter
  category: 'news', // company, research paper, news, pdf, github, etc.
  
  // Domain filters
  includeDomains: ['github.com', 'arxiv.org'],
  excludeDomains: ['wikipedia.org'],
  
  // Date filters (ISO 8601)
  startPublishedDate: '2024-01-01T00:00:00.000Z',
  endPublishedDate: '2024-12-31T23:59:59.999Z',
  
  // Text filters
  includeText: ['AI', 'machine learning'],
  excludeText: ['spam'],
  
  // Content options
  contents: {
    text: { maxCharacters: 1000 },
    summary: true,
    livecrawl: 'fallback', // never, fallback, always, preferred
  },
});

Search Types

TypeDescription
autoIntelligent hybrid search (recommended)
neuralSemantic/meaning-based search
fastQuick keyword-based search
deepComprehensive deep search

Category Filters

  • company - Company websites
  • research paper - Academic papers
  • news - News articles
  • pdf - PDF documents
  • github - GitHub repositories
  • personal site - Personal websites
  • linkedin profile - LinkedIn profiles
  • financial report - Financial documents

Advanced Example

import { Agent } from 'praisonai';
import { exaSearch } from 'praisonai/tools';

const agent = new Agent({
  name: 'CompanyResearcher',
  instructions: 'Research companies and provide detailed analysis.',
  tools: [
    exaSearch({
      type: 'auto',
      numResults: 6,
      category: 'company',
      contents: {
        text: { maxCharacters: 2000 },
        livecrawl: 'preferred',
        summary: true,
      },
    }),
  ],
});

const result = await agent.run(
  'Find AI startups in Europe founded after 2020 with recent funding'
);
console.log(result.text);

Using with AI SDK Directly

import { generateText, stepCountIs } from 'ai';
import { webSearch } from '@exalabs/ai-sdk';
import { openai } from '@ai-sdk/openai';

const { text } = await generateText({
  model: openai('gpt-4o'),
  prompt: 'What are the latest developments in quantum computing?',
  tools: {
    webSearch: webSearch({
      numResults: 5,
      type: 'auto',
    }),
  },
  stopWhen: stepCountIs(3),
});

console.log(text);

Response Format

interface ExaSearchResult {
  results: Array<{
    title: string;
    url: string;
    content: string;
    score?: number;
    publishedDate?: string;
    author?: string;
  }>;
  autopromptString?: string;
}

Best Practices

  1. Use appropriate search type - auto works well for most cases
  2. Filter by category - Narrow results for specific content types
  3. Set date ranges - Get recent or historical content as needed
  4. Use livecrawl - Get fresh content when needed
  5. Limit results - Start with fewer results and increase if needed

Error Handling

import { exaSearch } from 'praisonai/tools';

const tool = exaSearch();

try {
  const result = await tool.execute({ query: 'AI news' });
  console.log(result);
} catch (error) {
  if (error.message.includes('EXA_API_KEY')) {
    console.error('Missing API key. Set EXA_API_KEY environment variable.');
  } else {
    console.error('Search failed:', error.message);
  }
}