Skip to content

Commit 016f1a3

Browse files
authored
chore(docs): Add sample notebook and documentation for PGVectorStore (#172)
1 parent 324c1d4 commit 016f1a3

File tree

2 files changed

+725
-13
lines changed

2 files changed

+725
-13
lines changed

README.md

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
The `langchain-postgres` package implementations of core LangChain abstractions using `Postgres`.
1111

12-
The package is released under the MIT license.
12+
The package is released under the MIT license.
1313

1414
Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application.
1515

@@ -23,22 +23,65 @@ The package currently only supports the [psycogp3](https://www.psycopg.org/psyco
2323
pip install -U langchain-postgres
2424
```
2525

26-
## Change Log
26+
## Usage
2727

28-
0.0.6:
29-
- Remove langgraph as a dependency as it was causing dependency conflicts.
30-
- Base interface for checkpointer changed in langgraph, so existing implementation would've broken regardless.
28+
### Vectorstore
3129

32-
## Usage
30+
> [!NOTE]
31+
> See example for the [PGVector vectorstore here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/vectorstore.ipynb)
32+
`PGVector` is being deprecated. Please migrate to `PGVectorStore`.
33+
`PGVectorStore` is used for improved performance and manageability.
34+
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`.
35+
36+
> [!TIP]
37+
> All synchronous functions have corresponding asynchronous functions
38+
39+
```python
40+
from langchain_postgres import PGEngine, PGVectorStore
41+
from langchain_core.embeddings import DeterministicFakeEmbedding
42+
import uuid
43+
44+
# Replace these variable values
45+
engine = PGEngine.from_connection_string(url=CONNECTION_STRING)
46+
47+
VECTOR_SIZE = 768
48+
embedding = DeterministicFakeEmbedding(size=VECTOR_SIZE)
49+
50+
engine.init_vectorstore_table(
51+
table_name="destination_table",
52+
vector_size=VECTOR_SIZE,
53+
)
54+
55+
store = PGVectorStore.create_sync(
56+
engine=engine,
57+
table_name=TABLE_NAME,
58+
embedding_service=embedding,
59+
)
60+
61+
all_texts = ["Apples and oranges", "Cars and airplanes", "Pineapple", "Train", "Banana"]
62+
metadatas = [{"len": len(t)} for t in all_texts]
63+
ids = [str(uuid.uuid4()) for _ in all_texts]
64+
docs = [
65+
Document(id=ids[i], page_content=all_texts[i], metadata=metadatas[i]) for i in range(len(all_texts))
66+
]
67+
68+
store.add_documents(docs)
69+
70+
query = "I'd like a fruit."
71+
docs = store.similarity_search(query)
72+
print(docs)
73+
```
74+
75+
For a detailed example on `PGVectorStore` see [here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/pg_vectorstore.ipynb).
3376

3477
### ChatMessageHistory
3578

36-
The chat message history abstraction helps to persist chat message history
79+
The chat message history abstraction helps to persist chat message history
3780
in a postgres table.
3881

3982
PostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`.
4083

41-
The `table_name` is the name of the table in the database where
84+
The `table_name` is the name of the table in the database where
4285
the chat messages will be stored.
4386

4487
The `session_id` is a unique identifier for the chat session. It can be assigned
@@ -78,8 +121,3 @@ chat_history.add_messages([
78121

79122
print(chat_history.messages)
80123
```
81-
82-
83-
### Vectorstore
84-
85-
See example for the [PGVector vectorstore here](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/vectorstore.ipynb)

0 commit comments

Comments
 (0)