Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions langchain_postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
PickleCheckpointSerializer,
PostgresCheckpoint,
)
from langchain_postgres.vectorstores import PGVector

try:
__version__ = metadata.version(__package__)
Expand All @@ -19,4 +20,5 @@
"PostgresChatMessageHistory",
"PostgresCheckpoint",
"PickleCheckpointSerializer",
"PGVector",
]
47 changes: 31 additions & 16 deletions langchain_postgres/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,23 @@ def _results_to_docs(docs_and_scores: Any) -> List[Document]:
class PGVector(VectorStore):
"""`Postgres`/`PGVector` vector store.

To use, you should have the ``pgvector`` python package installed.
This code has been ported over from langchain_community with minimal changes
to allow users to easily transition from langchain_community to langchain_postgres.

To use, you need to have a Postgres database running and the `vector` extension
installed. The `vector` extension is a Postgres extension that provides
vector similarity search capabilities.

Example:
.. code-block:: python

from langchain_postgres.vectorstores import PGVector
from langchain_community.embeddings.openai import OpenAIEmbeddings
from langchain_openai.embeddings import OpenAIEmbeddings

CONNECTION_STRING = "postgresql+psycopg2://hwc@localhost:5432/test3"
COLLECTION_NAME = "state_of_the_union_test"
embeddings = OpenAIEmbeddings()
vectorestore = PGVector.from_documents(
vectorstore = PGVector.from_documents(
embedding=embeddings,
documents=docs,
collection_name=COLLECTION_NAME,
Expand All @@ -245,7 +250,7 @@ def __init__(
*,
connection: Optional[sqlalchemy.engine.Connection] = None,
engine_args: Optional[dict[str, Any]] = None,
use_jsonb: bool = False,
use_jsonb: bool = True,
create_extension: bool = True,
) -> None:
"""Initialize the PGVector store.
Expand Down Expand Up @@ -439,7 +444,7 @@ def __from(
connection_string: Optional[str] = None,
pre_delete_collection: bool = False,
*,
use_jsonb: bool = False,
use_jsonb: bool = True,
**kwargs: Any,
) -> PGVector:
if ids is None:
Expand Down Expand Up @@ -969,7 +974,7 @@ def from_texts(
ids: Optional[List[str]] = None,
pre_delete_collection: bool = False,
*,
use_jsonb: bool = False,
use_jsonb: bool = True,
**kwargs: Any,
) -> PGVector:
"""
Expand Down Expand Up @@ -1005,23 +1010,33 @@ def from_embeddings(
pre_delete_collection: bool = False,
**kwargs: Any,
) -> PGVector:
"""Construct PGVector wrapper from raw documents and pre-
generated embeddings.
"""Construct PGVector wrapper from raw documents and embeddings.

Return VectorStore initialized from documents and embeddings.
Postgres connection string is required
"Either pass it as a parameter
or set the PGVECTOR_CONNECTION_STRING environment variable.
Args:
text_embeddings: List of tuples of text and embeddings.
embedding: Embeddings object.
metadatas: Optional list of metadatas associated with the texts.
collection_name: Name of the collection.
distance_strategy: Distance strategy to use.
ids: Optional list of ids for the documents.
pre_delete_collection: If True, will delete the collection if it exists.
**Attention**: This will delete all the documents in the existing
collection.
kwargs: Additional arguments.

Returns:
PGVector: PGVector instance.

Example:
.. code-block:: python

from langchain_community.vectorstores import PGVector
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_postgres.vectorstores import PGVector
from langchain_openai.embeddings import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()
text_embeddings = embeddings.embed_documents(texts)
text_embedding_pairs = list(zip(texts, text_embeddings))
faiss = PGVector.from_embeddings(text_embedding_pairs, embeddings)
vectorstore = PGVector.from_embeddings(text_embedding_pairs, embeddings)
"""
texts = [t[0] for t in text_embeddings]
embeddings = [t[1] for t in text_embeddings]
Expand Down Expand Up @@ -1092,7 +1107,7 @@ def from_documents(
ids: Optional[List[str]] = None,
pre_delete_collection: bool = False,
*,
use_jsonb: bool = False,
use_jsonb: bool = True,
**kwargs: Any,
) -> PGVector:
"""
Expand Down
17 changes: 0 additions & 17 deletions scripts/check_imports.py

This file was deleted.

27 changes: 0 additions & 27 deletions scripts/check_pydantic.sh

This file was deleted.

18 changes: 0 additions & 18 deletions scripts/lint_imports.sh

This file was deleted.

1 change: 1 addition & 0 deletions tests/unit_tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
EXPECTED_ALL = [
"__version__",
"CheckpointSerializer",
"PGVector",
"PostgresChatMessageHistory",
"PostgresCheckpoint",
"PickleCheckpointSerializer",
Expand Down
2 changes: 2 additions & 0 deletions tests/unit_tests/test_vectorstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def test_pgvector_with_filter_in_set() -> None:
metadatas=metadatas,
connection_string=CONNECTION_STRING,
pre_delete_collection=True,
use_jsonb=False,
)
output = docsearch.similarity_search_with_score(
"foo", k=2, filter={"page": {"IN": ["0", "2"]}}
Expand All @@ -202,6 +203,7 @@ def test_pgvector_with_filter_nin_set() -> None:
metadatas=metadatas,
connection_string=CONNECTION_STRING,
pre_delete_collection=True,
use_jsonb=False,
)
output = docsearch.similarity_search_with_score(
"foo", k=2, filter={"page": {"NIN": ["1"]}}
Expand Down