Here is how you can train your chatbot with Sarufi PHP SDK

Here is how you can train your chatbot with Sarufi PHP SDK
Sarufi PHP SDK

Hi there, This article is the continuation of our previous article where we looked at how to get started with Sarufi AI as a PHP developer and created a simple website integration with sarufi using sarufi PHP SDK  successfully our question shifts to how we familiarize ourselves our chatbot with the business so much more that it can't be differentiated from the human operator? Mhh how?.

One will answer Training and that's very right. We need to train the chatbots to make sure they know as much as we want to know so that they can be able to elegant serve people successfully.

There are two ways to do this, either using No code Sarufi builder or integrating directly with Sarufi AI using Sarufi SDKs

Train your sarufi chatbot way easily with PHP SDK 

Today we are going to learn how to train our chatbot properly using SDKs First, let's install/clone our sarufi API repository into a new directory

git clone https://github.com/emmasenzota/sarufi-php-website.git .

Run this command on a new directory (i.e. training-safe) at this directory we will make all necessary changes that will allow us to train our chatbot from an array.

The array is a datatype that allows one variable to carry a collection of similar or related values also defined as an ordered map by the official PHP website. An array can hold a lot of data making it an excellent choice to train our chatbot.

public const trainingArray = array(
            'desc' => 'Sisi tunauza masaishuka na nguo za kiutamaduni,tunapatikana arusha',
            'intents'=>[
              'greets'=> [ 'hujambo', 'habari','za asubui', 'za mchana', 'mambo', 'niaje ','boss'
              ],
              'goodbye'=> [ 'Asante', 'Karibu tena', 'Asante sana', 'Nashukuru boss'
              ],
              'order_shuka'=> [ 'Nahitaji masaishuka', 'Naitaji mashuka', 'Masai shuka zipo', 'Natoa order ya masaishuka'
              ],
              'type_shuka'=> [ 'Zipo nyekundu', 'zipo mchanganyiko wa nyekundu na nyeusi', 'Kijani na bluu','Kijani','Nyekundu','bluu','ugoro','nyeusi','boks kubwa'],
            ],
            'flow'=>[
                "greets"=> [
                    "message"=> [
                    "Mambo vipi, nikusaidieje mkuu?"
                    ],
                    "next_state"=> "order_shuka"
                ],
                "order_shuka"=> [
                    "message"=> [
                    "Sawa boss, Nikuwekee aina ipi boss? naomba nitajie rangi"
                    ],
                    "next_state"=> "type_shuka"
                ],
                "type_shuka"=> [
                    "message"=> [
                    "Sawa boss, izo zipo, Niweke mashuka mangapi?"
                    ],
                    "next_state"=> "number_of_shuka"
                ],
                "number_of_shuka" =>[
                    "message"=> [
                    "Sawa boss, naomba anwani yako au nipeleke gari gani?"
                    ],
                    "next_state" => "address"
                ],
                "address" => [
                    "message" => [
                    "Naomba na namba yako ya simu?"
                    ],
                    "next_state" => "phone_number"
                ],
                "phone_number" => [
                    "message" => [
                    "YNashukuru boss order yako inashuhulikiwa",
                    "Asante sana boss mzigo wako uko tayari asante sana boss."
                    ],
                    "next_state" => "end"
                ],
                "goodbye" => [
                    "message" => [
                    "Bye",
                    "Tuendelee kuwasiliana boss wangu"
                    ],
                    "next_state" => "end"
                ]
            ]
    );

From our training array above, here are some words one must understand

1:- Flow refers to the sequence of interactions and responses that occur during a conversation between the user and the chatbot. It involves understanding the context of the conversation, recognizing user intents, and providing appropriate responses that guide the conversation coherently and logically.

Note while preparing the flow make sure you have connected all states properly so that your chatbot operates appropriately.

2:- Intent refers to the user's intention or purpose behind a particular message or query. It represents what the user tries to convey or achieve through their interaction with the chatbot. Identifying the intent of the user's message is crucial for the chatbot to provide relevant and accurate responses.

After preparing the training array we run a training class to train the chatbot. From here the bot will have a new understanding of what's required of it during a conversation with our users. Run this command on your terminal to train your chatbot at sarufi

Zod@EmmaSenzotasAir public % php index.php training
{
    "CSP": {}
}%                                                                                                                      Zod@EmmaSenzotasAir public % 
terminal command and its result

