ExtensityAI / symbolicai

Compositional Differentiable Programming Library
986 stars 58 forks source link

Add context manager to disable primitives for symbols #63

Closed markhofm closed 5 months ago

markhofm commented 5 months ago

It would be very helpful to be able to be able to quickly disable all, or a subset, of primitives with a context manager. This could look something like this:

with symai.disable_all_primitives():
    # ops on symbols with disabled mixins
futurisold commented 5 months ago

Added in a8b5964f809cf88dd3be49d6587261b0f4c01fdd

Usage:

from symai import Symbol, Expression, PrimitiveDisabler

def debug(sym):
    print('---')
    try:
        print(sym.query('Is this a test?'))
        print(sym.contains('test'))
    except Exception as e:
        print(f"Error: {e}")
    print('---', '\n')

if __name__ == "__main__":
    sym1 = Symbol("This is a test")
    sym2 = Symbol("This is another test")
    sym3 = Expression("This is a test")

    print("Mixin status before context manager:")
    debug(sym1)
    with PrimitiveDisabler():
        print("Mixin status inside context manager:")
        debug(sym1)
        debug(sym2)
        debug(sym3)

    print("Mixin status after context manager:")
    debug(sym1)

Inside the context manager, the primitives are None, so you can still call [symbol].[primitive] but it will return None.