guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
18.91k stars 1.04k forks source link

Cannot append to a variable created with model.set #969

Closed traysh closed 2 months ago

traysh commented 2 months ago

The bug If model.set('name', ['1', '2', '3']) is used to create a variable, it can't be appended by later calling gen.

To Reproduce

from guidance import models, gen, user, assistant

gpt = models.OpenAI("gpt-3.5-turbo", echo=False)

lm = gpt.set("list", ['1', '2', '3'])
with user():
    lm += 'write "4": '
with assistant():
    lm += gen(regex=r'\d', name='list', list_append=True)

print(lm['list'])

Output

Traceback (most recent call last):                                                                                                                                                                                                                         
  File ".../test.py", line 9, in <module>                                                                                                                                                                                          
    lm += gen(regex=r'\d', name='list', list_append=True)                                                                                                                                                                                                  
  File ".../venv/lib/python3.12/site-packages/guidance/models/_model.py", line 1155, in __add__                                                                                                                                    
    out = lm._run_stateless(value)                                                                                                                                                                                                                         
          ^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                         
  File ".../venv/lib/python3.12/site-packages/guidance/models/_model.py", line 1408, in _run_stateless                                                                                                                             
    lm._variables_log_probs[k].append(                                                                                                                                                                                                                     
    ~~~~~~~~~~~~~~~~~~~~~~~^^^                                                                                                                                                                                                                             
KeyError: 'list'

System info (please complete the following information):

Harsha-Nori commented 2 months ago

Hi @traysh, good catch -- we aren't creating a new entry in the _variables_log_probs on the set method. Should be an easy fix -- thanks for reporting!

Harsha-Nori commented 2 months ago

Have a candidate fix here: https://github.com/guidance-ai/guidance/pull/974/