Commit Graph

4 Commits

Author SHA1 Message Date
projectmoon 001da3ca2f Remove boxed dyn requirement on GBNF Limits.
This commit removes the use of dynamic dispatch for GBNF rule limits
by making use of some overly complicated generic trait bounds plus
associated types, along with two new types. Instead of wrapping GBNF
limits in a box dynamic trait object that produces GBNF limits, there
are two types: GbnfLimitedPrimitive and GbnfLimitedComplex.

These types contain the actual (not very complicated) logic to convert
their contained limit-values into the GBNF limits, which then get
converted to actual limits in the GBNF grammar. This removes heap
allocation and dynamic dispatch, as well as making creation of limit
objects more ergonomic.
2024-03-27 11:10:59 +01:00
projectmoon 6fff4ecacd Allow limiting of values on derive(Gbnf) structs.
With the new helper attribute #[gbnf_limit] on a field of a struct
that has #[derive(Gbnf)], it is now possible to constrain the output
of the LLM to a specific set of values for that field. This is
extremely useful, for example, when asking the LLM to put a specific
ID in a field.

This commit also addresses numerous shortcomings of the original
implementation of the derive macro.

Deserialization of certain LLM responses currently broken as of this
commit, due to different field names in the LLM output vs what serde
expects (camelCase vs snake_case issues).
2024-03-05 20:28:00 +01:00
projectmoon 64209738de GBNF: const generics for arrays; lock derive to owned data.
Remove the trait impl macros for arrays, as we can use const generics
for it instead.

Add DeserializeOwned trait bound to the blanket trait impls and
clarify the panic message that the derive macro only works with
structs that have owned data (no borrows).
2024-02-05 21:25:01 +01:00
projectmoon 1e80ae508e Implement derive macro to convert structs to GBNF rules.
This is the initial implementation of a derive macro implementation
that converts structs into GBNF grammars. This simplies the code by
allowing us to get rid of all the hardcoded GBNF strings, prevents
errors from manually editing or copying said strings, and other
benefits.

The main purpose of this implementation is to lay the foundation for
generating hyper-specific GBNF rules that will allow us to limit LLM
output to specific UUIDs. LLM can't generate a weird response for an
exit or entity ID if it is only allowed to generate a specific list of
UUIDs in its response.
2024-02-05 16:12:05 +01:00