querycrafter
Version:
QueryCrafter intelligently translates natural language into accurate SQL queries (NLP2SQL). This npm module simplifies database interactions by allowing you to retrieve data using plain English, eliminating the need for complex code. It's a powerful and i
27 lines (25 loc) • 1.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.askGemini = askGemini;
const generative_ai_1 = require("@google/generative-ai");
async function askGemini(query, schema, config) {
const genAI = new generative_ai_1.GoogleGenerativeAI(config.apiKey);
const model = genAI.getGenerativeModel({ model: config.model || 'gemini-pro' });
const prompt = `
You are an SQL generator.
Database Schema: ${JSON.stringify(schema, null, 2)}
Question: "${query}"
Rules:
- Return ONLY a valid SQL query (no explanation).
- Use only tables and columns from the schema.
- Generate SQL for a ${config.dbClient} database.
- If the user asks to see the tables, use the information_schema.tables to get the list of tables.
-- Example
-- Question: "show me the database tables"
-- SQL: SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
`;
const result = await model.generateContent(prompt);
const response = await result.response;
const text = await response.text();
return text.trim();
}