-
-
Notifications
You must be signed in to change notification settings - Fork 57
feat: add Gemini Embeddings #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Platform\Bridge\Google\Embeddings; | ||
use Symfony\AI\Platform\Bridge\Google\PlatformFactory; | ||
use Symfony\AI\Platform\Response\VectorResponse; | ||
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']); | ||
$embeddings = new Embeddings(); | ||
|
||
$response = $platform->request($embeddings, <<<TEXT | ||
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers. | ||
The people of Japan were very kind and hardworking. They loved their country very much and took care of it. The | ||
country was very peaceful and prosperous. The people lived happily ever after. | ||
TEXT); | ||
|
||
assert($response instanceof VectorResponse); | ||
|
||
echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL; |
This file contains hidden or 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,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\DBAL\Tools\DsnParser; | ||
use PhpLlm\LlmChain\Chain\Chain; | ||
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor; | ||
use PhpLlm\LlmChain\Chain\Toolbox\Tool\SimilaritySearch; | ||
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox; | ||
use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings; | ||
use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings\TaskType; | ||
use PhpLlm\LlmChain\Platform\Bridge\Google\Gemini; | ||
use PhpLlm\LlmChain\Platform\Bridge\Google\PlatformFactory; | ||
use PhpLlm\LlmChain\Platform\Message\Message; | ||
use PhpLlm\LlmChain\Platform\Message\MessageBag; | ||
use PhpLlm\LlmChain\Store\Bridge\MariaDB\Store; | ||
use PhpLlm\LlmChain\Store\Document\Metadata; | ||
use PhpLlm\LlmChain\Store\Document\TextDocument; | ||
use PhpLlm\LlmChain\Store\Indexer; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
require_once dirname(__DIR__, 2).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env'); | ||
|
||
if (empty($_ENV['GOOGLE_API_KEY']) || empty($_ENV['MARIADB_URI'])) { | ||
echo 'Please set GOOGLE_API_KEY and MARIADB_URI environment variables.'.\PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
// initialize the store | ||
$store = Store::fromDbal( | ||
connection: DriverManager::getConnection((new DsnParser())->parse($_ENV['MARIADB_URI'])), | ||
tableName: 'my_table', | ||
indexName: 'my_index', | ||
vectorFieldName: 'embedding', | ||
); | ||
|
||
// our data | ||
$movies = [ | ||
['title' => 'Inception', 'description' => 'A skilled thief is given a chance at redemption if he can successfully perform inception, the act of planting an idea in someone\'s subconscious.', 'director' => 'Christopher Nolan'], | ||
['title' => 'The Matrix', 'description' => 'A hacker discovers the world he lives in is a simulated reality and joins a rebellion to overthrow its controllers.', 'director' => 'The Wachowskis'], | ||
['title' => 'The Godfather', 'description' => 'The aging patriarch of an organized crime dynasty transfers control of his empire to his reluctant son.', 'director' => 'Francis Ford Coppola'], | ||
]; | ||
|
||
// create embeddings and documents | ||
foreach ($movies as $i => $movie) { | ||
$documents[] = new TextDocument( | ||
id: Uuid::v4(), | ||
content: 'Title: '.$movie['title'].\PHP_EOL.'Director: '.$movie['director'].\PHP_EOL.'Description: '.$movie['description'], | ||
metadata: new Metadata($movie), | ||
); | ||
} | ||
|
||
// initialize the table | ||
$store->initialize(['dimensions' => 768]); | ||
|
||
// create embeddings for documents | ||
$platform = PlatformFactory::create($_ENV['GOOGLE_API_KEY']); | ||
$embeddings = new Embeddings(options: ['dimensions' => 768, 'task_type' => TaskType::SemanticSimilarity]); | ||
$indexer = new Indexer($platform, $embeddings, $store); | ||
$indexer->index($documents); | ||
|
||
$model = new Gemini(Gemini::GEMINI_2_FLASH_LITE); | ||
|
||
$similaritySearch = new SimilaritySearch($platform, $embeddings, $store); | ||
$toolbox = Toolbox::create($similaritySearch); | ||
$processor = new ChainProcessor($toolbox); | ||
$chain = new Chain($platform, $model, [$processor], [$processor]); | ||
|
||
$messages = new MessageBag( | ||
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'), | ||
Message::ofUser('Which movie fits the theme of the mafia?') | ||
); | ||
$response = $chain->call($messages); | ||
|
||
echo $response->getContent().\PHP_EOL; |
This file contains hidden or 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 hidden or 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 | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\AI\Platform\Bridge\Google; | ||
|
||
use Symfony\AI\Platform\Bridge\Google\Embeddings\TaskType; | ||
use Symfony\AI\Platform\Capability; | ||
use Symfony\AI\Platform\Model; | ||
|
||
/** | ||
* @author Valtteri R <[email protected]> | ||
*/ | ||
class Embeddings extends Model | ||
{ | ||
/** Supported dimensions: 3072, 1536, or 768 */ | ||
public const GEMINI_EMBEDDING_EXP_03_07 = 'gemini-embedding-exp-03-07'; | ||
/** Fixed 768 dimensions */ | ||
public const TEXT_EMBEDDING_004 = 'text-embedding-004'; | ||
/** Fixed 768 dimensions */ | ||
public const EMBEDDING_001 = 'embedding-001'; | ||
|
||
/** | ||
* @param array{dimensions?: int, task_type?: TaskType|string} $options | ||
*/ | ||
public function __construct(string $name = self::GEMINI_EMBEDDING_EXP_03_07, array $options = []) | ||
{ | ||
parent::__construct($name, [Capability::INPUT_MULTIPLE], $options); | ||
} | ||
} |
This file contains hidden or 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,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\AI\Platform\Bridge\Google\Embeddings; | ||
|
||
use Symfony\AI\Platform\Bridge\Google\Embeddings; | ||
use Symfony\AI\Platform\Exception\RuntimeException; | ||
use Symfony\AI\Platform\Model; | ||
use Symfony\AI\Platform\ModelClientInterface; | ||
use Symfony\AI\Platform\Response\VectorResponse; | ||
use Symfony\AI\Platform\ResponseConverterInterface; | ||
use Symfony\AI\Platform\Vector\Vector; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
/** | ||
* @author Valtteri R <[email protected]> | ||
*/ | ||
final readonly class ModelClient implements ModelClientInterface, ResponseConverterInterface | ||
{ | ||
public function __construct( | ||
private HttpClientInterface $httpClient, | ||
#[\SensitiveParameter] | ||
private string $apiKey, | ||
) { | ||
} | ||
|
||
public function supports(Model $model): bool | ||
{ | ||
return $model instanceof Embeddings; | ||
} | ||
|
||
public function request(Model $model, array|string $payload, array $options = []): ResponseInterface | ||
{ | ||
$url = \sprintf('https://generativelanguage.googleapis.com/v1beta/models/%s:%s', $model->getName(), 'batchEmbedContents'); | ||
$modelOptions = $model->getOptions(); | ||
|
||
return $this->httpClient->request('POST', $url, [ | ||
'headers' => [ | ||
'x-goog-api-key' => $this->apiKey, | ||
], | ||
'json' => [ | ||
'requests' => array_map( | ||
static fn (string $text) => array_filter([ | ||
'model' => 'models/'.$model->getName(), | ||
'content' => ['parts' => [['text' => $text]]], | ||
'outputDimensionality' => $modelOptions['dimensions'] ?? null, | ||
'taskType' => $modelOptions['task_type'] ?? null, | ||
'title' => $options['title'] ?? null, | ||
]), | ||
\is_array($payload) ? $payload : [$payload], | ||
), | ||
], | ||
]); | ||
} | ||
|
||
public function convert(ResponseInterface $response, array $options = []): VectorResponse | ||
{ | ||
$data = $response->toArray(); | ||
|
||
if (!isset($data['embeddings'])) { | ||
throw new RuntimeException('Response does not contain data'); | ||
} | ||
|
||
return new VectorResponse( | ||
...array_map( | ||
static fn (array $item): Vector => new Vector($item['values']), | ||
$data['embeddings'], | ||
), | ||
); | ||
} | ||
} |
This file contains hidden or 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 | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\AI\Platform\Bridge\Google\Embeddings; | ||
|
||
enum TaskType: string | ||
{ | ||
/** Unset value, which will default to one of the other enum values. */ | ||
public const TaskTypeUnspecified = 'TASK_TYPE_UNSPECIFIED'; | ||
/** Specifies the given text is a query in a search/retrieval setting. */ | ||
public const RetrievalQuery = 'RETRIEVAL_QUERY'; | ||
/** Specifies the given text is a document from the corpus being searched. */ | ||
public const RetrievalDocument = 'RETRIEVAL_DOCUMENT'; | ||
/** Specifies the given text will be used for STS. */ | ||
public const SemanticSimilarity = 'SEMANTIC_SIMILARITY'; | ||
/** Specifies that the given text will be classified. */ | ||
public const Classification = 'CLASSIFICATION'; | ||
/** Specifies that the embeddings will be used for clustering. */ | ||
public const Clustering = 'CLUSTERING'; | ||
/** Specifies that the given text will be used for question answering. */ | ||
public const QuestionAnswering = 'QUESTION_ANSWERING'; | ||
/** Specifies that the given text will be used for fact verification. */ | ||
public const FactVerification = 'FACT_VERIFICATION'; | ||
/** Specifies that the given text will be used for code retrieval. */ | ||
public const CodeRetrievalQuery = 'CODE_RETRIEVAL_QUERY'; | ||
} |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.