Basic usage
Generate embeddings for a text input:embeddings property contains an array of floating-point numbers representing the text in vector space.
Supported models
Only Google Gemini models support embeddings:| Model | Dimensions | Use Case |
|---|---|---|
text-embedding-005 | 768 | General-purpose text embeddings |
text-embedding-004 | 768 | Previous generation embeddings |
textembedding-gecko@003 | 768 | Gecko model family |
textembedding-gecko-multilingual@001 | 768 | Multilingual support |
Embeddings are only supported for the Gemini schema. Partner models (Anthropic, Mistral, Meta, etc.) do not support embeddings through Vertex AI.
Complete example
Here’s a complete example showing how to generate and use embeddings:use Prism\Prism\Prism;
use Prism\Vertex\Enums\Vertex;
$response = Prism::embeddings()
->using(Vertex::Gemini, 'text-embedding-005')
->fromInput('The sky is blue')
->asEmbeddings();
$embeddings = $response->embeddings;
use Illuminate\Support\Facades\DB;
DB::table('documents')->insert([
'content' => 'The sky is blue',
'embedding' => json_encode($embeddings),
'created_at' => now(),
]);
function cosineSimilarity(array $a, array $b): float
{
$dotProduct = 0;
$normA = 0;
$normB = 0;
for ($i = 0; $i < count($a); $i++) {
$dotProduct += $a[$i] * $b[$i];
$normA += $a[$i] * $a[$i];
$normB += $b[$i] * $b[$i];
}
return $dotProduct / (sqrt($normA) * sqrt($normB));
}
// Generate embeddings for a query
$queryResponse = Prism::embeddings()
->using(Vertex::Gemini, 'text-embedding-005')
->fromInput('What color is the sky?')
->asEmbeddings();
$queryEmbedding = $queryResponse->embeddings;
// Compare with stored embeddings
$similarity = cosineSimilarity($embeddings, $queryEmbedding);
echo "Similarity: {$similarity}"; // High similarity score (close to 1.0)
Schema support
Embeddings are only supported for the Gemini schema:| Schema | Embeddings Support |
|---|---|
| Gemini | Yes |
| Anthropic | No |
| OpenAI | No |
Error handling
Handle embeddings errors appropriately:Unsupported schema error
Using embeddings with an unsupported schema throws an exception:Response object
TheasEmbeddings() method returns a Prism\Prism\Embeddings\Response object:
Use cases
Embeddings enable several powerful use cases:Semantic search
Find documents similar to a query:Document clustering
Group similar documents together:Recommendation systems
Recommend similar content based on user preferences:Best practices
// Index with text-embedding-005
$indexResponse = Prism::embeddings()
->using(Vertex::Gemini, 'text-embedding-005')
->fromInput('Document text')
->asEmbeddings();
// Query with the same model
$queryResponse = Prism::embeddings()
->using(Vertex::Gemini, 'text-embedding-005')
->fromInput('Query text')
->asEmbeddings();
function normalizeText(string $text): string
{
// Remove extra whitespace
$text = preg_replace('/\s+/', ' ', $text);
// Trim
$text = trim($text);
// Convert to lowercase (optional, depending on use case)
$text = strtolower($text);
return $text;
}
$response = Prism::embeddings()
->using(Vertex::Gemini, 'text-embedding-005')
->fromInput(normalizeText(' The Sky is BLUE '))
->asEmbeddings();
use Illuminate\Support\Facades\Cache;
function getEmbeddings(string $text): array
{
$cacheKey = 'embedding:' . md5($text);
return Cache::remember($cacheKey, now()->addDays(30), function () use ($text) {
$response = Prism::embeddings()
->using(Vertex::Gemini, 'text-embedding-005')
->fromInput($text)
->asEmbeddings();
return $response->embeddings;
});
}
$documents = [
'Document 1',
'Document 2',
'Document 3',
];
$embeddings = [];
foreach ($documents as $doc) {
$response = Prism::embeddings()
->using(Vertex::Gemini, 'text-embedding-005')
->fromInput($doc)
->asEmbeddings();
$embeddings[] = $response->embeddings;
// Add delay to avoid rate limits
usleep(100000); // 100ms
}
Authentication requirements
Embeddings support both Express and Standard modes:Express mode
config/prism.php
Standard mode
config/prism.php
Next steps
Text generation
Generate text responses using Vertex AI
Structured output
Generate JSON responses with schema validation
Multi-provider
Use multiple providers with different configurations