Skip to content
Logo Theodo

Create a PHP AI agent with LLPhant (powered by OpenAI)

Thomas Hercule8 min read

Automation is the lifeblood of modern tech. Whether it is managing e-commerce websites, providing customer support, optimizing sales, or complex searches, the power of automation can not be overstated. Let’s explore how it can transform complex tasks with the magic of OpenAI on your PHP project.

What tool to use to automate PHP tasks with OpenAI

AutoPHP is a tool from the open-source PHP generative AI framework LLphant 🐘 (by Theodo). AutoPHP fine-tune GPT engine to generate high-quality results from a given objective. To do so, it allows GPT to create tasks for himself and use PHP functions. AutoPHP is greatly inspired by AutoGPT but for PHP applications. While it is still in development and not as good as AutoGPT at the moment, it still shows great potential and can be used right now for simple tasks.

How does a PHP AI agent work?

AutoPHP uses GPT, so it takes sentences as input, more precisely as an objective.

Step 0: Create a first plan based on the objective and available tools / Step 1: Prioritize the next task / Step 2: Execution Agent: a. Choose the right tool / b. Execute task / c. Extract useful information. / Step 3: Check if we have have completed the objective / Step 4: Update the plan / Final step: return the result or make the final action

AutoPHP simple Example

GPT 3.5 Turbo is used for this example.

Currently, AutoPHP is working well for simple tasks that require some PHP functions. For example, if we want to find the name and age of the French player who scored the most tries during the 2023 Rugby World Cup.

Prerequisites : LLPhant composer require theodo-group/llphant

First, import the libraries:

<?php

use LLPhant\Experimental\Agent\AutoPHP;

require_once 'vendor/autoload.php';

Then describe the objective

$objective = 'Find the name and age of the french player who scored the most tries during the 2023 rugby world cup.';

Add some PHP functions to the agent so it can use them

1. Describe functions that could be called in addition of the prompt / 2. OpenAI chooses if any function should be called and provides values for the parameters / 3. LLPhant handles the response and calls the function

In this example, we are going to add SerpApiSearch which is a tool from the LLPhant framework that performs a Google search using the SerpApi and returns the title and snippet of the first results of the Google search.

$searchApi = new SerpApiSearch('VERY_REAL_API_KEY');
$searchFunction = FunctionBuilder::buildFunctionInfo($searchApi, 'googleSearch');

Finally, create an AutoPHP instance with the objective and the available functions, and run it!

// use LLPhant\Experimental\Agent\AutoPHP
$AutoPHP = new AutoPHP($objective, [$searchFunction]);
$response = $AutoPHP->run();

AutoPHP’s response

First, he defines a list of tasks:

