Spaces:
Sleeping
Sleeping
| // API Proxy for SearXNG and Tavily | |
| export default async function handler(req, res) { | |
| if (req.method !== 'POST') { | |
| return res.status(405).json({ message: 'Method not allowed' }); | |
| } | |
| const { query, engine } = req.body; | |
| try { | |
| let results = []; | |
| if (engine === 'tavily') { | |
| const TAVILY_API_KEY = process.env.TAVILY_API_KEY; | |
| if (TAVILY_API_KEY) { | |
| const response = await fetch('https://api.tavily.com/search', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| api_key: TAVILY_API_KEY, | |
| query: query, | |
| search_depth: "basic", | |
| max_results: 5 | |
| }), | |
| }); | |
| const data = await response.json(); | |
| results = data.results || []; | |
| } else { | |
| // Mock Tavily Results | |
| results = [ | |
| { | |
| title: `Tavily Search Result: ${query}`, | |
| url: 'https://tavily.com', | |
| content: 'This is a simulated result from Tavily. To see real results, please add the TAVILY_API_KEY to your environment variables.' | |
| }, | |
| { | |
| title: 'Understanding AI Search', | |
| url: '#', | |
| content: 'Tavily is a search engine built specifically for AI agents (LLMs), providing real-time, accurate, and factual results at speed.' | |
| } | |
| ]; | |
| } | |
| } else { | |
| // SearXNG Integration | |
| const SEARXNG_URL = process.env.SEARXNG_URL || 'http://localhost:8080'; | |
| // Mock SearXNG Results | |
| results = [ | |
| { | |
| title: `SearXNG Result: ${query}`, | |
| url: 'https://searxng.org', | |
| content: 'SearXNG is a free metasearch engine which aggregates results from more than 70 search services.' | |
| }, | |
| { | |
| title: 'Privacy Respecting Search', | |
| url: '#', | |
| content: 'SearXNG does not track or profile its users. Configure your own instance to ensure maximum privacy.' | |
| } | |
| ]; | |
| } | |
| return res.status(200).json({ results }); | |
| } catch (error) { | |
| console.error('Search Error:', error); | |
| return res.status(500).json({ message: 'Internal Server Error' }); | |
| } | |
| } |