Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: extend pgvector vector search functionality #17415

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class PGVectorStore(BasePydanticVectorStore):
user="postgres",
table_name="paul_graham_essay",
embed_dim=1536 # openai embedding dimension
vector_search_method="cosine_distance" # Optional specify vector search method. Default is cosine_distance.
)
```
"""
Expand Down Expand Up @@ -272,6 +273,7 @@ def from_params(
use_jsonb: bool = False,
hnsw_kwargs: Optional[Dict[str, Any]] = None,
create_engine_kwargs: Optional[Dict[str, Any]] = None,
vector_search_method: Optional[str] = "cosine_distance",
) -> "PGVectorStore":
"""Construct from params.

Expand All @@ -296,6 +298,7 @@ def from_params(
contains "hnsw_ef_construction", "hnsw_ef_search", "hnsw_m", and optionally "hnsw_dist_method". Defaults to None,
which turns off HNSW search.
create_engine_kwargs (Optional[Dict[str, Any]], optional): Engine parameters to pass to create_engine. Defaults to None.
vector_search_method (Optional[str], optional): Vector search method. Defaults to cosine_distance.

Returns:
PGVectorStore: Instance of PGVectorStore constructed from params.
Expand All @@ -307,6 +310,7 @@ def from_params(
async_conn_str = async_connection_string or (
f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{database}"
)
cls.vector_search_method = vector_search_method
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhhhh this is not the proper way to do this. It should be an attribute defined on the class earlier, like all the other attributes (table_name, schema_name, etc.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure let me make changes

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the changes or nah?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes i was thinking that if i want to use direct the from_params() without creating class instance as it is class method so how can we pass the vector_search_method if we add vector_search_method attribute in class init and someone use directly the class method so it wont be able to find vector_search_method. I would like to hear your thoughts on it.

return cls(
connection_string=conn_str,
async_connection_string=async_conn_str,
Expand Down Expand Up @@ -597,13 +601,72 @@ def _build_query(
) -> Any:
from sqlalchemy import select, text

stmt = select( # type: ignore
self._table_class.id,
self._table_class.node_id,
self._table_class.text,
self._table_class.metadata_,
self._table_class.embedding.cosine_distance(embedding).label("distance"),
).order_by(text("distance asc"))
match self.vector_search_method:
case "cosine_distance":
stmt = select( # type: ignore
self._table_class.id,
self._table_class.node_id,
self._table_class.text,
self._table_class.metadata_,
self._table_class.embedding.cosine_distance(embedding).label(
"distance"
),
).order_by(text("distance asc"))

case "max_inner_product":
stmt = select( # type: ignore
self._table_class.id,
self._table_class.node_id,
self._table_class.text,
self._table_class.metadata_,
self._table_class.embedding.max_inner_product(embedding).label(
"distance"
),
).order_by(text("distance asc"))

case "l2_distance":
stmt = select( # type: ignore
self._table_class.id,
self._table_class.node_id,
self._table_class.text,
self._table_class.metadata_,
self._table_class.embedding.l2_distance(embedding).label(
"distance"
),
).order_by(text("distance asc"))

case "l1_distance":
stmt = select( # type: ignore
self._table_class.id,
self._table_class.node_id,
self._table_class.text,
self._table_class.metadata_,
self._table_class.embedding.l1_distance(embedding).label(
"distance"
),
).order_by(text("distance asc"))

case "hamming_distance":
stmt = select( # type: ignore
self._table_class.id,
self._table_class.node_id,
self._table_class.text,
self._table_class.metadata_,
self._table_class.embedding.hamming_distance(embedding).label(
"distance"
),
).order_by(text("distance asc"))

case "jaccard_distance":
stmt = select( # type: ignore
self._table_class.id,
self._table_class.node_id,
self._table_class.text,
self._table_class.metadata_,
self._table_class.embedding.jaccard_distance(embedding).label(
"distance"
),
).order_by(text("distance asc"))

return self._apply_filters_and_limit(stmt, limit, metadata_filters)

Expand Down
Loading