Skip to content

Commit

Permalink
Adds prompt generator with interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Sep 4, 2024
1 parent fc71c4e commit f851a86
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
23 changes: 20 additions & 3 deletions app/src/Application/Bootloader/AgentsChatBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(),
);
},
];
}
}
8 changes: 6 additions & 2 deletions app/src/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -47,8 +53,6 @@ public function defineBootloaders(): array
Bootloader\SmartHomeBootloader::class,

// Agents
OpenAIClientBootloader::class,
SchemaMapperBootloader::class,
SiteStatusCheckerBootloader::class,
SmartHomeControlBootloader::class,
SymfonyConsoleBootloader::class,
Expand Down
12 changes: 8 additions & 4 deletions app/src/Domain/Chat/SimpleChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f851a86

Please sign in to comment.