sabatinomasala / laravel-llm-prompt
Laravel 的 LLM 提示助手
v1.0.0
2024-08-13 11:52 UTC
Requires
- php: ^8.2
- laravel/framework: ^11.9
README
此仓库允许您轻松管理 LLM 的提示。
安装
composer require sabatinomasala/laravel-llm-prompt
此包将被自动发现。
命令
此包允许您使用以下命令创建提示
php artisan make:prompt Example
提示将被添加到 app/Prompts/
目录。
使用方法
处理大型提示可能具有挑战性,尤其是在处理许多变量时。上面创建的示例提示看起来如下
<?php
namespace App\Prompts;
use SabatinoMasala\LaravelLlmPrompt\BasePrompt;
class Example extends BasePrompt
{
public function __construct(
public string $language = 'English'
) {}
public function getBasePrompt(): string
{
// You can use variables in the prompt
return 'Explain the pythagorean theorem in {language} as if I were a 5 year old.';
}
}
类上的每个 公开 变量都可供提示使用,并在获取提示时被替换
$prompt = new \App\Prompts\Example('French');
echo $prompt->get();
// Explain the pythagorean theorem in French as if I were a 5 year old.
您可以通过以下方式动态地向提示添加内容
<?php
namespace App\Prompts;
use SabatinoMasala\LaravelLlmPrompt\BasePrompt;
class Brainstorm extends BasePrompt
{
public function __construct(
public string $language,
public string $series,
){}
public function addHistory(array $history): void
{
$this->add('Make sure the following titles are not in the list:');
collect($history)->each(function($line) {
$this->add('- ' . $line);
});
}
public function getBasePrompt(): string
{
return 'Give me a list of 30 story titles in {language} I can write.
Only respond with a list of titles, no other information.
1 title per line.
A good title consists of 4-8 words.
Do not number the list.
You will be penalized if the language is not {language}
The story should fit in the series {series}';
}
}
$prompt = new \App\Prompts\Brainstorm('French', 'Roman empire');
$prompt->addHistory([
'The downfall of Caesar',
'Romulus & Remus: where it all started',
'The tortoise formation',
]);
echo $prompt->get();