Skip to content

Commit cf2973f

Browse files
committed
Add solution metadata classes to provide better DX
1 parent 7af6e95 commit cf2973f

15 files changed

+162
-2
lines changed

src/AgentExecutor/Interceptor/GeneratePromptInterceptor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
/**
1616
* This interceptor is responsible for generating the prompt for the agent.
17+
* If the input does not have a prompt, it will generate one using the prompt generator.
18+
* After the execution, it will remove temporary messages (which implement LLM\Agents\LLM\Prompt\Chat\TempMessageInterface) from the prompt.
1719
*/
1820
final readonly class GeneratePromptInterceptor implements ExecutorInterceptorInterface
1921
{

src/AgentExecutor/Interceptor/InjectOptionsInterceptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use LLM\Agents\AgentExecutor\ExecutorInterceptorInterface;
1111
use LLM\Agents\AgentExecutor\InterceptorHandler;
1212

13+
/**
14+
* This interceptor is responsible for injecting the agent's configuration options into the execution options.
15+
*/
1316
final readonly class InjectOptionsInterceptor implements ExecutorInterceptorInterface
1417
{
1518
public function __construct(

src/AgentExecutor/Interceptor/InjectResponseIntoPromptInterceptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use LLM\Agents\LLM\Response\ChatResponse;
1212
use LLM\Agents\LLM\Response\ToolCalledResponse;
1313

14+
/**
15+
* This interceptor is responsible for injecting the LLM response into the prompt history.
16+
*/
1417
final class InjectResponseIntoPromptInterceptor implements ExecutorInterceptorInterface
1518
{
1619
public function execute(

src/AgentExecutor/Interceptor/InjectToolsInterceptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use LLM\Agents\Tool\ToolInterface;
1717
use LLM\Agents\Tool\ToolRepositoryInterface;
1818

19+
/**
20+
* This interceptor is responsible for injecting the tools into the prompt if the agent has linked tools.
21+
*/
1922
final readonly class InjectToolsInterceptor implements ExecutorInterceptorInterface
2023
{
2124
public function __construct(

src/AgentExecutor/Interceptor/TokenLimitRetryInterceptor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use LLM\Agents\AgentExecutor\InterceptorHandler;
1111
use LLM\Agents\LLM\Exception\LimitExceededException;
1212

13+
/**
14+
* This interceptor is responsible for retrying the execution if the token limit is exceeded.
15+
* It will increment the limit by a specified step and retry the execution.
16+
*/
1317
final readonly class TokenLimitRetryInterceptor implements ExecutorInterceptorInterface
1418
{
1519
public function __construct(

src/AgentExecutor/Interceptor/ToolExecutorInterceptor.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
use LLM\Agents\LLM\Response\ToolCalledResponse;
1515
use LLM\Agents\Tool\ToolExecutor;
1616

17+
/**
18+
* This interceptor is responsible for calling the tools if LLM asks for it. After calling the tools, it adds the
19+
* tools responses to the prompt history and return the result of tools execution to the LLM.
20+
*
21+
* If the option 'return_tool_result' is set to true, the interceptor will return the tools result instead of adding
22+
* it to the prompt.
23+
*/
1724
final readonly class ToolExecutorInterceptor implements ExecutorInterceptorInterface
1825
{
1926
public function __construct(

src/Solution/AgentLink.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace LLM\Agents\Solution;
66

7+
/**
8+
* Represents a link to another agent.
9+
*/
710
class AgentLink extends Solution
811
{
912
public function __construct(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\Solution\Metadata;
6+
7+
/**
8+
* Instruct an agent to use a specific DTO class to format the output.
9+
* If the class is DTO it must implement \JsonSerializable.
10+
*/
11+
final readonly class DtoOutputFormatter extends Option
12+
{
13+
/**
14+
* @param class-string $dtoClass DTO class or an Enum
15+
*/
16+
public function __construct(string $dtoClass)
17+
{
18+
parent::__construct(
19+
key: 'output_formatter',
20+
content: $dtoClass,
21+
);
22+
}
23+
}

src/Solution/Metadata/Memory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\Solution\Metadata;
6+
7+
use LLM\Agents\Solution\MetadataType;
8+
use LLM\Agents\Solution\SolutionMetadata;
9+
10+
/**
11+
* Add some memory to an agent.
12+
*/
13+
final readonly class Memory extends SolutionMetadata
14+
{
15+
public function __construct(
16+
\Stringable|int|string $content,
17+
?string $key = 'memory',
18+
) {
19+
parent::__construct(
20+
type: MetadataType::Memory,
21+
key: $key,
22+
content: $content,
23+
);
24+
}
25+
}

src/Solution/Metadata/Option.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\Solution\Metadata;
6+
7+
use LLM\Agents\Solution\MetadataType;
8+
use LLM\Agents\Solution\SolutionMetadata;
9+
10+
readonly class Option extends SolutionMetadata
11+
{
12+
public function __construct(
13+
string $key,
14+
mixed $content,
15+
) {
16+
parent::__construct(
17+
type: MetadataType::Configuration,
18+
key: $key,
19+
content: $content,
20+
);
21+
}
22+
}

0 commit comments

Comments
 (0)