Translation API recommends OpenAI API (Introduction to prompts that can be used in ChatGPT)
投稿日: 2024/07/02
This article is a translation of the following article.
I recently conducted a cost comparison of translation using the DeepL API, OpenAI API, and Google Cloud Translation API. You can find the comparison here.
Ultimately, Noh uses the OpenAI API, so I would like to share our insights on that.
In Noh, we are ultimately using the OpenAI API with GPT-3.5-Turbo for the translation function. We use the prompt for translating markdown and believe it could also be helpful for translating repository documents.
Benefits of using OpenAI API for translation
The biggest advantage of using the OpenAI API for translation is its low cost. For more details, please read the article on DeepL API vs OpenAI API vs Google Cloud Translation API translation pricing comparison, but compared to the DeepL API, translation using GPT-3.5-Turbo costs less than one-tenth. Even using GPT-4o is cheaper than the DeepL API.
Also, the flexibility to translate markdown text like in Noh is excellent. DeepL Translation supports translation for formats like XML, but it does not understand markdown. You can give instructions for translation flexibly. When you try using it, you will truly realize how wonderful it is.
For example, by adding a prompt like You are a skilled engineer
, I feel like the translation can be made more natural (though I haven't conducted detailed verification). I feel that technical terms are less likely to be translated unnaturally.
I feel that the translation quality is quite high. There are many natural-sounding sentences, both good and bad. In my personal experience, GPT-3.5-Turbo has sufficient translation capabilities.
Disadvantages of using OpenAI API for translation
Of course, there are also disadvantages.
Since it is natural language processing, it is troublesome when you are given a high degree of freedom in responses by ignoring prompts. While you can prevent this to some extent by devising prompts, completely preventing it is technically difficult.
In reality, when you try it out, behaviors such as "starting to explain the input text," "outputting in the same language as the source," and "arbitrarily changing markdown elements" regularly occur.
Even when I add prompts to avoid problematic responses, they ignore the prompts themselves, which is a technical limitation.
There are also challenges in specifying the format of the response. The DeepL API can return values in an array, but the OpenAI API also has difficulty consistently returning values in Json. There are various possible solutions, but personally, I think that the most stable approach is to return only one translation (in other words, not returning it in Json at all).
The execution time is also quite long.
It is also important to pay attention to the Usage Limit.
There are detailed limits depending on the user's level (although personally, I feel that they are relatively tolerant and less likely to be a practical issue).
If you cannot tolerate the disadvantages mentioned above, it would be better to use the DeepL API.
Recommended Markdown Text Translation Prompt
You are an engineer and are only allowed to translate. Translate user input markdown text into English. Maintain the markdown structure exactly as it is in the input. Do not change header levels or other markdown formatting. Only translate the text content.
Recommended prompts.
I will elaborate.
You are an engineer and are only allowed to translate.
I tell them to translate as an engineer. I feel that they are getting stronger in technical vocabulary.
It also specifies that only translations are allowed. This was a fairly useful prompt for me personally. Conversely, the prompt do nothing but translate
was not helpful.
Translate user input markdown text into English.
This is a prompt asking for translation.
Maintain the markdown structure exactly as it is in the input. Do not change header levels or other markdown formatting.
We are careful not to break the structure of the markdown. This is the most important private matter if you are translating markdown text. Without this prompt, the markdown structure was changed without permission, such as H1 being changed to H2.
Only translate the text content.
I'm not too sure if this is necessary, but I'm adding it on a trial basis to reduce the extra output. It may be the same without it.
Points to Note and Consider When Using the OpenAI API
It goes without saying, but when using the OpenAI API or DeepL API, do not directly call the API from the client. Create an API wrapper for each API and manage the API tokens in server-side environment variables or secret manager.
This should also go without saying, but do not write the API tokens directly in the source code and push to Git.
When using the OpenAI API to create a translation feature, it is necessary to impose restrictions to prevent users from executing translations indefinitely, just like when using the DeepL API. This helps prevent potential attacks and mitigate the risk of incurring high costs due to implementation errors.
Measures against prompt injection are also necessary. Restricting as done earlier will be one of the measures. Also, I think it is effective to limit the number of output tokens depending on the amount of text in the source document. There is a certain correlation between the amount of text in the source document and the amount of text in the translated document, so when translating 10 tokens of English into Japanese, it should be fine to limit the output token count to around 20-30 tokens. This way, unintended outputs can be mitigated.
It is also useful to change the model used depending on the length of the input text.
Using gpt-3.5-turbo-16k
for long texts and gpt-3.5-turbo
for short texts can help to reduce costs.
Measures to deal with long execution times are also necessary. In Noh, the Noh translation API receives multiple sentences and sends multiple requests to the OpenAI API in parallel using Promise.all
to drastically speed up the translation of many sentences. This also helps with handling the fact that the OpenAI API is not efficient at serializing JSON.
And finally, the last tip is to have a broad mind. I have written detailed prompts, but sometimes I forget to follow them regularly. If this is a requirement that you cannot tolerate, you should consider other options.
Summary
Using the OpenAI API GPT-3.5-Turbo for translation is very cost-effective, and if you can tolerate a certain level of translation errors, I think it is practical in terms of accuracy. Conversely, if you are creating a system that cannot tolerate bizarre translations, then it would be better to use the DeepL API or Google Cloud Translation API. The DeepL API and Google Cloud Translation API are specialized in translation and do not provide responses other than translations. However, by crafting the prompt, non-translation behaviors in the OpenAI API translation could be significantly reduced.
Using the OpenAI API for translation allows you to translate Markdown text flexibly and conveniently.