Skip to content

Commit

Permalink
INTPYTHON-377 langgraph-checkpoint-mongodb (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyclements authored Nov 22, 2024
1 parent 8514d09 commit 21c0680
Show file tree
Hide file tree
Showing 38 changed files with 37,046 additions and 897 deletions.
10 changes: 6 additions & 4 deletions .github/scripts/check_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import sys
from typing import Dict

LIB_DIRS = ["libs/mongodb"]
LIB_DIRS = ["libs/mongodb", "libs/langgraph-checkpoint-mongodb"]

if __name__ == "__main__":
files = sys.argv[1:]
files = sys.argv[1:] # changed files

dirs_to_run: Dict[str, set] = {
"lint": set(),
Expand All @@ -26,21 +26,23 @@
".github/scripts/check_diff.py",
)
):
# add all LANGCHAIN_DIRS for infra changes
# add all LIB_DIRS for infra changes
dirs_to_run["test"].update(LIB_DIRS)

if any(file.startswith(dir_) for dir_ in LIB_DIRS):
for dir_ in LIB_DIRS:
if file.startswith(dir_):
dirs_to_run["test"].add(dir_)
if "langgraph-checkpoint-mongodb/tests" not in file:
dirs_to_run["lint"].add(dir_)
elif file.startswith("libs/"):
raise ValueError(
f"Unknown lib: {file}. check_diff.py likely needs "
"an update for this new library!"
)

outputs = {
"dirs-to-lint": list(dirs_to_run["lint"] | dirs_to_run["test"]),
"dirs-to-lint": list(dirs_to_run["lint"]),
"dirs-to-test": list(dirs_to_run["test"]),
}
for key, value in outputs.items():
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/_codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
working-directory: ${{ inputs.working-directory }}
run: |
# Use a Python script to extract the ignore words list from pyproject.toml
python ../../.github/workflows/extract_ignored_words_list.py
python ../../.github/scripts/extract_ignored_words_list.py
id: extract_ignore_words

- name: Codespell
Expand Down
55 changes: 0 additions & 55 deletions .github/workflows/_compile_integration_test.yml

This file was deleted.

12 changes: 6 additions & 6 deletions .github/workflows/_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
# It doesn't matter how you change it, any change will cause a cache-bust.
working-directory: ${{ inputs.working-directory }}
run: |
poetry install --with lint,typing
poetry install --with dev
- name: Get .mypy_cache to speed up mypy
uses: actions/cache@v4
Expand All @@ -85,7 +85,7 @@ jobs:
- name: Install unit+integration test dependencies
working-directory: ${{ inputs.working-directory }}
run: |
poetry install --with test,test_integration
poetry install --with dev
- name: Get .mypy_cache_test to speed up mypy
uses: actions/cache@v4
Expand All @@ -96,7 +96,7 @@ jobs:
${{ env.WORKDIR }}/.mypy_cache_test
key: mypy-test-${{ runner.os }}-${{ runner.arch }}-py${{ matrix.python-version }}-${{ inputs.working-directory }}-${{ hashFiles(format('{0}/poetry.lock', inputs.working-directory)) }}

- name: Analysing the code with our lint
working-directory: ${{ inputs.working-directory }}
run: |
make lint_tests
# - name: Analysing the code with our lint
# working-directory: ${{ inputs.working-directory }}
# run: |
# make lint_tests
4 changes: 2 additions & 2 deletions .github/workflows/_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
needs:
- build
uses:
./.github/workflows/_test_release.yml
./.github/workflows/_langchain-test_release.yml
permissions: write-all
with:
working-directory: ${{ inputs.working-directory }}
Expand Down Expand Up @@ -138,7 +138,7 @@ jobs:
poetry run python -c "import $IMPORT_NAME; print(dir($IMPORT_NAME))"
- name: Import test dependencies
run: poetry install --with test,test_integration
run: poetry install --with dev
working-directory: ${{ inputs.working-directory }}

# Overwrite the local version of the package with the test PyPI version.
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ jobs:

- name: Install dependencies
shell: bash
run: poetry install --with test
run: poetry install --with dev

- name: Start MongoDB
uses: supercharge/[email protected]

- name: Run core tests
shell: bash
Expand Down
13 changes: 1 addition & 12 deletions .github/workflows/check_diffs.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,9 @@ jobs:
working-directory: ${{ matrix.working-directory }}
secrets: inherit

