DEV Community

Kane Hooper
Kane Hooper

Posted on • Originally published at reinteractive.com

Creating an AI email parser using Ruby and OpenAI (GPT-3)

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 article, we’ll show you how to build an email parser that can easily extract crucial things from emails using Ruby and OpenAI’s GPT-3.

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

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'

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

require "ruby/openai"

def extract_entities(email)
 client = OpenAI::Client.new(access_token: 'YOUR_API_KEY')

 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
Enter fullscreen mode Exit fullscreen mode

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 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)
Enter fullscreen mode Exit fullscreen mode
# Output:
# Company name: "Big Buys",
# Email senders name: "Jenny McNamara",
# Theme: "Complaint about service"
Enter fullscreen mode Exit fullscreen mode

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 senders name: "Jenny McNamara",
# Summary: "Complaint about service, Tom causing problems"
Enter fullscreen mode Exit fullscreen mode

We now have more context about the complaint. You could continue to test and trail 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 prioritize responses.

Here are some other use cases for AI email parsing:

  1. Analyzing customer feedback to identify common issues and trends.
  2. Automatically categorizing 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. Summarizing 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 numerous other applications for an email parser like this. Experiment with the prompt to see what insights you can glean from your emails.

Summary

I hope you found this tutorial on how to create an email parser with Ruby and OpenAI’s GPT-3 useful. Many tedious tasks can be automated and time saved with a little creativity and programming skills.

Let’s see what creative ideas you can come up with using AI email parsing.


Kane Hooper is the CEO of reinteractive, a dedicated Ruby on Rails development firm.

You can contact Kane directly for any help with your Ruby on Rails application.

www.reinteractive.com
kane.hooper@reinteractive.com

Top comments (4)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.