Embeddings convert text into a vector of numbers that capture semantic meaning. Similar words/sentences have vectors that are close together — useful for semantic search, clustering, and similarity comparisons.
pnpm add @ai-sdk/openai
import { embed } from "ai";
import { openai } from "@ai-sdk/openai";
const { embedding } = await embed({
model: openai.embedding("text-embedding-3-small"),
value: "Artificial intelligence is transforming the world.",
});
console.log(embedding); // → [0.023, -0.041, 0.334, ...]
console.log(embedding.length); // → 1536 dimensions
Embed multiple texts in one call with embedMany:
import { embedMany } from "ai";
import { openai } from "@ai-sdk/openai";
const { embeddings } = await embedMany({
model: openai.embedding("text-embedding-3-small"),
values: [
"Artificial intelligence",
"Machine learning",
"Deep neural networks",
],
});
// embeddings is an array aligned with values
console.log(embeddings[0]); // → vector for "Artificial intelligence"
function cosineSimilarity(a: number[], b: number[]): number {
const dot = a.reduce((sum, val, i) => sum + val * b[i], 0);
const magA = Math.sqrt(a.reduce((sum, val) => sum + val * val, 0));
const magB = Math.sqrt(b.reduce((sum, val) => sum + val * val, 0));
return dot / (magA * magB);
}
const { embeddings } = await embedMany({
model: openai.embedding("text-embedding-3-small"),
values: ["cat", "kitten", "car"],
});
console.log(cosineSimilarity(embeddings[0], embeddings[1])); // → ~0.92 (similar)
console.log(cosineSimilarity(embeddings[0], embeddings[2])); // → ~0.41 (different)
| Use Case | How |
|---|---|
| Semantic search | Embed query + docs, find closest vectors |
| Duplicate detection | High similarity → likely duplicates |
| Clustering | Group similar items by vector distance |
| Recommendation | Find items similar to what a user liked |