Skip to content

Commit

Permalink
feat: override the chain's llm via option (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-hertel authored Dec 28, 2024
1 parent 3c48f5a commit c53edc1
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c53edc1

Please sign in to comment.