@lmql.query
def meaning_of_life():
'''lmql
# top-level strings are prompts
"Q: What is the answer to life, the \
universe and everything?"
# generation via (constrained) variables
"A: [ANSWER]" where \
len(ANSWER) < 120 and STOPS_AT(ANSWER, ".")
# results are directly accessible
print("LLM returned", ANSWER)
# use typed variables for guaranteed
# output format
"The answer is [NUM: int]"
# query programs are just functions
return NUM
'''
# so from Python, you can just do this
meaning_of_life() # 42
Created by the SRI Lab @ ETH Zurich and contributors.
LMQL now supports nested queries, enabling modularized local instructions and re-use of prompt components.
promptdown Execution Trace
Q: When was Obama born? 200 incontext
200ANSWER04/08/1961 200 incontext 200 incontext
200Q: When was Bruno Mars born? 200 incontext1 200ANSWER08/10/1985 200 incontext1 200 incontext1
200Q: When was Dua Lipa born? 200 incontext2 200ANSWER22/08/1995 200 incontext2 200 incontext2
200Out of these, who was born last?LASTDua Lipa
LMQL automatically makes your LLM code portable across several backends. You can switch between them with a single line of code.
Prompt construction and generation is implemented via expressive Python control flow and string interpolation.
# top level strings are prompts
"My packing list for the trip:"
# use loops for repeated prompts
for i in range(4):
# 'where' denotes hard constraints enforced by the runtime
"- [THING] \n" where THING in \
["Volleyball", "Sunscreen", "Bathing Suit"]
My packing list for the trip: - THING Volleyball - THING Bathing Suit - THING Sunscreen - THING Volleyball