google / langfun

OO for LLMs
Apache License 2.0
475 stars 34 forks source link

Introduce `__validate__`: User feedback for `lf.query` on autofix. #279

Closed copybara-service[bot] closed 1 month ago

copybara-service[bot] commented 1 month ago

Introduce __validate__: User feedback for lf.query on autofix.

Example:

 class Foo(pg.Object):
      x: int

      def __validate__(self):
        # This triggers autofix.
        if self.x > 1:
          raise ValueError('value should be less or equal than 1.')

        # This triggers rule-based fix (user modification)
        if self.x < 0:
          self.rebind(x=0, skip_notification=True)

   foo = lf.query('Generate a Foo object', Foo, lm=lm, autofix=2)
   assert foo.x in (0, 1)