lmql
lmql copied to clipboard
Minimal Syntax
In addition to LMQL's current style of clause-based syntax, we want to add a new "minimal" syntax mode, that allows to express programs in a more concise way. More specifically, we want to provide simplified syntax with reasonable defaults, that reads more like standard Python.
Example
To illustrate consider the following snippet:
"A list of things not to forget when going to the sea (not travelling): \n"
things = []
for i in range(4):
"-[THING]" where STOPS_AT(THING, "\n")
things.append(THING)
"My favourite is[FAVORITE]" where FAVORITE in things
From this, we derive the following semantics:
-
As shown, prompts can start directly on the top-level of query. If this is the case, we assume
argmaxas the standard decoding method for the top-most prompt. -
The model used to decode such prompts may either be defined by the surrounding context, e.g. by annotating the function with
@lmql.query(model=<model>)or by setting a globally-applicable LMQL default model, e.g.lmql.set_default_model(<model>). In general, this allows users to omit thefromclause, and, if needed, to switch to a different model across several queries, more easily. -
whereconditions can be specified inline, avoiding the need for a globalwhereclause. At the same time, this enables constraints likeFAVORITE in things, that refer to program variables likethings, currently not possible.
Overall, this simplified syntax aligns LMQL fully with Python, where the only extra construct needed beyond top-level strings is the where keyword.
Other applications
Sub-Queries
In place of template variables like [THING], we should allow sub-queries. For instance in the above, instead of [THING], users can specify:
"[sample(temperature=0.7) "-[THING]" where STOPS_AT(THING, "\n")]"
In this case, THING values are decoded using different decoding parameters, however the prompt should be inherited from the surrounding context.
One Line Syntax
With this simplified syntax mode, we can also express small programs more concisely. For instance the following would be a valid LMQL program:
"The country code of {country} is[CODE]"
In such one-line programs, it would also make sense to define the last template variable (e.g. CODE here), as the return value of the function. This is comparable to how lambda expressions work in Python.
0.0.6.5 added inline constraints, one line syntax (lmql.F) and the simplified syntax.