-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathREADME.md
265 lines (197 loc) · 8.04 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# langchain-postgres
[![Release Notes](https://img.shields.io/github/release/langchain-ai/langchain-postgres)](https://github.com/langchain-ai/langchain-postgres/releases)
[![CI](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml/badge.svg)](https://github.com/langchain-ai/langchain-postgres/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/langchainai.svg?style=social&label=Follow%20%40LangChainAI)](https://twitter.com/langchainai)
[![](https://dcbadge.vercel.app/api/server/6adMQxSpJS?compact=true&style=flat)](https://discord.gg/6adMQxSpJS)
[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langchain-postgres)](https://github.com/langchain-ai/langchain-postgres/issues)
The `langchain-postgres` package implementations of core LangChain abstractions using `Postgres`.
The package is released under the MIT license.
Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.
## Requirements
The package currently only supports the [psycogp3](https://www.psycopg.org/psycopg3/) driver.
## Installation
```bash
pip install -U langchain-postgres
```
## Change Log
0.0.6:
- Remove langgraph as a dependency as it was causing dependency conflicts.
- Base interface for checkpointer changed in langgraph, so existing implementation would've broken regardless.
## Usage
### HNSW index
```python
from langchain_postgres import PGVector, EmbeddingIndexType
PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
embedding_length=1536,
embedding_index=EmbeddingIndexType.hnsw,
embedding_index_ops="vector_cosine_ops",
)
```
- Embedding length is required for HNSW index.
- Allowed values for `embedding_index_ops` are described in the [pgvector HNSW](https://github.com/pgvector/pgvector?tab=readme-ov-file#hnsw).
Can set `ef_construction` and `m` parameters for HNSW index.
Refer to the [pgvector HNSW Index Options](https://github.com/pgvector/pgvector?tab=readme-ov-file#index-options) to better understand these parameters.
```python
from langchain_postgres import PGVector, EmbeddingIndexType
PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
embedding_length=1536,
embedding_index=EmbeddingIndexType.hnsw,
embedding_index_ops="vector_cosine_ops",
ef_construction=200,
m=48,
)
```
### IVFFlat index
```python
from langchain_postgres import PGVector, EmbeddingIndexType
PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
embedding_length=1536,
embedding_index=EmbeddingIndexType.ivfflat,
embedding_index_ops="vector_cosine_ops",
)
```
- Embedding length is required for HNSW index.
- Allowed values for `embedding_index_ops` are described in the [pgvector IVFFlat](https://github.com/pgvector/pgvector?tab=readme-ov-file#ivfflat).
### Binary Quantization
```python
from langchain_postgres import PGVector, EmbeddingIndexType
PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
embedding_length=1536,
embedding_index=EmbeddingIndexType.hnsw,
embedding_index_ops="bit_hamming_ops",
binary_quantization=True,
binary_limit=200,
)
```
- Works only with HNSW index with `bit_hamming_ops`.
- `binary_limit` increases the limit in the inner binary search. A higher value will increase the recall at the cost of speed.
Refer to the [pgvector Binary Quantization](https://github.com/pgvector/pgvector?tab=readme-ov-file#binary-quantization) to better understand.
### Partitioning
```python
from langchain_postgres import PGVector
PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
enable_partitioning=True,
)
```
- Create partitions of `langchain_pg_embedding` table by `collection_id`. Useful with a large number of embeddings with different collection.
Refer to the [pgvector Partitioning](https://github.com/pgvector/pgvector?tab=readme-ov-file#filtering)
### Iterative Scan
```python
from langchain_postgres import PGVector, EmbeddingIndexType, IterativeScan
PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
embedding_length=1536,
embedding_index=EmbeddingIndexType.hnsw,
embedding_index_ops="vector_cosine_ops",
iterative_scan=IterativeScan.relaxed_order
)
```
- `iterative_scan` can be set to `IterativeScan.relaxed_order` or `IterativeScan.strict_order` or disabled with `IterativeScan.off`.
- Requires an HNSW or IVFFlat index.
Refer to the [pgvector Iterative Scan](https://github.com/pgvector/pgvector?tab=readme-ov-file#iterative-index-scans) to better understand.
### Iterative Scan Options for HNSW index
```python
from langchain_postgres import PGVector, EmbeddingIndexType, IterativeScan
PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
embedding_length=1536,
embedding_index=EmbeddingIndexType.hnsw,
embedding_index_ops="vector_cosine_ops",
iterative_scan=IterativeScan.relaxed_order,
max_scan_tuples=40000,
scan_mem_multiplier=2
)
```
- `max_scan_tuples` control when the scan ends when `iterative_scan` is enabled.
- `scan_mem_multiplier` specify the max amount of memory to use for the scan.
Refer to the [pgvector Iterative Scan Options](https://github.com/pgvector/pgvector?tab=readme-ov-file#iterative-scan-options) to better understand.
### Full Text Search
Can be used by specifying `full_text_search` parameter.
```python
from langchain_postgres import PGVector
vectorstore = PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
)
vectorstore.similarity_search(
"hello world",
full_text_search=["foo", "bar & baz"]
)
```
This adds the following statement to the `WHERE` clause:
```sql
AND document_vector @@ to_tsquery('foo | bar & baz')
```
Can be used with retrievers like this:
```python
from langchain_postgres import PGVector
vectorstore = PGVector(
collection_name="test_collection",
embeddings=FakeEmbedding(),
connection=CONNECTION_STRING,
)
retriever = vectorstore.as_retriever(
search_kwargs={
"full_text_search": ["foo", "bar & baz"]
}
)
```
Refer to Postgres [Full Text Search](https://www.postgresql.org/docs/current/textsearch.html) for more information.
### ChatMessageHistory
The chat message history abstraction helps to persist chat message history
in a postgres table.
PostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`.
The `table_name` is the name of the table in the database where
the chat messages will be stored.
The `session_id` is a unique identifier for the chat session. It can be assigned
by the caller using `uuid.uuid4()`.
```python
import uuid
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
from langchain_postgres import PostgresChatMessageHistory
import psycopg
# Establish a synchronous connection to the database
# (or use psycopg.AsyncConnection for async)
conn_info = ... # Fill in with your connection info
sync_connection = psycopg.connect(conn_info)
# Create the table schema (only needs to be done once)
table_name = "chat_history"
PostgresChatMessageHistory.create_tables(sync_connection, table_name)
session_id = str(uuid.uuid4())
# Initialize the chat history manager
chat_history = PostgresChatMessageHistory(
table_name,
session_id,
sync_connection=sync_connection
)
# Add messages to the chat history
chat_history.add_messages([
SystemMessage(content="Meow"),
AIMessage(content="woof"),
HumanMessage(content="bark"),
])
print(chat_history.messages)
```
### Vectorstore
See example for the [PGVector vectorstore here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/vectorstore.ipynb)