Product Recommendation using AI in PHP Laravel

Product recommendation system using Machine Learning

In this blog post we will try to experience the use of AI and machine learning for product recommendations in ecommerce in PHP Laravel, we are going to use a recommendation engine called LightFM, which is a hybrid recommendation engine that uses both collaborative filtering and content-based filtering.

Here’s how you can implement LightFM in PHP Laravel:

  • Install the LightFM package using Composer:
composer require lightfm/lightfm
  • Create a controller to handle the product recommendations:
php artisan make:controller RecommendationController
  • In the RecommendationController, import the LightFM package and define a method to get product recommendations:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use LightFM\LightFM;
use LightFM\LightFM\Exception as LightFMException;
class RecommendationController extends Controller
{
    public function getProductRecommendations($user_id)
    {
        try {
            // Create a new LightFM instance
            $model = new LightFM();
            // Load the saved model
            $model->loadState('path/to/saved/model');
            // Get the product recommendations for the user
            $recommendations = $model->predict($user_id, range(1, $max_product_id));
            return response()->json(['recommendations' => $recommendations]);
        } catch (LightFMException $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}
  • In the above code, the $user_id parameter is the ID of the user for whom you want to get the product recommendations. The $max_product_id variable should be set to the maximum ID of the products in your database.
  • Train the LightFM model using your product data. You can use the following code to train the model:
$model = new LightFM();
$dataset = new LightFM\Data();
$max_product_id = DB::table('products')->max('id');
// Add the interactions (i.e., which users have interacted with which items)
$interactions = DB::table('interactions')->get();
foreach ($interactions as $interaction) {
    $dataset->addInteraction($interaction->user_id, $interaction->product_id, $interaction->rating);
}
// Add the item features (e.g., category, price, etc.)
$items = DB::table('products')->get();
foreach ($items as $item) {
    $features = ['category' => $item->category, 'price' => $item->price];
    $dataset->addItem($item->id, $features);
}
// Add the user features (e.g., age, gender, etc.)
$users = DB::table('users')->get();
foreach ($users as $user) {
    $features = ['age' => $user->age, 'gender' => $user->gender];
    $dataset->addUser($user->id, $features);
}
// Fit the model
$model->fit($dataset, ['epochs' => 100]);
// Save the model
$model->saveState('path/to/saved/model');
  • n the above code, we first create a new LightFM instance and a new LightFM\Data instance to hold our data. We then get the maximum product ID from our database and add the interactions (i.e., which users have interacted with which items) using the addInteraction method. We also add the item and user features using the addItem and addUser methods.
  • We then fit the model using the fit method and save it using the saveState method.
  • Finally, you can call the getProductRecommendations method from your frontend or other parts of your application to get the product

I hope you enjoyed. Like and comment.

Leave a Comment