-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: empower input processor to change LLM and MessageBag
- Loading branch information
1 parent
bec9272
commit aea0b24
Showing
4 changed files
with
100 additions
and
15 deletions.
There are no files selected for viewing
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
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain; | ||
|
||
use PhpLlm\LlmChain\Exception\InvalidArgumentException; | ||
use PhpLlm\LlmChain\Model\LanguageModel; | ||
|
||
final class LlmOverrideInputProcessor implements InputProcessor | ||
{ | ||
public function processInput(Input $input): void | ||
{ | ||
$options = $input->getOptions(); | ||
|
||
if (!array_key_exists('llm', $options)) { | ||
return; | ||
} | ||
|
||
if (!$options['llm'] instanceof LanguageModel) { | ||
throw new InvalidArgumentException(sprintf('Option "llm" must be an instance of %s.', LanguageModel::class)); | ||
} | ||
|
||
$input->llm = $options['llm']; | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Tests\Chain; | ||
|
||
use PhpLlm\LlmChain\Bridge\Anthropic\Claude; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\GPT; | ||
use PhpLlm\LlmChain\Chain\Input; | ||
use PhpLlm\LlmChain\Chain\LlmOverrideInputProcessor; | ||
use PhpLlm\LlmChain\Exception\InvalidArgumentException; | ||
use PhpLlm\LlmChain\Model\Message\MessageBag; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(LlmOverrideInputProcessor::class)] | ||
#[UsesClass(GPT::class)] | ||
#[UsesClass(Claude::class)] | ||
#[UsesClass(Input::class)] | ||
#[UsesClass(MessageBag::class)] | ||
#[Small] | ||
final class LlmOverrideInputProcessorTest extends TestCase | ||
{ | ||
#[Test] | ||
public function processInputWithValidLlmOption(): void | ||
{ | ||
$gpt = new GPT(); | ||
$claude = new Claude(); | ||
$input = new Input($gpt, new MessageBag(), ['llm' => $claude]); | ||
|
||
$processor = new LlmOverrideInputProcessor(); | ||
$processor->processInput($input); | ||
|
||
self::assertSame($claude, $input->llm); | ||
} | ||
|
||
#[Test] | ||
public function processInputWithoutLlmOption(): void | ||
{ | ||
$gpt = new GPT(); | ||
$input = new Input($gpt, new MessageBag(), []); | ||
|
||
$processor = new LlmOverrideInputProcessor(); | ||
$processor->processInput($input); | ||
|
||
self::assertSame($gpt, $input->llm); | ||
} | ||
|
||
#[Test] | ||
public function processInputWithInvalidLlmOption(): void | ||
{ | ||
self::expectException(InvalidArgumentException::class); | ||
self::expectExceptionMessage('Option "llm" must be an instance of PhpLlm\LlmChain\Model\LanguageModel.'); | ||
|
||
$gpt = new GPT(); | ||
$model = new Embeddings(); | ||
$input = new Input($gpt, new MessageBag(), ['llm' => $model]); | ||
|
||
$processor = new LlmOverrideInputProcessor(); | ||
$processor->processInput($input); | ||
} | ||
} |