Blog

Creating an AI Email Parser Using Ruby and OpenAI (GPT-3)

Team Avatar - Kane Hooper
Kane Hooper
January 19, 2023

You may frequently need to extract crucial information from emails in your role as a developer. This might be done to extract the sender’s identity, a list of the companies referenced in the email, or the email’s overall subject. An email parser is useful since manually extracting this information can be time-consuming and error-prone.


In this tutorial, we will be using Ruby and OpenAI’s GPT-3 to create an email parser that can extract important entities from emails with ease.


If you need an introduction to OpenAI, you can get the basics in this article:


Email Painting

Impressionist oil painting of an email created by OpenAI


Prerequisites

In order to follow this article you need to be familiar with Ruby methods, working with API keys and using Ruby gems.


AI Email Parser

In our example we will create an AI email parser that provides us with the name of the email sender, the company they are from and the theme of the email. This might be useful for a customer service organisation to help them priorities which customers they will respond to.


Of course, some of this information can already be extracted using standard Ruby gems, but the purpose of this example is to show how simple it is to parse an email for the information you need using AI.


First, let’s install the required gems:



gem install ruby-openai


Next, we’ll require the necessary libraries in our Ruby file:



require 'ruby-openai'


Now, let’s define a function that takes in an email as a string and returns the information we need from the AI. We will use OpenAI’s GTP-3 model to generate responses to a prompt asking for the desired information.



require "ruby/openai"


def extract_entities(email)
 client = OpenAI::Client.new(access_token: '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')


prompt = "Please extract the company names, email sender's name, and theme of the following email:\n\n#{email}"


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


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


Parameters

Model: This is the name of the AI model to utilise. Text-davinci-003 is the latest and most advanced model as of this writing.


Prompt: The prompt is the key variable here. This is the instruction provided to OpenAI. The accuracy of your response will be determined by how well you craft your prompt. This is known as prompt engineering.



prompt = "Please extract the company names, email sender's name, and theme of the following email:\n\n#{email}"


temperature: This tells the model how ‘creative’ to be. 0.1 will provide standard responses and is good when your answer is definite. 0.9 will provide more diverse responses from the model and is good for creative tasks.


max_tokens: The maximum size of the prompt plus the response. A token is equivalent to about 4 characters. The maximum limit is 4096. You can use this to limit the size of your response.


Testing our Model

Let’s test our method with an example email. Add the following code at the bottom of your file:



email = “
Dear Kane,

I have a complaint about the service. Tom is causing a lot of problems and I don't like what is happening.

Regards,

Jenny McNamara
Marketing Manager
Big Buys"

extract_entities(email)

# Output:
#   company_names: "Big Buys",
#   email_senders_name: "Jenny McNamara",
#   theme: "Complaint about service"


As you can see, our email parser was able to successfully extract the company names, email sender’s name, and theme of the email.


Prompt Engineering

There is a problem with our output. While it has provided some information about the email, it would be more useful if it gave us a little more detail. This is where prompt engineering comes in. The success of your output is primarily determined by the quality of your prompt.


Prompt engineering is the most complicated and time consuming element of working with OpenAI. In a future article I will deep dive into prompt engineering in a lot more detail. For now, let’s just test what happens if we ask the AI to provide us a summary of the email, rather than the theme.



prompt = "Please extract the company names, email sender's name, and summary of the following email:\n\n#{email}"



# Output:
#   Company name: "Big Buys",
#   Email sender’s name: "Jenny McNamara",
#   Summary: "Complaint about service, Tom causing problems"


Now we have more context about the complaint. You could continue to test different prompts until the AI provides you the exact output you are looking for.


Use Cases

The example provided above could, with some work, be used by customer service teams to Identifying important keywords and phrases in customer service complaints to prioritise responses.


Here are some other use cases for AI email parsing:


  1. Analysing customer feedback to identify common issues and trends.

  2. Automatically categorising and routing emails to the appropriate department or team member.

  3. Extracting relevant information, such as names and contact details, from emails to update customer records.

  4. Summarising the contents of long emails to provide a quick overview for the recipient.

  5. Generating automated responses to common inquiries to improve customer service efficiency.

  6. Identifying potential sales leads in incoming emails.

  7. Translating emails written in foreign languages for improved communication with international customers.


There are many other potential uses for an email parser like this. Play around with your prompt and see what insights you can extract from your emails.


Summary

I hope this tutorial has been helpful in showing you how to create an email parser using Ruby and OpenAI’s GPT-3. With a little bit of creativity and some programming skills, you can automate many tedious tasks and save yourself time.


Overall, using Ruby and OpenAI’s GPT-3 can be a powerful and efficient way to create an email parser that can save you time and effort in extracting important information from emails. I hope this tutorial has been helpful, and happy coding!