-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Justin Ramsey
committed
Dec 13, 2024
0 parents
commit f1e3cf1
Showing
8 changed files
with
576 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
**/build | ||
**/.venv | ||
**/dist | ||
**/langgraph_checkpoint_dynamodb.egg-info | ||
**/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 researchwiseai | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# langgraph-checkpoint-dynamodb | ||
|
||
Implementation of a LangGraph CheckpointSaver that uses a AWS's DynamoDB | ||
|
||
## Inspiration | ||
|
||
Based on: https://github.com/researchwiseai/langgraphjs-checkpoint-dynamodb | ||
|
||
## Required DynamoDB Tables | ||
|
||
To be able to use this checkpointer, two DynamoDB table's are needed, one to store | ||
checkpoints and the other to store writes. Below are some examples of how you | ||
can create the required tables. | ||
|
||
### Terraform | ||
|
||
```hcl | ||
# Variables for table names | ||
variable "checkpoints_table_name" { | ||
type = string | ||
} | ||
variable "writes_table_name" { | ||
type = string | ||
} | ||
# Checkpoints Table | ||
resource "aws_dynamodb_table" "checkpoints_table" { | ||
name = var.checkpoints_table_name | ||
billing_mode = "PAY_PER_REQUEST" | ||
hash_key = "thread_id" | ||
range_key = "checkpoint_id" | ||
attribute { | ||
name = "thread_id" | ||
type = "S" | ||
} | ||
attribute { | ||
name = "checkpoint_id" | ||
type = "S" | ||
} | ||
} | ||
# Writes Table | ||
resource "aws_dynamodb_table" "writes_table" { | ||
name = var.writes_table_name | ||
billing_mode = "PAY_PER_REQUEST" | ||
hash_key = "thread_id_checkpoint_id_checkpoint_ns" | ||
range_key = "task_id_idx" | ||
attribute { | ||
name = "thread_id_checkpoint_id_checkpoint_ns" | ||
type = "S" | ||
} | ||
attribute { | ||
name = "task_id_idx" | ||
type = "S" | ||
} | ||
} | ||
``` | ||
|
||
### AWS CDK | ||
|
||
```python | ||
from aws_cdk import ( | ||
Stack, | ||
aws_dynamodb as dynamodb, | ||
) | ||
from constructs import Construct | ||
|
||
class DynamoDbStack(Stack): | ||
def __init__(self, scope: Construct, id: str, **kwargs): | ||
super().__init__(scope, id, **kwargs) | ||
|
||
checkpoints_table_name = 'YourCheckpointsTableName' | ||
writes_table_name = 'YourWritesTableName' | ||
|
||
# Checkpoints Table | ||
dynamodb.Table( | ||
self, | ||
'CheckpointsTable', | ||
table_name=checkpoints_table_name, | ||
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST, | ||
partition_key=dynamodb.Attribute( | ||
name='thread_id', | ||
type=dynamodb.AttributeType.STRING, | ||
), | ||
sort_key=dynamodb.Attribute( | ||
name='checkpoint_id', | ||
type=dynamodb.AttributeType.STRING, | ||
), | ||
) | ||
|
||
# Writes Table | ||
dynamodb.Table( | ||
self, | ||
'WritesTable', | ||
table_name=writes_table_name, | ||
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST, | ||
partition_key=dynamodb.Attribute( | ||
name='thread_id_checkpoint_id_checkpoint_ns', | ||
type=dynamodb.AttributeType.STRING, | ||
), | ||
sort_key=dynamodb.Attribute( | ||
name='task_id_idx', | ||
type=dynamodb.AttributeType.STRING, | ||
), | ||
) | ||
``` | ||
|
||
## Using the Checkpoint Saver | ||
|
||
### Default | ||
|
||
To use the DynamoDB checkpoint saver, you only need to specify the names of | ||
the checkpoints and writes tables. In this scenario the DynamoDB client will | ||
be instantiated with the default configuration, great for running on AWS Lambda. | ||
|
||
```python | ||
from langgraph_checkpoint_dynamodb import DynamoDBSaver | ||
... | ||
checkpoints_table_name = 'YourCheckpointsTableName' | ||
writes_table_name = 'YourWritesTableName' | ||
|
||
memory = DynamoDBSaver( | ||
checkpoints_table_name=checkpoints_table_name, | ||
writes_table_name=writes_table_name, | ||
) | ||
|
||
graph = workflow.compile(checkpointer=memory) | ||
``` | ||
|
||
### Providing Client Configuration | ||
|
||
If you need to provide custom configuration to the DynamoDB client, you can | ||
pass in an object with the configuration options. Below is an example of how | ||
you can provide custom configuration. | ||
|
||
```python | ||
memory = DynamoDBSaver( | ||
checkpoints_table_name=checkpoints_table_name, | ||
writes_table_name=writes_table_name, | ||
client_config={ | ||
'region': 'us-west-2', | ||
'accessKeyId': 'your-access-key-id', | ||
'secretAccessKey': 'your-secret-access-key', | ||
} | ||
) | ||
``` |
Empty file.
Oops, something went wrong.