diff --git a/src/Chain.php b/src/Chain.php index f649218e..367d5a57 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -47,20 +47,31 @@ public function __construct( */ public function call(MessageBag $messages, array $options = []): ResponseInterface { - $input = new Input($this->llm, $messages, $options); + $llm = $this->llm; + + if (array_key_exists('llm', $options)) { + if (!$options['llm'] instanceof LanguageModel) { + throw new InvalidArgumentException(sprintf('Option "llm" must be an instance of %s.', LanguageModel::class)); + } + + $llm = $options['llm']; + unset($options['llm']); + } + + $input = new Input($llm, $messages, $options); array_map(fn (InputProcessor $processor) => $processor->processInput($input), $this->inputProcessor); - if ($messages->containsImage() && !$this->llm->supportsImageInput()) { - throw MissingModelSupport::forImageInput($this->llm::class); + if ($messages->containsImage() && !$llm->supportsImageInput()) { + throw MissingModelSupport::forImageInput($llm::class); } - $response = $this->platform->request($this->llm, $messages, $options = $input->getOptions()); + $response = $this->platform->request($llm, $messages, $options = $input->getOptions()); if ($response instanceof AsyncResponse) { $response = $response->unwrap(); } - $output = new Output($this->llm, $response, $messages, $options); + $output = new Output($llm, $response, $messages, $options); array_map(fn (OutputProcessor $processor) => $processor->processOutput($output), $this->outputProcessor); return $output->response;