Skip to content

Commit

Permalink
Merge pull request #10 from llm-agents-php/feature/agent-execution-in…
Browse files Browse the repository at this point in the history
…terceptors

Adds agent execution interceptors
  • Loading branch information
butschster authored Sep 4, 2024
2 parents ca0b910 + a346e5d commit fc3a18f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
7 changes: 4 additions & 3 deletions app/src/Agents/AgentsCaller/AskAgentTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace App\Agents\AgentsCaller;

use App\Domain\Tool\PhpTool;
use LLM\Agents\Agent\AgentExecutor;
use LLM\Agents\AgentExecutor\ExecutorInterface;
use LLM\Agents\LLM\Prompt\Chat\ToolCallResultMessage;
use LLM\Agents\LLM\Response\ToolCalledResponse;
use LLM\Agents\PromptGenerator\Context;
use LLM\Agents\Tool\ToolExecutor;

/**
Expand All @@ -18,7 +19,7 @@ final class AskAgentTool extends PhpTool
public const NAME = 'ask_agent';

public function __construct(
private readonly AgentExecutor $executor,
private readonly ExecutorInterface $executor,
private readonly ToolExecutor $toolExecutor,
) {
parent::__construct(
Expand Down Expand Up @@ -47,7 +48,7 @@ public function execute(object $input): string|\Stringable

// TODO: make async
while (true) {
$execution = $this->executor->execute($input->name, $prompt);
$execution = $this->executor->execute(agent: $input->name, prompt: $prompt, promptContext: new Context());
$result = $execution->result;
$prompt = $execution->prompt;

Expand Down
26 changes: 26 additions & 0 deletions app/src/Application/Bootloader/AgentsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
use LLM\Agents\Agent\AgentRegistry;
use LLM\Agents\Agent\AgentRegistryInterface;
use LLM\Agents\Agent\AgentRepositoryInterface;
use LLM\Agents\AgentExecutor\ExecutorInterface;
use LLM\Agents\AgentExecutor\ExecutorPipeline;
use LLM\Agents\AgentExecutor\Interceptor\GeneratePromptInterceptor;
use LLM\Agents\AgentExecutor\Interceptor\InjectModelInterceptor;
use LLM\Agents\AgentExecutor\Interceptor\InjectOptionsInterceptor;
use LLM\Agents\AgentExecutor\Interceptor\InjectResponseIntoPromptInterceptor;
use LLM\Agents\AgentExecutor\Interceptor\InjectToolsInterceptor;
use LLM\Agents\JsonSchema\Mapper\SchemaMapper;
use LLM\Agents\LLM\ContextFactoryInterface;
use LLM\Agents\LLM\OptionsFactoryInterface;
Expand Down Expand Up @@ -38,6 +45,25 @@ public function defineSingletons(): array
ContextFactoryInterface::class => ContextFactory::class,

SchemaMapperInterface::class => SchemaMapper::class,

ExecutorInterface::class => static function (
ExecutorPipeline $pipeline,

// Interceptors
GeneratePromptInterceptor $generatePrompt,
InjectModelInterceptor $injectModel,
InjectToolsInterceptor $injectTools,
InjectOptionsInterceptor $injectOptions,
InjectResponseIntoPromptInterceptor $injectResponseIntoPrompt,
) {
return $pipeline->withInterceptor(
$generatePrompt,
$injectModel,
$injectTools,
$injectOptions,
$injectResponseIntoPrompt,
);
},
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/Application/Bootloader/AgentsChatBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace App\Application\Bootloader;

use App\Domain\Chat\PromptGenerator\SessionContextInjector;
use App\Domain\Chat\SimpleChatService;
use App\Infrastructure\RoadRunner\Chat\ChatHistoryRepository;
use LLM\Agents\Chat\ChatHistoryRepositoryInterface;
use LLM\Agents\Chat\ChatServiceInterface;
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;
Expand Down
22 changes: 22 additions & 0 deletions app/src/Domain/Chat/PromptGenerator/Dump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Domain\Chat\PromptGenerator;

use LLM\Agents\LLM\Prompt\Chat\PromptInterface;
use LLM\Agents\PromptGenerator\InterceptorHandler;
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;

final class Dump implements PromptInterceptorInterface
{
public function generate(
PromptGeneratorInput $input,
InterceptorHandler $next,
): PromptInterface {
dump($input->prompt->format());

return $next($input);
}
}
45 changes: 45 additions & 0 deletions app/src/Domain/Chat/PromptGenerator/SessionContextInjector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Domain\Chat\PromptGenerator;

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\Context;
use LLM\Agents\PromptGenerator\InterceptorHandler;
use LLM\Agents\PromptGenerator\PromptGeneratorInput;
use LLM\Agents\PromptGenerator\PromptInterceptorInterface;

final class SessionContextInjector implements PromptInterceptorInterface
{
public function generate(
PromptGeneratorInput $input,
InterceptorHandler $next,
): PromptInterface {
\assert($input->prompt instanceof Prompt);

if (
(!$input->context instanceof Context)
|| $input->context->getAuthContext() === null
) {
return $next($input);
}

return $next(
input: $input->withPrompt(
$input->prompt
->withAddedMessage(
MessagePrompt::system(
prompt: 'Session context: {active_context}',
),
)->withValues(
values: [
'active_context' => \json_encode($input->context->getAuthContext()),
],
),
),
);
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"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.2",
"llm-agents/agents": "^1.3",
"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",
"llm-agents/prompt-generator": "^1.1",
"nesbot/carbon": "^3.4",
"nyholm/psr7": "^1.8",
"openai-php/client": "^0.10.1",
Expand Down

0 comments on commit fc3a18f

Please sign in to comment.