|
34 | 34 | }, |
35 | 35 | "outputs": [], |
36 | 36 | "source": [ |
37 | | - "%pip install --upgrade --quiet langchain-postgres" |
| 37 | + "%pip install --upgrade --quiet langchain-postgres\n", |
| 38 | + "# This tutorial also requires the following dependencies\n", |
| 39 | + "%pip install --upgrade --quiet langchain-core langchain-cohere SQLAlchemy" |
38 | 40 | ] |
39 | 41 | }, |
40 | 42 | { |
|
47 | 49 | "## Basic Usage" |
48 | 50 | ] |
49 | 51 | }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "This package requires a PostgreSQL database with the `pgvector` extension.\n", |
| 57 | + "\n", |
| 58 | + "You can run the following command to spin up a container for a `pgvector` enabled Postgres instance:\n", |
| 59 | + "\n", |
| 60 | + "```shell\n", |
| 61 | + "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", |
| 62 | + "```" |
| 63 | + ] |
| 64 | + }, |
50 | 65 | { |
51 | 66 | "cell_type": "markdown", |
52 | 67 | "id": "OMvzMWRrR6n7", |
53 | 68 | "metadata": { |
54 | 69 | "id": "OMvzMWRrR6n7" |
55 | 70 | }, |
56 | 71 | "source": [ |
57 | | - "### Set the postgres connection url" |
| 72 | + "### Set the postgres connection url\n", |
| 73 | + "\n", |
| 74 | + "`PGVectorStore` can be used with the `asyncpg` and `psycopg3` drivers." |
58 | 75 | ] |
59 | 76 | }, |
60 | 77 | { |
|
66 | 83 | }, |
67 | 84 | "outputs": [], |
68 | 85 | "source": [ |
69 | | - "# @title Set Your Values Here { display-mode: \"form\" }\n", |
70 | | - "POSTGRES_USER = \"postgres-user\" # @param {type: \"string\"}\n", |
71 | | - "POSTGRES_PASSWORD = \"postgres-password\" # @param {type: \"string\"}\n", |
72 | | - "POSTGRES_HOST = \"postgres-host\" # @param {type: \"string\"}\n", |
73 | | - "POSTGRES_PORT = \"postgres-port\" # @param {type: \"string\"}\n", |
74 | | - "POSTGRES_DB = \"postgres-db-name\" # @param {type: \"string\"}\n", |
| 86 | + "# @title Set your values or use the defaults to connect to Docker { display-mode: \"form\" }\n", |
| 87 | + "POSTGRES_USER = \"langchain\" # @param {type: \"string\"}\n", |
| 88 | + "POSTGRES_PASSWORD = \"langchain\" # @param {type: \"string\"}\n", |
| 89 | + "POSTGRES_HOST = \"localhost\" # @param {type: \"string\"}\n", |
| 90 | + "POSTGRES_PORT = \"6024\" # @param {type: \"string\"}\n", |
| 91 | + "POSTGRES_DB = \"langchain\" # @param {type: \"string\"}\n", |
75 | 92 | "TABLE_NAME = \"vectorstore\" # @param {type: \"string\"}\n", |
76 | | - "VECTOR_SIZE = 768 # @param {type: \"int\"}\n", |
77 | | - "\n", |
78 | | - "CONNECTION_STRING = (\n", |
79 | | - " f\"postgresql+asyncpg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}\"\n", |
80 | | - " f\":{POSTGRES_PORT}/{POSTGRES_DB}\"\n", |
81 | | - ")" |
82 | | - ] |
83 | | - }, |
84 | | - { |
85 | | - "cell_type": "markdown", |
86 | | - "metadata": {}, |
87 | | - "source": [ |
88 | | - "*Note:* `PGVectorStore` can only be used with the `postgresql+asyncpg` driver." |
| 93 | + "VECTOR_SIZE = 768 # @param {type: \"int\"}" |
89 | 94 | ] |
90 | 95 | }, |
91 | 96 | { |
|
111 | 116 | "**Note:** This tutorial demonstrates the async interface. All async methods have corresponding sync methods." |
112 | 117 | ] |
113 | 118 | }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": null, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [], |
| 124 | + "source": [ |
| 125 | + "# See docker command above to launch a Postgres instance with pgvector enabled.\n", |
| 126 | + "CONNECTION_STRING = (\n", |
| 127 | + " f\"postgresql+asyncpg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}\"\n", |
| 128 | + " f\":{POSTGRES_PORT}/{POSTGRES_DB}\"\n", |
| 129 | + ")\n", |
| 130 | + "# To use psycopg3 driver, set your connection string to `postgresql+psycopg://`" |
| 131 | + ] |
| 132 | + }, |
114 | 133 | { |
115 | 134 | "cell_type": "code", |
116 | 135 | "execution_count": null, |
|
374 | 393 | "metadata": {}, |
375 | 394 | "outputs": [], |
376 | 395 | "source": [ |
377 | | - "from langchain_postgres.indexes import IVFFlatIndex\n", |
| 396 | + "from langchain_postgres.v2.indexes import IVFFlatIndex\n", |
378 | 397 | "\n", |
379 | 398 | "index = IVFFlatIndex()\n", |
380 | 399 | "await store.aapply_vector_index(index)" |
|
0 commit comments