Skip to content

Commit

Permalink
Set chain of the processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Iyadhfaleh committed Dec 16, 2024
1 parent dfc8a7a commit 4cc95eb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public function __construct(
*/
public function process(MessageBag $messages, array $options = [], ?ChainAwareProcessor $chainProcessor = null): ResponseInterface
{
$input = new Input($this->llm, $messages, $options);
$chainProcessor?->setChain($this);

$input = new Input($this->llm, $messages, $options);
if ($chainProcessor) {
array_map(fn (InputProcessor $processor) => $processor->processInput($input), $chainProcessor->getInputProcessors());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Chain/ChainAwareProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace PhpLlm\LlmChain\Chain;

use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\ChainInterface;

interface ChainAwareProcessor
{
public function setChain(Chain $chain): void;
public function setChain(ChainInterface $chain): void;

public function addOutputProcessor(OutputProcessor $outputProcessor): self;

Expand Down
27 changes: 20 additions & 7 deletions src/Chain/ChainAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace PhpLlm\LlmChain\Chain;

use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Chain\ToolBox\ToolBoxInterface;
use PhpLlm\LlmChain\ChainInterface;
use PhpLlm\LlmChain\Exception\InvalidArgumentException;

trait ChainAwareTrait
{
private Chain $chain;
private ChainInterface $chain;

private ToolBoxInterface $toolBox;

Expand All @@ -23,7 +24,7 @@ trait ChainAwareTrait
*/
private array $outputProcessors;

public function setChain(Chain $chain): void
public function setChain(ChainInterface $chain): void
{
$this->chain = $chain;
}
Expand Down Expand Up @@ -56,16 +57,28 @@ public function addInputProcessor(InputProcessor $outputProcessor): self
return $this;
}

public function setOutputProcessors(array $outputProcessors): self
public function setOutputProcessors(iterable $outputProcessors): self
{
$this->outputProcessors = $outputProcessors;
foreach ($outputProcessors as $processor) {
if (!$processor instanceof OutputProcessor) {
throw new InvalidArgumentException(sprintf('Processor %s must implement %s interface.', $processor::class, OutputProcessor::class));
}

$this->addOutputProcessor($processor);
}

return $this;
}

public function setInputProcessors(array $inputProcessors): self
public function setInputProcessors(iterable $inputProcessors): self
{
$this->inputProcessors = $inputProcessors;
foreach ($inputProcessors as $processor) {
if (!$processor instanceof InputProcessor) {
throw new InvalidArgumentException(sprintf('Processor %s must implement %s interface.', $processor::class, InputProcessor::class));
}

$this->addInputProcessor($processor);
}

return $this;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Chain/ToolBox/ChainProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use PhpLlm\LlmChain\Chain\ChainAwareProcessor;
use PhpLlm\LlmChain\Chain\ChainAwareTrait;
use PhpLlm\LlmChain\Chain\Input;
use PhpLlm\LlmChain\Chain\InputProcessor;
use PhpLlm\LlmChain\Chain\Output;
use PhpLlm\LlmChain\Chain\OutputProcessor;
use PhpLlm\LlmChain\Exception\MissingModelSupport;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Response\ToolCallResponse;

final class ChainProcessor implements ChainAwareProcessor
final class ChainProcessor implements InputProcessor, OutputProcessor, ChainAwareProcessor
{
use ChainAwareTrait;

Expand Down

0 comments on commit 4cc95eb

Please sign in to comment.