-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from llm-agents-php/feature/token-limit-increaser
Adds a new execution interceptor to increase max_tokens when LimitExc…
- Loading branch information
Showing
3 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/AgentExecutor/Interceptor/TokenLimitRetryInterceptor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters