Skip to content

Commit dcb9639

Browse files
committed
Add embedding content support
1 parent 5dd8d46 commit dcb9639

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _This library is not developed or endorsed by Google._
2525
- [Text Generation using Image File](#text-generation-using-image-file)
2626
- [Text Generation using Image Data](#text-generation-using-image-data)
2727
- [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations)
28+
- [Text Embeddings](#text-embeddings)
2829
- [Tokens counting](#tokens-counting)
2930
- [Listing models](#listing-models)
3031
- [Credits](#credits)
@@ -150,6 +151,19 @@ print $chat->sendMessage('in Go');
150151
// This code will print "Hello World!" to the standard output.
151152
```
152153

154+
### Text Embeddings
155+
156+
```php
157+
use GeminiAPI\Laravel\Facades\Gemini;
158+
159+
print_r(Gemini::embedText('PHP in less than 100 chars'));
160+
// [
161+
// [0] => 0.041395925
162+
// [1] => -0.017692696
163+
// ...
164+
// ]
165+
```
166+
153167
### Tokens counting
154168

155169
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^8.1",
25-
"gemini-api-php/client": "^1.2",
25+
"gemini-api-php/client": "^1.3.1",
2626
"illuminate/support": "^9.0 || ^10.0 || ^11.0",
2727
"psr/container": "^1.0 || ^2.0",
2828
"psr/http-client": "^1.0"

src/Facades/Gemini.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/**
1212
* @method static int countTokens(string $prompt)
13+
* @method static float[] embedText(string $prompt)
1314
* @method static string generateText(string $prompt)
1415
* @method static string generateTextUsingImage(string $imageType, string $image, string $prompt = '')
1516
* @method static string generateTextUsingImageFile(string $imageType, string $imagePath, string $prompt = '')

src/Gemini.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ public function __construct(
3434
) {
3535
}
3636

37+
/**
38+
* @return float[]
39+
*
40+
* @throws ClientExceptionInterface
41+
*/
42+
public function embedText(string $prompt, ?string $title = null): array
43+
{
44+
$model = $this->client->embeddingModel(ModelName::Embedding);
45+
46+
$response = $title
47+
? $model->embedContentWithTitle($title, new TextPart($prompt))
48+
: $model->embedContent(new TextPart($prompt));
49+
50+
return $response->embedding->values;
51+
}
52+
3753
/**
3854
* @throws ClientExceptionInterface
3955
*/

0 commit comments

Comments
 (0)