Support the ongoing development of Laravel.io →

How to build your next PHP project with a touch of AI

28 Oct, 2022 5 min read

Photo by Google DeepMind on Unsplash

Illustration of a robot.

What is AI (Artificial Intelligence)?

Artificial intelligence (or AI for short) involves using computers to do things that would generally need human intelligence to get done.

This means creating algorithms (or sets of rules) to sort, study, and draw predictions from data.

Just like a tiny human child growing up into a smarter adult, AI systems "learn" by increasing their experience and processing more information.

What is OpenAI?

OpenAI is a research company that is focused on AI and machine learning.

The company was founded by several people, including Jack Hughes (one of the co-founders of Akamai Technologies) and Elon Musk (the founder of Tesla, SpaceX, and several other startups).

OpenAI's goal is to "advance digital intelligence in the way that is most likely to benefit humanity as a whole."

And the best of all? They make it easy for us to use their GPT-3 models in our projects. I will show you how.

The OpenAI API

The OpenAI API can be used to work with their GPT-3 models for tasks involving natural language processing and generation (in over 26 different languages!), as well as code understanding and generation. Each model have their specificity and cost.

For instance, GitHub Copilot (which I can't live without anymore) is built around the same API we'll learn to use!

Many other projects are created with the help of AI. You can see it for yourself on ProductHunt. There's no shortage of them.

I must tell you first, though: the OpenAI API isn't free!

But who cares? They recently lowered their prices, and you can start with $18 of free credit for three months. After that, it's incredibly cheap as long as you use it for testing purposes.

I recommend you to get up to speed by playing with GPT-3 using OpenAI's playground. Create an account, mess in the playground, and join me for the next step!

How to use the OpenAI API PHP client

The best way to learn is to build.

When I started playing with OpenAI, I tried to make an automated job offers aggregator powered by AI.

For this tutorial, we'll make a basic version of it where we extract unstructured data from a given job offer.

Installation

First, create a bare-minimum PHP project:

mkdir openai-test
cd openai-test
touch index.php

Next, install the OpenAI client:

composer require openai-php/client

Then, open the project in your favorite code editor and copy and paste this snippet:

<?php

require 'vendor/autoload.php';

$client = OpenAI::client('YOUR_API_KEY');

You can generate your own API key here..

Usage

  1. We need to copy and paste text from a job offer. It doesn't matter which one. (In the initial project, the crawler did it for me.)
  2. We give instructions to the GPT-3 model: "Extract the requirements for this job offer as a list.";
  3. Then, we call the API using PHP, which is way more convenient than manually making HTTP requests.
$prompt = <<<TEXT
Extract the requirements for this job offer as a list.

"We are seeking a PHP web developer to join our team. The ideal candidate will have experience with PHP, MySQL, HTML, CSS, and JavaScript. They will be responsible for developing and managing web applications and working with a team of developers to create high-quality and innovative software. The salary for this position is negotiable and will be based on experience."
TEXT;

$result = $client->completions()->create([
 'model' => 'text-davinci-002', // The most expensive one, but the best.
 'prompt' => $prompt,
]);

echo $result['choices'][0]['text'];

Run this code, and it will output:

- PHP
- MySQL
- HTML
- CSS
- JavaScript

And what is you ask:

$prompt = <<<TEXT
Extract the salary from this job offer.

"We are seeking a PHP web developer to join our team. The ideal candidate will have experience with PHP, MySQL, HTML, CSS, and JavaScript. They will be responsible for developing and managing web applications and working with a team of developers to create high-quality and innovative software. The salary for this position is negotiable and will be based on experience."
TEXT;

$result = $client->completions()->create([
 'model' => 'text-davinci-002',
 'prompt' => $prompt,
 'max_tokens' => 50, // A token is a basically a word.
]);

The AI will give you this:

The salary for this position is negotiable and will be based on experience.

Now, imagine what you could do. Store this in a database, aggregate the job offers on a website and help thousands of developers!

You could have an entirely automated project. And lazy people like me know these are the best kind of projects!

GPT-3 is the basis for a variety of great products such as Jasper, Tweet Hunter, and many more.

Your imagination is the limit. I hope you will create something unique thanks to the power of AI!

Learn more about the OpenAI API and the OpenAI PHP client on GitHub.

Did you like this article? Check out The web developer life of Benjamin Crozat and follow him on Twitter!

Last updated 6 days ago.

benjamincrozat, nahi3an, mostafa-amine, driesvints liked this article

4
Like this article? Let the author know and give them a clap!
benjamincrozat (Benjamin Crozat) Full-stack Laravel developer passionate about the TALL stack. Obsessed with SEO. Big appetite for UI and UX design.

Other articles you might like

November 4th 2024

Laravel Under The Hood - A Little Bit of Macros

Hello 👋 How often have you wished for a method that doesn't exist on collections or string helpers?...

Read article
February 9th 2024

PHP attributes in Laravel

Say Hi to PHP attributes Attributes offer the ability to add structured, machine-readable metadata...

Read article
October 30th 2024

Create a backup of your Laravel project on Dropbox

How to quickly create a backup of a Laravel project and store it on Dropbox using Spatie Laravel-bac...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.