langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications

Home Page:https://python.langchain.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Langchain PGVector is not working is not able to call the sql alchemy in vectorstore.py (missing package error)

sambasivaj24 opened this issue · comments

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import psycopg2
from langchain_postgres.vectorstores import PGVector
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import GooglePalmEmbeddings
from langchain_google_genai import GoogleGenerativeAI
from langchain_core.documents import Document
from langchain_postgres import PGVector

from langchain_postgres.vectorstores import PGVector

from langchain_community.vectorstores import pgvector

import pgvector

from pgvector.sqlalchemy import Vector

loader=TextLoader("/home/sambasiva/dev/fastapi-template/src/api/embedding_transformer/timeline.txt",encoding="utf8")
documents=loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)
chunks = text_splitter.split_documents(documents)
embeddings = GooglePalmEmbeddings(google_api_key=API)
connection = "postgresql+psycopg://vectorapi:vectorapipass@localhost:5432/vectordb"
collection_name = "my_docs"
db=PGVector.from_documents(embedding=embeddings,documents=chunks, collection_name=collection_name,connection=connection,)

print(len(chunks))
try:
conn = psycopg2.connect(
dbname="vectordb",
user="vectorapi",
password="vectorapipass",
host="localhost", # Optional, defaults to localhost
port="5432" # Optional, defaults to 5432
)

print("Connection successful!")
except Exception as e:
print(f"Error connecting to database: {e}")

Error Message and Stack Trace (if applicable)

sambasiva@USLDMJTG24:~/dev/fastapi-template$ /bin/python3 /home/sambasiva/dev/fastapi-template/src/api/embedding_transformer/test.py
Traceback (most recent call last):
File "/home/sambasiva/dev/fastapi-template/src/api/embedding_transformer/test.py", line 25, in
db=PGVector.from_documents(embedding=embeddings,documents=chunks, collection_name=collection_name,connection=connection,)
File "/home/sambasiva/.local/lib/python3.10/site-packages/langchain_postgres/vectorstores.py", line 1107, in from_documents
return cls.from_texts(
File "/home/sambasiva/.local/lib/python3.10/site-packages/langchain_postgres/vectorstores.py", line 975, in from_texts
return cls.__from(
File "/home/sambasiva/.local/lib/python3.10/site-packages/langchain_postgres/vectorstores.py", line 438, in __from
store = cls(
File "/home/sambasiva/.local/lib/python3.10/site-packages/langchain_postgres/vectorstores.py", line 308, in init
self.post_init()
File "/home/sambasiva/.local/lib/python3.10/site-packages/langchain_postgres/vectorstores.py", line 317, in post_init
EmbeddingStore, CollectionStore = _get_embedding_collection_store(
File "/home/sambasiva/.local/lib/python3.10/site-packages/langchain_postgres/vectorstores.py", line 91, in _get_embedding_collection_store
from pgvector.sqlalchemy import Vector # type: ignore
ModuleNotFoundError: No module named 'pgvector.sqlalchemy'; 'pgvector' is not a package

Description

I am trying to use the langchain library to perform a RAG application but while using the pgvector as a database i encountered th issue with the langchain package langchain_postgres/vectorstores.py

in which it is giving the error of module not found and that pgvector is not a package

System Info

langchain==0.1.19
langchain-community==0.0.38
langchain-core==0.1.52
langchain-google-genai==1.0.3
langchain-google-vertexai==1.0.3
langchain-postgres==0.0.4
langchain-text-splitters==0.0.1

platform windows
python version= 3.11

pip install pgvector should fix it

thank you, maximeperrindev, I tried the same, still getting the same error.