Download the PHP package fiveam-code/ada-laravel without Composer
On this page you can find all versions of the php package fiveam-code/ada-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fiveam-code/ada-laravel
More information about fiveam-code/ada-laravel
Files in fiveam-code/ada-laravel
Package ada-laravel
Short Description This package allows you to enhance your Laravel applications by seamlessly integrating word embeddings.
License MIT
Homepage https://github.com/5am-code/ada-laravel
Informations about the package ada-laravel
ada-laravel
The package ada-laravel
allows you to enhance your Laravel applications by seamlessly integrating text embeddings and
querying capabilities for your models. Utilizing OpenAI by default, it enables your models to generate and query
embeddings using
nearest neighbors techniques. This package requires a PostgreSQL database with the vector extension to store and manage
these embeddings efficiently as well as at least Laravel 11.
Originally created as a demo for the talk »Have you met ada? - Word Embeddings with Laravel and OpenAI« by Diana Scharf, this package is functional yet designed to encourage further development and contributions.
Please note that this package is still in development and may not be suitable for production use.
Installation
At the moment, the installation process depends on the "minimum-stability"
setting in your composer.json
. If you have
"minimum-stability": "dev"
, you can install the package directly:
Otherwise, you need to add the package with a temporary dev dependency to your composer.json
:
Then you can run composer update
to install the package.
Ensure that your database is configured to use PostgreSQL with the vector extension. The package will enable the extension via a migration if it is not already enabled.
Publish the migrations and run them:
This will enable the vector
extension in your database and create a table embeddings
to store the embeddings.
Configuration
Set the OpenAI API key in your .env
file:
Please note that you need an OpenAI key for API access, not just ChatGPT access.
Optionally, you can publish the configuration file if you want to make changes to the default settings:
The default configuration is as follows:
If you want to implement your own engine to handle embeddings, you can create a new class that implements the Index
interface with the appropriate engine and set it in the configuration.
Usage
Basic Usage
First, add the HasEmbeddings
trait to your Eloquent model:
Embed content
Embed content related to your model by calling the embed
method with a reference key and text:
This will generate an embedding for the text and store it in the database with a relation to the $paper
model and the
reference key "abstract"
.
Lookup embeddings
The lookup method allows for direct querying of your model's stored knowledge, facilitating an intelligent search that retrieves the most contextually relevant information using vector similarity.
This will create an embedding for the query and find the most similar embeddings in the database related to the $paper
model by using the
nearest neighbors technique of the vectors. The result will be the most similar text to the query and will be used as
context for a request
to the OpenAI API to generate an answer.
This is the default prompt text:
{context}
will be replaced with the result from the nearest neighbors query.
If you want to further customize the prompt, you can pass an object form a class inheriting Ada\Tools\Prompts\Prompt
to the lookup
method:
In case you need to further limit the lookup, you can pass a closure as a third parameter.
Advanced Usage
Customize the endpoint models and options by using the index or engines directly:
All versions of ada-laravel with dependencies
illuminate/contracts Version ^10.0|^11.0
openai-php/client Version ^0.8.4
pgvector/pgvector Version ^0.1.4
rajentrivedi/tokenizer-x Version dev-upgrade-laravel-11
symfony/http-client Version ^7.0