-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce chain awareness in chain processors to nest them
- Loading branch information
1 parent
1be8cb2
commit 009d216
Showing
9 changed files
with
147 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Message\Message; | ||
use PhpLlm\LlmChain\Message\MessageBag; | ||
use PhpLlm\LlmChain\OpenAI\Model\Gpt; | ||
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version; | ||
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI; | ||
use PhpLlm\LlmChain\StructuredOutput\ChainProcessor as StructuredOutputProcessor; | ||
use PhpLlm\LlmChain\StructuredOutput\ResponseFormatFactory; | ||
use PhpLlm\LlmChain\ToolBox\ChainProcessor as ToolProcessor; | ||
use PhpLlm\LlmChain\ToolBox\Tool\Clock; | ||
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer; | ||
use PhpLlm\LlmChain\ToolBox\ToolBox; | ||
use Symfony\Component\Clock\Clock as SymfonyClock; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); | ||
|
||
if (empty($_ENV['OPENAI_API_KEY'])) { | ||
echo 'Please set the OPENAI_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']); | ||
$llm = new Gpt($platform, Version::gpt4oMini()); | ||
|
||
$clock = new Clock(new SymfonyClock()); | ||
$toolBox = new ToolBox(new ToolAnalyzer(), [$clock]); | ||
$toolProcessor = new ToolProcessor($toolBox); | ||
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); | ||
$structuredOutputProcessor = new StructuredOutputProcessor(new ResponseFormatFactory(), $serializer); | ||
$chain = new Chain($llm, [$toolProcessor, $structuredOutputProcessor], [$toolProcessor, $structuredOutputProcessor]); | ||
|
||
$messages = new MessageBag(Message::ofUser('What date and time is it?')); | ||
$response = $chain->call($messages, ['response_format' => [ | ||
'type' => 'json_schema', | ||
'json_schema' => [ | ||
'name' => 'clock', | ||
'strict' => true, | ||
'schema' => [ | ||
'type' => 'object', | ||
'properties' => [ | ||
'date' => ['type' => 'string', 'description' => 'The current date in the format YYYY-MM-DD.'], | ||
'time' => ['type' => 'string', 'description' => 'The current time in the format HH:MM:SS.'], | ||
], | ||
'required' => ['date', 'time'], | ||
'additionalProperties' => false, | ||
], | ||
], | ||
]]); | ||
|
||
dump($response->getContent()); |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain; | ||
|
||
use PhpLlm\LlmChain\Chain; | ||
|
||
interface ChainAwareProcessor | ||
{ | ||
public function setChain(Chain $chain): 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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain; | ||
|
||
use PhpLlm\LlmChain\Chain; | ||
|
||
trait ChainAwareTrait | ||
{ | ||
private Chain $chain; | ||
|
||
public function setChain(Chain $chain): void | ||
{ | ||
$this->chain = $chain; | ||
} | ||
} |
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