Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty tool list in request is ending in 400 bad request #142

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}