List of tasks: / TODO 1. Find the french player who scored the most trials during the 2023 rugby world cup (This task involves sear ching for data on the French player who achieved the most &#x27;trials&#x27; during the 2023 Rugby World Cup. The output should include the player&#x27;s name.) / TODO 2. Find the age of this player (Once the name of the player is known, find out this player&#x27;s age in 2023.)

Then he tries to do the first one (here using the SerpApi):

ExecutionTaskAgent. Prompt: You are part of a big project. You need to perform the following task: This task inv olves searching for data on the French player who achieved the most &#x27;trials&#x27; during the 2823 Rugby World Cup. The out put should include the player&#x27;s name. If you have enough information, answer with only the relevant information relat ed to the task. Your answer: Executing tool SerpApi French player with most trials Rugby World Cup 2023. / Results from SerpApi: Discover the most high-profile French players Dupont, Ntamack, Fickou, meet ten of the players in the French squad ahead of the 2023 Rugby World Cup.Statistics Rugby World Cup 2023 France Tournament Stats; Mos t Tries. Will Jordan. 8 Tries; Most Conversions. Thomas Ramos. 21 Conversions; Runs. Ardie Savea. 82 Runs.France: R ugby World Cun 2023 review Thomas Ramos is RWC 2023&#x27;s leading points scopen by a distanco. so far He has proved ...

List of tasks: / DONE 1. Find the french player who scored the most trials during the 2023 rugby world cup (This task involves sear ching for data on the French player who achieved the most &#x27;trials&#x27; during the 2023 Rugby World Cup. The output should include the player&#x27;s name.) / TODO 2. Find the age of this player (Once the name of the player is known, find out this player&#x27;s age in 2023.)

It finds the player and updates the list of tasks:

List of tasks: DONE 1. Find the french player who scored the most trials during the 2023 rugby world cup (This task involves sear ching for data on the French player who achieved the most &#x27;trials&#x27; during the 2023 Rugby World Cup. The output should include the player&#x27;s name.) - The French player who achieved the most &#x27;trials&#x27; during the 2023 Rugby World Cup is Da mian Penaud with 12 tries. / DOING 2. Find the age of this player (Once the name of the player is known, find out this player&#x27;s age in 2023.) / TODO 3. Find the age of Damian Penaud (Search for the birth date of the french rugby player Damian Penaud and calculate his age in 2023.)

It made one more call to the SerpApi, which was enough to get the age directly, so it finished.

List of tasks: DONE 1. Find the french player who scored the most trials during the 2023 rugby world cup (This task involves sea rching for data on the French player who achieved the most &#x27;trials&#x27; during the 2023 Rugby World Cup. The output shou ld include the player&#x27;s name.) - The French player who achieved the most &#x27;trials&#x27; during the 2023 Rugby World Cup is Damian Penaud with 12 tries. / DONE 2. Find the age of this player (Once the name of the player is known, find out this player&#x27;s age in 2023.) - Damian Penaud was born on September 25, 1996. Therefore, he will be 27 years old in 2023. / TODO 3. Find the age of Damian Penaud (Search for the birth date of the french rugby player Damian Penaud and cal culate his age in 2023.) / Success! Result: The objective has been achieved. The name of the French player who scored the most tries during the 2023 rugby world cup is Damian Penaud and he will be 27 years old in 2023.

Now let’s try with a more complex example.

AutoPHP complex Example

Let’s put AutoPHP to the test with a harder example (GPT 4 engine is used).

Scenario: Automating Content Aggregation for a News Website

Imagine you’re running a news website about generative AI that aims to provide readers with up-to-the-minute news stories from a diverse range of sources. The challenge is not only to gather articles but also to categorize them, summarize their content, and translate them into English if needed. All of this needs to be done efficiently and consistently.

Building the AutoPHP Function

To achieve this, we’re going to use AutoPHP. We’ll define a specific objective with some instructions to help him. The objective :

You have to recover and process data for a news website focused on generative AI. Select FIVE articles url about generative AI using google. Then you HAVE TO get the content of the web page in order to create a summary the article (if you can t get the content from the url, you can use the meta description). Then categorize the article by topics such as research, applications, and breakthroughs. Give a formatted response with : title, url, summary, topics.

We will also provide him with the tools he needs to complete the task :

<?php

use LLPhant\Chat\FunctionInfo\FunctionBuilder;
use LLPhant\Experimental\Agent\AutoPHP;
use LLPhant\Tool\SerpApiSearch;
use LLPhant\Tool\WebPageTextGetter;

require_once 'vendor/autoload.php';

$objective = '
You have to recover and process data for a news website focused on generative AI. Select FIVE articles url about generative AI using google. Then you HAVE TO get the content of the web page in order to create a summary the article (if you can t get the content from the url, you can use the meta description). Then categorize the article by topics such as research, applications, and breakthroughs. Give a formatted response with : title, url, summary, topics.
';

$verbose = true;
$getWebPageText = new WebPageTextGetter($verbose);
$getMultipleWebPageText = FunctionBuilder::buildFunctionInfo($getWebPageText, 'getMultipleWebPageText');

$searchApi = new SerpApiSearch('VERY_REAL_API_KEY', $verbose);
$search = FunctionBuilder::buildFunctionInfo($searchApi, 'googleSearch');

$AutoPHP = new AutoPHP($objective, [$getMultipleWebPageText, $search], $verbose);
$response = $AutoPHP->run();

AutoPHP Execution

As always, AutoPHP starts by defining a list of tasks:

It searches for articles about generative AI using the SerpApi, then it gets the content of the web pages using the “WebPageTextGetter” tool :

Finnaly, it returns the result:

Unfortunately, all not articles have the required informations. All the articles should have a URL, a title, a summary and topics.

AutoPHP is in active development, ensuring it will handle this task within a few months, possibly sooner if you’re interested in contributing to LLPhant! Don’t hesitate to reach out; I’d be delighted to assist with your first contribution.

Conclusion: The Future of Task Automation with AutoPHP

In conclusion, AutoPHP will soon be a game-changing tool that empowers your tech startup with task automation, powered by OpenAI.

AutoPHP streamlines complex tasks, making them more accessible and efficient. It is time to explore the capabilities of AutoPHP and embrace the future of task automation.

Liked this article?