Now here your chatbot is going to use data from the array in its conversations with users. This method gives much superiority in comparison with a no-code method.

That being said here are very important code changes that give us the capability to use the SDK to train our chatbots

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Chat::index');
$routes->cli('training','Training::index'); // CLI routing

// for our Sarufi API
$routes->post('/chat','Chat::chatWithBot');

Here we look at the Route.php file in the Routes definition section We add a line that gives us the ability to run the Training class from the Command Line (CLI), This makes it so easy to train our chatbot with just one line in the terminal and Boom chatbot trained. Find it easy, Yes it is, I told you PHP is easy and now you can do almost everything with it.

Another change is the Training class. It inherits credentials from the Sarufi class ( you can find/create this in the Config directory under the app directory. This class is responsible for training our chatbot fast and making our work easy and fast like the good programmers we are, yes?!

<?php
namespace App\Controllers;

//Use this class to train your chatbot 
use Config\Sarufi as confSarufi;
use Alphaolomi\Sarufi\Sarufi as Sarufi;
use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;

class Training extends Controller{
    use ResponseTrait;

    // define some constant variables here 
    protected $Sarufi;
    public    $request;
    public    $Client;

    // trainig array 
    public const trainingArray = array(
            'desc' => 'Sisi tunauza masaishuka na nguo za kiutamaduni, tunapatikana arusha',
            'intents'=>[
              'greets'=> [ 'hujambo', 'habari','za asubui', 'za mchana', 'mambo', 'niaje ','boss'
              ],
              'goodbye'=> [ 'Asante', 'Karibu tena', 'Asante sana', 'Nashukuru boss'
              ],
              'order_shuka'=> [ 'Nahitaji masaishuka', 'Naitaji mashuka', 'Masai shuka zipo', 'Natoa order ya masaishuka'
              ],
              'type_shuka'=> [ 'Zipo nyekundu', 'zipo mchanganyiko wa nyekundu na nyeusi', 'Kijani na bluu','Kijani','Nyekundu','bluu','ugoro','nyeusi','boks kubwa'],
            ],
            'flow'=>[
                "greets"=> [
                    "message"=> [
                    "Mambo vipi, nikusaidieje mkuu?"
                    ],
                    "next_state"=> "order_shuka"
                ],
                "order_shuka"=> [
                    "message"=> [
                    "Sawa boss, Nikuwekee aina ipi boss? naomba nitajie rangi"
                    ],
                    "next_state"=> "type_shuka"
                ],
                "type_shuka"=> [
                    "message"=> [
                    "Sawa boss, izo zipo, Niweke mashuka mangapi?"
                    ],
                    "next_state"=> "number_of_shuka"
                ],
                "number_of_shuka" =>[
                    "message"=> [
                    "Sawa boss, naomba anwani yako au nipeleke gari gani?"
                    ],
                    "next_state" => "address"
                ],
                "address" => [
                    "message" => [
                    "Naomba na namba yako ya simu?"
                    ],
                    "next_state" => "phone_number"
                ],
                "phone_number" => [
                    "message" => [
                    "YNashukuru boss order yako inashuhulikiwa",
                    "Asante sana boss mzigo wako uko tayari asante sana boss."
                    ],
                    "next_state" => "end"
                ],
                "goodbye" => [
                    "message" => [
                    "Bye",
                    "Tuendelee kuwasiliana boss wangu"
                    ],
                    "next_state" => "end"
                ]
            ]
    );

    // initialise class during call 
    public function __construct()
    {
        $this->Sarufi = new Sarufi(confSarufi::$apiKey);
        $this->request = \Config\Services::request();
    }

    public function trainWithArray(array $array) {
        // check for empty array and send error/ exception 
        if(empty($array)){
            return $this->respond(['error' => 'Training araay is empty']);
        }
        else{
            $updates = $this->Sarufi->updateBot(confSarufi::$botId,'YaKwetu','Culture',$array['desc'],$array['intents'],$array['flow'],true);

            // after training with our array we terminate this method by notifying our front end 
            return $this->respond($updates);
        }
    }

    public function index() {
        // run the training method
        return $this->respond($this->trainWithArray($this::trainingArray));
    }

}

With this class, you can train your chatbot in an instant and see results on our chatting page.  Also, your bot should be visible on Sarufi Community Playground ready to chat, Check the git repository here. These are my results, show me yours

training sarufi flow

I believe you can replicate this work as easily. Welcome and chat, ask, and contribute at the emmasenzota X page