Blog

AI Language Translations using Ruby and OpenAI (Tldr: It’s easy)

Placeholder Avatar

February 12, 2023

The capacity to swiftly and accurately translate material into another language is a crucial advantage in today’s environment. With advancements in artificial intelligence (AI), it is now easier than ever to obtain trustworthy translation services. OpenAI GPT-3 provides one of the most advanced AI technologies that can help you immediately and accurately translate text into different languages. In this blog article, we’ll go over how to employ Ruby with OpenAI GPT-3 to translate text into various languages rapidly and efficiently. We’ll also go over some of its features and benefits, which make it an excellent alternative for anyone searching for a quick and precise translation service. So let’s get this party started!

Italian Women Speaking

An Italian woman speaking with a Chinese man in the style of Caravaggio by OpenAI


Prerequisites

In order to follow this article, you’ll need to have a good understanding of the Ruby programming language. This includes functions and working with gems.

Before working with the API you will need to sign up for an API key at https://beta.openai.com/.

If you need help with getting started with OpenAI go to my beginners guide to get started.


Creating a Simple Language Translation Script

Lets develop a basic Ruby programme to convert English text to another language. We will interface with the OpenAI GPT-3 API to improve translation accuracy and speed.


The OpenAI GPT-3 API allows you to translate text into several languages in a sophisticated and fast manner. We will use the supported ruby-openai gem to make this procedure easier. This gem is built on the OpenAI GPT-3 model and includes functions that let us to communicate with it in a simple and straightforward manner.


Start by installing the gem, run the following in your terminal:



gem install ruby-openai


In order to use the ruby-openai gem in our language translation script, we need to include it in the code. This is done using the “require” keyword at the top of our file.



require 'ruby-openai'


With its ‘completion’ endpoint, OpenAI provides an effective and quick approach to translate text. This endpoint allows you to supply the API with the text to be translated, and it will return an exact and reliable translation of that content in one or more languages.



require "ruby/openai"

def translate(text)
  client = OpenAI::Client.new(access_token: 'YOUR_API_KEY')

  prompt = "Translate the following text into French:\n\nText: #{text}"

  response = client.completions(
    parameters: 
      prompt: prompt,
      model: "text-davinci-003",
      temperature: 0.3,
      max_tokens: 100,
    }
  )

  puts response['choices'][0]['text']
end

translationText = "I would like a cup of milk please"

translate(translationText)


Parameters

prompt: This is the instruction you provide to the API. In this case we are asking OpenAI to translate the provided text into French.

model: OpenAI provides several different AI models. Davinci is the latest and most accurate model. It is the one you will want to use for now.

temperature: This controls the randomness or “creativity” of the generated text. A higher temperature will result in the model generating more diverse and unexpected completions, while a lower temperature will result in the model being more conservative and sticking closer to the input text. The temperature is typically set to a value between 0 and 1, with 0 being the most conservative and 1 being the most creative. For our purposes we want to keep the temperature fairly low to provide an accurate translation.

max_tokens: A token is equivalent to about 4 characters. This sets the maximum number of tokens for both the prompt and the response. 4096 is the limit for tokens.

The Output

In our example we asked OpenAI to translate ‘I want a cup on milk’ into French. OpenAI provides us with the following output:



Je voudrais une tasse de lait s'il vous plaît.


Questioning the Accuracy of the Translation

When translating text, accuracy is one of the most important criteria. The OpenAI GPT-3 API claims to provide accurate translations with its ‘completion’ endpoint. In translations the key to assessing the accuracy of a translation is to convert it back into the original language and compare it.



require "ruby/openai"

def translate(text)
  client = OpenAI::Client.new(access_token: 'YOUR_API_KEY')

  prompt = "Translate the following text into English:\n\nText: #{text}"

  response = client.completions(
    parameters: 
      prompt: prompt,
      model: "text-davinci-003",
      temperature: 0.3,
      max_tokens: 100,
    }
  )

  puts response['choices'][0]['text']
end

translationText = "Je voudrais une tasse de lait s'il vous plaît."

translate(translationText)


I modified the prompt to ask OpenAI to translate into English, and the input text providing the French sentence.


The Output


I would like a cup of milk please


This is identical to the original English text, which means we have a perfect translation.


Some Issues

As I was testing this model I did discover that OpenAI had a difficult time accurately translating some English phrases into Afrikaans. But the more I used the model the more accurate my responses became. I could sometime cause it issues with silly statements like:



I think I smell funny. What do you think I should do about this predicament?


