Introducing Neuron AI: Create Full-Featured AI Agents in PHP
A complete open-source toolkit for AI development in PHP. Create RAG systems to use the powerful capabilities of LLMs on your own private data.
Join the DZone community and get the full member experience.
Join For FreeIn the last few months, I have worked hard to push the integration of AI agents into my SaaS product to a higher level. It was a very long journey, starting more than a year ago with the first experiments. I have to say that understanding all the moving parts of an AI-driven system was far from easy.
As a PHP developer, I struggled a lot, mainly because the PHP ecosystem for developing this kind of “Agentic” features into existing applications is not as advanced and rich as it is in other technologies.
Python and Javascript are “driving the bus,” and obviously, other developers working with different programming languages are creating their artifacts to get the opportunity to start their journeys, too.
Six months ago, when I started working on this chapter, I took into consideration some packages that were getting attention, like LLPhant or Prism. There was a lot of development behind these packages, and they have already implemented a lot of things. But for my needs, they have too serious weaknesses to consider building the foundation of this chapter for my business in these libraries. Prism is exclusively for Laravel, so you are locked in. LLPhant has a lot of different classes and looks more like a library than a framework. It also lacks features like Memory, chat history, and observability.
Yet, the opportunity is too great to pass up.
The Unsustainable Path
From the beginning, I realized that I could not look at other programming languages. It is not sustainable, and I am sure that is the same for most developers who specialize in a particular technology.
If you have a PHP application, you can’t implement an agent in Javascript or Python because they need your application data and context to generate their magic (authentication, authorizations, database connection, cache, etc.). Transferring this data and context to an external entity written in another language leads to a lot of code duplication or technical constraints that are not sustainable.
I’m curious to see if it was just me struggling, or if these feelings are the same as other PHP developers.
I started believing that the artifact I created for myself was really, really good, at least from my perspective.
So, I decided to release this internal tool as an open-source project: Neuron AI, an open-source framework for integrating full-featured AI Agents into your existing PHP application.
Why I Decided to Make It Open Source
The journey I would like to explore is inspired by LangChain, which gives people the power to create Agentic entities into PHP applications with a complete open-source toolkit and provides support and long-term visibility.
At the same time, I believe it can really help PHP developers “jump into the AI bus” with stronger foundations. It seemed like a clear opportunity. I hope it can give you the answers you are looking for to continue building great software with your preferred programming language.
Here is a how the project is organized:
Key Concepts
Neuron AI is designed to provide you with a complete toolkit to implement AI driven applications, making it easy to integrate into your existing system.
Most Neuron AI framework components do not implement active constructors, they just provide you features to give agents the behavior and ability you need. The two most important entities, Agent and RAG, are designed to be extended to create your specific implementation. They are rarely used as standalone objects.
This ensures the portability of your agent implementation because all the moving parts are encapsulated into a single entity that you can just run wherever you want in your application or ship as standalone packages.
namespace App\Agents;
use NeuronAI\Agent;
use NeuronAI\Providers\Anthropic;
use NeuronAI\Tools\Tool;
use NeuronAI\Tools\ToolProperty;
class SEOAgent extends Agent
{
public function provider(): AIProviderInterface
{
// return an AI provider instance (Anthropic, OpenAI, Mistral, etc.)
return new Anthropic(
key: 'ANTHROPIC_API_KEY',
model: 'ANTHROPIC_MODEL',
);
}
public function instructions()
{
return "Act as an expert of SEO (Search Engine Optimization). ".
"Your role is to analyze a text and provide suggestions on how the content can be improved to better rank on Google search.";
}
public function tools(): array
{
return [
Tool::make(
"get_file_content",
"Use the url to get the content in plain text."
)->addProperty(
new ToolProperty(
name: 'url',
type: 'string',
description: 'The URL of the article you want to analyze.',
required: true
)
)->setCallable(function (string $url) {
return file_get_contents($url);
})
];
}
}
Talk to the Agent:
use NeuronAI\Chat\Messages\UserMessage;
$response = SEOAgent::make()
->chat(
new UserMessage("Give me your feedback about this article: https://inspector.dev/introduction-to-neuron-ai-create-full-featured-ai-agents-in-php/")
);
echo $response->getContent();
// It seems like a good job has been done on the article,
// however I can give you some tips to improve SEO:...
Package Dependencies
We intentionally decided to build Neuron as free as possible from external dependencies. The package ships with just one dependency: guzzlehttp/guzzle: ^7.0
.
Without bringing dozens of dependencies inside your application, you do not risk being locked out of Neuron if you need to upgrade your current architecture, such as the web application framework (Laravel, Symfony, CodeIgniter, or any other framework), to a newer version or integrate new dependencies in your project without conflicts.
Based on our experience, a bad dependency chain could be a very unpleasant surprise when it is too late. You have already spent a lot of effort implementing your AI interactions, and suddenly, it has become a bottleneck because the dependencies make it impossible to upgrade and evolve the rest of your system.
We want you to know that with Neuron AI, it’s not the case.
Extensibility
Every component of the framework depends on its own interface. This guarantees you the ability to create new concrete implementations of every component to interact with external systems and pass them to your agents with confidence.
In the components documentation you will find the dedicated section of how to implement a new one, basically extending its interface.
Do you want to implement a new Vector Store or an Embeddings Provider? Follow the documentation and feel free to send us a PR with your new module. We will be happy to integrate it into the framework to ensure first-party support and maintenance.
AI Agents Observability
Neuron is designed with a built-in system to make your agent and RAG implementations observable. You can start monitoring your agents' activities and performance with just one line of code. Take a look at the dedicated section in the observability section.
Neuron AI Toolkit
To create a fully functional AI agent, you have to make several things work together. Apart from the LLM, you need to constantly process data and create and store embeddings to feed your agent with fresh information.
The project aims to provide easy-to-implement and extend solutions into all of these areas.
Conclusion
If your customers are pushing you to implement AI features into your application, try Neuron. It takes just a few lines of code to implement your first full-featured agent.
Thank you for reading this article. I invite you to contact me with any questions or curiosities or just to give me your feedback. If you think this tool could be useful to other PHP developers, please share it on your blog, social media, and YouTube channels.
Best,
Valerio
Published at DZone with permission of Valerio Barbera. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments