Skip to content

docs: update PGVectorStore quickstart #177

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

Merged
merged 2 commits into from
Apr 7, 2025
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
63 changes: 41 additions & 22 deletions examples/pg_vectorstore.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langchain-postgres"
"%pip install --upgrade --quiet langchain-postgres\n",
"# This tutorial also requires the following dependencies\n",
"%pip install --upgrade --quiet langchain-core langchain-cohere SQLAlchemy"
Copy link
Collaborator

Choose a reason for hiding this comment

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

is SQLAlchemy not included a sub dependency? I think it's common to keep the package name lower-cased in pip install instructions

]
},
{
Expand All @@ -47,14 +49,29 @@
"## Basic Usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This package requires a PostgreSQL database with the `pgvector` extension.\n",
"\n",
"You can run the following command to spin up a container for a `pgvector` enabled Postgres instance:\n",
"\n",
"```shell\n",
"docker run --name pgvector-container -e POSTGRES_USER=langchain -e POSTGRES_PASSWORD=langchain -e POSTGRES_DB=langchain -p 6024:5432 -d pgvector/pgvector:pg16\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "OMvzMWRrR6n7",
"metadata": {
"id": "OMvzMWRrR6n7"
},
"source": [
"### Set the postgres connection url"
"### Set the postgres connection url\n",
"\n",
"`PGVectorStore` can be used with the `asyncpg` and `psycopg3` drivers."
]
},
{
Expand All @@ -66,26 +83,14 @@
},
"outputs": [],
"source": [
"# @title Set Your Values Here { display-mode: \"form\" }\n",
"POSTGRES_USER = \"postgres-user\" # @param {type: \"string\"}\n",
"POSTGRES_PASSWORD = \"postgres-password\" # @param {type: \"string\"}\n",
"POSTGRES_HOST = \"postgres-host\" # @param {type: \"string\"}\n",
"POSTGRES_PORT = \"postgres-port\" # @param {type: \"string\"}\n",
"POSTGRES_DB = \"postgres-db-name\" # @param {type: \"string\"}\n",
"# @title Set your values or use the defaults to connect to Docker { display-mode: \"form\" }\n",
"POSTGRES_USER = \"langchain\" # @param {type: \"string\"}\n",
"POSTGRES_PASSWORD = \"langchain\" # @param {type: \"string\"}\n",
"POSTGRES_HOST = \"localhost\" # @param {type: \"string\"}\n",
"POSTGRES_PORT = \"6024\" # @param {type: \"string\"}\n",
"POSTGRES_DB = \"langchain\" # @param {type: \"string\"}\n",
"TABLE_NAME = \"vectorstore\" # @param {type: \"string\"}\n",
"VECTOR_SIZE = 768 # @param {type: \"int\"}\n",
"\n",
"CONNECTION_STRING = (\n",
" f\"postgresql+asyncpg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}\"\n",
" f\":{POSTGRES_PORT}/{POSTGRES_DB}\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Note:* `PGVectorStore` can only be used with the `postgresql+asyncpg` driver."
"VECTOR_SIZE = 768 # @param {type: \"int\"}"
]
},
{
Expand All @@ -111,6 +116,20 @@
"**Note:** This tutorial demonstrates the async interface. All async methods have corresponding sync methods."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# See docker command above to launch a Postgres instance with pgvector enabled.\n",
"CONNECTION_STRING = (\n",
" f\"postgresql+asyncpg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}\"\n",
" f\":{POSTGRES_PORT}/{POSTGRES_DB}\"\n",
")\n",
"# To use psycopg3 driver, set your connection string to `postgresql+psycopg://`"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -374,7 +393,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_postgres.indexes import IVFFlatIndex\n",
"from langchain_postgres.v2.indexes import IVFFlatIndex\n",
"\n",
"index = IVFFlatIndex()\n",
"await store.aapply_vector_index(index)"
Expand Down
2 changes: 1 addition & 1 deletion examples/vectorstore.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"\n",
"This code has been ported over from langchain_community. The following changes have been made:\n",
"\n",
"* langchain_postgres works only with psycopg3. Please update your connnecion strings from `postgresql+psycopg2://...` to `postgresql+psycopg://langchain:langchain@...` (yes, it's the driver name is `psycopg` not `psycopg3`, but it'll use `psycopg3`.\n",
"* langchain_postgres works only with psycopg3. Please update your connection strings from `postgresql+psycopg2://...` to `postgresql+psycopg://langchain:langchain@...` (yes, it's the driver name is `psycopg` not `psycopg3`, but it'll use `psycopg3`.\n",
"* The schema of the embedding store and collection have been changed to make add_documents work correctly with user specified ids.\n",
"* One has to pass an explicit connection object now.\n",
"\n",
Expand Down