Spaces:
Sleeping
Sleeping
Upload pages/api/search.js with huggingface_hub
Browse files- pages/api/search.js +68 -0
pages/api/search.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
const TAVILY_API_KEY = process.env.TAVILY_API_KEY;
|
| 14 |
+
|
| 15 |
+
if (TAVILY_API_KEY) {
|
| 16 |
+
const response = await fetch('https://api.tavily.com/search', {
|
| 17 |
+
method: 'POST',
|
| 18 |
+
headers: { 'Content-Type': 'application/json' },
|
| 19 |
+
body: JSON.stringify({
|
| 20 |
+
api_key: TAVILY_API_KEY,
|
| 21 |
+
query: query,
|
| 22 |
+
search_depth: "basic",
|
| 23 |
+
max_results: 5
|
| 24 |
+
}),
|
| 25 |
+
});
|
| 26 |
+
const data = await response.json();
|
| 27 |
+
results = data.results || [];
|
| 28 |
+
} else {
|
| 29 |
+
// Mock Tavily Results
|
| 30 |
+
results = [
|
| 31 |
+
{
|
| 32 |
+
title: `Tavily Search Result: ${query}`,
|
| 33 |
+
url: 'https://tavily.com',
|
| 34 |
+
content: 'This is a simulated result from Tavily. To see real results, please add the TAVILY_API_KEY to your environment variables.'
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
title: 'Understanding AI Search',
|
| 38 |
+
url: '#',
|
| 39 |
+
content: 'Tavily is a search engine built specifically for AI agents (LLMs), providing real-time, accurate, and factual results at speed.'
|
| 40 |
+
}
|
| 41 |
+
];
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
} else {
|
| 45 |
+
// SearXNG Integration
|
| 46 |
+
const SEARXNG_URL = process.env.SEARXNG_URL || 'http://localhost:8080';
|
| 47 |
+
|
| 48 |
+
// Mock SearXNG Results
|
| 49 |
+
results = [
|
| 50 |
+
{
|
| 51 |
+
title: `SearXNG Result: ${query}`,
|
| 52 |
+
url: 'https://searxng.org',
|
| 53 |
+
content: 'SearXNG is a free metasearch engine which aggregates results from more than 70 search services.'
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
title: 'Privacy Respecting Search',
|
| 57 |
+
url: '#',
|
| 58 |
+
content: 'SearXNG does not track or profile its users. Configure your own instance to ensure maximum privacy.'
|
| 59 |
+
}
|
| 60 |
+
];
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
return res.status(200).json({ results });
|
| 64 |
+
} catch (error) {
|
| 65 |
+
console.error('Search Error:', error);
|
| 66 |
+
return res.status(500).json({ message: 'Internal Server Error' });
|
| 67 |
+
}
|
| 68 |
+
}
|