PHPackages                             jaxwilko/ai-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jaxwilko/ai-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jaxwilko/ai-php
===============

A package providing base php functions powered by ai

10PHP

Since Jul 4Pushed 12mo agoCompare

[ Source](https://github.com/jaxwilko/ai-php)[ Packagist](https://packagist.org/packages/jaxwilko/ai-php)[ RSS](/packages/jaxwilko-ai-php/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

AiPhp
=====

[](#aiphp)

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
----------

[](#-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
--------------

[](#-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
---------

[](#-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
--------------

[](#-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?
------

[](#-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
--------------

[](#️-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
-------------

[](#-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:

[](#-recompile-with)

```
php build.php
```

🐛 Debugging
-----------

[](#-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

[](#-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.
