Skip to content

Conversation

@sushmanthreddy
Copy link
Contributor

@sushmanthreddy sushmanthreddy commented Nov 2, 2024

Currently, loading a predictor after optimization requires two separate lines:

predictor = dspy.Predict("question -> answer")
predictor.load(saved_path)

This PR adds support for chainable loading to improve code ergonomics:

predictor = dspy.Predict("question -> answer").load(saved_path, return_self=True)

Changes

  • Modified load_state() and _load_state_legacy() to return self for chaining
  • Added a new return_self parameter to load() method
  • Added comprehensive tests for both traditional and chainable loading patterns
  • Maintained backward compatibility with existing code by defaulting return_self=False

Testing

Added two new test cases:

  • test_chainable_load: Verifies both traditional and chainable loading methods
  • test_load_state_chaining: Ensures state loading supports chaining in both normal and legacy modes

Backward Compatibility

The changes are fully backward compatible:

  • Existing code using load() without return_self continues to work as before
  • New code can opt into chainable loading by setting return_self=True

Closes #1738

@isaacbmiller
Copy link
Collaborator

Nice thanks for writing this!

@isaacbmiller isaacbmiller merged commit 0069195 into stanfordnlp:main Nov 3, 2024
4 checks passed
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.

Add ability to return a predictor instead of modifying in place.

2 participants