From fc88d745579da9862d8a52ecb621980dde024dcb Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Thu, 25 Jul 2024 08:28:23 +0400 Subject: [PATCH] Update translateToHuman.js The improvements to the code include enhanced input validation, which clarifies the error message to indicate that both the query and API key are required. The API request payload has been separated into its own `payload` variable, improving readability. Error handling has been improved by adding a `try-catch` block to manage network or request errors more effectively. Descriptive error messages have been incorporated to aid in debugging, providing clearer information about issues encountered. Additionally, response handling has been refined with checks to ensure that `choices[0]` and `choices[0].text` exist before accessing them, and a default message is provided if no translation is available. --- src/translateToHuman.js | 62 ++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/translateToHuman.js b/src/translateToHuman.js index 8765158..fab8609 100644 --- a/src/translateToHuman.js +++ b/src/translateToHuman.js @@ -3,35 +3,47 @@ import fetch from "isomorphic-unfetch"; const translateToHuman = async (query, apiKey) => { // Validate inputs if (!query || !apiKey) { - throw new Error("Missing query or API key."); + throw new Error("Both query and API key are required."); } - const response = await fetch("https://api.openai.com/v1/completions", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${apiKey}`, - }, - body: JSON.stringify({ - prompt: `Translate this SQL query into natural language:\n\n"${query}"\n\nNatural language query:`, - temperature: 0.5, - max_tokens: 2048, - n: 1, - stop: "\\n", - model: "text-davinci-003", - frequency_penalty: 0.5, - presence_penalty: 0.5, - logprobs: 10, - }), - }); + // Construct the API request payload + const payload = { + prompt: `Translate the following SQL query into natural language:\n\n"${query}"\n\nNatural language query:`, + temperature: 0.5, + max_tokens: 2048, + n: 1, + stop: "\\n", + model: "text-davinci-003", + 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 human language."); - } + try { + // Make the API request + 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 query to natural language."); + } - return data.choices[0].text.trim(); + // Parse and return the response + const { choices } = await response.json(); + return choices[0]?.text?.trim() || "No translation available."; + } catch (error) { + console.error("Request Error:", error); + throw new Error("Error processing the request."); + } }; export default translateToHuman;