Help

Translation of records through OpenApi using scripting

Topic Labels: Scripting extentions
2576 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Lamia_Bel_Khade
4 - Data Explorer
4 - Data Explorer

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 = /[^A-Za-z0-9](.+)/.exec(data.choices[0].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!

3 Replies 3
Lamia_Bel_Khade
4 - Data Explorer
4 - Data Explorer

Sorry I meant OpenAi. I just realized that I have been writing it wrong.

Hi @Lamia_Bel_Khadem ,

If you are looking for translation I would say Open AI is not the right tool. I am playing with it right now and on the text side is is creating plenty of creative gibberish…

The right API to use would be Google Translate API (or equivalents from Azure, Amazon):

I have made a video about it some time ago, which describes how translate text into multiple languages at the same time. You can see the code and video below. It should be easy to change it just to a single language.

If anything did not age well (the Google API might have been updated since), do let me know!

I hope that is helpful and will protect you from having overly creative translations from Open AI… :rofl:

Khushi100
4 - Data Explorer
4 - Data Explorer

Hi,

The inconsistencies you're facing might be due to the response from OpenApi, not necessarily your script. To improve accuracy, consider refining your script to handle incomplete translations or unexpected outputs. Also, review the API documentation for any specific requirements or limitations that might affect translation reliability within AI Translation.

Best regards, 

Khushi