Today weβll make your GPT recommendation system self-improving by feeding in global click trend data into the GPT prompt β making suggestions smarter with every user interaction.
π Step 1: Fetch Global Click Trend Keywords
In ProductRecommendationService.php
, add:
use App\Models\ProductClick;
use Illuminate\Support\Facades\DB;
private function getTopGlobalClicks(int $limit = 5): array
{
return ProductClick::select('product_id', DB::raw('count(*) as total'))
->groupBy('product_id')
->orderByDesc('total')
->with('product:id,name')
->limit($limit)
->get()
->pluck('product.name')
->filter()
->toArray();
}
π§ Step 2: Add to GPT Prompt
Update your GPT prompt generation in recommend()
:
$topGlobal = $this->getTopGlobalClicks();
$messages = [
[
'role' => 'user',
'content' => 'User viewed: ' . json_encode($viewed)
],
[
'role' => 'user',
'content' => 'User clicked: ' . json_encode($clicked)
],
[
'role' => 'user',
'content' => 'Top global trending items: ' . json_encode($topGlobal)
],
[
'role' => 'user',
'content' => 'Suggest 5 products relevant to the user based on the above data.'
],
];
Now GPT will consider:
- This user’s behavior
- Global click trends
- Current product inventory
β¦before giving back recommendations.
π Optional: Adjust weight of global vs local data
In future, you can:
- Prioritize global trends if the user has low activity
- Tune prompt messages for weighting (e.g., βPrioritize trending items if user data is limitedβ)
β Summary
β Today you:
- Incorporated real-time click trends into GPT prompts
- Made your product recommender system adaptive and data-informed
- Closed the feedback loop with a powerful hybrid strategy
π That wraps up the 10-day Laravel + GPT Product Recommendation Engine journey!
You now have:
- Dynamic GPT-powered suggestions
- Click tracking + trend analytics
- Cached results
- Adaptive AI-driven UX
π Repeat the cycle monthly with retraining and category tuning to keep it sharp!