This text when translated to Afrikaans and back to English gave me:



I think I smell something strange. What do you think I should do about 
this situation?


Translate picture


It appears OpenAI works very well translating other languages into English, but has some difficulty in the opposite direction. This will improve over time as the model is provided with more training data and reinforced learning.


The other slight issue I found, is it will add punctuation automatically. In many cases this is fine, such as adding a missing period at the end of the sentence.


However, when I was benchmarking the translations using a BLEU script, (BLEU is an algorithm for evaluating the quality of AI translated language), I had to remove the punctuation to get accurate comparisons. What I was finding with converting Afrikaans back into English was a percentage accuracy between 40–100%, which is quite a wide range.


Advantages of OpenAI Translations Over Google Translate API

The OpenAI GPT-3 API has some advantages over the Google Translate API. You can utilise prompt engineering to modify your request to OpenAI to vary the response slightly or drastically. The nice thing about this is that you can make a variety of responses simply by changing the prompt variable in your script.


With a simple change in the variable you can translate your text into as many languages as you like. Let’s translate ‘I would like a cup of milk please’ into Italian, Mandarin, Polish and Swahili. We just need to alter the prompt variable:



prompt = "Translate the following text into Italian, Mandarin, Polish 
          and Swahili:\n\nText: #{text}"

Output:

Italian: Vorrei un bicchiere di latte per favore
Mandarin: 我想要一杯牛奶,請
Polish: Poproszę szklankę mleka
Swahili: Nataka glasi ya maziwa tafadhali


What is Prompt Engineering?

Prompt engineering is the act of creating a phrase or combination of queries that can be used to lead the AI model to provide targeted, accurate responses. This necessitates a thorough understanding of language structure, terminology, and context, as well as the capacity to create relevant prompts for the model. It is quite an art.


Prompt engineering is basically how do you craft your prompt to get the best result from the AI. I am still learning and working through this, but it is quite amazing what you can do. I can give OpenAI the following prompt and it will output the translation in many languages:



prompt = "Translate the following text into 15 different European 
          languages:\n\nText: #{text}"



Output:

1. French: Je voudrais un verre de lait s'il vous plaît
2. Spanish: Me gustaría un vaso de leche por favor
3. German: Ich hätte gerne ein Glas Milch bitte
4. Italian: Vorrei un bicchiere di latte per favore
5. Dutch: Ik zou graag een glas melk alstublieft
6. Portuguese: Eu gostaria de um copo de leite por favor
7. Swedish: Jag skulle vilja ha ett glas mjölk tack
8. Danish: Jeg vil gerne have et glas mælk tak
9. Polish: Poproszę szklankę mleka
10. Finnish: Haluaisin lasi maitoa kiitos
11. Hungarian: Kérnék egy pohár tejet kérem
12. Czech: Chtěl bych sklenici mléka prosím
13. Slovak: Rád by som si dal pohár mlieka prosím
14. Romanian: Aș dori un pahar de lapte vă rog
15. Greek: Θα ήθελα ένα ποτήρι γάλα παρακαλώ


That only took me 1 minute to adjust the code to produce that output. I did have to change the max_tokens as my first attempt cut me off after 5 translations.


Prompt engineering is a fascinating area of working with OpenAI and I will be providing quite a number of articles in the future around this topic.


OpenAI Translation Use Cases

Here are some potential use case that you as a developer can use to take advantage of OpenAI’s language translations in your applications.


  1. Businesses: Companies can use OpenAI Translate to communicate with customers in multiple languages. By providing a more accurate translation of complex documents and presentations, businesses can ensure they are accurately communicating their objectives and plans in any language.


  1. Educational Institutions: Schools, universities and other educational institutions can use OpenAI to quickly translate content from one language to another. This allows educators to rapidly provide students with materials that are easily accessible regardless of the language spoken or written by the student.


  1. Government Agencies: Government agencies can use OpenAI Translate to translate legal documents, regulations, and other important information into different languages for dissemination among foreign countries or international audiences.


  1. Medical Professionals: Medical professionals can utilize OpenAI Translation to communicate with patients who do not speak the same language as them without having to rely on an interpreter or translator outside of their practice or hospital facility. This increased communication accuracy ensures that both doctor and patient are on the same page when it comes to understanding medical procedures, treatments, diagnoses and prescriptions.


Summary

You can use OpenAI’s natural language processing to create relevant prompts for the AI to produce specific outputs. With Open AI it is now possible quickly and accurately process text translations using simple language commands.

You can find out more about our AI Integration Services here

Have fun!