UNPKG

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

57 lines (52 loc) 2.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.askOllama = askOllama; const node_fetch_1 = __importDefault(require("node-fetch")); async function askOllama(query, schema, config) { const prompt = ` You are an expert SQL generator. Database Schema: ${JSON.stringify(schema, null, 2)} Question: "${query}" Strict Output Rules: 1. Return ONLY the SQL query. 2. Do NOT add explanations, comments, markdown, or formatting. 3. Do NOT include words like "SQL", "query", "answer", "output", or any extra text. 4. Output must be exactly ONE valid SQL statement ending with a semicolon. 5. Generate SQL for a ${config.dbClient} database, and correctly interpret table and column names for consistent use in ${config.dbClient} based queries. 6. strictly Use only tables and columns from the schema. 7. Do NOT use semicolons inside except at the very end. 8. The entire SQL must be returned in a **single line** without line breaks or indentation. Now generate the SQL: `; const res = await (0, node_fetch_1.default)("http://localhost:11434/api/generate", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ model: config.model || "llama3", prompt: prompt, stream: false, }), }); const data = await res.json(); if (!data || !data.response) { console.error("❌ Ollama returned unexpected response:", data); throw new Error("Ollama did not return a valid SQL response."); } let sqlQuery = data.response.trim(); console.log(`ollama raw result : ${sqlQuery}`); // Remove markdown fences if present const sqlRegex = /```(?:sql)?\s*([\s\S]*?)\s*```/i; const match = sqlQuery.match(sqlRegex); if (match && match[1]) { sqlQuery = match[1].trim(); } // Force it into one line sqlQuery = sqlQuery.replace(/\s+/g, " ").trim(); console.log(`ollama cleaned SQL : ${sqlQuery}`); return sqlQuery; }