Skip to content

Commit d6d843e

Browse files
authored
langchain-postgres: Initial package with postgres chat history implementation (langchain-ai#19884)
- [x] Add in code examples for the chat message history class - [ ] ~Add docs with notebook examples~ (can this be done later?) - [x] Update README.md
1 parent d293431 commit d6d843e

19 files changed

+1940
-310
lines changed

libs/partners/postgres/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -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.

libs/partners/postgres/Makefile

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.PHONY: all format lint test tests integration_tests docker_tests help extended_tests
2+
3+
# Default target executed when no arguments are given to make.
4+
all: help
5+
6+
# Define a variable for the test file path.
7+
TEST_FILE ?= tests/unit_tests/
8+
integration_test integration_tests: TEST_FILE = tests/integration_tests/
9+
10+
test tests integration_test integration_tests:
11+
poetry run pytest $(TEST_FILE)
12+
13+
######################
14+
# LINTING AND FORMATTING
15+
######################
16+
17+
# Define a variable for Python and notebook files.
18+
PYTHON_FILES=.
19+
MYPY_CACHE=.mypy_cache
20+
lint format: PYTHON_FILES=.
21+
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/partners/postgres --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
22+
lint_package: PYTHON_FILES=langchain_postgres
23+
lint_tests: PYTHON_FILES=tests
24+
lint_tests: MYPY_CACHE=.mypy_cache_test
25+
26+
lint lint_diff lint_package lint_tests:
27+
poetry run ruff .
28+
poetry run ruff format $(PYTHON_FILES) --diff
29+
poetry run ruff --select I $(PYTHON_FILES)
30+
mkdir -p $(MYPY_CACHE); poetry run mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)
31+
32+
format format_diff:
33+
poetry run ruff format $(PYTHON_FILES)
34+
poetry run ruff --select I --fix $(PYTHON_FILES)
35+
36+
spell_check:
37+
poetry run codespell --toml pyproject.toml
38+
39+
spell_fix:
40+
poetry run codespell --toml pyproject.toml -w
41+
42+
check_imports: $(shell find langchain_postgres -name '*.py')
43+
poetry run python ./scripts/check_imports.py $^
44+
45+
######################
46+
# HELP
47+
######################
48+
49+
help:
50+
@echo '----'
51+
@echo 'check_imports - check imports'
52+
@echo 'format - run code formatters'
53+
@echo 'lint - run linters'
54+
@echo 'test - run unit tests'
55+
@echo 'tests - run unit tests'
56+
@echo 'test TEST_FILE=<test_file> - run all tests in file'

libs/partners/postgres/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from importlib import metadata
2+
3+
from langchain_postgres.chat_message_histories import PostgresChatMessageHistory
4+
5+
try:
6+
__version__ = metadata.version(__package__)
7+
except metadata.PackageNotFoundError:
8+
# Case where package metadata is not available.
9+
__version__ = ""
10+
11+
__all__ = [
12+
"__version__",
13+
"PostgresChatMessageHistory",
14+
]

0 commit comments

Comments
 (0)