logoalt Hacker News

creativeSlumber • yesterday at 2:53 PM • 4 replies • view on HN

Can the same mechanism be used to make sure it outputs syntactically correct code?


Replies

HanClinto • yesterday at 4:21 PM

Yes. All you need is a formal grammar that defines the syntax of the text being generated. The llama.cpp repo has a decent writeup on the way that grammar-constrained decoding works in general [0], and it includes a bit of a simplistic example for generating syntactically-correct C code [1]. No reason this couldn't be extended to other things -- I personally have run experiments with generating syntactically-correct SQL statements when doing text-to-sql [2], and I think it's pretty cool.

[0] - https://github.com/ggml-org/llama.cpp/tree/master/grammars [1] - https://github.com/ggml-org/llama.cpp/blob/master/grammars/c... [2] - https://gist.github.com/HanClinto/b853571136fd42f8e2bde4c99c...

mmastrac • yesterday at 2:55 PM

Yes, absolutely. The problem is integrating it into the inference engine. Not impossible, just _very hard_.

➕ show 2 replies
wolttam • yesterday at 3:09 PM

If the model is predicting syntactically incorrect tokens in code you probably have bigger issues

vonneumannstan • yesterday at 3:05 PM

Is this even a real issue? Are you consistently getting code out of frontier models + harnesses(Codex, Claude Code, etc) that have syntax errors?