Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ouadie-limouni/711861636bc97743debcf663a7ae5957 to your computer and use it in GitHub Desktop.
Save ouadie-limouni/711861636bc97743debcf663a7ae5957 to your computer and use it in GitHub Desktop.
chatGPT request
app.post('/prompt', async (req, res) => {
const { message } = req.body;
if (!message) {
res.send({
success: false,
error: 'Something failed! Send data as JSON { "message": "prompt" }.',
});
} else {
let script = '';
var start = new Date();
const openai = new OpenAIApi(configuration);
try {
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: 'Respond in Qlik Sense Syntax.',
},
{
role: 'user',
content: 'Qlik inline LOAD script for C,D in table TBL',
},
{
role: 'assistant',
content: '\nTbl:\nLOAD * INLINE [\nC,D\nc1,d1\nc2,d2\n];\n',
},
{
role: 'user',
content: 'Store table A into QVD in ABC space',
},
{
role: 'assistant',
content: "\nSTORE A INTO '[lib://ABC:DataFiles/A.qvd]' (qvd);\n",
},
{
role: 'user',
content: `Without any explanations, comments, or code formating. Include table name from the nature of the data. Include ":DataFiles" after the space name. ${message}`,
},
],
max_tokens: 2048,
temperature: 0,
});
var end = new Date() - start;
console.info('Execution time: %dms', end);
if (response.data.choices.length) {
if (response.data.choices[0].message.content.slice(-1) === '.') {
script = response.data.choices[0].message.content.slice(0, -1);
} else {
script = response.data.choices[0].message.content;
}
// Format Data ----
const lines = script.split('\n').map((line) => line.trim());
const headers = lines[2].split(',').map((header) => header.trim());
const endIndex = lines.findIndex((line) => line.includes(']'));
const data = lines.slice(3, endIndex).map((line) => {
const values = line.split(',').map((val) => val.trim());
const obj = {};
for (let i = 0; i < headers.length; i++) {
obj[headers[i]] = values[i];
}
return obj;
});
//-----------------
// Return Response
res.send({ success: true, response: script, data: data });
} else {
res.send({
success: false,
error: 'No response returned. Try again. ',
});
}
} catch (err) {
res.send({
success: false,
error: 'Something failed! Try again.' + err,
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment