-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathtranslateToSQL.js
More file actions
41 lines (34 loc) · 1.17 KB
/
Copy pathtranslateToSQL.js
File metadata and controls
41 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import fetch from "isomorphic-unfetch";
const translateToSQL = async (query, apiEndPointURL, 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:`;
console.log(prompt);
const response = await fetch(`${apiEndPointURL}/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: process.env.MODEL_ID || "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();
};
export default translateToSQL;