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.
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 here.
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.
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}"
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,
}
)
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"]}"
The Output
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 hand to re-engineer 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'. \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!