compile-integration-tests:
name: cd ${{ matrix.working-directory }}
needs: [ build ]
if: ${{ needs.build.outputs.dirs-to-test != '[]' }}
strategy:
matrix:
working-directory: ${{ fromJson(needs.build.outputs.dirs-to-test) }}
uses: ./.github/workflows/_compile_integration_test.yml
with:
working-directory: ${{ matrix.working-directory }}
secrets: inherit
ci_success:
name: "CI Success"
needs: [build, lint, test, compile-integration-tests]
needs: [build, lint, test]
if: |
always()
runs-on: ubuntu-latest
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 🦜️🔗 LangChain MongoDB

This repository contains 1 package with MongoDB integrations with LangChain:
This is a Monorepo containing partner packages of MongoDB and LangChainAI.
It includes integrations between MongoDB, Atlas, LangChain, and LangGraph.

- [langchain-mongodb](https://pypi.org/project/langchain-mongodb/)
It contains the following packages.

- `langchain-mongodb` ([PyPI](https://pypi.org/project/langchain-mongodb/))
- `langgraph-checkpoint-mongodb`
42 changes: 42 additions & 0 deletions libs/langgraph-checkpoint-mongodb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.PHONY: test lint format help

######################
# TESTING AND COVERAGE
######################

test tests:
poetry run pytest tests

######################
# LINTING AND FORMATTING
######################

# Define a variable for Python and notebook files.
PYTHON_FILES=.
MYPY_CACHE=.mypy_cache
lint format: PYTHON_FILES=.
lint_diff format_diff: PYTHON_FILES=$(shell git diff --name-only --relative --diff-filter=d main . | grep -E '\.py$$|\.ipynb$$')
lint_package: PYTHON_FILES=langgraph
lint_tests: PYTHON_FILES=tests
lint_tests: MYPY_CACHE=.mypy_cache_test

lint lint_diff lint_package lint_tests:
poetry run ruff check .
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES) --diff
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff check --select I $(PYTHON_FILES)
[ "$(PYTHON_FILES)" = "" ] || mkdir -p $(MYPY_CACHE)
[ "$(PYTHON_FILES)" = "" ] || poetry run mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)

format format_diff:
poetry run ruff format $(PYTHON_FILES)
poetry run ruff check --select I --fix $(PYTHON_FILES)

######################
# HELP
######################

help:
@echo '----'
@echo 'format - run code formatters'
@echo 'lint - run linters'
@echo 'test - run unit tests'
95 changes: 95 additions & 0 deletions libs/langgraph-checkpoint-mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# LangGraph Checkpoint MongoDB

Implementation of LangGraph CheckpointSaver that uses MongoDB.

## Usage

```python
from langgraph.checkpoint.mongodb import MongoDBSaver

write_config = {"configurable": {"thread_id": "1", "checkpoint_ns": ""}}
read_config = {"configurable": {"thread_id": "1"}}

MONGODB_URI = "mongodb://localhost:27017"
DB_NAME = "checkpoint_example"

with MongoDBSaver.from_conn_string(MONGODB_URI, DB_NAME) as checkpointer:
# call .setup() the first time you're using the checkpointer
checkpointer.setup()
checkpoint = {
"v": 1,
"ts": "2024-07-31T20:14:19.804150+00:00",
"id": "1ef4f797-8335-6428-8001-8a1503f9b875",
"channel_values": {
"my_key": "meow",
"node": "node"
},
"channel_versions": {
"__start__": 2,
"my_key": 3,
"start:node": 3,
"node": 3
},
"versions_seen": {
"__input__": {},
"__start__": {
"__start__": 1
},
"node": {
"start:node": 2
}
},
"pending_sends": [],
}

# store checkpoint
checkpointer.put(write_config, checkpoint, {}, {})

# load checkpoint
checkpointer.get(read_config)

# list checkpoints
list(checkpointer.list(read_config))
```

### Async

```python
from langgraph.checkpoint.pymongo import AsyncMongoDBSaver

async with AsyncMongoDBSaver.from_conn_string(MONGODB_URI) as checkpointer:
checkpoint = {
"v": 1,
"ts": "2024-07-31T20:14:19.804150+00:00",
"id": "1ef4f797-8335-6428-8001-8a1503f9b875",
"channel_values": {
"my_key": "meow",
"node": "node"
},
"channel_versions": {
"__start__": 2,
"my_key": 3,
"start:node": 3,
"node": 3
},
"versions_seen": {
"__input__": {},
"__start__": {
"__start__": 1
},
"node": {
"start:node": 2
}
},
"pending_sends": [],
}

# store checkpoint
await checkpointer.aput(write_config, checkpoint, {}, {})

# load checkpoint
await checkpointer.aget(read_config)

# list checkpoints
[c async for c in checkpointer.alist(read_config)]
```
Loading

0 comments on commit 21c0680

Please sign in to comment.