@@ -9,20 +9,21 @@ def test_same_value_caching(tmp_path):
9
9
10
10
# Test with same value multiple times
11
11
for _ in range (3 ):
12
+
12
13
def prompt_func ():
13
14
return f"Say '1'. Do not explain."
14
-
15
+
15
16
prompter = Prompter (
16
17
prompt_func = prompt_func ,
17
18
model_name = "gpt-4o-mini" ,
18
19
)
19
20
result = prompter (working_dir = str (tmp_path ))
20
21
values .append (result .to_pandas ().iloc [0 ]["response" ])
21
-
22
+
22
23
# Count cache directories, excluding metadata.db
23
24
cache_dirs = [d for d in tmp_path .glob ("*" ) if d .name != "metadata.db" ]
24
25
assert len (cache_dirs ) == 1 , f"Expected 1 cache directory but found { len (cache_dirs )} "
25
- assert values == ['1' , '1' , '1' ], "Same value should produce same results"
26
+ assert values == ["1" , "1" , "1" ], "Same value should produce same results"
26
27
27
28
28
29
def test_different_values_caching (tmp_path ):
@@ -31,9 +32,10 @@ def test_different_values_caching(tmp_path):
31
32
32
33
# Test with different values
33
34
for x in [1 , 2 , 3 ]:
35
+
34
36
def prompt_func ():
35
37
return f"Say '{ x } '. Do not explain."
36
-
38
+
37
39
prompter = Prompter (
38
40
prompt_func = prompt_func ,
39
41
model_name = "gpt-4o-mini" ,
@@ -44,7 +46,8 @@ def prompt_func():
44
46
# Count cache directories, excluding metadata.db
45
47
cache_dirs = [d for d in tmp_path .glob ("*" ) if d .name != "metadata.db" ]
46
48
assert len (cache_dirs ) == 3 , f"Expected 3 cache directories but found { len (cache_dirs )} "
47
- assert values == ['1' , '2' , '3' ], "Different values should produce different results"
49
+ assert values == ["1" , "2" , "3" ], "Different values should produce different results"
50
+
48
51
49
52
def test_same_dataset_caching (tmp_path ):
50
53
"""Test that using the same dataset multiple times uses cache."""
@@ -53,7 +56,7 @@ def test_same_dataset_caching(tmp_path):
53
56
prompt_func = lambda x : x ["instruction" ],
54
57
model_name = "gpt-4o-mini" ,
55
58
)
56
-
59
+
57
60
result = prompter (dataset = dataset , working_dir = str (tmp_path ))
58
61
assert result .to_pandas ().iloc [0 ]["response" ] == "1"
59
62
@@ -62,7 +65,7 @@ def test_same_dataset_caching(tmp_path):
62
65
63
66
# Count cache directories, excluding metadata.db
64
67
cache_dirs = [d for d in tmp_path .glob ("*" ) if d .name != "metadata.db" ]
65
- assert len (cache_dirs ) == 1 , f"Expected 1 cache directory but found { len (cache_dirs )} "
68
+ assert len (cache_dirs ) == 1 , f"Expected 1 cache directory but found { len (cache_dirs )} "
66
69
67
70
68
71
def test_different_dataset_caching (tmp_path ):
@@ -82,4 +85,31 @@ def test_different_dataset_caching(tmp_path):
82
85
83
86
# Count cache directories, excluding metadata.db
84
87
cache_dirs = [d for d in tmp_path .glob ("*" ) if d .name != "metadata.db" ]
85
- assert len (cache_dirs ) == 2 , f"Expected 2 cache directory but found { len (cache_dirs )} "
88
+ assert len (cache_dirs ) == 2 , f"Expected 2 cache directory but found { len (cache_dirs )} "
89
+
90
+
91
+ def test_nested_call_caching (tmp_path ):
92
+ """Test that changing a nested upstream function invalidates the cache."""
93
+
94
+ def value_generator ():
95
+ return 1
96
+
97
+ def prompt_func ():
98
+ return f"Say '{ value_generator ()} '. Do not explain."
99
+
100
+ prompter = Prompter (
101
+ prompt_func = prompt_func ,
102
+ model_name = "gpt-4o-mini" ,
103
+ )
104
+ result = prompter (working_dir = str (tmp_path ))
105
+ assert result .to_pandas ().iloc [0 ]["response" ] == "1"
106
+
107
+ def value_generator ():
108
+ return 2
109
+
110
+ result = prompter (working_dir = str (tmp_path ))
111
+ assert result .to_pandas ().iloc [0 ]["response" ] == "2"
112
+
113
+ # Count cache directories, excluding metadata.db
114
+ cache_dirs = [d for d in tmp_path .glob ("*" ) if d .name != "metadata.db" ]
115
+ assert len (cache_dirs ) == 2 , f"Expected 2 cache directory but found { len (cache_dirs )} "
0 commit comments