Skip to content

Commit

Permalink
Merge pull request guidance-ai#677 from VincentToups/main
Browse files Browse the repository at this point in the history
Removed side effect on the stop argument from _gen
  • Loading branch information
slundberg authored Mar 14, 2024
2 parents 0bb6551 + eea14f5 commit 793821b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion guidance/library/_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def gen(lm, name=None, *, max_tokens=1000, list_append=False, regex=None,
if isinstance(stop, str):
stop = [stop]
if regex is None:
stop.append(select([eos_token(), active_role_end()]))
stop = stop + [select([eos_token(), active_role_end()])]

if stop_regex is None:
stop_regex = []
Expand Down
16 changes: 15 additions & 1 deletion tests/library/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ def test_stop_char():
lm += "Count to 10: 1, 2, 3, 4, 5, 6, 7, " + gen('text', stop=",")
assert lm["text"] == "8"

def test_stop_list_side_effect():
'''Tests a bug where a stop list has an item appended to it in place instead of being updated non-destructively. The bug only occurs whe regex is also None'''
stop_list = ['\nStep', '\n\n', '\nAnswer'];
stop_list_length = len(stop_list);
lm = get_model("transformers:gpt2")
lm + '''Question: Josh decides to try flipping a house.  He buys a house for $80,000 and then puts in $50,000 in repairs.  This increased the value of the house by 150%.  How much profit did he make?
Let's think step by step, and then write the answer:
Step 1''' + gen('steps', list_append=True, stop=['\nStep', '\n\n', '\nAnswer'], temperature=0.7, max_tokens=20) + '\n'
assert stop_list_length == len(stop_list)
i = 2
lm + f'Step {i}:' + gen('steps', list_append=True, stop=['\nStep', '\n\n', '\nAnswer'], temperature=0.7, max_tokens=20) + '\n'
assert stop_list_length == len(stop_list)


def test_save_stop():
lm = models.Mock(b"<s>Count to 10: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10")
lm += "Count to 10: 1, 2, 3, 4, 5, 6, 7, " + gen('text', stop=",", save_stop_text='stop_text')
Expand Down Expand Up @@ -271,4 +285,4 @@ def calculator(lm):
calculator_tool = Tool(calculator_call(), calculator)
gpt2 = selected_model
lm = gpt2 + 'Here are five expressions:\nCalculator(3 * 3) = 33\nCalculator(2 + 1 * 3) = 5\n'
lm += gen(max_tokens=30, temperature=0.5, tools=[calculator_tool], stop='\n\n')
lm += gen(max_tokens=30, temperature=0.5, tools=[calculator_tool], stop='\n\n')

0 comments on commit 793821b

Please sign in to comment.