nach ein paar weiteren fragen klappt das script nun 😄 ...
const ACCESS_TOKEN = "token";
const MODEL_ID = "text-davinci-002";
const QUESTION_DP_ID = "0_userdata.0.GlobalVars.OpenAIQuestion";
const ANSWER_DP_ID = "0_userdata.0.GlobalVars.OpenAIAnswer";
const https = require('https');
on({id: QUESTION_DP_ID, change: "ne"}, function (obj) {
// Get the current value of the question datapoint
const question = getState(QUESTION_DP_ID).val;
console.log(`Question: ${question}`);
// Set the value of the answer datapoint to "no answer available"
setState(ANSWER_DP_ID, "no answer available");
// Create the HTTP POST options
const options = {
host: 'api.openai.com',
path: '/v1/completions',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${ACCESS_TOKEN}`
}
};
// Create the HTTP POST request
const req = https.request(options, (res) => {
console.log(`Status: ${res.statusCode}`);
console.log(`Headers: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
console.log(`Answer: ${responseData}`);
const responseJson = JSON.parse(responseData);
if (responseJson.hasOwnProperty('choices')) {
const answer = responseJson.choices[0].text;
// Set the value of the answer datapoint to the first answer
setState(ANSWER_DP_ID, answer);
}
});
});
req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
});
// Send the HTTP POST request
req.write(JSON.stringify({
"model": MODEL_ID,
"prompt": question,
"max_tokens": 128
}));
req.end();
});