Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add node write cache support and implement for Postgres checkpointer #2786

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

mhk197
Copy link

@mhk197 mhk197 commented Dec 16, 2024

Summary:

Added caching support for individual nodes, and implemented in Postgres. Added tests for caching.

Details:

  • Added cache parameter to add_node method in langgraph/graph/state.py. Takes a CachePolicy object.
  • Implemented CachePolicy type in langgraph/types.py with two parameters: required cache_key and optional ttl (in seconds). cache_key is a function that generates a hash based on (1) the current state of the graph during runtime and (2) the config.
  • Implemented task_id based on the cache_key function for nodes that have a defined CachePolicy, in langgraph/pregel/algo.py. cache_key will generate a hash based on the current input and config.
  • Implemented pending writes retrieval for nodes that have a defined CachePolicy in langgraph/pregel/loop.py.
  • Added get_writes(task_id, ttl) method to base checkpointer in checkpoint/langgraph/checkpoint/base/__init__.py.
  • Implemented get_writes() method for Postgres checkpointer in checkpoint-postgres/langgraph/checkpoint/postgres/__init__.py.
  • Added basic tests in langgraph/tests/test_cache.py.

@mhk197 mhk197 changed the title Add node write cache support and implemented for Postgres checkpointer Add node write cache support and implement for Postgres checkpointer Dec 16, 2024
Copy link
Contributor

@nfcampos nfcampos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks really good!

@@ -180,6 +180,69 @@ def list(
self._load_writes(value["pending_writes"]),
)

def get_writes(self, task_id: str, ttl: int = None) -> List[Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should accept a list of task ids ideally, as when we do these queries we might have multiple tasks to get cached writes for (and will be more efficient to run a single query for all)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

proc = processes[packet.node]

if proc.cache_policy:
cache_key = proc.cache_policy.cache_key(packet.arg, config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit: might be nice to let users also write cache key functions that accept only the first arg (in which case here we'd conditionally call with 1 or 2 args depending on signature

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

if self.exists_cached_node and self.checkpointer:
for tid, task in self.tasks.items():
if task.cache_policy:
cached_writes = self.checkpointer.get_writes(tid, task.cache_policy.ttl)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will actually need to be slightly more complicated (possibly out of scope for this pr) in reality as when using AsyncPregelLoop this call cannot block

"stop_condition": input["stop_condition"] + 1,
"dependent_field_1": (input["dependent_field_1"] + 1) % 2,
"dependent_field_2": (input["dependent_field_2"] + 1) % 4,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit formatting

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Matt Katz added 2 commits December 18, 2024 15:31
… used as filter in postgres query. formatting. also get_writes now finds writes for list of task_ids.
@avinoam134
Copy link

Hey @mhk197 would this new feature mean that I can decide which intermidiete super-steps would be persisted and which would be cached?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants