fiveam-code / ada-laravel
该包允许您通过无缝集成词嵌入来增强您的Laravel应用程序。
Requires
- php: ^8.2
- illuminate/contracts: ^10.0|^11.0
- openai-php/client: ^0.8.4
- pgvector/pgvector: ^0.1.4
- rajentrivedi/tokenizer-x: dev-upgrade-laravel-11
- symfony/http-client: ^7.0
Requires (Dev)
- laravel/pint: ^1.15
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.20
- pestphp/pest-plugin-laravel: ^2.3
README
包 ada-laravel
允许您通过无缝集成文本嵌入和查询能力来增强您的Laravel应用程序。默认使用OpenAI,它使您的模型能够通过最近邻技术生成和查询嵌入。此包需要具有向量扩展的PostgreSQL数据库来有效地存储和管理这些嵌入,以及至少Laravel 11。
最初作为Diana Scharf的演讲»你见过ada吗? - 使用Laravel和OpenAI进行词嵌入«的演示而创建,这个包虽然功能齐全,但旨在鼓励进一步的开发和贡献。
请注意,这个包仍在开发中,可能不适合生产使用。
安装
目前,安装过程取决于您的composer.json
中的"minimum-stability"
设置。如果您有"minimum-stability": "dev"
,则可以直接安装该包
composer require fiveam-code/ada-laravel
否则,您需要在composer.json
中添加一个临时的开发依赖项来添加该包
{ "require": { "fiveam-code/ada-laravel": "^0.1.0", "rajentrivedi/tokenizer-x": "dev-upgrade-laravel-11 as 1.0.0" } }
然后,您可以通过运行composer update
来安装包。
确保您的数据库已配置为使用具有向量扩展的PostgreSQL。如果未启用,包将通过迁移启用扩展。
发布迁移并运行它们
php artisan vendor:publish --provider="Ada\AdaServiceProvider" --tag="ada-migrations" php artisan migrate
这将启用数据库中的vector
扩展并创建一个名为embeddings
的表来存储嵌入。
配置
在您的.env
文件中设置OpenAI API密钥
ADA_CLIENT_TOKEN=your_openai_api_key
请注意,您需要一个OpenAI密钥进行API访问,而不仅仅是ChatGPT访问。
可选地,如果您想更改默认设置,可以发布配置文件
php artisan vendor:publish --provider="Ada\AdaServiceProvider" --tag="ada-config"
默认配置如下
return [ 'client_token' => env('ADA_CLIENT_TOKEN'), 'index_class' => \Ada\Index\DefaultIndex::class, 'default_prompt_view' => 'ada::default-prompt' ];
如果您想实现自己的引擎来处理嵌入,可以创建一个新的类来实现Index
接口,并使用适当的引擎设置它到配置中。
使用
基本用法
首先,将HasEmbeddings
特质添加到您的Eloquent模型中
<?php namespace App\Models; use Ada\Traits\HasEmbeddings; class Paper extends Model { use HasEmbeddings; }
嵌入内容
通过调用带有引用键和文本的embed
方法将相关内容嵌入到您的模型中
use App\Models\Paper; $paper = Paper::first(); $paper->embed("abstract", $paper->abstract);
这将生成文本的嵌入并将其与$paper
模型和引用键"abstract"
关联存储在数据库中。
查找嵌入
查找方法允许直接查询您模型的存储知识,通过使用向量相似度来检索最相关的上下文信息,从而实现智能搜索。
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."
这将创建查询的嵌入,并使用向量的最近邻技术找到与$paper
模型相关的数据库中最相似的嵌入。结果将是最接近查询的文本,并用于请求OpenAI API生成答案的上下文。
这是默认的提示文本
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}
将被最近邻查询的结果替换。
如果您想进一步自定义提示,可以将从继承自Ada\Tools\Prompts\Prompt
的类传递的对象作为参数传递给lookup
方法。
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);
如果您需要进一步限制查找,可以将闭包作为第三个参数传递。
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 });
高级用法
通过直接使用索引或引擎来定制端点模型和选项
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