DEV Community

Kane Hooper
Kane Hooper

Posted on

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.

Image description

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

Prerequisites

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.

If you are interested in creating an email parser with OpenAI I have written an article about that:

Instructions

Here are the instructions to implement our code.

Install openai gem
Configure openai with the API key
Create the prompt (instructions) to the AI requesting the response in JSON
Call the createCompletion method and get the JSON
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: https://openai.com/api/

```require 'ruby/openai’

openai = OpenAI::Client.new(access_token: 'YOUR_API_KEY')




###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.




```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}”’```



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.



Enter fullscreen mode Exit fullscreen mode

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




##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.



Enter fullscreen mode Exit fullscreen mode

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

puts "Email Author: #{jsonResponse["firstName"]}"
puts "Summary: #{jsonResponse["summary"]}"



##The output



Enter fullscreen mode Exit fullscreen mode

Email Author: Jimmy
Summary: The author is interested in purchasing one of the new X-225 lawn mowers and inquiring about availability.




##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.



Enter fullscreen mode Exit fullscreen mode

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'. \n\n'#{email}'"



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. 

##Summary

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.

www.reinteractive.com

kane.hooper@reinteractive.com
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
ahmednadar profile image
Ahmed Nadar

Thanks for the post Kane.

I will try it for sure. I'm like you, love Ruby, Rails and have a huge interest in AI. Since GPT3 and I'm in love and learning as much as I can.

I think the post format didn't render that way it intended to.
Do you have a public repo for your example?

Nice website and services at reinteractive.com/About-us. Are you based on Australia or the US?

Collapse
 
kanehooper profile image
Kane Hooper

Hi Ahmed,

I will fix the post.

I am working on some more advanced Jason output at the moment. If you have a specific use case let me know and I will put some code together for you.

In terms of the business we are based on Australia but look after clients in US and around the world.

Collapse
 
ahmednadar profile image
Ahmed Nadar

At the moment I'm digging into GPT using Ruby and Rails. I like to learn by reading more code. I'd appreciate if you share some of your repos.

In this example. How did you share email content? As a long "text", or external text file? Or formatted JSON?

Collapse
 
ahmednadar profile image
Ahmed Nadar

One more thing.
You wrote "I think there will be a job 5 years from now known as a Prompt Engineer."
I wish I could agree with you. It already exist.

I watched a Professional Prompt Engineer talked about her job and how challenging and fun it is.
My goal to be a one that powered by Ruby.

Collapse
 
kanehooper profile image
Kane Hooper

Ahmed, yeah you are right. My prediction was a little off on this one.