From 37b4fc8574f36ec0a1f8ab279febf1d6891edc0d Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Thu, 25 Jul 2024 08:26:53 +0400 Subject: [PATCH] Update translateToSQL.js The improvements to the code include enhanced error handling by incorporating a `try-catch` block around the `fetch` call, which allows for more graceful handling of network or request errors. Additionally, response handling has been improved with checks to ensure that `choices[0]` and `choices[0].text` exist before attempting to access them, preventing potential errors from undefined values. The error messages have been made more descriptive to aid in debugging and provide clearer information about issues. Finally, the API request payload has been separated into its own `payload` variable, which improves the clarity and readability of the code. --- src/translateToSQL.js | 69 +++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/translateToSQL.js b/src/translateToSQL.js index 1d5ab6e..8d5af9b 100644 --- a/src/translateToSQL.js +++ b/src/translateToSQL.js @@ -1,41 +1,54 @@ import fetch from "isomorphic-unfetch"; const translateToSQL = async (query, apiKey, tableSchema = "") => { - // Validate inputs if (!query || !apiKey) { throw new Error("Missing query or API key."); } - const prompt = `Translate this natural language query into SQL without changing the case of the entries given by me:\n\n"${query}"\n\n${tableSchema ? `Use this table schema:\n\n${tableSchema}\n\n` : ''}SQL Query:`; - + // Construct the prompt + const prompt = `Translate the following natural language query into SQL without altering the case of any entries:\n\n"${query}"\n\n${tableSchema ? `Use the following table schema:\n\n${tableSchema}\n\n` : ''}SQL Query:`; + console.log(prompt); - const response = await fetch("https://api.openai.com/v1/completions", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${apiKey}`, - }, - body: JSON.stringify({ - prompt, - temperature: 0.5, - max_tokens: 2048, - n: 1, - stop: "\\n", - model: "gpt-3.5-turbo-instruct", - frequency_penalty: 0.5, - presence_penalty: 0.5, - logprobs: 10, - }), - }); - - const data = await response.json(); - if (!response.ok) { - console.error("API Error:", response.status, data); - throw new Error(data.error || "Error translating to SQL."); - } - return data.choices[0].text.trim(); + // API request payload + const payload = { + prompt, + temperature: 0.5, + max_tokens: 2048, + n: 1, + stop: "\\n", + model: "gpt-3.5-turbo-instruct", + frequency_penalty: 0.5, + presence_penalty: 0.5, + logprobs: 10, + }; + + // Make the API request + try { + const response = await fetch("https://api.openai.com/v1/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${apiKey}`, + }, + body: JSON.stringify(payload), + }); + + // Check for API errors + if (!response.ok) { + const errorData = await response.json(); + console.error("API Error:", response.status, errorData); + throw new Error(errorData.error?.message || "Error translating to SQL."); + } + + // Parse the response + const { choices } = await response.json(); + return choices[0]?.text?.trim() || "No SQL query returned."; + } catch (error) { + console.error("Request Error:", error); + throw new Error("Error processing the request."); + } }; export default translateToSQL;