Live data from Hacker News

Show HN: LLMs can generate valid JSON 100% of the time

github.com

291–300 of 315 posts

Re: Show HN: LLMs can generate valid JSON 100% of the time

#291
post #290

Earlier quoted context omitted.

Given how this works, I don’t think that is possible unless OpenAI implements it themselves.

really? the docs seem to promise something like that "can work with any model"

Yes, any model that you can run on your computer. It changes the way that the tokens are sampled from the LLM, and OpenAI does not give you deep enough access into the pipeline to affect that.

Re: Show HN: LLMs can generate valid JSON 100% of the time

#292

I'm not sure how this is different than: https://github.com/1rgs/jsonformer or https://github.com/newhouseb/clownfish or https://github.com/mkuchnik/relm or https://github.com/ggerganov/llama.cpp/pull/1773 or https://github.com/Shopify/torch-grammar Overall there are a ton of these logit based guidance systems, the reason they don't get tons of traction is the SOTA models are behind REST APIs that don't enable this f…

Thanks for bringing clownfish and relm to my attention! afaik other libraries loop over the entire vocabulary at every step of the generation. We on the other hand build an index at initialization by looping once over the vocabulary. Then generation is just as fast as standard generation.

Hi, author of ReLM here. We use automata as well, like you describe, if I understand correctly.

Re: Show HN: LLMs can generate valid JSON 100% of the time

#293
post #283

Earlier quoted context omitted.

I wasn't talking about deterministic Vs nondeterministic. > If you are willing to spend the effort to write a grammar, you can do that without an LLM. How are you taking, for example, a request to make a "fun but not over the top character from the middle ages, with relevant weapons and a backstory. Game theme is a world populated by anthropomorphic vegetables." And get back a character for the game in a specific JSO…

As far as I can tell you won't be able to use the approach proposed here to create a character matching your above description unless every element of it is encoded in the guiding grammar (including the possibility for the character to have middle ages-relevant weapons, and the anthropomorphic vegetables). At which point, again I have to ask: what do you need the LLM for? You've already done all the hard work by hand…

> As far as I can tell you won't be able to use the approach proposed here to create a character matching your above description unless every element of it is encoded in the guiding grammar (including the possibility for the character to have middle ages-relevant weapons, and the anthropomorphic vegetables).

You wouldn't need to, that's the point here. You let the LLM work on generating semantically valid responses and use a tool like this to restrict it to syntactically correct ones.

Here's an example jsonschema (a bit handwritten so maybe some errors but it should be clear enough). Let the LLM deal with coming up with a name and backstory that work, making sure the description and type of the weapon make sense (gpt4 suggested a close range carrot dagger for example), and let this work as your type structure.

    {
      "type": "object",
      "title": "character",
      "properties": {
        "backstory": {
          "type": "string"
        },
        "weapons": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "weapon_type": {
                "type": "string",
                "enum": ["ranged", "close", "magic"]
              },
              "range": {
                "minimum": 0,
                "maximum": 150
              },
              "damage": {
                "type": "number"
              }
            },
            "required": [
              "name",
              "description",
              "range",
              "damage"
            ]
          }
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "backstory",
        "weapons",
        "name"
      ]
    }

> Then what? What do you mean by "random string"?

Nonsense. Like "Colorless green ideas sleep furiously" the famous sentence that's grammatically correct but utter nonsense.

> Plus, if you already have the grammar that can cover the anthropomorphic vegetable world it's only a bit more work to use it to parse such natural language requests, anyway.

I really do not think this is the case. Parsing and understanding arbitrary requests about something like this?

Re: Show HN: LLMs can generate valid JSON 100% of the time

#294
I've spent two days trying to make this work with anything other than gpt2 and I just can't get it to work.

GPT2 doesn't seem to take instruction well. I've tried llama gpt-medium etc etc.

They all either pick up a different language, or freeze.

EDIT: I see tons of activity and work in the github issues, so ignore this for now.

Super excited when I'll be able to have this working for myself!

Re: Show HN: LLMs can generate valid JSON 100% of the time

#295
post #96
post #8

I can make GPT4 return valid JSON simply by providing examples in the system message. This works nine times out of ten. But it's still probabilistic, and nine times out of ten isn't good enough. Occasionally it will hallucinate responses like this: {"key1": "value1", "key2": "value2" for i in range(n)} Re-prompting with the parsing error message is usually enough to get it on the second try. But escaping double-quote…

That re-prompting on error trick is what this new Microsoft library does, too: https://github.com/microsoft/TypeChat Here's their prompt for that: https://github.com/microsoft/TypeChat/blob/c45460f4030938da3... I think the approach using grammars (seen here, but also in things like https://github.com/ggerganov/llama.cpp/pull/1773 ) is a much more elegant solution.

A "repair prompt" instead of rewinding and starting back from the error seems like the wrong choice, and might only make sense with how payment for OpenAI API usage currently works.

Re: Show HN: LLMs can generate valid JSON 100% of the time

#296
post #283

Earlier quoted context omitted.

I wasn't talking about deterministic Vs nondeterministic. > If you are willing to spend the effort to write a grammar, you can do that without an LLM. How are you taking, for example, a request to make a "fun but not over the top character from the middle ages, with relevant weapons and a backstory. Game theme is a world populated by anthropomorphic vegetables." And get back a character for the game in a specific JSO…

