Spaces:
Sleeping
Sleeping
Upload pages/api/search.js with huggingface_hub
Browse files- pages/api/search.js +57 -0
pages/api/search.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// API Proxy for SearXNG and Tavily
|
| 2 |
+
export default async function handler(req, res) {
|
| 3 |
+
if (req.method !== 'POST') {
|
| 4 |
+
return res.status(405).json({ message: 'Method not allowed' });
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
const { query, engine } = req.body;
|
| 8 |
+
|
| 9 |
+
try {
|
| 10 |
+
let results = [];
|
| 11 |
+
|
| 12 |
+
if (engine === 'tavily') {
|
| 13 |
+
// Tavily API Integration
|
| 14 |
+
const TAVILY_API_KEY = process.env.TAVILY_API_KEY;
|
| 15 |
+
|
| 16 |
+
if (TAVILY_API_KEY) {
|
| 17 |
+
const response = await fetch('https://api.tavily.com/search', {
|
| 18 |
+
method: 'POST',
|
| 19 |
+
headers: { 'Content-Type': 'application/json' },
|
| 20 |
+
body: JSON.stringify({
|
| 21 |
+
api_key: TAVILY_API_KEY,
|
| 22 |
+
query: query,
|
| 23 |
+
search_depth: "basic",
|
| 24 |
+
max_results: 5
|
| 25 |
+
}),
|
| 26 |
+
});
|
| 27 |
+
const data = await response.json();
|
| 28 |
+
results = data.results || [];
|
| 29 |
+
} else {
|
| 30 |
+
// Mock Tavily Results
|
| 31 |
+
results = [
|
| 32 |
+
{ title: `Tavily Result for: ${query}`, url: '#', content: 'This is a mock result because TAVILY_API_KEY is not set.' },
|
| 33 |
+
{ title: 'AI Search Overview', url: '#', content: 'Tavily provides advanced search capabilities for AI agents.' }
|
| 34 |
+
];
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
} else {
|
| 38 |
+
// SearXNG Integration
|
| 39 |
+
const SEARXNG_URL = process.env.SEARXNG_URL || 'http://localhost:8080';
|
| 40 |
+
|
| 41 |
+
// const response = await fetch(`${SEARXNG_URL}/search?q=${encodeURIComponent(query)}&format=json`);
|
| 42 |
+
// const data = await response.json();
|
| 43 |
+
// results = data.results;
|
| 44 |
+
|
| 45 |
+
// Mock SearXNG Results
|
| 46 |
+
results = [
|
| 47 |
+
{ title: `SearXNG Result: ${query}`, url: '#', content: 'Open source metasearch engine result.' },
|
| 48 |
+
{ title: 'Privacy Respecting Search', url: '#', content: 'SearXNG aggregates results from various engines.' }
|
| 49 |
+
];
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
return res.status(200).json({ results });
|
| 53 |
+
} catch (error) {
|
| 54 |
+
console.error('Search Error:', error);
|
| 55 |
+
return res.status(500).json({ message: 'Internal Server Error' });
|
| 56 |
+
}
|
| 57 |
+
}
|