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
26 lines (25 loc) • 1.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.askGroq = askGroq;
const groq_sdk_1 = __importDefault(require("groq-sdk"));
async function askGroq(query, schema, config) {
var _a, _b, _c;
const groq = new groq_sdk_1.default({ apiKey: config.apiKey });
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.
`;
const chatCompletion = await groq.chat.completions.create({
messages: [{ role: 'user', content: prompt }],
model: config.model || 'gemma2-9b-it',
});
return ((_c = (_b = (_a = chatCompletion.choices[0]) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.content) === null || _c === void 0 ? void 0 : _c.trim()) || '';
}