@retrivora-ai/rag-engine 1.1.9 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -423,4 +423,59 @@ Optimized Search Query:`;
423
423
 
424
424
  return rewrite.trim() || question;
425
425
  }
426
+
427
+ /**
428
+ * Generate 3-5 short, relevant questions based on the vector database content.
429
+ */
430
+ async getSuggestions(query: string, namespace?: string): Promise<string[]> {
431
+ if (!query || query.trim().length < 3) {
432
+ return [];
433
+ }
434
+
435
+ await this.initialize();
436
+ const ns = namespace ?? this.config.projectId;
437
+
438
+ try {
439
+ // 1. Retrieve relevant context (top 3 matches)
440
+ const { sources } = await this.retrieve(query, { namespace: ns, topK: 3 });
441
+
442
+ if (sources.length === 0) {
443
+ return [];
444
+ }
445
+
446
+ // 2. Generate suggestions using LLM
447
+ const context = sources.map((s) => s.content).join('\n\n---\n\n');
448
+ const prompt = `
449
+ Based on the following snippets from a document, what are 3 short, relevant questions a user might ask?
450
+ Focus on questions that can be answered by the context.
451
+ Keep each question under 10 words and make them very specific to the content.
452
+ Return ONLY a JSON array of strings like ["Question 1", "Question 2", "Question 3"].
453
+
454
+ Context:
455
+ ${context}
456
+
457
+ Suggestions:`;
458
+
459
+ const response = await this.llmProvider.chat(
460
+ [
461
+ { role: 'system', content: 'You are a helpful assistant that generates search suggestions.' },
462
+ { role: 'user', content: prompt }
463
+ ],
464
+ ''
465
+ );
466
+
467
+ // Simple parsing of JSON array from LLM response
468
+ const match = response.match(/\[[\s\S]*\]/);
469
+ if (match) {
470
+ const suggestions = JSON.parse(match[0]);
471
+ if (Array.isArray(suggestions)) {
472
+ return suggestions.map(s => String(s)).slice(0, 3);
473
+ }
474
+ }
475
+ } catch (error) {
476
+ console.error('[Pipeline] Failed to generate suggestions:', error);
477
+ }
478
+
479
+ return [];
480
+ }
426
481
  }
@@ -81,4 +81,11 @@ export class VectorPlugin {
81
81
  return this.pipeline.ingest(documents, namespace);
82
82
  }
83
83
 
84
+ /**
85
+ * Get auto-suggestions based on a query prefix.
86
+ */
87
+ async getSuggestions(query: string, namespace?: string): Promise<string[]> {
88
+ await this.validationPromise;
89
+ return this.pipeline.getSuggestions(query, namespace);
90
+ }
84
91
  }
@@ -261,5 +261,35 @@ export function createUploadHandler(configOrPlugin?: Partial<RagConfig> | Vector
261
261
  };
262
262
  }
263
263
 
264
+ /**
265
+ * createSuggestionsHandler — factory for the auto-suggestions endpoint.
266
+ */
267
+ export function createSuggestionsHandler(configOrPlugin?: Partial<RagConfig> | VectorPlugin) {
268
+ const plugin =
269
+ configOrPlugin instanceof VectorPlugin
270
+ ? configOrPlugin
271
+ : new VectorPlugin(configOrPlugin);
272
+
273
+ return async function POST(req: NextRequest) {
274
+ try {
275
+ const body = await req.json();
276
+ const { query, namespace } = body as {
277
+ query: string;
278
+ namespace?: string;
279
+ };
280
+
281
+ if (typeof query !== 'string') {
282
+ return NextResponse.json({ error: 'query is required' }, { status: 400 });
283
+ }
284
+
285
+ const suggestions = await plugin.getSuggestions(query, namespace);
286
+ return NextResponse.json({ suggestions });
287
+ } catch (err) {
288
+ const message = err instanceof Error ? err.message : 'Internal server error';
289
+ return NextResponse.json({ error: message }, { status: 500 });
290
+ }
291
+ };
292
+ }
293
+
264
294
  // Re-export SSE helper so host apps can use it in custom handlers
265
295
  export { sseFrame, sseTextFrame, sseMetaFrame, sseErrorFrame };
@@ -50,3 +50,17 @@ export interface RetrievalResult {
50
50
  export interface IRetriever {
51
51
  retrieve(query: string, options?: Record<string, unknown>): Promise<RetrievalResult>;
52
52
  }
53
+
54
+ export interface SuggestionsResponse {
55
+ suggestions: string[];
56
+ }
57
+
58
+ export interface Product {
59
+ id: string | number;
60
+ name: string;
61
+ brand?: string;
62
+ price?: string | number;
63
+ image?: string;
64
+ link?: string;
65
+ description?: string;
66
+ }