DEV Community

Cover image for How to Generate GPT Output in JSON Format for Ruby developers
Kane Hooper
Kane Hooper

Posted on • Originally published at Medium

How to Generate GPT Output in JSON Format for Ruby developers

I was playing around with OpenAI (GPT-3) today, building a reasonably complicated email parser for a client. I was running into issues working with the AI’s response. Because OpenAI (GPT-3) is based on a natural language model the response is always a string. While you can prompt the AI model on how you would like it to structure this string, it was going to require me to write processing methods to extract the output I needed.

I was looking for a way to easily convert the response into JSON format. I had a bright idea, why don’t I ask ChatGPT how I can get responses in JSON. In this blog post, I’ll show you how to do it quickly and easily with Ruby.

ChatGPT discussion on JSON format

If you are new to OpenAI, I have written an article on how to get started using Ruby.


An intermediate understanding of Ruby.

What we will build

We are going to write a short Ruby script that will extract the sender's first name and a summary of the email. The AI will return the data in JSON so it can be easily be manipulated within the code.


Here are the instructions to implement our code.

  1. Install openai gem
  2. Configure openai with the API key
  3. Create the prompt (instructions) to the AI requesting the response in JSON
  4. Call the createCompletion method and get the JSON
  5. Convert the JSON into a Ruby object

Step 1: Install the openai gem

First things first, you’ll need to install the OpenAI Ruby gem. You can do this by running the following command in your terminal:

gem install ruby-openai

Step 2: Require the openai gem

You will need an OpenAI API key, which you can obtain from this URL:

require 'ruby/openai'

openai = 'YOUR_API_KEY')

Enter fullscreen mode Exit fullscreen mode

Step 3: Create the prompt

Start by adding the text for the email we want the AI model to parse.

Now for the prompt. This is the most important part of working with the OpenAI model. You need to craft the prompt in such a way that it will provide you the results you are specifically looking for.

We’ll ask the AI to extract the first name and summary from the email and return it as JSON.

email = "I would love to purchase one of your new X-225 
         lawn mowers. Do you still have some in stock? If 
         so I will purchase it today. 
         \n\nRegards,\nJimmy Jackson"

prompt = ‘Analyse the following email and return the author’s
          first name and a summary of the email. Your
          response should be in JSON. \n\n”#{email}”’
Enter fullscreen mode Exit fullscreen mode

The AI model has been trained to respond to natural language requests. You will find much better results if you make your requests in a similar manner.

Step 4: Call the completions method

The completions method is how we make our request to the AI model. It takes several parameters. The most important are the model, prompt, temperature and max_tokens.

model: The AI model to use. The most advanced model is the davinci-003 model.

prompt: This is the instruction provided to the AI.

temperature: This is a number between 0 and 1 and sets the ‘creativity’ of the AIs response. We don’t want the AI to be particularly creative, so we will set it quite low for this example.

max_tokens: A token is 4 characters. It sets the total size of the prompt plus the AI response. We can keep it low for this example.

response = openai.completions(
 parameters: {
   model: "text-davinci-003",
   prompt: prompt,
   temperature: 0.3,
   max_tokens: 500,
Enter fullscreen mode Exit fullscreen mode

Step 5. Convert the JSON into a Ruby object

We will use the built-in JSON library in Ruby to convert JSON into a Ruby object. To do this, we use the JSON.parse() method.

jsonResponse = JSON.parse(response['choices'][0]['text'].lstrip)

puts "Email Author: #{jsonResponse["firstName"]}"
puts "Summary: #{jsonResponse["summary"]}"
Enter fullscreen mode Exit fullscreen mode

The output

Email Author: Jimmy
Summary: The author is interested in purchasing one of the
         new X-225 lawn mowers and inquiring about
Enter fullscreen mode Exit fullscreen mode

I found an issue

As I was testing my code I found an issue. The AI model was not providing the same parameter names in the JSON response each time.

Sometimes it set the first parameter to “authorsFirstName”, this of course caused to program to terminate.

I han to reengineer the prompt a few times until I found a wording which provided a consistent response each time.

prompt = "Analyse the following email and return the author's
          first name and a summary of the email. Your
          response should be in JSON format with two
          parameters 'firstName' and 'summary'. 
Enter fullscreen mode Exit fullscreen mode

Effectively I am telling the AI model how to structure the JSON output.

Prompt engineering is definitely an art rather than an exacting science. You have to think like the AI model and adjust the prompt to get the results you are looking for. For an application like an email parser you have to be very explicit about how the data is presented. For something more creative such as providing suggestions for a blog title, you want to allow the model to be more deterministic and ‘creative’.

I think there will be a job 5 years from now known as a Prompt Engineer.


And there you have it! With just a few lines of code, you can easily generate OpenAI (GPT) output in JSON format for your Ruby application.

Good luck, and happy coding!

Kane Hooper is the CEO of reinteractive, the longest running dedicated Ruby on Rails development firm in the world.
You can contact Kane directly for any help with your Ruby on Rails application.

Top comments (0)