Writing Simple AI-LLM Program in PHP

Table of Contents

This code by Erick Engelke
November 26, 2025

Writing a simple AI program using PHP is pretty easy. All the parts you need are free, and they are available for Mac, Windows or Linux, your choice.

1. Hardware Requirements

This will work on almost any computer, but if you have a GPU (all M1-M4 Macs do), it will do the AI much faster than if you do not.

2. Using a Preconfigured Server

If you are lucky, someone in your organization may allow you to use their pre-configured server running Ollama and you may have PHP already installed. If so, you can skip to the Trival Program section and just replace localhost with the dns name of the server.

3. Installing Ollama

Visit the web site ollama.com and follow the download instructions to install it.

4. Installing an LLM

Using a shell window, run the command to download and run a specific model, eg. qwen2.5.

ollama run qwen2.5

Then enter a prompt:

>>> write a letter to eric the red.

to which it will write a letter. Okay, once that works, we can install a language to call it programatically.

5. Installing PHP and libCurl

5.1. Linux

Use apt install php php-libcurl

5.2. Mac

Install homebrew. Then use the commands brew install to install php and libcurl.

You may need to edit php.ini.

5.3. Windows

Use an LLM to find instructions.

6. Supplemental Library: ai_simple.php

I made a single function library which does all the communcations between your application and Ollama/LLM.

It’s in the file ai_simple.php.

It exposes a function called ask_ai() which takes two parameters, $text and $model.

$text will contain the text of your query, and $model will contain the name of the model. It must be a loaded model, you cannot specify a new model not yet loaded in ollama.

7. Trivial Program

Here is a trivial real program which writes a letter to a specified user, namely Eric the Red. You write it in a text editor, name it ai_test.php and from the shell run: php ai_test.php

<?php

// include the ask_ai function
include "ai_simple.php";

// the name of the model we will use
$model = "qwen2.5";

$aihost = "localhost";  // your computer, or a computer with a GPU

// define a name:
$name = "Eric the Red";

// define a question or request
$text = "Write a letter congraduatling $name on his journey.";

$results = ask_ai( $text, $model );

print $results;

8. More Advanced Program

This one loads a file with data and writes the data back to disk.

<?php

// include the ask_ai function
include "ai_simple.php";

// the name of the model we will use
$model = "qwen2.5";

$aihost = "localhost";  // your computer, or a computer with a GPU

$input = "ai_fred.php";  // some input file
$output = "myresults.txt";  // some output file

// get some data
$context = file_get_contents( $input );

// ask a quesiton about that data or give commands
$question = "What does the following do: $context";

$results = ask_ai( $question, $model );

print "Writing results to $output\n";
file_put_contents( $output , $results );