From d842dc996ab7cf16a04d6ed41b74d02538ef998d Mon Sep 17 00:00:00 2001 From: Santosh Bhavani Date: Sun, 19 Oct 2025 19:57:11 -0700 Subject: [PATCH] feat(api): add LLM-enhanced graph query endpoint - Create new /api/graph-query-llm endpoint for graph search + LLM generation - Retrieves triples using graph search and generates answers using LLM - Supports both traditional and vector-based graph search - Makes traditional graph search comparable to RAG for benchmarking --- .../frontend/app/api/graph-query-llm/route.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 nvidia/txt2kg/assets/frontend/app/api/graph-query-llm/route.ts diff --git a/nvidia/txt2kg/assets/frontend/app/api/graph-query-llm/route.ts b/nvidia/txt2kg/assets/frontend/app/api/graph-query-llm/route.ts new file mode 100644 index 0000000..963dd06 --- /dev/null +++ b/nvidia/txt2kg/assets/frontend/app/api/graph-query-llm/route.ts @@ -0,0 +1,45 @@ +import { NextRequest, NextResponse } from 'next/server'; +import backendService from '@/lib/backend-service'; +import { getGraphDbType } from '../settings/route'; + +/** + * API endpoint for LLM-enhanced graph query + * This retrieves triples using graph search and generates an answer using LLM + * Makes traditional graph search comparable to RAG for fair benchmarking + * POST /api/graph-query-llm + */ +export async function POST(request: NextRequest) { + try { + const { query, topK = 5, useTraditional = true, llmModel, llmProvider } = await request.json(); + + if (!query) { + return NextResponse.json({ error: 'Query is required' }, { status: 400 }); + } + + // Initialize backend if needed with the selected graph DB type + if (!backendService.isInitialized) { + const graphDbType = getGraphDbType(); + console.log(`Initializing backend with graph DB type: ${graphDbType}`); + await backendService.initialize(graphDbType); + } + + console.log(`Graph query with LLM: "${query}", topK=${topK}, traditional=${useTraditional}, model=${llmModel || 'default'}, provider=${llmProvider || 'default'}`); + + // Query the backend with LLM enhancement + const result = await backendService.queryWithLLM(query, topK, useTraditional, llmModel, llmProvider); + + // Return results + return NextResponse.json({ + query, + answer: result.answer, + triples: result.triples, + count: result.count, + message: `Retrieved ${result.count} triples and generated answer using ${useTraditional ? 'traditional' : 'vector'} graph search + LLM` + }); + } catch (error) { + console.error('Error in graph query with LLM:', error); + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; + return NextResponse.json({ error: errorMessage }, { status: 500 }); + } +} +