langchain-postgres 0.0.12__py3-none-any.whl → 0.0.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_postgres/__init__.py +6 -0
- langchain_postgres/chat_message_histories.py +7 -1
- langchain_postgres/utils/pgvector_migrator.py +321 -0
- langchain_postgres/v2/__init__.py +0 -0
- langchain_postgres/v2/async_vectorstore.py +1268 -0
- langchain_postgres/v2/engine.py +348 -0
- langchain_postgres/v2/indexes.py +155 -0
- langchain_postgres/v2/vectorstores.py +842 -0
- langchain_postgres/vectorstores.py +4 -4
- langchain_postgres-0.0.14.dist-info/METADATA +170 -0
- langchain_postgres-0.0.14.dist-info/RECORD +16 -0
- langchain_postgres-0.0.12.dist-info/METADATA +0 -109
- langchain_postgres-0.0.12.dist-info/RECORD +0 -10
- {langchain_postgres-0.0.12.dist-info → langchain_postgres-0.0.14.dist-info}/LICENSE +0 -0
- {langchain_postgres-0.0.12.dist-info → langchain_postgres-0.0.14.dist-info}/WHEEL +0 -0
@@ -5,6 +5,7 @@ import contextlib
|
|
5
5
|
import enum
|
6
6
|
import logging
|
7
7
|
import uuid
|
8
|
+
import warnings
|
8
9
|
from typing import (
|
9
10
|
Any,
|
10
11
|
AsyncGenerator,
|
@@ -48,6 +49,8 @@ from sqlalchemy.orm import (
|
|
48
49
|
|
49
50
|
from langchain_postgres._utils import maximal_marginal_relevance
|
50
51
|
|
52
|
+
warnings.simplefilter("once", PendingDeprecationWarning)
|
53
|
+
|
51
54
|
|
52
55
|
class DistanceStrategy(str, enum.Enum):
|
53
56
|
"""Enumerator of the Distance strategies."""
|
@@ -197,9 +200,7 @@ def _get_embedding_collection_store(vector_dimension: Optional[int] = None) -> A
|
|
197
200
|
|
198
201
|
__tablename__ = "langchain_pg_embedding"
|
199
202
|
|
200
|
-
id = sqlalchemy.Column(
|
201
|
-
sqlalchemy.String, nullable=True, primary_key=True, index=True, unique=True
|
202
|
-
)
|
203
|
+
id = sqlalchemy.Column(sqlalchemy.String, primary_key=True)
|
203
204
|
|
204
205
|
collection_id = sqlalchemy.Column(
|
205
206
|
UUID(as_uuid=True),
|
@@ -269,7 +270,6 @@ class PGVector(VectorStore):
|
|
269
270
|
Instantiate:
|
270
271
|
.. code-block:: python
|
271
272
|
|
272
|
-
from langchain_postgres import PGVector
|
273
273
|
from langchain_postgres.vectorstores import PGVector
|
274
274
|
from langchain_openai import OpenAIEmbeddings
|
275
275
|
|
@@ -0,0 +1,170 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: langchain-postgres
|
3
|
+
Version: 0.0.14
|
4
|
+
Summary: An integration package connecting Postgres and LangChain
|
5
|
+
Home-page: https://github.com/langchain-ai/langchain-postgres
|
6
|
+
License: MIT
|
7
|
+
Requires-Python: >=3.9,<4.0
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
14
|
+
Requires-Dist: asyncpg (>=0.30.0,<0.31.0)
|
15
|
+
Requires-Dist: langchain-core (>=0.2.13,<0.4.0)
|
16
|
+
Requires-Dist: numpy (>=1.21,<2.0)
|
17
|
+
Requires-Dist: pgvector (>=0.2.5,<0.4)
|
18
|
+
Requires-Dist: psycopg (>=3,<4)
|
19
|
+
Requires-Dist: psycopg-pool (>=3.2.1,<4.0.0)
|
20
|
+
Requires-Dist: sqlalchemy (>=2,<3)
|
21
|
+
Project-URL: Repository, https://github.com/langchain-ai/langchain-postgres
|
22
|
+
Project-URL: Source Code, https://github.com/langchain-ai/langchain-postgres/tree/master/langchain_postgres
|
23
|
+
Description-Content-Type: text/markdown
|
24
|
+
|
25
|
+
# langchain-postgres
|
26
|
+
|
27
|
+
[](https://github.com/langchain-ai/langchain-postgres/releases)
|
28
|
+
[](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml)
|
29
|
+
[](https://opensource.org/licenses/MIT)
|
30
|
+
[](https://twitter.com/langchainai)
|
31
|
+
[](https://discord.gg/6adMQxSpJS)
|
32
|
+
[](https://github.com/langchain-ai/langchain-postgres/issues)
|
33
|
+
|
34
|
+
The `langchain-postgres` package implementations of core LangChain abstractions using `Postgres`.
|
35
|
+
|
36
|
+
The package is released under the MIT license.
|
37
|
+
|
38
|
+
Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.
|
39
|
+
|
40
|
+
## Requirements
|
41
|
+
|
42
|
+
The package supports the [asyncpg](https://github.com/MagicStack/asyncpg) and [psycogp3](https://www.psycopg.org/psycopg3/) drivers.
|
43
|
+
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
```bash
|
47
|
+
pip install -U langchain-postgres
|
48
|
+
```
|
49
|
+
|
50
|
+
## Usage
|
51
|
+
|
52
|
+
### Vectorstore
|
53
|
+
|
54
|
+
> [!WARNING]
|
55
|
+
> In v0.0.14+, `PGVector` is deprecated. Please migrate to `PGVectorStore`
|
56
|
+
> Version 0.0.14+ has not been released yet, but you can test version of the vectorstore on the main branch. Until official release do not use in production.
|
57
|
+
> for improved performance and manageability.
|
58
|
+
> See the [migration guide](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/migrate_pgvector_to_pgvectorstore.md) for details on how to migrate from `PGVector` to `PGVectorStore`.
|
59
|
+
|
60
|
+
For a detailed example on `PGVectorStore` see [here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/pg_vectorstore.ipynb).
|
61
|
+
|
62
|
+
```python
|
63
|
+
from langchain_core.documents import Document
|
64
|
+
from langchain_core.embeddings import DeterministicFakeEmbedding
|
65
|
+
from langchain_postgres import PGEngine, PGVectorStore
|
66
|
+
|
67
|
+
# Replace the connection string with your own Postgres connection string
|
68
|
+
CONNECTION_STRING = "postgresql+psycopg3://langchain:langchain@localhost:6024/langchain"
|
69
|
+
engine = PGEngine.from_connection_string(url=CONNECTION_STRING)
|
70
|
+
|
71
|
+
# Replace the vector size with your own vector size
|
72
|
+
VECTOR_SIZE = 768
|
73
|
+
embedding = DeterministicFakeEmbedding(size=VECTOR_SIZE)
|
74
|
+
|
75
|
+
TABLE_NAME = "my_doc_collection"
|
76
|
+
|
77
|
+
engine.init_vectorstore_table(
|
78
|
+
table_name=TABLE_NAME,
|
79
|
+
vector_size=VECTOR_SIZE,
|
80
|
+
)
|
81
|
+
|
82
|
+
store = PGVectorStore.create_sync(
|
83
|
+
engine=engine,
|
84
|
+
table_name=TABLE_NAME,
|
85
|
+
embedding_service=embedding,
|
86
|
+
)
|
87
|
+
|
88
|
+
docs = [
|
89
|
+
Document(page_content="Apples and oranges"),
|
90
|
+
Document(page_content="Cars and airplanes"),
|
91
|
+
Document(page_content="Train")
|
92
|
+
]
|
93
|
+
|
94
|
+
store.add_documents(docs)
|
95
|
+
|
96
|
+
query = "I'd like a fruit."
|
97
|
+
docs = store.similarity_search(query)
|
98
|
+
print(docs)
|
99
|
+
```
|
100
|
+
|
101
|
+
> [!TIP]
|
102
|
+
> All synchronous functions have corresponding asynchronous functions
|
103
|
+
|
104
|
+
### ChatMessageHistory
|
105
|
+
|
106
|
+
The chat message history abstraction helps to persist chat message history
|
107
|
+
in a postgres table.
|
108
|
+
|
109
|
+
PostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`.
|
110
|
+
|
111
|
+
The `table_name` is the name of the table in the database where
|
112
|
+
the chat messages will be stored.
|
113
|
+
|
114
|
+
The `session_id` is a unique identifier for the chat session. It can be assigned
|
115
|
+
by the caller using `uuid.uuid4()`.
|
116
|
+
|
117
|
+
```python
|
118
|
+
import uuid
|
119
|
+
|
120
|
+
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
|
121
|
+
from langchain_postgres import PostgresChatMessageHistory
|
122
|
+
import psycopg
|
123
|
+
|
124
|
+
# Establish a synchronous connection to the database
|
125
|
+
# (or use psycopg.AsyncConnection for async)
|
126
|
+
conn_info = ... # Fill in with your connection info
|
127
|
+
sync_connection = psycopg.connect(conn_info)
|
128
|
+
|
129
|
+
# Create the table schema (only needs to be done once)
|
130
|
+
table_name = "chat_history"
|
131
|
+
PostgresChatMessageHistory.create_tables(sync_connection, table_name)
|
132
|
+
|
133
|
+
session_id = str(uuid.uuid4())
|
134
|
+
|
135
|
+
# Initialize the chat history manager
|
136
|
+
chat_history = PostgresChatMessageHistory(
|
137
|
+
table_name,
|
138
|
+
session_id,
|
139
|
+
sync_connection=sync_connection
|
140
|
+
)
|
141
|
+
|
142
|
+
# Add messages to the chat history
|
143
|
+
chat_history.add_messages([
|
144
|
+
SystemMessage(content="Meow"),
|
145
|
+
AIMessage(content="woof"),
|
146
|
+
HumanMessage(content="bark"),
|
147
|
+
])
|
148
|
+
|
149
|
+
print(chat_history.messages)
|
150
|
+
```
|
151
|
+
|
152
|
+
## Google Cloud Integrations
|
153
|
+
|
154
|
+
[Google Cloud](https://python.langchain.com/docs/integrations/providers/google/) provides Vector Store, Chat Message History, and Data Loader integrations for [AlloyDB](https://cloud.google.com/alloydb) and [Cloud SQL](https://cloud.google.com/sql) for PostgreSQL databases via the following PyPi packages:
|
155
|
+
|
156
|
+
* [`langchain-google-alloydb-pg`](https://github.com/googleapis/langchain-google-alloydb-pg-python)
|
157
|
+
|
158
|
+
* [`langchain-google-cloud-sql-pg`](https://github.com/googleapis/langchain-google-cloud-sql-pg-python)
|
159
|
+
|
160
|
+
Using the Google Cloud integrations provides the following benefits:
|
161
|
+
|
162
|
+
- **Enhanced Security**: Securely connect to Google Cloud databases utilizing IAM for authorization and database authentication without needing to manage SSL certificates, configure firewall rules, or enable authorized networks.
|
163
|
+
- **Simplified and Secure Connections:** Connect to Google Cloud databases effortlessly using the instance name instead of complex connection strings. The integrations creates a secure connection pool that can be easily shared across your application using the `engine` object.
|
164
|
+
|
165
|
+
| Vector Store | Metadata filtering | Async support | Schema Flexibility | Improved metadata handling | Hybrid Search |
|
166
|
+
|--------------------------|--------------------|----------------|--------------------|----------------------------|---------------|
|
167
|
+
| Google AlloyDB | ✓ | ✓ | ✓ | ✓ | ✗ |
|
168
|
+
| Google Cloud SQL Postgres| ✓ | ✓ | ✓ | ✓ | ✗ |
|
169
|
+
|
170
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
langchain_postgres/__init__.py,sha256=-ovoLrNuzL-kMUV-RrIxoEI8wmgOAg4vfE8xevYSA3Q,702
|
2
|
+
langchain_postgres/_utils.py,sha256=N_OBzYFCb_bsHOnZ-YRg6izhmuudorQhupgeG-rSKUc,2848
|
3
|
+
langchain_postgres/chat_message_histories.py,sha256=Et5AgXSRBCghLC5sn6EEUDd1xupaiPv-A5IyNBjpaTc,14213
|
4
|
+
langchain_postgres/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
+
langchain_postgres/translator.py,sha256=6cTS2RJUodMUdsurJM-f-vgPXl6Ad6bfMo8ECuh5Jr4,1524
|
6
|
+
langchain_postgres/utils/pgvector_migrator.py,sha256=OxW2_FxaomZw5kqPAz-3lmZ5t2hSXU4ZW3xK6O62MH4,11771
|
7
|
+
langchain_postgres/v2/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
8
|
+
langchain_postgres/v2/async_vectorstore.py,sha256=FMV-IwH7cJ2VuxnrMCy0X0WWG65oHNXfKAwsdf0Tp20,51786
|
9
|
+
langchain_postgres/v2/engine.py,sha256=FaxGEM3Z6j6IGIAeddJN-qPEp9rrJAZax390WJiXJu8,14197
|
10
|
+
langchain_postgres/v2/indexes.py,sha256=aLCFGYiIbLBUr88drMLD6l41MPRI7lv0ALMVRWfqdq4,4888
|
11
|
+
langchain_postgres/v2/vectorstores.py,sha256=R17q1KIEZPBwEHgE6JYiRSiN8rZXzVPCmBoJobiyjM8,37198
|
12
|
+
langchain_postgres/vectorstores.py,sha256=vzRbPwU1Rn-pOsnTsz1u72cSYD7H8jMlW4N7A58QIt4,83826
|
13
|
+
langchain_postgres-0.0.14.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
|
14
|
+
langchain_postgres-0.0.14.dist-info/METADATA,sha256=9DUsmBkHxzj65n7tfVkOv61sFiEoFeVmjC6hRhEWGJs,7176
|
15
|
+
langchain_postgres-0.0.14.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
16
|
+
langchain_postgres-0.0.14.dist-info/RECORD,,
|
@@ -1,109 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: langchain-postgres
|
3
|
-
Version: 0.0.12
|
4
|
-
Summary: An integration package connecting Postgres and LangChain
|
5
|
-
Home-page: https://github.com/langchain-ai/langchain-postgres
|
6
|
-
License: MIT
|
7
|
-
Requires-Python: >=3.9,<4.0
|
8
|
-
Classifier: License :: OSI Approved :: MIT License
|
9
|
-
Classifier: Programming Language :: Python :: 3
|
10
|
-
Classifier: Programming Language :: Python :: 3.9
|
11
|
-
Classifier: Programming Language :: Python :: 3.10
|
12
|
-
Classifier: Programming Language :: Python :: 3.11
|
13
|
-
Classifier: Programming Language :: Python :: 3.12
|
14
|
-
Requires-Dist: langchain-core (>=0.2.13,<0.4.0)
|
15
|
-
Requires-Dist: numpy (>=1,<2)
|
16
|
-
Requires-Dist: pgvector (>=0.2.5,<0.3.0)
|
17
|
-
Requires-Dist: psycopg (>=3,<4)
|
18
|
-
Requires-Dist: psycopg-pool (>=3.2.1,<4.0.0)
|
19
|
-
Requires-Dist: sqlalchemy (>=2,<3)
|
20
|
-
Project-URL: Repository, https://github.com/langchain-ai/langchain-postgres
|
21
|
-
Project-URL: Source Code, https://github.com/langchain-ai/langchain-postgres/tree/master/langchain_postgres
|
22
|
-
Description-Content-Type: text/markdown
|
23
|
-
|
24
|
-
# langchain-postgres
|
25
|
-
|
26
|
-
[](https://github.com/langchain-ai/langchain-postgres/releases)
|
27
|
-
[](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml)
|
28
|
-
[](https://opensource.org/licenses/MIT)
|
29
|
-
[](https://twitter.com/langchainai)
|
30
|
-
[](https://discord.gg/6adMQxSpJS)
|
31
|
-
[](https://github.com/langchain-ai/langchain-postgres/issues)
|
32
|
-
|
33
|
-
The `langchain-postgres` package implementations of core LangChain abstractions using `Postgres`.
|
34
|
-
|
35
|
-
The package is released under the MIT license.
|
36
|
-
|
37
|
-
Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.
|
38
|
-
|
39
|
-
## Requirements
|
40
|
-
|
41
|
-
The package currently only supports the [psycogp3](https://www.psycopg.org/psycopg3/) driver.
|
42
|
-
|
43
|
-
## Installation
|
44
|
-
|
45
|
-
```bash
|
46
|
-
pip install -U langchain-postgres
|
47
|
-
```
|
48
|
-
|
49
|
-
## Change Log
|
50
|
-
|
51
|
-
0.0.6:
|
52
|
-
- Remove langgraph as a dependency as it was causing dependency conflicts.
|
53
|
-
- Base interface for checkpointer changed in langgraph, so existing implementation would've broken regardless.
|
54
|
-
|
55
|
-
## Usage
|
56
|
-
|
57
|
-
### ChatMessageHistory
|
58
|
-
|
59
|
-
The chat message history abstraction helps to persist chat message history
|
60
|
-
in a postgres table.
|
61
|
-
|
62
|
-
PostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`.
|
63
|
-
|
64
|
-
The `table_name` is the name of the table in the database where
|
65
|
-
the chat messages will be stored.
|
66
|
-
|
67
|
-
The `session_id` is a unique identifier for the chat session. It can be assigned
|
68
|
-
by the caller using `uuid.uuid4()`.
|
69
|
-
|
70
|
-
```python
|
71
|
-
import uuid
|
72
|
-
|
73
|
-
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
|
74
|
-
from langchain_postgres import PostgresChatMessageHistory
|
75
|
-
import psycopg
|
76
|
-
|
77
|
-
# Establish a synchronous connection to the database
|
78
|
-
# (or use psycopg.AsyncConnection for async)
|
79
|
-
conn_info = ... # Fill in with your connection info
|
80
|
-
sync_connection = psycopg.connect(conn_info)
|
81
|
-
|
82
|
-
# Create the table schema (only needs to be done once)
|
83
|
-
table_name = "chat_history"
|
84
|
-
PostgresChatMessageHistory.create_tables(sync_connection, table_name)
|
85
|
-
|
86
|
-
session_id = str(uuid.uuid4())
|
87
|
-
|
88
|
-
# Initialize the chat history manager
|
89
|
-
chat_history = PostgresChatMessageHistory(
|
90
|
-
table_name,
|
91
|
-
session_id,
|
92
|
-
sync_connection=sync_connection
|
93
|
-
)
|
94
|
-
|
95
|
-
# Add messages to the chat history
|
96
|
-
chat_history.add_messages([
|
97
|
-
SystemMessage(content="Meow"),
|
98
|
-
AIMessage(content="woof"),
|
99
|
-
HumanMessage(content="bark"),
|
100
|
-
])
|
101
|
-
|
102
|
-
print(chat_history.messages)
|
103
|
-
```
|
104
|
-
|
105
|
-
|
106
|
-
### Vectorstore
|
107
|
-
|
108
|
-
See example for the [PGVector vectorstore here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/vectorstore.ipynb)
|
109
|
-
|
@@ -1,10 +0,0 @@
|
|
1
|
-
langchain_postgres/__init__.py,sha256=iH8JoksQ4c_36xRmbi9WoYzfBRETTH8r8PPDD_wrRt8,502
|
2
|
-
langchain_postgres/_utils.py,sha256=N_OBzYFCb_bsHOnZ-YRg6izhmuudorQhupgeG-rSKUc,2848
|
3
|
-
langchain_postgres/chat_message_histories.py,sha256=gh6hjBlrJ5GSo5kePQdh3VhiUYoWWdP37GXtZ1e25a4,14033
|
4
|
-
langchain_postgres/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
-
langchain_postgres/translator.py,sha256=6cTS2RJUodMUdsurJM-f-vgPXl6Ad6bfMo8ECuh5Jr4,1524
|
6
|
-
langchain_postgres/vectorstores.py,sha256=Z2nPxj6234pgUtTfVYEAs2lON840Ugw9YuOjjpiJcc4,83866
|
7
|
-
langchain_postgres-0.0.12.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
|
8
|
-
langchain_postgres-0.0.12.dist-info/METADATA,sha256=EcCWeNDeBzs0ZSYOT9ovDndwgglXu_ghEmXdUvQzMts,4018
|
9
|
-
langchain_postgres-0.0.12.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
10
|
-
langchain_postgres-0.0.12.dist-info/RECORD,,
|
File without changes
|
File without changes
|