|
| 1 | +# langchain-postgres |
| 2 | + |
| 3 | +The `langchain-postgres` package is an integration package managed by the core LangChain team. |
| 4 | + |
| 5 | +This package contains implementations of core abstractions using `Postgres`. |
| 6 | + |
| 7 | +The package is released under the MIT license. |
| 8 | + |
| 9 | +Feel free to use the abstraction as provided or else modify them / extend them as appropriate for your own application. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install -U langchain-postgres |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### ChatMessageHistory |
| 20 | + |
| 21 | +The chat message history abstraction helps to persist chat message history |
| 22 | +in a postgres table. |
| 23 | + |
| 24 | +PostgresChatMessageHistory is parameterized using a `table_name` and a `session_id`. |
| 25 | + |
| 26 | +The `table_name` is the name of the table in the database where |
| 27 | +the chat messages will be stored. |
| 28 | + |
| 29 | +The `session_id` is a unique identifier for the chat session. It can be assigned |
| 30 | +by the caller using `uuid.uuid4()`. |
| 31 | + |
| 32 | +```python |
| 33 | +import uuid |
| 34 | + |
| 35 | +from langchain_core.messages import SystemMessage, AIMessage, HumanMessage |
| 36 | +from langchain_postgres import PostgresChatMessageHistory |
| 37 | +import psycopg |
| 38 | + |
| 39 | +# Establish a synchronous connection to the database |
| 40 | +# (or use psycopg.AsyncConnection for async) |
| 41 | +conn_info = ... # Fill in with your connection info |
| 42 | +sync_connection = psycopg.connect(conn_info) |
| 43 | + |
| 44 | +# Create the table schema (only needs to be done once) |
| 45 | +table_name = "chat_history" |
| 46 | +PostgresChatMessageHistory.create_schema(sync_connection, table_name) |
| 47 | + |
| 48 | +session_id = str(uuid.uuid4()) |
| 49 | + |
| 50 | +# Initialize the chat history manager |
| 51 | +chat_history = PostgresChatMessageHistory( |
| 52 | + table_name, |
| 53 | + session_id, |
| 54 | + sync_connection=sync_connection |
| 55 | +) |
| 56 | + |
| 57 | +# Add messages to the chat history |
| 58 | +chat_history.add_messages([ |
| 59 | + SystemMessage(content="Meow"), |
| 60 | + AIMessage(content="woof"), |
| 61 | + HumanMessage(content="bark"), |
| 62 | +]) |
| 63 | + |
| 64 | +print(chat_history.messages) |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +### PostgresCheckpoint |
| 69 | + |
| 70 | +An implementation of the `Checkpoint` abstraction in LangGraph using Postgres. |
| 71 | + |
| 72 | + |
| 73 | +Async Usage: |
| 74 | + |
| 75 | +```python |
| 76 | +from psycopg_pool import AsyncConnectionPool |
| 77 | +from langchain_postgres import ( |
| 78 | + PostgresCheckpoint, PickleCheckpointSerializer |
| 79 | +) |
| 80 | + |
| 81 | +pool = AsyncConnectionPool( |
| 82 | + # Example configuration |
| 83 | + conninfo="postgresql://user:password@localhost:5432/dbname", |
| 84 | + max_size=20, |
| 85 | +) |
| 86 | + |
| 87 | +# Uses the pickle module for serialization |
| 88 | +# Make sure that you're only de-serializing trusted data |
| 89 | +# (e.g., payloads that you have serialized yourself). |
| 90 | +# Or implement a custom serializer. |
| 91 | +checkpoint = PostgresCheckpoint( |
| 92 | + serializer=PickleCheckpointSerializer(), |
| 93 | + async_connection=pool, |
| 94 | +) |
| 95 | + |
| 96 | +# Use the checkpoint object to put, get, list checkpoints, etc. |
| 97 | +``` |
| 98 | + |
| 99 | +Sync Usage: |
| 100 | + |
| 101 | +```python |
| 102 | +from psycopg_pool import ConnectionPool |
| 103 | +from langchain_postgres import ( |
| 104 | + PostgresCheckpoint, PickleCheckpointSerializer |
| 105 | +) |
| 106 | + |
| 107 | +pool = ConnectionPool( |
| 108 | + # Example configuration |
| 109 | + conninfo="postgresql://user:password@localhost:5432/dbname", |
| 110 | + max_size=20, |
| 111 | +) |
| 112 | + |
| 113 | +# Uses the pickle module for serialization |
| 114 | +# Make sure that you're only de-serializing trusted data |
| 115 | +# (e.g., payloads that you have serialized yourself). |
| 116 | +# Or implement a custom serializer. |
| 117 | +checkpoint = PostgresCheckpoint( |
| 118 | + serializer=PickleCheckpointSerializer(), |
| 119 | + sync_connection=pool, |
| 120 | +) |
| 121 | + |
| 122 | +# Use the checkpoint object to put, get, list checkpoints, etc. |
| 123 | +``` |
0 commit comments