Building Real Agentic AI in PHP with Neuron AI, Gemini, and Tools

In this article, I have discussed how to create chat agent for your php website. May be your site based on any frameworks or cms like wordpress, laravel or drupal etc. Let’s focus on code how to implement …..

First. create a folder and hit this below command into terminal.

composer require neuron-core/neuron-ai

Now, create some files for this …. into your local pc or computer. After this using chatgpt youcan extend this code for your website as a tool.

File: index.php

<!DOCTYPE html>
<html>
<head>
<title>Neuron AI Chatbot</title>

<style>
body{
    max-width:800px;
    margin:40px auto;
    font-family:Arial;
}

#chat{
    border:1px solid #ddd;
    padding:20px;
    min-height:300px;
    margin-bottom:20px;
}

input{
    width:80%;
    padding:10px;
}

button{
    padding:10px 20px;
}
</style>

</head>
<body>

<h2>Company Chatbot</h2>

<div id="chat"></div>

<input id="message" type="text">

<button onclick="sendMessage()">
Send
</button>

<script>

async function sendMessage()
{
    let message =
        document.getElementById('message').value;

    let formData = new FormData();

    formData.append('message', message);

    let response = await fetch(
        'chat.php',
        {
            method:'POST',
            body:formData
        }
    );

    let answer = await response.text();

    document.getElementById('chat').innerHTML +=
        '<p><b>You:</b> ' +
        message +
        '</p>';

    document.getElementById('chat').innerHTML +=
        '<p><b>AI:</b> ' +
        answer +
        '</p>';

    document.getElementById('message').value = '';
}

</script>

</body>
</html>

File: CompanyAgent.php

<?php

require_once 'vendor/autoload.php';

use NeuronAI\Agent\Agent;
use NeuronAI\Agent\SystemPrompt;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Gemini\Gemini;

class CompanyAgent extends Agent
{
    protected function provider(): AIProviderInterface
    {
        return new Gemini(
            key: "ENTER_YOUR_GEMINI_KEY",
            model: 'gemini-2.5-flash'
        );
    }

    protected function instructions(): string
    {
        return (string) new SystemPrompt(
            background: [
                'You are a company assistant.'
            ],
            output: [
                'Answer only using supplied knowledge.'
            ]
        );
    }
}

File: Chat,php

<?php

session_start();

require_once 'CompanyAgent.php';

use NeuronAI\Chat\Messages\UserMessage;

$question = $_POST['message'] ?? '';

if (!$question) {
    exit;
}

if (!isset($_SESSION['history'])) {
    $_SESSION['history'] = [];
}

$knowledge = file_get_contents('data.txt');

$historyText = '';

foreach ($_SESSION['history'] as $msg) {

    $historyText .= strtoupper($msg['role']);
    $historyText .= ': ';
    $historyText .= $msg['content'];
    $historyText .= "\n";
}

$prompt = "
KNOWLEDGE:

{$knowledge}

CHAT HISTORY:

{$historyText}

USER:

{$question}

Answer only from the knowledge.
";

$agent = CompanyAgent::make();

$response = $agent->chat(
    new UserMessage($prompt)
);

$answer = $response->getMessage()->getContent();

$_SESSION['history'][] = [
    'role' => 'user',
    'content' => $question
];

$_SESSION['history'][] = [
    'role' => 'assistant',
    'content' => $answer
];

echo $answer;

File: data.txt

Company Name: Microcodes global it Solutions

Business Hours:
Monday-Friday: 10 AM - 7 PM(flexible hours)

Refund Policy:
Customers can request refunds within 30 days of purchase.

Support Email:
shimanta@microcodes.in

Services:
Web Development(wordpress, shopify, laravel, nodejs, fastapi(python) react native, react js/next js)
Mobile App Development(flutter, react native)
SEO Services, Social Media Ads(Facebook, Instagram, Youtube etc.)
Digital Ads + AI

Our Team
Boss: Domnick Torreto
Project Manager: Letty
Tech Lead: Shimanta Das

See, youtube video for this …

Now, if you want to download the source code then ……

Leave a Reply

Your email address will not be published. Required fields are marked *

Phone +91 9330497982 WhatsApp +91 9330497982 Instagram Follow Us