Skip to content

Commit 02f9c4a

Browse files
committed
feat: override the chain's llm via option
1 parent f7d8aa0 commit 02f9c4a

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/Chain.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,31 @@ public function __construct(
4747
*/
4848
public function call(MessageBag $messages, array $options = []): ResponseInterface
4949
{
50-
$input = new Input($this->llm, $messages, $options);
50+
$llm = $this->llm;
51+
52+
if (array_key_exists('llm', $options)) {
53+
if (!$options['llm'] instanceof LanguageModel) {
54+
throw new InvalidArgumentException(sprintf('Option "llm" must be an instance of %s.', LanguageModel::class));
55+
}
56+
57+
$llm = $options['llm'];
58+
unset($options['llm']);
59+
}
60+
61+
$input = new Input($llm, $messages, $options);
5162
array_map(fn (InputProcessor $processor) => $processor->processInput($input), $this->inputProcessor);
5263

53-
if ($messages->containsImage() && !$this->llm->supportsImageInput()) {
54-
throw MissingModelSupport::forImageInput($this->llm::class);
64+
if ($messages->containsImage() && !$llm->supportsImageInput()) {
65+
throw MissingModelSupport::forImageInput($llm::class);
5566
}
5667

57-
$response = $this->platform->request($this->llm, $messages, $options = $input->getOptions());
68+
$response = $this->platform->request($llm, $messages, $options = $input->getOptions());
5869

5970
if ($response instanceof AsyncResponse) {
6071
$response = $response->unwrap();
6172
}
6273

63-
$output = new Output($this->llm, $response, $messages, $options);
74+
$output = new Output($llm, $response, $messages, $options);
6475
array_map(fn (OutputProcessor $processor) => $processor->processOutput($output), $this->outputProcessor);
6576

6677
return $output->response;

0 commit comments

Comments
 (0)