lmql icon indicating copy to clipboard operation
lmql copied to clipboard

Compiler: Subscript in f-string is interpreted as template variable

Open lbeurerkellner opened this issue 2 years ago • 1 comments

import lmql

dictionary = {'one': "it's a test no 1", 'two': "it's a test no 2", 'three': "it's a test no 3"}

@lmql.query
def run():
    '''lmql
    sample(temperature=0.3)
        inputs = []
        print(dictionary.keys())
        for name in dictionary.keys():
            print("before", name)
            '    # {dictionary[name]}\n' # after putting this line in, 'name' changes to None
            print("after", name)
            '    "{name}": "[INPUT]",\n'
            inputs.append(INPUT)
    from 
        lmql.model(f"local:llama.cpp:models/llama-2-13b-chat.ggmlv3.q3_K_L.bin", n_ctx=2048)
    where
        STOPS_BEFORE(INPUT, '"')
    '''
    
print('\n', run()[0].prompt)

Here, the compiler mistakenly interprets [name] in the f-string, as template variable, leading to a name space collision with name. As a consequence, the following code is generated, which will override the original value of name:

name = (yield lmql.runtime_support.context_call('get_var', 'name'))

lbeurerkellner avatar Jul 31 '23 09:07 lbeurerkellner

The current workaround is to factor out dictionary[name] into a separate variable, i.e. write the following instead:

value = dictionary[name]
'    # {value}\n' # after putting this line in, 'name' changes to None

lbeurerkellner avatar Jul 31 '23 09:07 lbeurerkellner