openai-apichatgpt-apichatgpt-pluginchat-gpt-4

OpenAI API GPT-3.5 Translation Issue: Incorrect Output for Turkish to Japanese Translation


I am using the OpenAI API and trying to translate Turkish data to Japanese using the GPT-3.5 model. However, the results I'm getting are not as expected. The provided JSON data contains Turkish language codes ("tr") that should be replaced with the correct Japanese language code.

I expect the output to have the correct Japanese language code ("ja") instead of "JA" and to show the proper translations for the provided Turkish data.

Can someone please help me identify the issue and suggest a solution to obtain the correct Turkish to Japanese translations using the OpenAI API and GPT-3.5 model? Thank you!

Here's the code snippet I'm using:

const { Configuration, OpenAIApi } = require("openai");
const { getLanguageString } = require('../../../helpers/auto_translate_helpers');

async function gptTranslateHelper(chunks, schema, target_language) {
    const responseData = [];
    const configuration = new Configuration({
        apiKey: process.env.OPENAI_API_KEY,
    });

    const openai = new OpenAIApi(configuration);
    try {
        for (const chunk of chunks) {
            const userMessage = {
                "role": "user",
                "content": `I am a restaurant owner and I manage my data in Turkish.My data : ${chunk.data} Please translate my data to ${getLanguageString(target_language)}. Replace all occurrences of "TR" with ${target_language}.
                `,
            };

            const completion = await openai.createChatCompletion({
                model: "gpt-3.5-turbo-16k",
                messages: [
                    { "role": "system", "content": "You are a helpful assistant." },
                    userMessage,
                ],
                functions: [{ name: "set_schema", parameters: schema }],
                function_call: { name: "set_schema" },
            });

            const generatedText = completion.data.choices[0].message.content;
            const parsedMenu = JSON.parse(generatedText);

            responseData.push(...parsedMenu.data);
        }
    } catch (e) {
        console.log(e);
    }
    console.log(responseData);

    return responseData;
}

module.exports = { gptTranslateHelper };

Prompt:

I am a restaurant owner and I manage my data in Turkish.My data : [{"_id":"64adce3a897a661ba27dcc4b","name":{"tr":"Yemekler"}},{"_id":"64adce44897a661ba27dcc4c","name":{"tr":"İçecekler"}}] Please translate my data to Japanese. Replace all occurrences of "tr" with ja.

Incorrect Result: [ { _id: '64adce3a897a661ba27dcc4b', name: { ja: 'Yemekler' } }, { _id: '64adce44897a661ba27dcc4c', name: { ja: 'İçecekler' } } ]

Schema


Solution

  • You could use a schema like this:

    {
      "name": "translate",
      "description": "Return Japanese translation of input",
      "parameters": {
        "type": "object",
        "properties": {
          "tr": {
            "type": "string",
            "description": "Turkish word"
          },
          "ja": {
            "type": "string",
            "description": "Japanese translation of Turkish word"
          }
        },
        "required": ["ja"]
      }
    }
    

    OpenAI's function calling is intented to generate parameters for known functions in your instead of generating results of that function. Like in this weather example, ChatGPT doesn't return the actual weather information rather it extracts the location information that can be used to obtain the weather information.

    In your example, think of a function called translate and it takes two arguments; "tr" and "ja". "Yemekler" would be extracted and placed in the "tr" argument and its translation would be placed in "ja".