Skip to content

Commit 5fa1b6d

Browse files
committed
Copy over repo from langchain main repo
0 parents  commit 5fa1b6d

27 files changed

+5164
-0
lines changed

.gitignore

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
### Python template
2+
# Byte-compiled / optimized / DLL files
3+
__pycache__/
4+
*.py[cod]
5+
*$py.class
6+
7+
# C extensions
8+
*.so
9+
10+
# Distribution / packaging
11+
.Python
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
cover/
54+
55+
# Translations
56+
*.mo
57+
*.pot
58+
59+
# Django stuff:
60+
*.log
61+
local_settings.py
62+
db.sqlite3
63+
db.sqlite3-journal
64+
65+
# Flask stuff:
66+
instance/
67+
.webassets-cache
68+
69+
# Scrapy stuff:
70+
.scrapy
71+
72+
# Sphinx documentation
73+
docs/_build/
74+
75+
# PyBuilder
76+
.pybuilder/
77+
target/
78+
79+
# Jupyter Notebook
80+
.ipynb_checkpoints
81+
82+
# IPython
83+
profile_default/
84+
ipython_config.py
85+
86+
# pyenv
87+
# For a library or package, you might want to ignore these files since the code is
88+
# intended to run in multiple environments; otherwise, check them in:
89+
# .python-version
90+
91+
# pipenv
92+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
94+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
95+
# install all needed dependencies.
96+
#Pipfile.lock
97+
98+
# poetry
99+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
100+
# This is especially recommended for binary packages to ensure reproducibility, and is more
101+
# commonly ignored for libraries.
102+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
103+
#poetry.lock
104+
105+
# pdm
106+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
107+
#pdm.lock
108+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
109+
# in version control.
110+
# https://pdm.fming.dev/#use-with-ide
111+
.pdm.toml
112+
113+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
114+
__pypackages__/
115+
116+
# Celery stuff
117+
celerybeat-schedule
118+
celerybeat.pid
119+
120+
# SageMath parsed files
121+
*.sage.py
122+
123+
# Environments
124+
.env
125+
.venv
126+
env/
127+
venv/
128+
ENV/
129+
env.bak/
130+
venv.bak/
131+
132+
# Spyder project settings
133+
.spyderproject
134+
.spyproject
135+
136+
# Rope project settings
137+
.ropeproject
138+
139+
# mkdocs documentation
140+
/site
141+
142+
# mypy
143+
.mypy_cache/
144+
.dmypy.json
145+
dmypy.json
146+
147+
# Pyre type checker
148+
.pyre/
149+
150+
# pytype static type analyzer
151+
.pytype/
152+
153+
# Cython debug symbols
154+
cython_debug/
155+
156+
# PyCharm
157+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
159+
# and can be added to the global gitignore or merged into this file. For a more nuclear
160+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
161+
#.idea/
162+
.DS_Store
163+
164+
# Pycharm
165+
.idea
166+
167+
# pyenv virtualenv
168+
.python-version

LICENSE

Lines changed: 21 additions & 0 deletions
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.

Makefile

Lines changed: 56 additions & 0 deletions
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'

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
```

langchain_postgres/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from importlib import metadata
2+
3+
from langchain_postgres.chat_message_histories import PostgresChatMessageHistory
4+
from langchain_postgres.checkpoint import (
5+
CheckpointSerializer,
6+
PickleCheckpointSerializer,
7+
PostgresCheckpoint,
8+
)
9+
10+
try:
11+
__version__ = metadata.version(__package__)
12+
except metadata.PackageNotFoundError:
13+
# Case where package metadata is not available.
14+
__version__ = ""
15+
16+
__all__ = [
17+
"__version__",
18+
"CheckpointSerializer",
19+
"PostgresChatMessageHistory",
20+
"PostgresCheckpoint",
21+
"PickleCheckpointSerializer",
22+
]

0 commit comments

Comments
 (0)