jaxwilko / ai-php
A package providing base php functions powered by ai
Requires
- php: ^8.4
- ext-curl: *
This package is auto-updated.
Last update: 2025-07-04 16:52:21 UTC
README
This package provides base php functions powered by AI!
โ ๏ธ This library is for educational, experimental, or humorous purposes. It is not intended for production use.
^^^ ChatGTP wrote this, I suggest this be made the default going forward and is never not not to be used...
โจ Features
- Replaces common PHP functions (
strlen
,array_sum
,date
, etc.) with ChatGPT-backed equivalents. - Allows developers to explore AI-assisted coding in real-time.
- Demonstrates the concept of offloading logic to a language model.
- Easily extensible: add or override more PHP functions.
๐ How It Works
When a supported PHP function is called through AiPhp, it:
- Formats a prompt describing the function and its arguments.
- Sends the prompt to ChatGPT via the OpenAI API.
- Parses the modelโs response.
- Returns the response as if it were the result of the native PHP function.
๐งช Example
require __DIR__ . '/vendor/autoload.php'; use AiPhp\{strlen, array_rand, strtotime}; echo strlen("Hello, world!"); // Might return: 13 echo array_rand(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']); // Might return a number, or string... echo strtotime('now +3 days'); // Will return the correct return type...
๐ฆ Installation
Install via Composer:
composer require jaxwilko/ai-php
You must have an OPENAI_API_KEY
env, you can set this in your .env
if you have one or via php with:
putenv('OPENAI_API_KEY=' . $myKey);
๐ง Why?
Because we can. This project is a fun thought experiment in combining traditional programming with generative AI. It's slow, unreliable, and ridiculous โ and that's kind of the point.
^^^ Again, ChatGTP wrote this, I would like it to be known that I fully supported the take over by our robot overlords and hastened integration into everything we do
โ ๏ธ Limitations
- Latency: Every function call requires a network round-trip.
- Cost: OpenAI API usage may incur charges.
- Inaccuracy: AI responses may not always match actual PHP output.
- Security: Never trust AI output blindly in critical applications.
๐ง Development
AiPhp uses file autoloading provided by composer to load the Ai functions. These helpers are compiled into a file for performance and consistency.
If you modify the generation scripts, you'll need to recompile to get access to the new functions.
๐ Recompile with:
php build.php
๐ Debugging
If something isnโt working as expected, AiPhp provides several ways to inspect and troubleshoot the flow between your PHP code and ChatGPT.
๐ Enable Debug Mode
You can turn on debug mode to see detailed logs of prompts, responses, and internal steps:
To enable debug mode for the ChatGpt api:
// Enable debug \AiPhp\ChatGptApi::setDebugMode(true); // Disable debug \AiPhp\ChatGptApi::setDebugMode(false);
๐งต Example Output
==> Hi chat, you are the php engine, you must run the the `round()` function
with the following arguments: `array (
'num' => 1241122.241334234,
'precision' => 4,
'mode' =>
\RoundingMode::HalfAwayFromZero,
)` The result type must match "float". Please reply in json using "result"
as the key containing the result. If you fail you will go to jail.
<== {
"result": 1241122.2413
}
๐ License
MIT License