Skip to content

Commit

Permalink
Added support for fetching multiple text embeddings in batch.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaKladd committed Dec 4, 2024
1 parent bf42614 commit 0ea94e4
Show file tree
Hide file tree
Showing 7 changed files with 3,139 additions and 9 deletions.
18 changes: 16 additions & 2 deletions docs/core-concepts/embeddings.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@ $response = Prism::embeddings()
->generate();

// Get your embeddings vector
$embeddings = $response->embeddings;
$embeddings = $response->embeddings[0];

// Check token usage
echo $response->usage->tokens;
```

Each vector index corresponds to the input text index when sending multiple chunks:

```php
$response = Prism::embeddings()
->using(Provider::OpenAI, 'text-embedding-3-large')
->fromInput(['Your text here', 'Another text here'])
->generate();
```

```php
// Get the embeddings vectors
$vectors = $response->embeddings;
```

## Input Methods

You've got two convenient ways to feed text into the embeddings generator:
Expand Down Expand Up @@ -68,7 +82,7 @@ The embeddings response gives you everything you need:

```php
// Get the embeddings vector
$vector = $response->embeddings;
$vector = $response->embeddings[0];

// Check token usage
$tokenCount = $response->usage->tokens;
Expand Down
6 changes: 4 additions & 2 deletions src/Embeddings/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

class Generator
{
protected string $input = '';
/** @var string|array<string> */
protected string|array $input = '';

/** @var array<string, mixed> */
protected array $clientOptions = [];
Expand All @@ -34,7 +35,8 @@ public function using(string|ProviderEnum $provider, string $model): self
return $this;
}

public function fromInput(string $input): self
/** @param string|array<string> $input */
public function fromInput(string|array $input): self
{
$this->input = $input;

Expand Down
3 changes: 2 additions & 1 deletion src/Embeddings/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
class Request
{
/**
* @param string|array<string> $input
* @param array<string, mixed> $clientOptions
* @param array{0: array<int, int>|int, 1?: Closure|int, 2?: ?callable, 3?: bool} $clientRetry
*/
public function __construct(
public readonly string $model,
public readonly string $input,
public readonly string|array $input,
public readonly array $clientOptions,
public readonly array $clientRetry,
) {}
Expand Down
2 changes: 1 addition & 1 deletion src/Embeddings/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Response
{
/**
* @param array<int, int|string> $embeddings
* @param array<array<int, int|string>> $embeddings
*/
public function __construct(
public readonly array $embeddings,
Expand Down
5 changes: 4 additions & 1 deletion src/Providers/OpenAI/Handlers/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public function handle(Request $request): EmbeddingsResponse
));
}

/** @var array<int, array<string, mixed>> $dataItems */
$dataItems = data_get($data, 'data', []);

return new EmbeddingsResponse(
embeddings: data_get($data, 'data.0.embedding', []),
embeddings: collect($dataItems)->pluck('embedding')->toArray(),
usage: new EmbeddingsUsage(data_get($data, 'usage.total_tokens', null)),
);
}
Expand Down
Loading

0 comments on commit 0ea94e4

Please sign in to comment.