Embedding Use Cases
Search, recommendations, clustering, deduplication, and classification.
Once text is embedded, many features become easy because you can compare meaning at scale.
| Use case | How embeddings help |
|---|---|
| Semantic search | Find documents that mean the same thing as the query. |
| Recommendations | Suggest items similar to what a user liked. |
| Clustering | Group similar texts (topics, tickets, feedback) automatically. |
| Deduplication | Detect near-duplicate content by high similarity. |
| Classification | Label text by nearest labelled example. |
The most important use case for AI apps is powering RAG, covered next.
Example
{
"feature": "support ticket routing",
"idea": "Embed each incoming ticket, compare to team topic vectors, route to the closest team.",
"example_match": {"ticket": "card was charged twice", "routed_to": "Billing"}
}When to use it
- A SaaS help-desk uses embeddings to find the three most relevant help articles for each incoming support ticket and pre-populates a suggested reply.
- An e-learning platform clusters student essay embeddings to group similar responses for batch grading, reducing teacher review time by 40%.
- A fraud team deduplicates thousands of user-reported scam emails by removing entries whose embeddings are within 0.02 cosine distance of each other.
More examples
Semantic document search
Ranks FAQ entries by semantic similarity to a user query to power a help-desk search feature.
import numpy as np
from openai import OpenAI
client = OpenAI()
def embed(text):
return np.array(
client.embeddings.create(model='text-embedding-3-small', input=text).data[0].embedding
)
cosine = lambda a, b: np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
docs = {
'faq-1': 'How to reset your password in three steps.',
'faq-2': 'Billing and payment options explained.',
'faq-3': 'Setting up two-factor authentication.',
}
query = 'I forgot my login credentials'
q_vec = embed(query)
ranked = sorted(docs.items(), key=lambda kv: cosine(q_vec, embed(kv[1])), reverse=True)
print('Top result:', ranked[0][0], '—', ranked[0][1])Cluster texts with K-means
Embeds texts and clusters them with K-means, grouping semantically related sentences together.
import numpy as np
from sklearn.cluster import KMeans
from openai import OpenAI
client = OpenAI()
texts = [
'I love machine learning.',
'Deep learning is fascinating.',
'Pizza is my favourite food.',
'I enjoy pasta dishes.',
'Transformers changed NLP.',
]
vectors = np.array([
client.embeddings.create(model='text-embedding-3-small', input=t).data[0].embedding
for t in texts
])
labels = KMeans(n_clusters=2, random_state=42, n_init='auto').fit_predict(vectors)
for text, label in zip(texts, labels):
print(f'[Cluster {label}] {text}')Deduplicate with similarity threshold
Filters a list to remove entries whose embeddings are within the deduplication similarity threshold.
import numpy as np
from openai import OpenAI
client = OpenAI()
def embed(text):
return np.array(
client.embeddings.create(model='text-embedding-3-small', input=text).data[0].embedding
)
cosine = lambda a, b: np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
entries = [
'Special offer: click here to win a prize!',
'Click here now to claim your prize!', # near-duplicate
'Machine learning tutorial for beginners.',
]
DUP_THRESHOLD = 0.92
unique = [entries[0]]
for entry in entries[1:]:
if all(cosine(embed(entry), embed(u)) < DUP_THRESHOLD for u in unique):
unique.append(entry)
print('Unique entries:', unique)
Discussion