As far as I can tell you won't be able to use the approach proposed here to create a character matching your above description unless every element of it is encoded in the guiding grammar (including the possibility for the character to have middle ages-relevant weapons, and the anthropomorphic vegetables). At which point, again I have to ask: what do you need the LLM for? You've already done all the hard work by hand…

> I think people forget that grammars were the staple for parsing natural language and stuffing it into structured form for a very long time before LLMs, and they still mostly are.

This is a rewritten history of natural language processing tech. Years of fine-tuned theory-heavy grammar coding for parsing and generating human language got the field basically nowhere.

Re: Show HN: LLMs can generate valid JSON 100% of the time

#297
post #293

Earlier quoted context omitted.

As far as I can tell you won't be able to use the approach proposed here to create a character matching your above description unless every element of it is encoded in the guiding grammar (including the possibility for the character to have middle ages-relevant weapons, and the anthropomorphic vegetables). At which point, again I have to ask: what do you need the LLM for? You've already done all the hard work by hand…

> As far as I can tell you won't be able to use the approach proposed here to create a character matching your above description unless every element of it is encoded in the guiding grammar (including the possibility for the character to have middle ages-relevant weapons, and the anthropomorphic vegetables). You wouldn't need to, that's the point here. You let the LLM work on generating semantically valid responses a…

>> Here's an example jsonschema (a bit handwritten so maybe some errors but it should be clear enough).

That'd be nice, but it's not how this tool works. If you look at the repo, there's an example of following a json schema or pydantic model. It's clear that if you wanted a "carrot dagger" in your json, you'd need to define it beforehand:

  class Weapon(str, Enum):
      sword = "sword"
      axe = "axe"
      mace = "mace"
      spear = "spear"
      bow = "bow"
      crossbow = "crossbow"
But perhaps I'm underestimating the tool's capabilities. If so, hopefully remilouf can correct me (and give an example of how the tool can be made to work as you want it).

>> I really do not think this is the case. Parsing and understanding arbitrary requests about something like this?

Not arbitrary. See my casting-to-type analogy. The point I'm trying really hard to get across is that generating free-form text is all nice and cool, but if you want to give it structure, you need to have the entire structure defined before-hand, otherwise the text that can't be made to conform to it simply won't.

So if you haven't got anthropomorphic vegetables in your json schema, your LLM may generate them, they'll never end up in your json.

Re: Show HN: LLMs can generate valid JSON 100% of the time

#298
post #293

Earlier quoted context omitted.

> As far as I can tell you won't be able to use the approach proposed here to create a character matching your above description unless every element of it is encoded in the guiding grammar (including the possibility for the character to have middle ages-relevant weapons, and the anthropomorphic vegetables). You wouldn't need to, that's the point here. You let the LLM work on generating semantically valid responses a…

>> Here's an example jsonschema (a bit handwritten so maybe some errors but it should be clear enough). That'd be nice, but it's not how this tool works. If you look at the repo, there's an example of following a json schema or pydantic model. It's clear that if you wanted a "carrot dagger" in your json, you'd need to define it beforehand: class Weapon(str, Enum): sword = "sword" axe = "axe" mace = "mace" spear = "sp…

You can definitely let the model improvise by defining `weapon` as `Union[Weapon, str]` if that's what you're asking.

Re: Show HN: LLMs can generate valid JSON 100% of the time

#299
Generating an FSM over the vocabulary is a really interesting approach to guided sampling! I'm hacking on a structured inference library (https://github.com/gsuuon/ad-llama) - I also tried to add a vocab preprocessing step to generate a valid tokens mask (just with regex or static strings initially) but discovered that doing so would cause unlikely / unnatural tokens to be masked rather than the token which represents the natural encoding given the existing sampled tokens.

Given the stateful nature of tokenizers, I decided that trying to preprocess the individual token ids was a losing battle. Even in the simple case of whitespace - tokenizer merges can really screw up generating a static mask, e.g. we expect a space next, but a token decodes to 'foo', but is actually a '_foo' and would've decoded with a whitespace if it were following a valid pair. When I go to construct the static vocab mask, it would then end up matching against 'foo' instead of ' foo'.

How did you work around this for the FSM approach? Does it somehow include information about merges / whitespace / tokenizer statefulness?

Re: Show HN: LLMs can generate valid JSON 100% of the time

#300
post #194

Mechanistically, I think this library takes the simple idea of masking part of the vocabulary space and steps in time efficiently. Great! I am curious, however, for the ones who have played around with such libraries wrapping base LLMs with output structure: do base models like Llama2 work very well? My experience says "hell no!" and you do need a fair bit of instruction-tuning for specific use cases to actually get…

I'm quite impressed with Llama 2 13B - the more time I spend with it the more I think it might be genuinely useful for more than just playing around with local LLMs. I'm using the MLC version (since that works with a GPU on my M2 Mac) via my https://github.com/simonw/llm-mlc plugin.

Even the 7B model is shockingly good! I've been hacking on a project also built on MLC (but the web runtime) and the completions I'm seeing from Llama 2 7B, just running on my laptop's browser, have been really impressive. There's a demo page here: https://ad-llama.vercel.app/
Post reply on HN