- Notifications
You must be signed in to change notification settings - Fork328
Added blog post semantic search in postgres in 15 minutes#1535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
18f8f44
00bd75d
068af92
a9148da
3e0fa33
c71fcd2
9b6e75f
b451c9b
d418deb
84872ac
1686f93
b2b9d88
b8766bd
4574183
4db2149
68368e2
af8dd3e
2c156ae
faf0be1
27445f5
427f77f
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -106,7 +106,7 @@ For instance let’s say that we have the following documents: | ||
| Document ID | Document text | | ||
-----|----------| | ||
| 1 | The pgml.transform function is a PostgreSQL function for calling LLMs in the database. | | ||
| 2 | I thinktomatoes are incredible on burgers. | | ||
and a user is looking for the answer to the question: "What is the pgml.transform function?". If we embed the search query and all of the documents using a model like _mixedbread-ai/mxbai-embed-large-v1_, we can compare the query embedding to all of the document embeddings, and select the document that has the closest embedding in vector space, and therefore in meaning, to the to the answer. | ||
@@ -130,7 +130,7 @@ This is a somewhat confusing formula but luckily _pgvector_ provides an operato | ||
!!! generic | ||
!!! code_block time="64.643 ms" | ||
```postgresql | ||
SELECT '[1,2,3]'::vector <=> '[2,3,4]'::vector; | ||
@@ -176,7 +176,7 @@ SELECT pgml.embed( | ||
<=> | ||
pgml.embed( | ||
'mixedbread-ai/mxbai-embed-large-v1', | ||
'I thinktomatoes are incredible on burgers.' | ||
)::vector AS cosine_distance; | ||
``` | ||
@@ -191,14 +191,14 @@ cosine_distance | ||
cosine_distance | ||
-------------------- | ||
0.7328613577628744 | ||
``` | ||
!!! | ||
!!! | ||
You'll notice that the distance between "What is the pgml.transform function?" and "The pgml.transform function is a PostgreSQL function for calling LLMs in the database." is much smaller than the cosine distance between "What is the pgml.transform function?" and "I thinktomatoes are incredible on burgers". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It's probably worth mentioning "out of sample" tokens, since they are rife on domain specific topics like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm not sure what that is? | ||
## Making it fast! | ||
@@ -228,10 +228,10 @@ VALUES | ||
), | ||
( | ||
'I thinktomatoes are incredible on burgers.', | ||
pgml.embed( | ||
'mixedbread-ai/mxbai-embed-large-v1', | ||
'I thinktomatoes are incredible on burgers.' | ||
) | ||
); | ||
``` | ||
@@ -282,7 +282,7 @@ LIMIT 1; | ||
!!! | ||
This query is fast for now, but as we add more data to the table, it will slow down because we have not indexed the embedding column. | ||
Let's demonstrate this by inserting 100,000 additional embeddings: | ||
@@ -344,7 +344,7 @@ LIMIT 1; | ||
This somewhat less than ideal performance can be fixed by indexing the embedding column. There are two types of indexes available in _pgvector_: IVFFlat and HNSW. | ||
SilasMarvin marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
IVFFlat indexes clusters the table into sublists, and when searching, only searches over a fixed number of sublists. Inour example, if we were to add an IVFFlat index with 10 lists: | ||
!!! generic | ||
@@ -360,7 +360,7 @@ WITH (lists = 10); | ||
!!! | ||
and search again, we would get much betterperformance: | ||
!!! generic | ||
Uh oh!
There was an error while loading.Please reload this page.