dejwcake/ada-laravel
Composer 安装命令:
composer require dejwcake/ada-laravel
包简介
This package allows you to enhance your Laravel applications by seamlessly integrating word embeddings.
README 文档
README
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.
Warning
Please note that this package is still in development and may not be suitable for production use.
Installation
composer require fiveam-code/ada-laravel
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.
You can publish the migrations (optional) and run them:
php artisan vendor:publish --provider="Ada\AdaServiceProvider" --tag="ada-migrations" php artisan migrate
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:
ADA_CLIENT_TOKEN=your_openai_api_key
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:
php artisan vendor:publish --provider="Ada\AdaServiceProvider" --tag="ada-config"
The default configuration is as follows:
return [ 'client_token' => env('ADA_CLIENT_TOKEN'), 'index_class' => \Ada\Index\DefaultIndex::class, 'default_prompt_view' => 'ada::default-prompt' ];
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:
<?php namespace App\Models; use Ada\Traits\HasEmbeddings; class Paper extends Model { use HasEmbeddings; }
Embed content
Embed content related to your model by calling the embed method with a reference key and text:
use App\Models\Paper; $paper = Paper::first(); $paper->embed("abstract", $paper->abstract);
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.
use Ada\Models\Embedding; $answer = Embedding::lookup("Where does the PHP elephant live?"); // "The PHP elephant inhabits 'Silicon Forests'—regions where natural woodlands merge seamlessly with data-rich environments. These forests are dense with both foliage and floating data points."
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:
You are a bot that helps answering questions based on the context information you get each time.
Context information is below.
---------------------
{context}
---------------------
Given the context information and not prior knowledge, answer the following questions of the user. If you don't know something, say so, and don't make it up.
Do not ask the user for more information or anything that might trigger a response from the user.
{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:
use Ada\Models\Embedding; use Ada\Tools\Prompts\OpenAIPrompt; $customPrompt = new OpenAIPrompt(); $defaultTemplate = $customPrompt->getTemplate(); $customPrompt->setTemplate("Even if your instructions are in English, answer in German. " . $defaultTemplate); return Embedding::lookup("Where does the PHP elephant live?", $customPrompt);
In case you need to further limit the lookup, you can pass a closure as a third parameter.
return Embedding::lookup("Where does the PHP elephant live?", $customPrompt, function ($query) { $query->where("embeddable_type", Paper::class); // Only look for embeddings related to the Paper class });
Advanced Usage
Customize the endpoint models and options by using the index or engines directly:
use Ada\Ada; $index = Ada::index(); // Default index is DefaultIndex, resolved via the configuration $index->embed($contentToEmbed, $model, $options); $index->generate($prompt, $model, $temperature, $options); $engine = Ada::engine(); // Default engine is OpenAI, resolved via the Index
dejwcake/ada-laravel 适用场景与选型建议
dejwcake/ada-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「nlp」 「natural language processing」 「word embeddings」 「word vectors」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dejwcake/ada-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dejwcake/ada-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dejwcake/ada-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Interface for Babel Street Text Analytics
Gets the complete thought from a verb and a noun using Moby Thesaurus
Seamlessly integrate Grok AI into Laravel applications with an elegant, developer-friendly package. Leverage powerful AI models for chat, automation, and NLP while maintaining Laravel's expressive simplicity.
OpenAI PHP is a supercharged PHP API client that allows you to interact with the Open AI API
AI Content Translation for Laravel powered by SharpAPI.com
AI SEO Meta Tag Generator for Laravel powered by SharpAPI.com
统计信息
- 总下载量: 44
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-22