use Prism\Prism\Facades\Prism;$response = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromInput('Hello, this is a test sentence') ->asEmbeddings();$embedding = $response->embeddings[0];$vector = $embedding->embedding; // Array of floats
Generate embeddings for multiple texts in a single request:
$response = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromArray([ 'First sentence to embed', 'Second sentence to embed', 'Third sentence to embed' ]) ->asEmbeddings();// Process each embeddingforeach ($response->embeddings as $embedding) { $vector = $embedding->embedding; // Store or process the vector}
use Prism\Prism\ValueObjects\Media\Image;$image = Image::fromLocalPath('/path/to/image.jpg');
Not all providers support image embeddings. Common providers with image embedding support include CLIP-based models and multimodal embedding models like BGE-VL.
// Index documents with embeddings$documents = [ 'Machine learning is a subset of AI', 'Deep learning uses neural networks', 'Natural language processing analyzes text'];$response = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromArray($documents) ->asEmbeddings();// Store embeddings in databaseforeach ($response->embeddings as $index => $embedding) { Document::create([ 'content' => $documents[$index], 'embedding' => json_encode($embedding->embedding) ]);}// Search with query embedding$queryResponse = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromInput('What is deep learning?') ->asEmbeddings();$queryVector = $queryResponse->embeddings[0]->embedding;// Calculate cosine similarity with stored embeddings
// Generate embeddings for user preferences$userInterests = User::find($userId)->interests; // Array of text$userResponse = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromArray($userInterests) ->asEmbeddings();// Average the embeddings to get user profile$userProfile = calculateAverageEmbedding( $userResponse->embeddings);// Find similar content$contentResponse = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromArray($availableContent) ->asEmbeddings();// Calculate similarity and recommend$recommendations = findMostSimilar( $userProfile, $contentResponse->embeddings);
// Detect duplicate or near-duplicate content$newContent = 'This is a new article';// Get embedding for new content$newResponse = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromInput($newContent) ->asEmbeddings();$newEmbedding = $newResponse->embeddings[0]->embedding;// Compare with existing content embeddings$existingEmbeddings = Article::all() ->pluck('embedding') ->map(fn($e) => json_decode($e, true));foreach ($existingEmbeddings as $existing) { $similarity = cosineSimilarity($newEmbedding, $existing); if ($similarity > 0.95) { echo "Duplicate content detected!\n"; break; }}
// Group similar documents using embeddings$documents = Article::all()->pluck('content')->toArray();$response = Prism::embeddings() ->using('openai', 'text-embedding-3-small') ->fromArray($documents) ->asEmbeddings();// Extract embeddings as vectors$vectors = array_map( fn($embedding) => $embedding->embedding, $response->embeddings);// Apply clustering algorithm (e.g., K-means)$clusters = performKMeansClustering($vectors, $k = 5);// Assign articles to clustersforeach ($clusters as $clusterId => $articleIndices) { foreach ($articleIndices as $index) { Article::where('id', $index)->update([ 'cluster' => $clusterId ]); }}
The generate() method is deprecated. Use asEmbeddings() instead:
// Deprecated$response = Prism::embeddings()->fromInput('text')->generate();// Use this instead$response = Prism::embeddings()->fromInput('text')->asEmbeddings();