PGVector(
self,
connection_string: str,
embedding_function: Embeddings,
embedding_length: Optional| Name | Type | Description |
|---|---|---|
connection_string* | str | Postgres connection string. |
embedding_function* | Embeddings | Any embedding function implementing
|
embedding_length | Optional[int] | Default: NoneThe length of the embedding vector. (default: None) NOTE: This is not mandatory. Defining it will prevent vectors of any other size to be added to the embeddings table but, without it, the embeddings can't be indexed. |
collection_name | str | Default: _LANGCHAIN_DEFAULT_COLLECTION_NAME |
distance_strategy | DistanceStrategy | Default: DEFAULT_DISTANCE_STRATEGY |
pre_delete_collection | bool | Default: False |
engine_args | Optional[dict[str, Any]] | Default: None |
use_jsonb | bool | Default: False |
create_extension | bool | Default: True |
| Name | Type |
|---|---|
| connection_string | str |
| embedding_function | Embeddings |
| embedding_length | Optional[int] |
| collection_name | str |
| collection_metadata | Optional[dict] |
| distance_strategy | DistanceStrategy |
| pre_delete_collection | bool |
| logger | Optional[logging.Logger] |
| relevance_score_fn | Optional[Callable[[float], float]] |
| connection | Optional[sqlalchemy.engine.Connection] |
| engine_args | Optional[dict[str, Any]] |
| use_jsonb | bool |
| create_extension | bool |
Postgres/PGVector vector store.
DEPRECATED: This class is pending deprecation and will likely receive
no updates. An improved version of this class is available in
langchain_postgres as PGVector. Please use that class instead.
When migrating please keep in mind that:
* The new implementation works with psycopg3, not with psycopg2
(This implementation does not work with psycopg3).
* Filtering syntax has changed to use $ prefixed operators for JSONB
metadata fields. (New implementation only uses JSONB field for metadata)
* The new implementation made some schema changes to address issues
with the existing implementation. So you will need to re-create
your tables and re-index your data or else carry out a manual
migration.
To use, you should have the pgvector python package installed.
Example:
.. code-block:: python
from langchain_community.vectorstores import PGVector
from langchain_community.embeddings.openai import OpenAIEmbeddings
CONNECTION_STRING = "postgresql+psycopg2://hwc@localhost:5432/test3"
COLLECTION_NAME = "state_of_the_union_test"
embeddings = OpenAIEmbeddings()
vectorestore = PGVector.from_documents(
embedding=embeddings,
documents=docs,
collection_name=COLLECTION_NAME,
connection_string=CONNECTION_STRING,
use_jsonb=True,
The name of the collection to use. (default: langchain) NOTE: This is not the name of the table, but the name of the collection. The tables will be created when initializing the store (if not exists) So, make sure the user has the right permissions to create tables.
The distance strategy to use. (default: COSINE)
If True, will delete the collection if it exists. (default: False). Useful for testing.
SQLAlchemy's create engine arguments.
Use JSONB instead of JSON for metadata. (default: True) Strongly discouraged from using JSON as it's not as efficient for querying. It's provided here for backwards compatibility with older versions, and will be removed in the future.
If True, will create the vector extension if it doesn't exist. disabling creation is useful when using ReadOnly Databases.