Hi!
I have recently discovered OpenApi and I am working on a project that requires me to translate text from English to French. I used scripting extension. The script fetches the translation from OpenApi and outputs it to “Translated field”. For some records, Everything seems to work fine. However, for other records the translation doesn’t always work and sometimes it outputs incomplete sentences or random links.
Is this because of my script or is the OpenApi random? What can I do to make it work?
// Change this to the name of a table in your base
let table = base.getTable('Translation');
let openaiUrl = "https://api.openai.com/v1/engines/text-davinci-002/completions";
// Prompt the user to pick a record
// If this script is run from a button field, this will use the button's record instead.
let record = await input.recordAsync('Select a record to use', table);
if (record) {
// Construct the body
// Change the field name to match yours.
let body = {
"prompt": "Translate this into French /n"+record.getCellValue("Notes"),
"temperature": 0.4,
"max_tokens": 60,
"top_p": 1.0,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}
// Make a request to OpenAI API
// Add your own API key in the place of Xses.
let response = await fetch(openaiUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer XXXXXXXXXXXXXXXXXX',
},
});
let data = await response.json();
// Comment this out if you don't want to see any outputs
console.log(data)
let str = /t^A-Za-z0-9](.+)/.exec(data.choicesh0].text).1];
// Write to a cell, change field name to match your table.
table.updateRecordAsync(record,
{
'Translated': str
});
}
I am also open to other suggestions to do the translation!