-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support streaming of responses
- Loading branch information
1 parent
ddf5110
commit c15e9dd
Showing
17 changed files
with
250 additions
and
31 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,36 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Anthropic\Model\Claude; | ||
use PhpLlm\LlmChain\Anthropic\Platform\Anthropic; | ||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Message\Message; | ||
use PhpLlm\LlmChain\Message\MessageBag; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); | ||
|
||
if (empty($_ENV['ANTHROPIC_API_KEY'])) { | ||
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = new Anthropic(HttpClient::create(), $_ENV['ANTHROPIC_API_KEY']); | ||
$llm = new Claude($platform); | ||
|
||
$chain = new Chain($llm); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are a thoughtful philosopher.'), | ||
Message::ofUser('What is the purpose of an ant?'), | ||
); | ||
$response = $chain->call($messages, [ | ||
'stream' => true, // enable streaming of response text | ||
]); | ||
|
||
assert($response instanceof Generator); | ||
|
||
foreach ($response as $word) { | ||
echo $word; | ||
} | ||
echo 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,37 @@ | ||
<?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 Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\HttpClient\EventSourceHttpClient; | ||
|
||
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(new EventSourceHttpClient(), $_ENV['OPENAI_API_KEY']); | ||
$llm = new Gpt($platform, Version::gpt4oMini()); | ||
|
||
$chain = new Chain($llm); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are a thoughtful philosopher.'), | ||
Message::ofUser('What is the purpose of an ant?'), | ||
); | ||
$response = $chain->call($messages, [ | ||
'stream' => true, // enable streaming of response text | ||
]); | ||
|
||
assert($response instanceof Generator); | ||
|
||
foreach ($response as $word) { | ||
echo $word; | ||
} | ||
echo 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
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
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
Oops, something went wrong.