Skip to content

Embeddings

Create embeddings with Workers AI models through Laravel AI.

Updated

Call the Embeddings class from Laravel AI:

use Laravel\Ai\Embeddings;

$response = Embeddings::for(['hello', 'world'])
    ->generate(provider: 'workers-ai', model: '@cf/baai/bge-base-en-v1.5');

$response->embeddings; // one vector per input

$response->tokens;                // laravel/ai 0.9 to 0.11: total tokens
$response->usage->inputTokens;    // laravel/ai 1.x: total tokens

With no model:, the package uses @cf/baai/bge-large-en-v1.5. To change the default, set models.embeddings.default.

Pass extra request fields

To add request fields, call withProviderOptions():

Embeddings::for(['hello'])
    ->withProviderOptions(['encoding_format' => 'base64'])
    ->generate(provider: 'workers-ai');

Before laravel/ai 0.9, the method was called providerOptions().

The package sets model and input itself, so it removes those two keys from your options.

Dimensions

The config key models.embeddings.dimensions defaults to 1024. The package reports this value to Laravel AI but doesn’t send it to Cloudflare, because the model decides the real vector size. So when you change the model, set dimensions to that model’s size. Otherwise a vector column sized from this value won’t match your vectors.

Gateway note

On an authenticated gateway, embeddings on the gateway provider path workers-ai/v1 returned 401 in tests on 2026-09-09, and 200 with gateway_token on 2026-11-05. The default /compat path hasn’t had this problem. To use /compat, remove gateway_path. See Routes.

Type to search…

↑↓ navigate↵ selectEsc close