guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
18.13k stars 1.01k forks source link

Role blocks fail silently inside of stateless grammars #916

Open hudson-ai opened 1 week ago

hudson-ai commented 1 week ago

The bug Role blocks fail silently inside of stateless grammars -- I think it's okay if they are only supported in stateful contexts, but the user should get an error.

To Reproduce

import guidance
@guidance(stateless=True)
def foo(lm):
    with guidance.user():
        lm += "hello!"
    return lm
model = guidance.models.Mock()
model += foo()
str(model)
>> `hello!`

System info (please complete the following information):

hudson-ai commented 1 week ago

Same with the block context manager. I know that this may be impossible to implement with the current design for these context managers, but it's worth thinking about.

Harsha-Nori commented 1 week ago

wow great catch, had no idea these were failing!

hudson-ai commented 1 week ago

@nking-1 and I were deep in the trenches yesterday 😂

nking-1 commented 1 week ago

@hudson-ai do you remember where the root cause of the bug was? Is it in __add__() in model.py?

hudson-ai commented 1 week ago

It's sort of inherent to the implementation of context blocks. Opening/closing them just adds/removes something from a class-level dict on Model that affects the __add__ method on Model instances.

The problem is that we never hit that __add__ inside of stateless grammars, because no Model instances ever gets passed to the function. They are "compiled" by passing an empty string in the lm slot of the signature.

A workaround may be to send an instance of a "fake" Model subclass through rather than the empty string, but I think it may honestly be worth taking this opportunity to rethink the context managers a bit.

michael-newsrx commented 5 days ago

See also: https://github.com/guidance-ai/guidance/discussions/786#discussion-6587630