1. Go to this page and download the library: Download llm-agents/prompt-generator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
llm-agents / prompt-generator example snippets
public function defineBootloaders(): array
{
return [
// ... other bootloaders ...
\LLM\Agents\PromptGenerator\Integration\Spiral\PromptGeneratorBootloader::class,
];
}
namespace App\Application\Bootloader;
use Spiral\Boot\Bootloader\Bootloader;
use LLM\Agents\PromptGenerator\Interceptors\AgentMemoryInjector;
use LLM\Agents\PromptGenerator\Interceptors\InstructionGenerator;
use LLM\Agents\PromptGenerator\Interceptors\LinkedAgentsInjector;
use LLM\Agents\PromptGenerator\Interceptors\UserPromptInjector;
use LLM\Agents\PromptGenerator\PromptGeneratorPipeline;
class PromptGeneratorBootloader extends Bootloader
{
public function defineSingletons(): array
{
return [
PromptGeneratorPipeline::class => static function (
LinkedAgentsInjector $linkedAgentsInjector,
): PromptGeneratorPipeline {
$pipeline = new PromptGeneratorPipeline();
return $pipeline->withInterceptor(
new InstructionGenerator(),
new AgentMemoryInjector(),
$linkedAgentsInjector,
new UserPromptInjector(),
// ...
);
},
];
}
}
use App\Domain\Chat\PromptGenerator\SessionContextInjector;
use LLM\Agents\PromptGenerator\Interceptors\AgentMemoryInjector;
use LLM\Agents\PromptGenerator\Interceptors\InstructionGenerator;
use LLM\Agents\PromptGenerator\Interceptors\LinkedAgentsInjector;
use LLM\Agents\PromptGenerator\Interceptors\UserPromptInjector;
use LLM\Agents\PromptGenerator\PromptGeneratorPipeline;
$generator = new PromptGeneratorPipeline();
$generator = $generator->withInterceptor(
new InstructionGenerator(),
new AgentMemoryInjector(),
new LinkedAgentsInjector($agents, $schemaMapper),
new SessionContextInjector(),
new UserPromptInjector()
);
$prompt = $generator->generate($agent, $userPrompt, $context, $initialPrompt);
namespace App\PromptGenerator\Interceptors;
use LLM\Agents\LLM\Prompt\Chat\MessagePrompt;
use LLM\Agents\LLM\Prompt\Chat\Prompt;
use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
use LLM\Agents\PromptGenerator\InterceptorHandler;
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;
use App\Services\UserPreferenceService;
use App\Services\WeatherService;
class ContextAwarePromptInjector implements PromptInterceptorInterface
{
public function __construct(
private UserPreferenceService $userPreferenceService,
private WeatherService $weatherService,
) {}
public function generate(PromptGeneratorInput $input, InterceptorHandler $next): PromptInterface
{
$userId = $input->context->getUserId(); // Assuming we have this method in our context
$userPreferences = $this->userPreferenceService->getPreferences($userId);
$currentTime = new \DateTime();
$currentWeather = $this->weatherService->getCurrentWeather($userPreferences->getLocation());
$contextMessage = $this->generateContextMessage($currentTime, $userPreferences, $currentWeather);
$modifiedPrompt = $input->prompt;
if ($modifiedPrompt instanceof Prompt) {
$modifiedPrompt = $modifiedPrompt->withAddedMessage(
MessagePrompt::system($contextMessage),
);
}
return $next($input->withPrompt($modifiedPrompt));
}
private function generateContextMessage(\DateTime $currentTime, $userPreferences, $currentWeather): string
{
$timeOfDay = $this->getTimeOfDay($currentTime);
$greeting = $this->getGreeting($timeOfDay);
return <<<PROMPT
{$greeting} Here's some context for this conversation:
- It's currently {$timeOfDay}.
- The weather is {$currentWeather->getDescription()} with a temperature of {$currentWeather->getTemperature()}°C.
- The user prefers {$userPreferences->getCommunicationStyle()} communication.
- The user's interests
use LLM\Agents\LLM\PromptContextInterface;
class ChatContext implements PromptContextInterface
{
public function __construct(
private string $userId,
private array $sessionData = [],
) {}
public function getUserId(): string
{
return $this->userId;
}
public function getSessionData(): array
{
return $this->sessionData;
}
// Add any other methods you need for your specific use case
}
$context = new ChatContext($userId, $sessionData);
$prompt = $generator->generate($agent, $userPrompt, $context);
class ContextAwarePromptInjector implements PromptInterceptorInterface
{
public function generate(PromptGeneratorInput $input, InterceptorHandler $next): PromptInterface
{
$userId = $input->context->getUserId();
$sessionData = $input->context->getSessionData();
// Use this data to customize your prompt
// ...
return $next($input);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.