-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ab2764
commit ff58c37
Showing
15 changed files
with
220 additions
and
38 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
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
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain; | ||
|
||
use PhpLlm\LlmChain\LanguageModel; | ||
use PhpLlm\LlmChain\Message\MessageBag; | ||
|
||
final class Input | ||
{ | ||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
public readonly LanguageModel $llm, | ||
public readonly MessageBag $messages, | ||
private array $options, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function setOptions(array $options): void | ||
{ | ||
$this->options = $options; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain; | ||
|
||
interface InputProcessor | ||
{ | ||
public function processInput(Input $input): void; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain; | ||
|
||
use PhpLlm\LlmChain\LanguageModel; | ||
use PhpLlm\LlmChain\Message\MessageBag; | ||
use PhpLlm\LlmChain\Response\Response; | ||
|
||
final readonly class Output | ||
{ | ||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
public LanguageModel $llm, | ||
public Response $response, | ||
public MessageBag $messages, | ||
public array $options, | ||
) { | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain; | ||
|
||
interface OutputProcessor | ||
{ | ||
public function processOutput(Output $output): mixed; | ||
} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\StructuredOutput; | ||
|
||
use PhpLlm\LlmChain\Chain\Input; | ||
use PhpLlm\LlmChain\Chain\InputProcessor; | ||
use PhpLlm\LlmChain\Chain\Output; | ||
use PhpLlm\LlmChain\Chain\OutputProcessor; | ||
use PhpLlm\LlmChain\Exception\MissingModelSupport; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
final class ChainProcessor implements InputProcessor, OutputProcessor | ||
{ | ||
private string $outputStructure; | ||
|
||
public function __construct( | ||
private readonly ResponseFormatFactory $responseFormatFactory, | ||
private readonly SerializerInterface $serializer, | ||
) { | ||
} | ||
|
||
public function processInput(Input $input): void | ||
{ | ||
if (!$input->llm->supportsStructuredOutput()) { | ||
throw MissingModelSupport::forStructuredOutput($input->llm::class); | ||
} | ||
|
||
$options = $input->getOptions(); | ||
$options['response_format'] = $this->responseFormatFactory->create($options['output_structure']); | ||
|
||
$this->outputStructure = $options['output_structure']; | ||
unset($options['output_structure']); | ||
|
||
$input->setOptions($options); | ||
} | ||
|
||
public function processOutput(Output $output): object | ||
{ | ||
return $this->serializer->deserialize($output->response->getContent(), $this->outputStructure, 'json'); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\ToolBox; | ||
|
||
use PhpLlm\LlmChain\Chain\Input; | ||
use PhpLlm\LlmChain\Chain\InputProcessor; | ||
use PhpLlm\LlmChain\Chain\Output; | ||
use PhpLlm\LlmChain\Chain\OutputProcessor; | ||
use PhpLlm\LlmChain\Exception\MissingModelSupport; | ||
use PhpLlm\LlmChain\Message\Message; | ||
|
||
final readonly class ChainProcessor implements InputProcessor, OutputProcessor | ||
{ | ||
public function __construct( | ||
private ToolBox $toolBox, | ||
) { | ||
} | ||
|
||
public function processInput(Input $input): void | ||
{ | ||
if (!$input->llm->supportsToolCalling()) { | ||
throw MissingModelSupport::forToolCalling($input->llm::class); | ||
} | ||
|
||
$options['tools'] = $this->toolBox->getMap(); | ||
$input->setOptions($options); | ||
} | ||
|
||
public function processOutput(Output $output): mixed | ||
{ | ||
$response = $output->response; | ||
$messages = clone $output->messages; | ||
|
||
while ($response->hasToolCalls()) { | ||
$messages[] = Message::ofAssistant(toolCalls: $response->getToolCalls()); | ||
|
||
foreach ($response->getToolCalls() as $toolCall) { | ||
$result = $this->toolBox->execute($toolCall); | ||
$messages[] = Message::ofToolCall($toolCall, $result); | ||
} | ||
|
||
$response = $output->llm->call($messages, $output->options); | ||
} | ||
|
||
return $response->getContent(); | ||
} | ||
} |