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
LMQL now supports nested queries, enabling modularized local instructions and re-use of prompt components.
Q: When was Obama born?wait200beginincontext
dateformat(respond in DD/MM/YYYY)endincontextwait200ANSWER04/08/1961wait200fadeincontextwait200hideincontextwait200 Q: When was Bruno Mars born?wait200beginincontext1
dateformat(respond in DD/MM/YYYY)endincontext1wait200ANSWER08/10/1985wait200fadeincontext1wait200hideincontext1wait200 Q: When was Dua Lipa born?wait200beginincontext2
dateformat(respond in DD/MM/YYYY)endincontext2wait200ANSWER22/08/1995wait200fadeincontext2wait200hideincontext2wait200 Out of these, who was born last?LASTDua Lipa
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 Suite"]
My packing list for the trip: - THING Volleyball - THING Bathing Suite - THING Sunscreen - THING Volleyball