lmql
lmql copied to clipboard
Compiler: Subscript in f-string is interpreted as template variable
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'))
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