jaxwilko/ai-php

A package providing base php functions powered by ai

dev-main 2025-07-04 16:50 UTC

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:

  1. Formats a prompt describing the function and its arguments.
  2. Sends the prompt to ChatGPT via the OpenAI API.
  3. Parses the modelโ€™s response.
  4. 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