Skip to content

Commit

Permalink
Adds agent executor and 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 0292400 commit bfc9121
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 8,732 deletions.
7 changes: 4 additions & 3 deletions app/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\Agents\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
22 changes: 22 additions & 0 deletions app/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\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/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\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()),
],
),
),
);
}
}
2 changes: 1 addition & 1 deletion app/Providers/AgentsChatServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Providers;

use App\Chat\ChatHistoryRepository;
use App\Chat\PromptGenerator\SessionContextInjector;
use App\Chat\SimpleChatService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
Expand All @@ -13,7 +14,6 @@
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;

Expand Down
20 changes: 20 additions & 0 deletions app/Providers/AgentsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@

namespace App\Providers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
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 All @@ -34,6 +42,18 @@ public function register(): void
$this->app->singleton(ContextFactoryInterface::class, ContextFactory::class);

$this->app->singleton(SchemaMapperInterface::class, SchemaMapper::class);

$this->app->singleton(ExecutorInterface::class, static function (
Application $app,
) {
return $app->make(ExecutorPipeline::class)->withInterceptor(
$app->make(GeneratePromptInterceptor::class),
$app->make(InjectModelInterceptor::class),
$app->make(InjectToolsInterceptor::class),
$app->make(InjectOptionsInterceptor::class),
$app->make(InjectResponseIntoPromptInterceptor::class),
);
});
}

public function boot(
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"laravel/framework": "^11.9",
"llm-agents/agent-site-status-checker": "^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/prompt-generator": "^1.0",
"llm-agents/prompt-generator": "^1.1",
"llm-agents/json-schema-mapper": "^1.0",
"llm-agents/openai-client": "^1.0",
"openai-php/laravel": "^0.10.1"
Expand Down
Loading

0 comments on commit bfc9121

Please sign in to comment.