borah/knowledge-base-laravel

Laravel wrapper for the Knowledge-Base API

v0.7.6 2024-05-14 17:28 UTC

README

This package is a Laravel wrapper for the Knowledge Base API. It will create and maintain a Knowledge Base for all the configured Eloquent models.

Installation

You can install the package via composer:

composer require borah/knowledge-base-laravel

You must publish the migrations:

php artisan vendor:publish --tag="knowledge-base-laravel-migrations"

After the migration has been published you can run them using php artisan migrate.

Also, if you want, you can publish the config file with:

php artisan vendor:publish --tag="knowledge-base-laravel-config"

This is the contents of the published config file:

return [
    'connection' => [
        'host' => env('KNOWLEDGE_BASE_HOST', 'http://localhost:8100'),
    ],
    'models' => [
        'knowledge_base_id' => \Borah\KnowledgeBase\Models\KnowledgeBaseId::class,
    ],
];

Requirements

This package is a wrapper for Knowledge Base API, so you need to have it running in order to use this package.

Usage

To use it, you need to add the Borah\KnowledgeBase\Traits\HasKnowledgeBase trait to the models you want to add to the Knowledge Base. Also, these models should implement the Borah\KnowledgeBase\Contracts\Embeddable interface.

For example:

<?php

namespace App\Models;

use Borah\KnowledgeBase\Contracts\Embeddable;
use Borah\KnowledgeBase\DTO\KnowledgeEmbeddingText;
use Borah\KnowledgeBase\Traits\BelongsToKnowledgeBase;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Post extends Model implements Embeddable
{
    use HasFactory;
    use BelongsToKnowledgeBase;

    public function getEmbeddingsTexts(): KnowledgeEmbeddingText|array
    {
        return [
            new KnowledgeEmbeddingText(
                text: $this->content,
                entity: class_basename($this),
            ),
        ];
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.