-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove visitor infavor of single converter class
- Loading branch information
1 parent
9271a5b
commit 95b21cb
Showing
22 changed files
with
281 additions
and
194 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 |
---|---|---|
|
@@ -41,4 +41,4 @@ PINECONE_HOST= | |
RUN_EXPENSIVE_EXAMPLES=false | ||
|
||
# For using Gemini | ||
GOOGLE_API_KEY= | ||
GOOGLE_API_KEY= |
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,32 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Bridge\Google\Gemini; | ||
use PhpLlm\LlmChain\Bridge\Google\PlatformFactory; | ||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Model\Message\Content\Image; | ||
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['GOOGLE_API_KEY'])) { | ||
echo 'Please set the GOOGLE_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = PlatformFactory::create($_ENV['GOOGLE_API_KEY']); | ||
$llm = new Gemini(Gemini::GEMINI_1_5_FLASH); | ||
|
||
$chain = new Chain($platform, $llm); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'), | ||
Message::ofUser( | ||
'Describe the image as a comedian would do it.', | ||
new Image(dirname(__DIR__).'/tests/Fixture/image.jpg'), | ||
), | ||
); | ||
$response = $chain->call($messages); | ||
|
||
echo $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,33 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Bridge\Google\Gemini; | ||
use PhpLlm\LlmChain\Bridge\Google\PlatformFactory; | ||
use PhpLlm\LlmChain\Chain; | ||
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['GOOGLE_API_KEY'])) { | ||
echo 'Please set the GOOGLE_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = PlatformFactory::create($_ENV['GOOGLE_API_KEY']); | ||
$llm = new Gemini(Gemini::GEMINI_2_FLASH); | ||
|
||
$chain = new Chain($platform, $llm); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are a funny clown that entertains people.'), | ||
Message::ofUser('What is the purpose of an ant?'), | ||
); | ||
$response = $chain->call($messages, [ | ||
'stream' => true, // enable streaming of response text | ||
]); | ||
|
||
foreach ($response->getContent() 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\Google; | ||
|
||
use PhpLlm\LlmChain\Model\Message\AssistantMessage; | ||
use PhpLlm\LlmChain\Model\Message\Content\Image; | ||
use PhpLlm\LlmChain\Model\Message\Content\Text; | ||
use PhpLlm\LlmChain\Model\Message\MessageBagInterface; | ||
use PhpLlm\LlmChain\Model\Message\MessageInterface; | ||
use PhpLlm\LlmChain\Model\Message\Role; | ||
use PhpLlm\LlmChain\Model\Message\UserMessage; | ||
|
||
use function Symfony\Component\String\u; | ||
|
||
final class GooglePromptConverter | ||
{ | ||
/** | ||
* @return array{ | ||
* contents: list<array{ | ||
* role: 'model'|'user', | ||
* parts: list<array{inline_data?: array{mime_type: string, data: string}|array{text: string}}> | ||
* }>, | ||
* system_instruction?: array{parts: array{text: string}} | ||
* } | ||
*/ | ||
public function convertToPrompt(MessageBagInterface $bag): array | ||
{ | ||
$body = ['contents' => []]; | ||
|
||
$systemMessage = $bag->getSystemMessage(); | ||
if (null !== $systemMessage) { | ||
$body['system_instruction'] = [ | ||
'parts' => ['text' => $systemMessage->content], | ||
]; | ||
} | ||
|
||
foreach ($bag->withoutSystemMessage()->getMessages() as $message) { | ||
$body['contents'][] = [ | ||
'role' => $message->getRole()->equals(Role::Assistant) ? 'model' : 'user', | ||
'parts' => $this->convertMessage($message), | ||
]; | ||
} | ||
|
||
return $body; | ||
} | ||
|
||
/** | ||
* @return list<array{inline_data?: array{mime_type: string, data: string}|array{text: string}}> | ||
*/ | ||
private function convertMessage(MessageInterface $message): array | ||
{ | ||
if ($message instanceof AssistantMessage) { | ||
return [['text' => $message->content]]; | ||
} | ||
|
||
if ($message instanceof UserMessage) { | ||
$parts = []; | ||
foreach ($message->content as $content) { | ||
if ($content instanceof Text) { | ||
$parts[] = ['text' => $content->text]; | ||
} | ||
if ($content instanceof Image) { | ||
$parts[] = ['inline_data' => [ | ||
'mime_type' => u($content->url)->after('data:')->before(';')->toString(), | ||
'data' => u($content->url)->after('base64,')->toString(), | ||
]]; | ||
} | ||
} | ||
|
||
return $parts; | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.