Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite258f09

Browse files
SilasMarvinlevkk
andauthored
Added new blog post (#1025)
Co-authored-by: Lev Kokotov <levkk@users.noreply.github.com>
1 parent70f4d60 commite258f09

File tree

8 files changed

+156
-7
lines changed

8 files changed

+156
-7
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
author:Silas Marvin
3+
description:HNSW indexing is the latest upgrade in vector recall performance. In this post we announce our updated SDK that utilizes HNSW indexing to give world class performance in vector search.
4+
image:https://postgresml.org/dashboard/static/images/blog/announcing_hnsw_support.webp
5+
image_alt:HNSW provides a significant improvement in recall speed compared to IVFFlat
6+
---
7+
8+
#Announcing HNSW Support in Our SDK
9+
10+
<divclass="d-flex align-items-center mb-4">
11+
<imgwidth="54px"height="54px"src="/dashboard/static/images/team/silas.jpg"style="border-radius:50%;"alt="Author" />
12+
<divclass="ps-3 d-flex justify-content-center flex-column">
13+
<p class="m-0">Silas Marvin</p>
14+
<p class="m-0">September 21, 2023</p>
15+
</div>
16+
</div>
17+
18+
PostgresML makes it easy to use machine learning with your database and to scale workloads horizontally in our cloud. Our SDK makes it even easier.
19+
20+
<imgsrc="/dashboard/static/images/blog/announcing_hnsw_support.webp"alt="data is always the best medicine" />
21+
<center><p><i>HNSW (hierarchical navigable small worlds) is an indexing method that greatly improves vector recall</i></p></center>
22+
23+
##Introducing HNSW
24+
25+
Underneath the hood our SDK utilizes[pgvector](https://github.com/pgvector/pgvector) to store, index, and recall vectors. Up until this point our SDK used IVFFlat indexing to divide vectors into lists, search a subset of those lists, and return the closest vector matches.
26+
27+
While the IVFFlat indexing method is fast, it is not as fast as HNSW. Thanks to the latest update of[pgvector](https://github.com/pgvector/pgvector) our SDK now utilizes HNSW indexing, creating multi-layer graphs instead of lists and removing the required training step IVFFlat imposed.
28+
29+
The results are not disappointing.
30+
31+
##Comparing HNSW and IVFFlat
32+
33+
In one of our previous posts:[Tuning vector recall while generating query embeddings in the database](/blog/tuning-vector-recall-while-generating-query-embeddings-in-the-database) we were working on a dataset with over 5 million Amazon Movie Reviews, and after embedding the reviews, performed semantic similarity search to get the closest 5 reviews.
34+
35+
Let's run that query again:
36+
37+
!!! generic
38+
39+
!!! code_block time="89.118 ms"
40+
41+
```postgresql
42+
WITH request AS (
43+
SELECT pgml.embed(
44+
'intfloat/e5-large',
45+
'query: Best 1980''s scifi movie'
46+
)::vector(1024) AS embedding
47+
)
48+
49+
SELECT
50+
id,
51+
1 - (
52+
review_embedding_e5_large <=> (
53+
SELECT embedding FROM request
54+
)
55+
) AS cosine_similarity
56+
FROM pgml.amazon_us_reviews
57+
ORDER BY review_embedding_e5_large <=> (SELECT embedding FROM request)
58+
LIMIT 5;
59+
```
60+
61+
!!!
62+
63+
!!! results
64+
65+
| review_body | product_title | star_rating | total_votes | cosine_similarity
66+
| -------------------------------------------------| -------------------------------------------------------------| -------------| -----------| ------------------|
67+
| best 80s SciFi movie ever| The Adventures of Buckaroo Banzai Across the Eighth Dimension| 5| 1| 0.9495371273162286|
68+
| the best of 80s sci fi horror!| The Blob| 5| 2| 0.9097434758143605|
69+
| Three of the best sci-fi movies of the seventies| Sci-Fi: Triple Feature (BD)[Blu-ray]| 5| 0| 0.9008723412875651|
70+
| best sci fi movie ever| The Day the Earth Stood Still (Special Edition)[Blu-ray]| 5| 2| 0.8943620968858654|
71+
| Great Science Fiction movie| Bloodsport / Timecop (Action Double Feature)[Blu-ray]| 5| 0| 0.894282454374093|
72+
73+
!!!
74+
75+
!!!
76+
77+
This query utilized IVFFlat indexing and queried through over 5 million rows in 89.118ms. Pretty fast!
78+
79+
Let's drop our IVFFlat index and create an HNSW index.
80+
81+
!!! generic
82+
83+
!!! code_block time="10255099.233 ms (02:50:55.099)"
84+
85+
```postgresql
86+
DROP INDEX index_amazon_us_reviews_on_review_embedding_e5_large;
87+
CREATE INDEX CONCURRENTLY ON pgml.amazon_us_reviews USING hnsw (review_embedding_e5_large vector_cosine_ops);
88+
```
89+
90+
!!!
91+
92+
!!! results
93+
94+
|CREATE INDEX|
95+
|------------|
96+
97+
!!!
98+
99+
!!!
100+
101+
Now let's try the query again utilizing the new HNSW index we created.
102+
103+
!!! generic
104+
105+
!!! code_block time="17.465 ms"
106+
107+
```postgresql
108+
WITH request AS (
109+
SELECT pgml.embed(
110+
'intfloat/e5-large',
111+
'query: Best 1980''s scifi movie'
112+
)::vector(1024) AS embedding
113+
)
114+
115+
SELECT
116+
id,
117+
1 - (
118+
review_embedding_e5_large <=> (
119+
SELECT embedding FROM request
120+
)
121+
) AS cosine_similarity
122+
FROM pgml.amazon_us_reviews
123+
ORDER BY review_embedding_e5_large <=> (SELECT embedding FROM request)
124+
LIMIT 5;
125+
```
126+
127+
!!!
128+
129+
!!! results
130+
131+
| review_body | product_title | star_rating | total_votes | cosine_similarity
132+
| ---------------------------------| -------------------------------------------------------------| -------------| -----------| ------------------|
133+
| best 80s SciFi movie ever| The Adventures of Buckaroo Banzai Across the Eighth Dimension| 5| 1| 0.9495371273162286|
134+
| the best of 80s sci fi horror!| The Blob| 5| 2| 0.9097434758143605|
135+
| One of the Better 80's Sci-Fi| Krull (Special Edition)| 3| 5| 0.9093884940741694|
136+
| Good 1980s movie| Can't Buy Me Love| 4| 0| 0.9090294438721961|
137+
| great 80's movie| How I Got Into College| 5| 0| 0.9016508795301296|
138+
139+
!!!
140+
141+
!!!
142+
143+
Not only are the results better (the`cosine_similarity` is higher overall), but HNSW is over 5x faster, reducing our search and embedding time to 17.465ms.
144+
145+
This is a massive upgrade to the recall speed utilized by our SDK and greatly improves overall performance.
146+
147+
For a deeper dive into HNSW checkout[Jonathan Katz's excellent article on HNSW in pgvector](https://jkatz05.com/post/postgres/pgvector-hnsw-performance/).

‎pgml-dashboard/content/blog/tuning-vector-recall-while-generating-query-embeddings-in-the-database.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ SELECT
144144
)
145145
) AS cosine_similarity
146146
FROM pgml.amazon_us_reviews
147-
ORDER BYcosine_similarity
147+
ORDER BYreview_embedding_e5_large <=> (SELECT embedding FROM request)
148148
LIMIT 5;
149149
```
150150

‎pgml-dashboard/src/api/docs.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ async fn blog_handler<'a>(path: PathBuf, cluster: &Cluster) -> Result<ResponseOk
8080
cluster,
8181
&path,
8282
vec![
83+
NavLink::new("Announcing HNSW Support in Our SDK")
84+
.href("/blog/announcing-hnsw-support-in-our-sdk"),
8385
NavLink::new("How-to Improve Search Results with Machine Learning")
8486
.href("/blog/how-to-improve-search-results-with-machine-learning"),
8587
NavLink::new("pgml-chat: A command-line tool for deploying low-latency knowledge-based chatbots: Part I")

‎pgml-dashboard/src/components/navbar/template.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</li> -->
2424
<% } %>
2525
<liclass="nav-item d-flex align-items-center">
26-
<aclass="nav-link p-0"href="/blog/announcing-support-for-aws-us-east-1-region">Blog</a>
26+
<aclass="nav-link p-0"href="/blog/announcing-hnsw-support-in-our-sdk">Blog</a>
2727
</li>
2828
</ul>
2929

‎pgml-dashboard/src/components/navbar_web_app/template.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<divclass="vr my-2 opacity-100 d-lg-block d-none"style="width: 2px"></div>
5555

5656
<liclass="nav-item d-flex align-items-center">
57-
<aclass="nav-link p-lg-0"href="/blog/announcing-support-for-aws-us-east-1-region">Blog</a>
57+
<aclass="nav-link p-lg-0"href="/blog/announcing-hnsw-support-in-our-sdk">Blog</a>
5858
</li>
5959

6060
<% if !account_management_nav.links.is_empty() { %>
@@ -82,7 +82,7 @@
8282
</li>
8383

8484
<liclass="menu-item rounded-0 d-flex align-items-center">
85-
<ahref="/blog/announcing-support-for-aws-us-east-1-region">Blog</a>
85+
<ahref="/blog/announcing-hnsw-support-in-our-sdk">Blog</a>
8686
</li>
8787

8888
<% if !standalone_dashboard { %>
30 KB
Loading

‎pgml-dashboard/templates/layout/nav/top.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</li> -->
2424
<% } %>
2525
<liclass="nav-item d-flex align-items-center">
26-
<aclass="nav-link p-0"href="/blog/how-to-improve-search-results-with-machine-learning">Blog</a>
26+
<aclass="nav-link p-0"href="/blog/announcing-hnsw-support-in-our-sdk">Blog</a>
2727
</li>
2828
</ul>
2929

‎pgml-dashboard/templates/layout/nav/top_web_app.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<divclass="vr my-2 opacity-100 d-lg-block d-none"style="width: 2px"></div>
5454

5555
<liclass="nav-item d-flex align-items-center">
56-
<aclass="nav-link p-lg-0"href="/blog/announcing-support-for-aws-us-east-1-region">Blog</a>
56+
<aclass="nav-link p-lg-0"href="/blog/announcing-hnsw-support-in-our-sdk">Blog</a>
5757
</li>
5858

5959
<% if !account_management_nav.links.is_empty() { %>
@@ -91,7 +91,7 @@
9191
</li>
9292

9393
<liclass="menu-item rounded-0 d-flex align-items-center">
94-
<ahref="/blog/announcing-support-for-aws-us-east-1-region">Blog</a>
94+
<ahref="/blog/announcing-hnsw-support-in-our-sdk">Blog</a>
9595
</li>
9696

9797
<% if !standalone_dashboard { %>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp