UNPKG

gcloud-sonar-ai

Version:

A production-ready NPM package that provides a Perplexity Sonar alternative using Google Cloud Vertex AI and Gemini models.

54 lines 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.POST = POST; const gcloud_sonar_ai_1 = require("gcloud-sonar-ai"); const sonar = new gcloud_sonar_ai_1.SonarAI({ projectId: 'thematic-lore-456816-s2', location: 'us-central1', model: 'gemini-1.5-flash-001', }); async function POST(req) { try { const { query } = (await req.json()); const stream = new ReadableStream({ async start(controller) { try { for await (const chunk of sonar.searchStream(query)) { const chunkData = { text: chunk.text, isComplete: chunk.isComplete, sources: chunk.sources, tokenCount: chunk.tokenCount, }; controller.enqueue(JSON.stringify(chunkData) + '\n'); } } catch (error) { console.error('Sonar stream search failed:', error); const errorMessage = `Sonar stream search failed: ${error.message}`; controller.enqueue(JSON.stringify({ error: errorMessage }) + '\n'); } finally { controller.close(); } }, }); return new Response(stream, { headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-cache', Connection: 'keep-alive', }, }); } catch (error) { console.error('Error in POST /api/sonar-stream:', error); return new Response(JSON.stringify({ error: 'Failed to process request' }), { status: 500, headers: { 'Content-Type': 'application/json', }, }); } } //# sourceMappingURL=route.js.map