Skip to content

Add embedding content support #4

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 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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