langchain-postgres 0.0.1__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 +24 -0
- langchain_postgres/_utils.py +82 -0
- langchain_postgres/chat_message_histories.py +372 -0
- langchain_postgres/checkpoint.py +587 -0
- langchain_postgres/py.typed +0 -0
- langchain_postgres/vectorstores.py +1348 -0
- langchain_postgres-0.0.1.dist-info/LICENSE +21 -0
- langchain_postgres-0.0.1.dist-info/METADATA +187 -0
- langchain_postgres-0.0.1.dist-info/RECORD +10 -0
- langchain_postgres-0.0.1.dist-info/WHEEL +4 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 LangChain, Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,187 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: langchain-postgres
|
3
|
+
Version: 0.0.1
|
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.1,<0.2)
|
15
|
+
Requires-Dist: langgraph (>=0.0.32,<0.0.33)
|
16
|
+
Requires-Dist: numpy (>=1.26.4,<2.0.0)
|
17
|
+
Requires-Dist: pgvector (>=0.2.5,<0.3.0)
|
18
|
+
Requires-Dist: psycopg (>=3.1.18,<4.0.0)
|
19
|
+
Requires-Dist: psycopg-pool (>=3.2.1,<4.0.0)
|
20
|
+
Requires-Dist: sqlalchemy (>=2.0.29,<3.0.0)
|
21
|
+
Project-URL: Repository, https://github.com/langchain-ai/langchain-postgres
|
22
|
+
Description-Content-Type: text/markdown
|
23
|
+
|
24
|
+
# langchain-postgres
|
25
|
+
|
26
|
+
The `langchain-postgres` package implementations of core LangChain abstractions using `Postgres`.
|
27
|
+
|
28
|
+
The package is released under the MIT license.
|
29
|
+
|
30
|
+
Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.
|
31
|
+
|
32
|
+
## Requirements
|
33
|
+
|
34
|
+
The package currently only supports the [psycogp3](https://www.psycopg.org/psycopg3/) driver.
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
```bash
|
39
|
+
pip install -U langchain-postgres
|
40
|
+
```
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
### PostgresSaver (LangGraph Checkpointer)
|
45
|
+
|
46
|
+
The LangGraph checkpointer can be used to add memory to your LangGraph application.
|
47
|
+
|
48
|
+
`PostgresSaver` is an implementation of the checkpointer saver using
|
49
|
+
Postgres as the backend.
|
50
|
+
|
51
|
+
Currently, only the psycopg3 driver is supported.
|
52
|
+
|
53
|
+
Sync usage:
|
54
|
+
|
55
|
+
```python
|
56
|
+
from psycopg_pool import ConnectionPool
|
57
|
+
from langchain_postgres import (
|
58
|
+
PostgresSaver, PickleCheckpointSerializer
|
59
|
+
)
|
60
|
+
|
61
|
+
pool = ConnectionPool(
|
62
|
+
# Example configuration
|
63
|
+
conninfo="postgresql://langchain:langchain@localhost:6024/langchain",
|
64
|
+
max_size=20,
|
65
|
+
)
|
66
|
+
|
67
|
+
PostgresSaver.create_tables(pool)
|
68
|
+
|
69
|
+
checkpointer = PostgresSaver(
|
70
|
+
serializer=PickleCheckpointSerializer(),
|
71
|
+
sync_connection=pool,
|
72
|
+
)
|
73
|
+
|
74
|
+
# Set up the langgraph workflow with the checkpointer
|
75
|
+
workflow = ... # Fill in with your workflow
|
76
|
+
app = workflow.compile(checkpointer=checkpointer)
|
77
|
+
|
78
|
+
# Use with the sync methods of `app` (e.g., `app.stream())
|
79
|
+
|
80
|
+
pool.close() # Remember to close the connection pool.
|
81
|
+
```
|
82
|
+
|
83
|
+
Async usage:
|
84
|
+
|
85
|
+
```python
|
86
|
+
from psycopg_pool import AsyncConnectionPool
|
87
|
+
from langchain_postgres import (
|
88
|
+
PostgresSaver, PickleCheckpointSerializer
|
89
|
+
)
|
90
|
+
|
91
|
+
pool = AsyncConnectionPool(
|
92
|
+
# Example configuration
|
93
|
+
conninfo="postgresql://langchain:langchain@localhost:6024/langchain",
|
94
|
+
max_size=20,
|
95
|
+
)
|
96
|
+
|
97
|
+
# Create the tables in postgres (only needs to be done once)
|
98
|
+
await PostgresSaver.acreate_tables(pool)
|
99
|
+
|
100
|
+
checkpointer = PostgresSaver(
|
101
|
+
serializer=PickleCheckpointSerializer(),
|
102
|
+
async_connection=pool,
|
103
|
+
)
|
104
|
+
|
105
|
+
# Set up the langgraph workflow with the checkpointer
|
106
|
+
workflow = ... # Fill in with your workflow
|
107
|
+
app = workflow.compile(checkpointer=checkpointer)
|
108
|
+
|
109
|
+
# Use with the async methods of `app` (e.g., `app.astream()`)
|
110
|
+
|
111
|
+
await pool.close() # Remember to close the connection pool.
|
112
|
+
```
|
113
|
+
|
114
|
+
#### Testing
|
115
|
+
|
116
|
+
If testing with the postgres checkpointer it may be useful to both create and
|
117
|
+
drop the tables before and after the tests.
|
118
|
+
|
119
|
+
```python
|
120
|
+
from psycopg_pool import ConnectionPool
|
121
|
+
from langchain_postgres import (
|
122
|
+
PostgresSaver
|
123
|
+
)
|
124
|
+
with ConnectionPool(
|
125
|
+
# Example configuration
|
126
|
+
conninfo="postgresql://langchain:langchain@localhost:6024/langchain",
|
127
|
+
max_size=20,
|
128
|
+
) as conn:
|
129
|
+
PostgresSaver.create_tables(conn)
|
130
|
+
PostgresSaver.drop_tables(conn)
|
131
|
+
# Run your unit tests with langgraph
|
132
|
+
```
|
133
|
+
|
134
|
+
|
135
|
+
### ChatMessageHistory
|
136
|
+
|
137
|
+
The chat message history abstraction helps to persist chat message history
|
138
|
+
in a postgres table.
|
139
|
+
|
140
|
+
PostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`.
|
141
|
+
|
142
|
+
The `table_name` is the name of the table in the database where
|
143
|
+
the chat messages will be stored.
|
144
|
+
|
145
|
+
The `session_id` is a unique identifier for the chat session. It can be assigned
|
146
|
+
by the caller using `uuid.uuid4()`.
|
147
|
+
|
148
|
+
```python
|
149
|
+
import uuid
|
150
|
+
|
151
|
+
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
|
152
|
+
from langchain_postgres import PostgresChatMessageHistory
|
153
|
+
import psycopg
|
154
|
+
|
155
|
+
# Establish a synchronous connection to the database
|
156
|
+
# (or use psycopg.AsyncConnection for async)
|
157
|
+
conn_info = ... # Fill in with your connection info
|
158
|
+
sync_connection = psycopg.connect(conn_info)
|
159
|
+
|
160
|
+
# Create the table schema (only needs to be done once)
|
161
|
+
table_name = "chat_history"
|
162
|
+
PostgresChatMessageHistory.create_tables(sync_connection, table_name)
|
163
|
+
|
164
|
+
session_id = str(uuid.uuid4())
|
165
|
+
|
166
|
+
# Initialize the chat history manager
|
167
|
+
chat_history = PostgresChatMessageHistory(
|
168
|
+
table_name,
|
169
|
+
session_id,
|
170
|
+
sync_connection=sync_connection
|
171
|
+
)
|
172
|
+
|
173
|
+
# Add messages to the chat history
|
174
|
+
chat_history.add_messages([
|
175
|
+
SystemMessage(content="Meow"),
|
176
|
+
AIMessage(content="woof"),
|
177
|
+
HumanMessage(content="bark"),
|
178
|
+
])
|
179
|
+
|
180
|
+
print(chat_history.messages)
|
181
|
+
```
|
182
|
+
|
183
|
+
|
184
|
+
### Vectorstore
|
185
|
+
|
186
|
+
See example for the [PGVector vectorstore here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/vectorstore.ipynb)
|
187
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
langchain_postgres/__init__.py,sha256=IMtNMHfY5cWhBuJ8M8diGlOD51L_4Dd741nYa_FeLsY,621
|
2
|
+
langchain_postgres/_utils.py,sha256=Johm50HEgA4qScLDaSDIfJfF3DygjA5KTBtNeVcD63I,2914
|
3
|
+
langchain_postgres/chat_message_histories.py,sha256=gh6hjBlrJ5GSo5kePQdh3VhiUYoWWdP37GXtZ1e25a4,14033
|
4
|
+
langchain_postgres/checkpoint.py,sha256=B0c03jyQcS0bAaNPljWEG3IuhlelIF2z3vSXWATi9-4,23519
|
5
|
+
langchain_postgres/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
+
langchain_postgres/vectorstores.py,sha256=u0mMWep6HSp5r0uNkoSE1VS22c0x8AfvKBj5UVjByJQ,49908
|
7
|
+
langchain_postgres-0.0.1.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
|
8
|
+
langchain_postgres-0.0.1.dist-info/METADATA,sha256=9jY1xiz_lVgDnpTRNLOa7Dp-xUHh8iiw1xtqosNzbUc,5186
|
9
|
+
langchain_postgres-0.0.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
10
|
+
langchain_postgres-0.0.1.dist-info/RECORD,,
|