I’ve long been fascinated by the potential of Artificial Intelligence to enhance productivity, and as a PHP developer, I wanted to explore how I could integrate AI capabilities into my applications. This led me to Neuron AI, a powerful PHP framework designed for building AI-driven agents.
Artificial Intelligence (AI) is rapidly transforming how developers create intelligent applications. AI agents can now perform a wide range of tasks, from automating workflows to generating insights through text analysis.
Recently, I built my first AI agent using Neuron AI and ran it locally with Ollama. This project provided an excellent opportunity to learn how to set up and interact with a local AI model, while simultaneously addressing a practical problem: improving technical content through AI-powered analysis.
💡 Note: The article was written initially for the Neuron AI version. It has been upgraded and reviewed for version 2 of Neuron AI.
In this article, I’ll walk you through the process of building my first AI agent.
Understanding Neuron AI and Ollama
Before diving into the implementation, let's take a moment to understand the key technologies powering this project:
- Neuron AI: A PHP framework that streamlines AI integration, reducing the need to manage complex APIs.
- Ollama: A lightweight model runner for executing AI locally, ensuring privacy and control.
- Gemma: An open family of lightweight LLMs from Google DeepMind, optimized for efficiency, adaptability, and transparency.
- PHP: A widely used server-side scripting language for dynamic web applications, also versatile for automation, data processing, and system tools.
Why Neuron AI (2)?
Neuron AI is a lightweight, developer-friendly PHP framework for adding AI to applications. I chose it for my projects because it offers:
- Simplicity: hides complex AI interactions, making integration straightforward.
- Flexibility: works with multiple AI providers, enabling easy model switching.
- Extensibility: supports custom AI agents for tailored use cases.
- Local execution: Integrates with tools like Ollama for private, cloud-free AI.
Setting up Neuron AI
Neuron AI is available as an open-source PHP package and can be easily installed with Composer:
composer require neuron-core/neuron-ai
💡 Note: If you come across tutorials using
composer require inspector-apm/neuron-ai
, be aware that inspector-apm was the old organization for Neuron AI. The correct and updated package is:neuron-core/neuron-ai
This sets up all the necessary dependencies, allowing you to start building AI-powered agents immediately.
Creating an AI Agent
🎯 The goal: As a PHP developer, I aimed to create an AI agent that reviews technical articles and provides suggestions for improvement.
Below is the PHP code that defines my AI agent (creating my-agent.php
file):
<?php
use NeuronAI\Agent;
use NeuronAI\Chat\Messages\UserMessage;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Ollama\Ollama;
use NeuronAI\SystemPrompt;
require './vendor/autoload.php';
class ReviewerAgent extends Agent
{
public function provider(): AIProviderInterface
{
return new Ollama(
url: "http://localhost:11434/api",
model: "gemma3:4b"
);
}
public function instructions(): string
{
$prompt = "Review and rewrite the following article in clear, grammatically correct, and professional English. Improve readability, correct any errors, and ensure the tone remains neutral and coherent. Do not change the meaning of the text. Output only the corrected and polished version of the article.";
return (string) new SystemPrompt(
background: [
$prompt
],
)
;
}
}
$reviewerAgent = ReviewerAgent::make();
$articleMarkdownFile = './article-1.md';
try {
$response = $reviewerAgent->chat(
new UserMessage("This is the article: " . file_get_contents($articleMarkdownFile))
);
echo $response->getContent();
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
echo 'Error: Unable to communicate with the AI service. Please ensure Ollama is running.' . PHP_EOL;
}
How It Works
I created the ReviewerAgent
class extending Agent
.
In the ReviewerAgent
class, I implemented two methods for:
- Defining the provider: The
provider()
method specifies that the AI agent will use Ollama as the backend model, running locally. The nice thing here is that you can replace the provider if you prefer to run a remote agent with OpenAI or some other platform. - Setting instructions: the
instructions()
method defines the role of the AI—analyzing technical articles and providing suggestions. So here you can provide more context to your agent to act with specific skills/roles/tone of voice.
Once I created the agent class, I started using it:
- Creating the agent instance via
ReviewerAgent::make()
. - Interacting with the agent, using the
chat()
method to send messages and get responses. - Using
try
andcatch
, if the local Ollama service is unavailable, the script may fail to connect to the AI model. To handle potential issues, I added basic error handling in the script.
Running the AI agent
Remember: to connect with the local AI provided, ensure Ollama is installed and running at
http://localhost:11434
.
Then, execute the PHP script:
php my-agent.php
The AI will respond to queries and analyze the $articleMarkdownFile
file, providing insights into improving the article’s clarity and effectiveness.
Final Thoughts
Building this AI agent with Neuron AI and Ollama was a smooth and rewarding experience. The simplicity of integrating an AI model in PHP opens up many possibilities for content analysis, automation, and beyond.
To ensure long-term effectiveness, consider the following best practices for maintaining and updating your AI agent:
- Regularly update dependencies: keep Neuron AI, Ollama, and other dependencies updated to benefit from performance improvements and security patches.
- Monitor AI responses: evaluate the AI’s responses periodically to ensure they align with the intended use case and adjust the model or prompts as needed.
- Optimize performance: If the AI agent starts slowing down, consider optimizing input prompts, using caching mechanisms, or experimenting with different AI models.
- Implement logging and error handling: maintain logs of AI interactions to troubleshoot issues and improve accuracy over time.
- Enhance security: validate and sanitize inputs to prevent unintended interactions or potential exploits.
If you’re interested in AI-driven PHP applications, I highly recommend experimenting with Neuron AI!
YouTube live coding session
Top comments (2)
Updated for Neuron AI version 2
amazing bro