Skip to content

Commit

Permalink
Add embedding content support
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkose committed Dec 27, 2023
1 parent 5dd8d46 commit c8c8a47
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ _This library is not developed or endorsed by Google._
- [Text Generation using Image File](#text-generation-using-image-file)
- [Text Generation using Image Data](#text-generation-using-image-data)
- [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations)
- [Text Embeddings](#text-embeddings)
- [Tokens counting](#tokens-counting)
- [Listing models](#listing-models)
- [Credits](#credits)
Expand Down Expand Up @@ -150,6 +151,19 @@ print $chat->sendMessage('in Go');
// This code will print "Hello World!" to the standard output.
```

### Text Embeddings

```php
use GeminiAPI\Laravel\Facades\Gemini;

print_r(Gemini::embedText('PHP in less than 100 chars'));
// [
// [0] => 0.041395925
// [1] => -0.017692696
// ...
// ]
```

### Tokens counting

```php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^8.1",
"gemini-api-php/client": "^1.2",
"gemini-api-php/client": "^1.3.1",
"illuminate/support": "^9.0 || ^10.0 || ^11.0",
"psr/container": "^1.0 || ^2.0",
"psr/http-client": "^1.0"
Expand Down
1 change: 1 addition & 0 deletions src/Facades/Gemini.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* @method static int countTokens(string $prompt)
* @method static float[] embedText(string $prompt)
* @method static string generateText(string $prompt)
* @method static string generateTextUsingImage(string $imageType, string $image, string $prompt = '')
* @method static string generateTextUsingImageFile(string $imageType, string $imagePath, string $prompt = '')
Expand Down
16 changes: 16 additions & 0 deletions src/Gemini.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ public function __construct(
) {
}

/**
* @return float[]
*
* @throws ClientExceptionInterface
*/
public function embedText(string $prompt, string $title = null): array
{
$model = $this->client->embeddingModel(ModelName::Embedding);

$response = $title
? $model->embedContentWithTitle($title, new TextPart($prompt))
: $model->embedContent(new TextPart($prompt));

return $response->embedding->values;
}

/**
* @throws ClientExceptionInterface
*/
Expand Down

0 comments on commit c8c8a47

Please sign in to comment.