Skip to content

Commit

Permalink
fix empty tool box chain processor is setting invalid tools option to…
Browse files Browse the repository at this point in the history
… input
  • Loading branch information
DZunke committed Nov 14, 2024
1 parent 1116782 commit d98a898
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ToolBox/ChainProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ public function processInput(Input $input): void
throw MissingModelSupport::forToolCalling($input->llm::class);
}

$toolMap = $this->toolBox->getMap();
if ([] === $toolMap) {
return;
}

$options = $input->getOptions();
$options['tools'] = $this->toolBox->getMap();
$options['tools'] = $toolMap;
$input->setOptions($options);
}

Expand Down
71 changes: 71 additions & 0 deletions tests/ToolBox/ChainProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\ToolBox;

use PhpLlm\LlmChain\Chain\Input;
use PhpLlm\LlmChain\Exception\MissingModelSupport;
use PhpLlm\LlmChain\LanguageModel;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\ToolBox\ChainProcessor;
use PhpLlm\LlmChain\ToolBox\ToolBoxInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(ChainProcessor::class)]
#[UsesClass(Input::class)]
#[UsesClass(MessageBag::class)]
#[UsesClass(MissingModelSupport::class)]
class ChainProcessorTest extends TestCase
{
#[Test]
public function processInputWithoutRegisteredToolsWillResultInNoOptionChange(): void
{
$toolBox = $this->createStub(ToolBoxInterface::class);
$toolBox->method('getMap')->willReturn([]);

$llm = $this->createMock(LanguageModel::class);
$llm->method('supportsToolCalling')->willReturn(true);

$chainProcessor = new ChainProcessor($toolBox);
$input = new Input($llm, new MessageBag(), []);

$chainProcessor->processInput($input);

self::assertSame([], $input->getOptions());
}

#[Test]
public function processInputWithRegisteredToolsWillResultInOptionChange(): void
{
$toolBox = $this->createStub(ToolBoxInterface::class);
$toolBox->method('getMap')->willReturn(['tool1' => 'tool1', 'tool2' => 'tool2']);

$llm = $this->createMock(LanguageModel::class);
$llm->method('supportsToolCalling')->willReturn(true);

$chainProcessor = new ChainProcessor($toolBox);
$input = new Input($llm, new MessageBag(), []);

$chainProcessor->processInput($input);

self::assertSame(['tools' => ['tool1' => 'tool1', 'tool2' => 'tool2']], $input->getOptions());
}

#[Test]
public function processInputWithUnsupportedToolCallingWillThrowException(): void
{
$this->expectException(MissingModelSupport::class);

$llm = $this->createMock(LanguageModel::class);
$llm->method('supportsToolCalling')->willReturn(false);

$chainProcessor = new ChainProcessor($this->createStub(ToolBoxInterface::class));
$input = new Input($llm, new MessageBag(), []);

$chainProcessor->processInput($input);
}
}

0 comments on commit d98a898

Please sign in to comment.