Published:March 20, 2023
Updated: April 5, 2023
Views: 3,230
Have you ever tried parsing the body of an email using traditional tools?
It can be a surprisingly tough task! Emails come in all shapes and sizes, with complex structures and various formatting elements such as headers, footers, signatures, images, and even those sneaky nested threads. Plus, every writer has their unique tone and style, which adds to the challenge of accurately identifying and extracting the information you're looking for.
But don't worry! We've got an exciting solution for you - integrating OpenAI's ChatGPT-4 with Laravel. This powerful combination will make the process of fishing out valuable nuggets from email bodies a breeze, turning it into a seamless, efficient, and enjoyable experience.
Prerequisites
First, open your terminal and navigate to your desired directory. Run the following command to install a new Laravel project:
composer create-project --prefer-dist laravel/laravel email-parser
After the installation is complete, cd into the email-parser directory:
cd email-parser
In order to use the Laravel HTTP Client we first need to install Guzzle with the following command:
composer require guzzlehttp/guzzle
Next, let's install the OpenAI PHP client to make interacting with the ChatGPT-4 API more convenient:
composer require openai-php/client
To store the API key and other configuration details, add the following lines to the .env
file in the root directory of your project:
OPENAI_KEY=your_openai_api_key
We will also update our config/services.php
to include the new API Key:
<?php
return [
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
// Here is our OpenAI key:
'open_ai' => env('OPEN_AI_KEY'),
];
Let's create the main controller: EmailParserController. To do this, run the following command:
php artisan make:controller EmailParserController --invokable
Now with our controller in place here is the code that will take an email HTML and extract only the body of the email:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use OpenAI;
class EmailParserController extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
{
$validated = $request->validate([
'email' => 'required',
]);
$prompt = <<<'EOT'
You are an email parsing expert.
Parse the body of the email without the replies, filler text, or any other signatures.
Don\'t add any text in the beginning or in the end, your reply should only consist of the extract text.
Do not modify the original email reply in any way, don't add or remove any text.
EOT;
$client = OpenAI::client(config('services.open_ai'));
$response = $client->chat()->create([
'model' => 'gpt-4',
'messages' => [
[
'role' => 'system',
'content' => Str::squish($prompt),
],
[
'role' => 'user',
'content' => sprintf('Parse the body of this email without the replies, filler text, or any other signatures: %s', $validated['email']),
],
],
]);
return $response->choices[0]->message->content;
}
}
Okay now let's break down what's happening in this controller:
__invoke
method is called first we validate the request $prompt
variable. This prompt contains instructions for parsing the body of an email, specifically to extract the text without replies, filler text, or any other signatures. It also specifies that the extracted text should remain unmodified.config('services.open_ai')
.chat()
method. The model parameter specifies which model to use (in this case, GPT-4). The messages parameter accepts an array of messages with specific roles (system and user) and their associated content.$request->input('email')
) and asks GPT-4 to parse it based on the instructions provided by the system role messageFinally, add a new route to your routes/web.php
file to access the email parser through a POST request:
<?php
use App\Http\Controllers\EmailParserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::post('/parse', EmailParserController::class);
Please note that since the parser is designed to process raw email text (including HTML), the text from the provided HTML emails may require some clean-up to ensure the parser accurately extracts the relevant portions. This might include eliminating HTML tags or unnecessary elements before passing the text content to the parser. Consider using a package like Html Purifier
Done!
Do you have a Laravel project in mind? Learn more about our expertise in Laravel development.
That's it! Now you have a fully functional email parser utilizing the power of ChatGPT-4 and Laravel. You can test it out by sending an email text through a POST request to /parse
.
As always, don't forget to optimize and enhance the code to fit your specific needs.
Happy parsing!
Stay up to date
Be updated with all news, products and tips we share!