Skip to content

Commit

Permalink
add nested calling test
Browse files Browse the repository at this point in the history
  • Loading branch information
vutrung96 committed Nov 21, 2024
1 parent 82ced1a commit 94111a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/bespokelabs/curator/prompter/prompt_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pydantic import BaseModel

from bespokelabs.curator.request_processor.generic_request import GenericRequest

T = TypeVar("T")


Expand Down
46 changes: 38 additions & 8 deletions tests/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ def test_same_value_caching(tmp_path):

# Test with same value multiple times
for _ in range(3):

def prompt_func():
return f"Say '1'. Do not explain."

prompter = Prompter(
prompt_func=prompt_func,
model_name="gpt-4o-mini",
)
result = prompter(working_dir=str(tmp_path))
values.append(result.to_pandas().iloc[0]["response"])

# Count cache directories, excluding metadata.db
cache_dirs = [d for d in tmp_path.glob("*") if d.name != "metadata.db"]
assert len(cache_dirs) == 1, f"Expected 1 cache directory but found {len(cache_dirs)}"
assert values == ['1', '1', '1'], "Same value should produce same results"
assert values == ["1", "1", "1"], "Same value should produce same results"


def test_different_values_caching(tmp_path):
Expand All @@ -31,9 +32,10 @@ def test_different_values_caching(tmp_path):

# Test with different values
for x in [1, 2, 3]:

def prompt_func():
return f"Say '{x}'. Do not explain."

prompter = Prompter(
prompt_func=prompt_func,
model_name="gpt-4o-mini",
Expand All @@ -44,7 +46,8 @@ def prompt_func():
# Count cache directories, excluding metadata.db
cache_dirs = [d for d in tmp_path.glob("*") if d.name != "metadata.db"]
assert len(cache_dirs) == 3, f"Expected 3 cache directories but found {len(cache_dirs)}"
assert values == ['1', '2', '3'], "Different values should produce different results"
assert values == ["1", "2", "3"], "Different values should produce different results"


def test_same_dataset_caching(tmp_path):
"""Test that using the same dataset multiple times uses cache."""
Expand All @@ -53,7 +56,7 @@ def test_same_dataset_caching(tmp_path):
prompt_func=lambda x: x["instruction"],
model_name="gpt-4o-mini",
)

result = prompter(dataset=dataset, working_dir=str(tmp_path))
assert result.to_pandas().iloc[0]["response"] == "1"

Expand All @@ -62,7 +65,7 @@ def test_same_dataset_caching(tmp_path):

# Count cache directories, excluding metadata.db
cache_dirs = [d for d in tmp_path.glob("*") if d.name != "metadata.db"]
assert len(cache_dirs) == 1, f"Expected 1 cache directory but found {len(cache_dirs)}"
assert len(cache_dirs) == 1, f"Expected 1 cache directory but found {len(cache_dirs)}"


def test_different_dataset_caching(tmp_path):
Expand All @@ -82,4 +85,31 @@ def test_different_dataset_caching(tmp_path):

# Count cache directories, excluding metadata.db
cache_dirs = [d for d in tmp_path.glob("*") if d.name != "metadata.db"]
assert len(cache_dirs) == 2, f"Expected 2 cache directory but found {len(cache_dirs)}"
assert len(cache_dirs) == 2, f"Expected 2 cache directory but found {len(cache_dirs)}"


def test_nested_call_caching(tmp_path):
"""Test that changing a nested upstream function invalidates the cache."""

def value_generator():
return 1

def prompt_func():
return f"Say '{value_generator()}'. Do not explain."

prompter = Prompter(
prompt_func=prompt_func,
model_name="gpt-4o-mini",
)
result = prompter(working_dir=str(tmp_path))
assert result.to_pandas().iloc[0]["response"] == "1"

def value_generator():
return 2

result = prompter(working_dir=str(tmp_path))
assert result.to_pandas().iloc[0]["response"] == "2"

# Count cache directories, excluding metadata.db
cache_dirs = [d for d in tmp_path.glob("*") if d.name != "metadata.db"]
assert len(cache_dirs) == 2, f"Expected 2 cache directory but found {len(cache_dirs)}"

0 comments on commit 94111a4

Please sign in to comment.