forked from AsherBond/dspy
-
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.
Fix broken inspect_history and broken prompt cache (stanfordnlp#1744)
* Fix broken inspect_history and broken prompt cache * Remove errant print statement * Move global inspect history into base_lm * Remove skip parameter * Delete examples/temp.py * Minor adjustment to make adapters go back to original behavior --------- Co-authored-by: Omar Khattab <[email protected]>
- Loading branch information
1 parent
2d3ed8d
commit 9170658
Showing
7 changed files
with
94 additions
and
19 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
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .lm import LM | ||
from .base_lm import BaseLM | ||
from .base_lm import BaseLM, inspect_history |
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
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
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
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,68 @@ | ||
import pytest | ||
from dspy.utils.dummies import DummyLM | ||
from dspy.clients.base_lm import GLOBAL_HISTORY | ||
import dspy | ||
|
||
@pytest.fixture(autouse=True) | ||
def clear_history(): | ||
GLOBAL_HISTORY.clear() | ||
yield | ||
|
||
def test_inspect_history_basic(capsys): | ||
# Configure a DummyLM with some predefined responses | ||
lm = DummyLM([{"response": "Hello"}, {"response": "How are you?"}]) | ||
dspy.settings.configure(lm=lm) | ||
|
||
# Make some calls to generate history | ||
predictor = dspy.Predict("query: str -> response: str") | ||
predictor(query="Hi") | ||
predictor(query="What's up?") | ||
|
||
# Test inspecting all history | ||
history = GLOBAL_HISTORY | ||
print(capsys) | ||
assert len(history) > 0 | ||
assert isinstance(history, list) | ||
assert all(isinstance(entry, dict) for entry in history) | ||
assert all("messages" in entry for entry in history) | ||
|
||
def test_inspect_history_with_n(capsys): | ||
lm = DummyLM([{"response": "One"}, {"response": "Two"}, {"response": "Three"}]) | ||
dspy.settings.configure(lm=lm) | ||
|
||
# Generate some history | ||
predictor = dspy.Predict("query: str -> response: str") | ||
predictor(query="First") | ||
predictor(query="Second") | ||
predictor(query="Third") | ||
|
||
dspy.inspect_history(n=2) | ||
# Test getting last 2 entries | ||
out, err = capsys.readouterr() | ||
assert not "First" in out | ||
assert "Second" in out | ||
assert "Third" in out | ||
|
||
def test_inspect_empty_history(capsys): | ||
# Configure fresh DummyLM | ||
lm = DummyLM([]) | ||
dspy.settings.configure(lm=lm) | ||
|
||
# Test inspecting empty history | ||
dspy.inspect_history() | ||
history = GLOBAL_HISTORY | ||
assert len(history) == 0 | ||
assert isinstance(history, list) | ||
|
||
def test_inspect_history_n_larger_than_history(capsys): | ||
lm = DummyLM([{"response": "First"}, {"response": "Second"}]) | ||
dspy.settings.configure(lm=lm) | ||
|
||
predictor = dspy.Predict("query: str -> response: str") | ||
predictor(query="Query 1") | ||
predictor(query="Query 2") | ||
|
||
# Request more entries than exist | ||
dspy.inspect_history(n=5) | ||
history = GLOBAL_HISTORY | ||
assert len(history) == 2 # Should return all available entries |