pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: RakshitKhajuria (@RakshitKhajuria) <noreply+RakshitKhajuria@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #636: Improving Query Performance in LangChain with PGVector
Date: Tue, 06 Aug 2024 10:01:13 +0000
Message-ID: <gh-pgvector-pgvector-636@github.com> (raw)

# 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?


view thread (8+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-636@github.com>
Permalink:  ../gh-pgvector-pgvector-636@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