DEV Community

Cover image for Introducing Neuron AI – Create full featured AI Agents in PHP
Valerio for Inspector.dev

Posted on • Originally published at inspector.dev

Introducing Neuron AI – Create full featured AI Agents in PHP

In the last few months I heavily worked to push the AI agents integration into my SaaS product to a higher level. It was a very long journey, started more than a year ago with the first experiments. I had 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 to develop this kind of "Agentic" features into existing applications it’s 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 journey too.

Six months ago when I started working on this chapter I took in 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 on these libraries. Prism is exclusively for Laravel, so you are locked in, and LLPhant has a lot of different classes and looks more like a library than a framework. It also lacks features like Memory, chat history, 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 constraint 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 in my perspective.

So I decided to release this internal tool as an open source project: Neuron AI, open source Framework to integrate full featured AI Agents into your existing PHP application.

https://docs.neuron-ai.dev

Why I decided to make it open source

The journey I would like to explore is inspired by LangChain, giving people the power to create Agentic entities into PHP applications, with a complete open source toolkit. And provide support and long term visibility thanks to the professional monitoring and debugging service powered by Inspector.dev

At the same time I believe it can really help PHP developers “jump into the AI bus” with stronger foundations.

It seemed to me a clear opportunity. I hope it can get you the answers you are looking for to continue to build great software with your preferred programming language.

Here is a how the project is organized:

Neuron AI PHP Framework

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.

Here is an example a SEOAgent:

namespace App\Agents;

use NeuronAI\Agent;
use NeuronAI\Providers\Anthropic;
use NeuronAI\Tools\Tool;

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 ToolPropertry(
                    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);
            })
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

Talk to the agent:

use NeuronAI\Chat\Messages\UserMessage;

$response = SEOAgent::make($user)
    ->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:...
Enter fullscreen mode Exit fullscreen mode

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, like 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 to implement 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 them as a part of 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, 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.

What's next after the launch

At Inspector we embrace organic growth. So we would like to start helping developers create their first agents to discover edge cases, new needs, and obviously bug fix.

We are already at work with our internal user base of +10K PHP developers that are starting their Agents right now and a lot of exciting things are emerging yet.

Real use cases will be the driver of the framework evolution, so don’t hesitate to contact us if you want to grab some knowledge from our experience. We are here to help.

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 for any questions, curiosities, or just to give me your feedback. And if you think this tool could be useful to others PHP developers, please share it on your blog, social media and YouTube channels.

Learn more about Inspector on the website: https://inspector.dev

Best,

Valerio

Top comments (0)