-
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: introducing async responses for parallel calling
- Loading branch information
1 parent
31a28d9
commit d811a0e
Showing
6 changed files
with
145 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\GPT; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; | ||
use PhpLlm\LlmChain\Model\Message\Message; | ||
use PhpLlm\LlmChain\Model\Message\MessageBag; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
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 = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$llm = new GPT(GPT::GPT_4O_MINI, [ | ||
'temperature' => 0.5, // default options for the model | ||
]); | ||
|
||
$messages = new MessageBag( | ||
Message::forSystem('You will be given a letter and you answer with only the next letter of the alphabet.'), | ||
); | ||
|
||
echo 'Initiating parallel calls to GPT on platform ...'.PHP_EOL; | ||
$responses = []; | ||
foreach (range('A', 'D') as $letter) { | ||
echo ' - Request for the letter '.$letter.' initiated.'.PHP_EOL; | ||
$responses[] = $platform->request($llm, $messages->with(Message::ofUser($letter))); | ||
} | ||
|
||
echo 'Waiting for the responses ...'.PHP_EOL; | ||
foreach ($responses as $response) { | ||
echo 'Next Letter: '.$response->getContent().PHP_EOL; | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; | ||
use PhpLlm\LlmChain\Model\Response\VectorResponse; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
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 = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$ada = new Embeddings(Embeddings::TEXT_ADA_002); | ||
$small = new Embeddings(Embeddings::TEXT_3_SMALL); | ||
$large = new Embeddings(Embeddings::TEXT_3_LARGE); | ||
|
||
echo 'Initiating parallel embeddings calls to platform ...'.PHP_EOL; | ||
$responses = []; | ||
foreach (['ADA' => $ada, 'Small' => $small, 'Large' => $large] as $name => $model) { | ||
echo ' - Request for model '.$name.' initiated.'.PHP_EOL; | ||
$responses[] = $platform->request($model, 'Hello, world!'); | ||
} | ||
|
||
echo 'Waiting for the responses ...'.PHP_EOL; | ||
foreach ($responses as $response) { | ||
assert($response instanceof VectorResponse); | ||
echo 'Dimensions: '.$response->getContent()[0]->getDimensions().PHP_EOL; | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Model\Response; | ||
|
||
use PhpLlm\LlmChain\Platform\ResponseConverter; | ||
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; | ||
|
||
final class AsyncResponse implements ResponseInterface | ||
{ | ||
private bool $isConverted = false; | ||
private ResponseInterface $convertedResponse; | ||
|
||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
private readonly ResponseConverter $responseConverter, | ||
private readonly HttpResponse $response, | ||
private readonly array $options = [], | ||
) { | ||
} | ||
|
||
public function getContent(): string|iterable|object|null | ||
{ | ||
return $this->unwrap()->getContent(); | ||
} | ||
|
||
public function unwrap(): ResponseInterface | ||
{ | ||
if (!$this->isConverted) { | ||
$this->convertedResponse = $this->responseConverter->convert($this->response, $this->options); | ||
$this->isConverted = true; | ||
} | ||
|
||
return $this->convertedResponse; | ||
} | ||
} |
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