Skip to content

Commit

Permalink
Merge pull request #14 from llm-agents-php/feature/token-limit-increaser
Browse files Browse the repository at this point in the history
Adds a new execution interceptor to increase max_tokens when LimitExc…
  • Loading branch information
butschster authored Sep 5, 2024
2 parents 37b1ba9 + 7e273af commit e549cdd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/AgentExecutor/Interceptor/TokenLimitRetryInterceptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace LLM\Agents\AgentExecutor\Interceptor;

use LLM\Agents\Agent\Execution;
use LLM\Agents\AgentExecutor\ExecutionInput;
use LLM\Agents\AgentExecutor\ExecutorInterceptorInterface;
use LLM\Agents\AgentExecutor\InterceptorHandler;
use LLM\Agents\LLM\Exception\LimitExceededException;

final readonly class TokenLimitRetryInterceptor implements ExecutorInterceptorInterface
{
public function __construct(
private int $maxRetries = 3,
private int $incrementStep = 500,
private string $limitKey = 'max_tokens',
) {}

public function execute(
ExecutionInput $input,
InterceptorHandler $next,
): Execution {
$retries = 0;

while (true) {
try {
return $next($input);
} catch (LimitExceededException $e) {
if (++$retries > $this->maxRetries) {
throw $e;
}

$newLimit = $e->currentLimit + $this->incrementStep;
$input = $input->withOptions(
$input->options->with($this->limitKey, $newLimit),
);
}
}
}
}
11 changes: 10 additions & 1 deletion src/LLM/Exception/LimitExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@

final class LimitExceededException extends LLMException
{

public function __construct(
public readonly int $currentLimit,
) {
parent::__construct(
\sprintf(
'Tokens limit exceeded: %d',
$currentLimit,
),
);
}
}
8 changes: 7 additions & 1 deletion src/LLM/Exception/TimeoutException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@

final class TimeoutException extends LLMException
{

public function __construct(
string $message = "Request timed out",
int $code = 0,
?\Throwable $previous = null,
) {
parent::__construct($message, $code, $previous);
}
}

0 comments on commit e549cdd

Please sign in to comment.