pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
8+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 10:01 "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
  0 siblings, 0 replies; 8+ messages in thread

From: RakshitKhajuria (@RakshitKhajuria) @ 2024-08-06 10:01 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

# Improving Query Performance in LangChain with PGVector

## Problem Statement

I have created a database using LangChain and am facing performance issues with query execution time. 

### Initial Setup

The following code was used to set up the database with LangChain:

```python
from langchain.vectorstores.pgvector import PGVector

CONNECTION_STRING = "postgresql+psycopg2://postgres:xxxxxxxx@localhost:5432/vdb"
COLLECTION_NAME = 'chunk'

db = PGVector.from_documents(
    embedding=embeddings,
    documents=document_1,
    collection_name=COLLECTION_NAME,
    connection_string=CONNECTION_STRING,
)
```

### Query Performance Issue

When executing a similarity search query:

```python
%%time
query = "some text"
similar = db.similarity_search_with_score(query, k=2)
```

**The time is 30s** to execute this.

## Attempt to Improve Performance with Indexing

I attempted to create an index to improve query performance. These were the steps I followed:

### Connection Setup

```python
# Connection parameters
conn = psycopg2.connect(
    dbname='xxx',
    user='xxx',
    password='xxx',
    host='localhost',  
    port='5432'  
)
cur = conn.cursor()

# Install pgvector
cur.execute("CREATE EXTENSION IF NOT EXISTS vector")
conn.commit()
```

### Creating Index

```python
num_lists = 2000

# Use the cosine distance measure, which is what we'll later use for querying
cur.execute(f'CREATE INDEX ON langchain_pg_embedding USING ivfflat (embedding vector_cosine_ops) WITH (lists = {num_lists});')
conn.commit()
```

However, I encountered the following error:

```
Error: column does not have dimensions
```

## Question

Is there a way I can alter the `embedding` column to have a dimension of 768, or how should I proceed?


^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 10:06 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: hlinnaka (@hlinnaka) @ 2024-08-06 10:06 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

```
alter table langchain_pg_embedding alter column embedding type vector(768);
```

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 11:01 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: RakshitKhajuria (@RakshitKhajuria) @ 2024-08-06 11:01 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thank you @hlinnaka for replying.
I tried this before but i am getting this error

```
{
	"name": "DiskFull",
	"message": "could not extend file \"base/118752/10750542.15\": wrote only 4096 of 8192 bytes at block 2043565
HINT:  Check free disk space.
",
	"stack": "---------------------------------------------------------------------------
DiskFull                                  Traceback (most recent call last)
Cell In[47], line 1
----> 1 cur.execute(\"alter table langchain_pg_embedding alter column embedding type vector(768);\")

DiskFull: could not extend file \"base/118752/10750542.15\": wrote only 4096 of 8192 bytes at block 2043565
HINT:  Check free disk space.
"
}
```

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 11:04 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: hlinnaka (@hlinnaka) @ 2024-08-06 11:04 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

The message is pretty self-explanatory: you're out of disk space.

The ALTER TABLE command needs to rewrite the table, which means that you temporarily need 2x the disk space, to hold the old and the new copy.

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 11:14 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: RakshitKhajuria (@RakshitKhajuria) @ 2024-08-06 11:14 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Is there any alternative to do this? What will be the best way to proceed further? How can i specify the dim while creating the embeddings via langchain?

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 11:25 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: hlinnaka (@hlinnaka) @ 2024-08-06 11:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> Is there any alternative to do this? What will be the best way to proceed further?

Buy a bigger disk? Delete some other files you don't need? Drop the table, recreate it with the right dimensions, and reload it ?

> How can i specify the dim while creating the embeddings via langchain?

I don't know. Please check the langchain documentation for that.

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 11:33 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: RakshitKhajuria (@RakshitKhajuria) @ 2024-08-06 11:33 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thank you for the help 

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
@ 2024-08-06 11:33 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: RakshitKhajuria (@RakshitKhajuria) @ 2024-08-06 11:33 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@RakshitKhajuria closed this.

^ permalink  raw  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2024-08-06 11:33 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-08-06 10:01 [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
2024-08-06 10:06 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-08-06 11:01 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
2024-08-06 11:04 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-08-06 11:14 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
2024-08-06 11:25 ` "hlinnaka (@hlinnaka)" <noreply+hlinnaka@github.com>
2024-08-06 11:33 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>
2024-08-06 11:33 ` "RakshitKhajuria (@RakshitKhajuria)" <noreply+RakshitKhajuria@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox