Skip to content

Recommendation engine using Open AI embedding and PostgresSQL pgvector

License

Notifications You must be signed in to change notification settings

thesubhendu/embedvector-laravel

Repository files navigation

Recommendation engine using Open AI embedding and PostgresSQL pgvector

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a recommendation engine using Open AI embedding and PostgresSQL pgvector. It uses the openai api to generate embeddings and stores them in the database. It then uses pgvector to search for similar embeddings.

⚠️ Notice: This package is currently in development and not yet ready for production use. Please use at your own risk.

Installation

composer require thesubhendu/embedvector-laravel

You can publish and run the migrations with:

php artisan vendor:publish --tag="recommender-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="embed-vector-config"

This is the contents of the published config file:

return [
    'openai_api_key' => env('OPENAI_API_KEY', ''),
];

Add your openai api key to the .env file as OPENAI_API_KEY

To get api key login or signup at https://platform.openai.com/api-keys and create a new api key

Usage

Example: say you want to find AI matching jobs for the customer

Step 0: Prepare your model

Implement EmbeddableContract to Eloquent model use EmbeddableTrait

<?php

namespace App\Models;

use Subhendu\EmbedVector\Contracts\EmbeddableContract;
use Subhendu\EmbedVector\Traits\EmbeddableTrait;

class Customer extends Model implements EmbeddableContract
{
    use EmbeddableTrait;
}

Once model is ready run

Step 1:

php artisan embedding:batch {modelName} {--type=sync|init} {--force}

Example: php artisan embedding:batch App\\Models\\Customer --type=init

This will generate a jsonl file in storage/app/embeddings/ which is uploaded to openai api

Step 2:

php artisan process-completed-batch

This will process the embeddings (generated in step 1) and store them in the database

  • Repeat step 1 and 2 with other models if needed

Step 3 :You can now search using the matchingResults method:

$customer = Customer::find(1);
$customer->matchingResults(Job::class);

Testing

composer test

Credits

About

Recommendation engine using Open AI embedding and PostgresSQL pgvector

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages