From f851a86ee782c0e3874547af0ac3df25a64572c9 Mon Sep 17 00:00:00 2001 From: Pavel Buchnev Date: Wed, 4 Sep 2024 22:20:13 +0400 Subject: [PATCH] Adds prompt generator with interceptors --- .../Bootloader/AgentsChatBootloader.php | 23 ++++++++++++++++--- app/src/Application/Kernel.php | 8 +++++-- app/src/Domain/Chat/SimpleChatService.php | 12 ++++++---- composer.json | 9 ++++---- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/src/Application/Bootloader/AgentsChatBootloader.php b/app/src/Application/Bootloader/AgentsChatBootloader.php index 1384a64..756e57b 100644 --- a/app/src/Application/Bootloader/AgentsChatBootloader.php +++ b/app/src/Application/Bootloader/AgentsChatBootloader.php @@ -6,10 +6,14 @@ use App\Domain\Chat\SimpleChatService; use App\Infrastructure\RoadRunner\Chat\ChatHistoryRepository; -use LLM\Agents\Chat\AgentPromptGenerator; use LLM\Agents\Chat\ChatHistoryRepositoryInterface; use LLM\Agents\Chat\ChatServiceInterface; -use LLM\Agents\LLM\AgentPromptGeneratorInterface; +use LLM\Agents\PromptGenerator\Interceptors\AgentMemoryInjector; +use LLM\Agents\PromptGenerator\Interceptors\InstructionGenerator; +use LLM\Agents\PromptGenerator\Interceptors\LinkedAgentsInjector; +use LLM\Agents\PromptGenerator\Interceptors\SessionContextInjector; +use LLM\Agents\PromptGenerator\Interceptors\UserPromptInjector; +use LLM\Agents\PromptGenerator\PromptGeneratorPipeline; use Spiral\Boot\Bootloader\Bootloader; use Spiral\Cache\CacheStorageProviderInterface; @@ -19,12 +23,25 @@ public function defineSingletons(): array { return [ ChatServiceInterface::class => SimpleChatService::class, - AgentPromptGeneratorInterface::class => AgentPromptGenerator::class, ChatHistoryRepositoryInterface::class => static fn( CacheStorageProviderInterface $cache, ): ChatHistoryRepositoryInterface => new ChatHistoryRepository( $cache->storage('chat-messages'), ), + + PromptGeneratorPipeline::class => static function ( + LinkedAgentsInjector $linkedAgentsInjector, + ): PromptGeneratorPipeline { + $pipeline = new PromptGeneratorPipeline(); + + return $pipeline->withInterceptor( + new InstructionGenerator(), + new AgentMemoryInjector(), + $linkedAgentsInjector, + new SessionContextInjector(), + new UserPromptInjector(), + ); + }, ]; } } diff --git a/app/src/Application/Kernel.php b/app/src/Application/Kernel.php index 83b728a..ebe1a78 100644 --- a/app/src/Application/Kernel.php +++ b/app/src/Application/Kernel.php @@ -9,6 +9,7 @@ use LLM\Agents\Agent\SymfonyConsole\Integrations\Spiral\SymfonyConsoleBootloader; use LLM\Agents\JsonSchema\Mapper\Integration\Spiral\SchemaMapperBootloader; use LLM\Agents\OpenAI\Client\Integration\Spiral\OpenAIClientBootloader; +use LLM\Agents\PromptGenerator\Integration\Spiral\PromptGeneratorBootloader; use Spiral\Boot\Bootloader\CoreBootloader; use Spiral\DotEnv\Bootloader\DotenvBootloader; use Spiral\Prototype\Bootloader\PrototypeBootloader; @@ -38,6 +39,11 @@ public function defineBootloaders(): array // Prototyping PrototypeBootloader::class, + // LLM + PromptGeneratorBootloader::class, + OpenAIClientBootloader::class, + SchemaMapperBootloader::class, + // Application Bootloader\AppBootloader::class, Bootloader\EventsBootloader::class, @@ -47,8 +53,6 @@ public function defineBootloaders(): array Bootloader\SmartHomeBootloader::class, // Agents - OpenAIClientBootloader::class, - SchemaMapperBootloader::class, SiteStatusCheckerBootloader::class, SmartHomeControlBootloader::class, SymfonyConsoleBootloader::class, diff --git a/app/src/Domain/Chat/SimpleChatService.php b/app/src/Domain/Chat/SimpleChatService.php index b54fc9d..899b6f6 100644 --- a/app/src/Domain/Chat/SimpleChatService.php +++ b/app/src/Domain/Chat/SimpleChatService.php @@ -19,6 +19,7 @@ use LLM\Agents\LLM\Response\ChatResponse; use LLM\Agents\LLM\Response\ToolCall; use LLM\Agents\LLM\Response\ToolCalledResponse; +use LLM\Agents\PromptGenerator\Context; use LLM\Agents\Solution\SolutionMetadata; use LLM\Agents\Tool\ToolExecutor; use Psr\EventDispatcher\EventDispatcherInterface; @@ -158,6 +159,12 @@ private function handleResult(Execution $execution, Session $session): void private function buildAgent(Session $session, ?Prompt $prompt): AgentExecutorBuilder { + $context = new Context(); + $context->setAuthContext([ + 'account_uuid' => (string) $session->accountUuid, + 'session_uuid' => (string) $session->uuid, + ]); + $agent = $this->builder ->withAgentKey($session->agentName) ->withStreamChunkCallback( @@ -166,10 +173,7 @@ private function buildAgent(Session $session, ?Prompt $prompt): AgentExecutorBui eventDispatcher: $this->eventDispatcher, ), ) - ->withSessionContext([ - 'account_uuid' => (string) $session->accountUuid, - 'session_uuid' => (string) $session->uuid, - ]); + ->withPromptContext($context); if ($prompt === null) { return $agent; diff --git a/composer.json b/composer.json index 0f80f5d..881f8e6 100644 --- a/composer.json +++ b/composer.json @@ -13,17 +13,18 @@ "llm-agents/agent-site-status-checker": "^1.0", "llm-agents/agent-smart-home-control": "^1.0", "llm-agents/agent-symfony-console": "^1.0", - "llm-agents/agents": "^1.0", - "llm-agents/cli-chat": "^1.0", + "llm-agents/agents": "^1.2", + "llm-agents/cli-chat": "^1.2", "llm-agents/json-schema-mapper": "^1.0", "llm-agents/openai-client": "^1.0", + "llm-agents/prompt-generator": "^1.0", "nesbot/carbon": "^3.4", "nyholm/psr7": "^1.8", "openai-php/client": "^0.10.1", "spiral-packages/league-event": "^1.0", "spiral/cycle-bridge": "^2.9", - "spiral/framework": "^3.13", - "spiral/roadrunner-bridge": "^3.6" + "spiral/framework": "^3.14", + "spiral/roadrunner-bridge": "^4.0" }, "require-dev": { "buggregator/trap": "^1.7",