File size: 2,958 Bytes
bd699fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import { GoogleGenerativeAI } from '@google/generative-ai';
import dotenv from 'dotenv';
dotenv.config();
class RAGEngine {
constructor() {
this.genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
// Using Gemini Pro with correct model path format
this.model = this.genAI.getGenerativeModel({ model: 'models/gemini-pro' });
}
/**
* Split text into chunks for processing
*/
chunkText(text, chunkSize = 2000, overlap = 200) {
const chunks = [];
let start = 0;
while (start < text.length) {
const end = Math.min(start + chunkSize, text.length);
chunks.push(text.slice(start, end));
start = end - overlap;
}
return chunks;
}
/**
* Generate embeddings for text (simplified version)
* In production, use proper embedding models
*/
async generateEmbedding(text) {
// For now, we'll use the text directly
// In production, implement proper vector embeddings
return text;
}
/**
* Perform semantic search on chunks
*/
async semanticSearch(query, chunks) {
// Simplified semantic search
// In production, use vector similarity search
return chunks.filter(chunk =>
chunk.toLowerCase().includes(query.toLowerCase())
);
}
/**
* Generate response using RAG approach
*/
async generateResponse(query, context) {
try {
const prompt = `
You are an AI assistant analyzing disaster management training data for the Government of India (NDMA).
Context from uploaded documents:
${context}
Query: ${query}
Please provide a detailed, data-driven response based on the context provided. Include specific numbers, statistics, and insights where available.
`;
const result = await this.model.generateContent(prompt);
const response = await result.response;
return response.text();
} catch (error) {
throw new Error(`RAG generation failed: ${error.message}`);
}
}
/**
* Main RAG query method
*/
async query(question, documentText) {
try {
// Split document into chunks
const chunks = this.chunkText(documentText);
// Find relevant chunks (simplified semantic search)
const relevantChunks = await this.semanticSearch(question, chunks);
// Combine relevant chunks as context
const context = relevantChunks.slice(0, 3).join('\n\n');
// Generate response
const response = await this.generateResponse(question, context);
return response;
} catch (error) {
throw new Error(`RAG query failed: ${error.message}`);
}
}
}
export default new RAGEngine();
|