🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 5 Min Lesezeit
0

Cosine Similarity Search on Vectors in Postgres with pgvector

↗ Quelle (dev.to)
🗣️ Stimme:

In the realm of modern data processing, vector embeddings have become increasingly popular for applications in recommendation systems, search engines, NLP, and more. Vectors allow us to represent complex data (like documents, images, or user profiles) in a way that is both mathematically consistent and computationally efficient. Among various similarity measures, Cosine Similarity is widely used because it effectively measures the angle between two vectors, offering a reliable way to determine similarity. This article explores how to perform Cosine Similarity searches in PostgreSQL using the pgvector extension.



What is Cosine Similarity?

Cosine Similarity calculates the cosine of the angle between two vectors in a multi-dimensional space. The formula for cosine similarity between vectors A and B is:








Cosine Similarity=A⋅B∣∣A∣∣  ∣∣B∣∣
\text{Cosine Similarity} = \frac{A \cdot B}{||A|| \; ||B||}
Cosine Similarity=∣∣A∣∣∣∣B∣∣AB




Where:


  • A⋅B represents the dot product of vectors

  • ∣∣A∣∣ and ∣∣B∣∣ are the magnitudes (or lengths) of vectors A and B.




Cosine similarity is particularly effective for text data, where we want to ignore differences in magnitude and focus on the direction of vectors. This is why it's commonly used for tasks like text classification, image retrieval, and recommendation systems.



Introducing pgvector

Postgres's pgvector extension allows us to store and perform similarity searches on vectors. pgvector supports three primary distance metrics:




  • Euclidean distance

  • Inner product

  • Cosine similarity



For applications where direction is more important than magnitude, cosine similarity is often preferred.



Setting up pgvector in PostgreSQL

To use pgvector, you must first install it in your PostgreSQL instance. You can do this by running:




CODE
CREATE EXTENSION IF NOT EXISTS vector;






To perform a cosine similarity search on vectors stored in your django model, you’ll need to utilize PostgreSQL’s pgvector extension’s cosine similarity function. Here’s how you can implement this in Django:




CODE
from django.db import models
from pgvector.django import VectorField
from django.contrib.postgres.indexes import GinIndex
from sentence_transformers import SentenceTransformer

# Load SentenceTransformer model once
model = SentenceTransformer('all-MiniLM-L6-v2')

class Blog(models.Model):
title = models.CharField(max_length=255, db_index=True)
slug = models.SlugField(unique=True, max_length=255)
detail = models.TextField()
embeddings = VectorField(dimensions=384)

class Meta:
indexes = [
GinIndex(fields=['embeddings'], opclasses=['vector_cosine_ops'])
]

def save(self, *args, **kwargs):
# Calculate embeddings directly using SentenceTransformer
blog_text = f'{self.title} - {self.detail}'
self.embeddings = model.encode(blog_text).tolist()
super().save(*args, **kwargs)







In this model, each blog post’s text is converted into a 384-dimensional vector using a pre-trained model from SentenceTransformer. These embeddings are then stored in a VectorField provided by pgvector.




Testing Embeddings with Blog Entry




CODE
# Create a Blog Entry
blog = Blog(title="Test Blog", detail="This is a sample blog post.")
blog.save()

# Verify Embeddings
print(blog.embeddings) # Outputs a 384-dimensional embedding






Searching for Similar Blogs

We use the cosine similarity measure to find blogs similar to a given query. Here is a Django function that searches for similar blogs:




CODE
import numpy as np
from django.db.models import FloatField
from django.db.models.expressions import RawSQL
from .models import Blog

def find_similar_blog(query_vector, top_n=10):
"""
Finds blogs with the highest cosine similarity to the query vector.

Args:
- query_vector: Numpy array representing the query vector.
- top_n: Number of top results to return.

Returns:
- QuerySet of Blog objects with an additional 'similarity' field.
"""
# Ensure the query vector has the same dimensions (384) as the stored vectors
if isinstance(query_vector, np.ndarray):
query_vector = query_vector.tolist() # Convert numpy array to list

query_vector = query_vector[:384] # Ensure it's 384 dimensions

# Using RawSQL to calculate cosine similarity
similarity_annotation = RawSQL(
"1 - (embeddings <=> %s::vector)", # Cosine similarity calculation
(query_vector,),
output_field=FloatField()
)

# Query the database and annotate each JobVector with the similarity score
similar_blogs = Blog.objects.annotate(similarity=similarity_annotation).order_by('-similarity')[:top_n]

return similar_blogs







This function takes a query vector and finds the top n similar blog posts by annotating each Blog object with a similarity score calculated using PostgreSQL's <=> operator, which computes the cosine distance




Example of Using find_similar_blog function




CODE
# Retrieve the blog entry from the database
blog = Blog.objects.first()
# Extract the embeddings of the blog
blog_vector = blog.embeddings

# Pass the blog vector and the number of results you want to the function
similar_blogs = find_similar_blog(query_vector, top_n=5)

# Iterate through the returned QuerySet to display the similar blogs
for blog in similar_blogs:
print(f"{blog.title}")






This method is particularly useful for content-based recommendation systems where you might want to show users similar content to what they are reading. By using the embeddings of an existing blog post:





  • Relevance: The search results are based on semantic similarity in the context of the content, which can enhance user engagement by providing more relevant recommendations.


  • Ease of Use: Directly using database-stored vectors simplifies the process as there’s no need to compute the embeddings in real time for the query.


  • Efficiency: Since the embeddings are precomputed and stored, this method is efficient and leverages the fast vector search capabilities of pgvector.



Implementing cosine similarity with pgvector in PostgreSQL offers a robust way to enhance the semantic search capabilities of your applications. By understanding and leveraging vector search, you can significantly improve the relevancy and precision of search results, providing a better user experience in content-heavy applications like blogs or news sites.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Cosine Similarity Search on Vectors in Postgres with pgvector

Thematisch verwandte Begriffe: Cosine, Similarity, Search, Vectors · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...