News for hackers. Stuff that matters.
Re:Show HN: LLMs can generate valid JSON 100% of the time by panarky on Monday August 14, 2023 @03:20PM (#37125563)
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-quotes and newline characters is less reliable. Even after giving it multiple examples, it correctly escapes only about half the time.

Re-prompting for escaping errors still yields a ~50% success rate.

Re:Show HN: LLMs can generate valid JSON 100% of the time by keiferwiseman on Monday August 14, 2023 @03:31PM (#37125700)
It took some iterations but I've managed to get the OpenAI API to give me valid JSON 100% of the time now(based on my testing). I think I put in the prompt to never use newlines because it was causing issues lol.
Re:Show HN: LLMs can generate valid JSON 100% of the time by padolsey on Monday August 14, 2023 @04:44PM (#37126587)
I've had more luck with getting it to output XML as (1) You can imbue XML with actual language/meaning (which LLMs adore) and (2) parsers can be made to be more forgiving. I get why people want to make JSON, but to me it's a bit like trying to get a cat to swim - you might eventually succeed, but it's not their natural inclination.
Re:Show HN: LLMs can generate valid JSON 100% of the time by gowld on Monday August 14, 2023 @05:30PM (#37127071)
How do you imbue XML with meaning?
Re:Show HN: LLMs can generate valid JSON 100% of the time by padolsey on Monday August 14, 2023 @05:35PM (#37127102)
XML Elements themselves: their naming, their attributes, comments, indentation. There's more opportunity at every level of the hierarchy to demarkate and establish meaning. Having closing-tags as well, I've found, is a massive boon; LLMs can better understand what "finishing" looks like if its delimited in a semantic way - with a name.
Re:Show HN: LLMs can generate valid JSON 100% of the time by BoorishBears on Monday August 14, 2023 @11:24PM (#37129798)
Same works for JSON. Naming JSON keys works for adjusting what the output is nicely, and you can comment in your definitions (by defining them in a JSON Schema, or inserting placeholder text like `"someKeyWithClarifyingDetails": <some detailed instruction>`)

I'm actually partial to CSV these days though, it can really cut down on response times just not needing to return all the extra tokens for JSON/XML delimiters

Re:Show HN: LLMs can generate valid JSON 100% of the time by padolsey on Tuesday August 15, 2023 @03:46AM (#37131298)
Ostenibly yeh JSON should be able to encapsulate mose of that semantic stuff but having replaced an XML schema in the system prompt with gpt's function-calling API I've been very umimpressed. It feels much less capable. I would have to provide a lot more clarifying prompts to make it more capable. I think I will, for now, bias to using schemas that are closest to prose.
Re:Show HN: LLMs can generate valid JSON 100% of the time by DonHopkins on Tuesday August 15, 2023 @05:07AM (#37131731)
Yikes. This makes me think that JSON's stubborn mistake of not allowing comments is yet another "Billion-Dollar Mistake", since it's way too late to just change the standard to allow comments, update all the JSON content on the internet to use comments, and retrain all the LLMs to understand comments.

Great point about CSVs! But using placeholder keys for JSON comments in untenable, and using schema instead of inline comments is clumsy and indirect. Of course JSON schema are quite useful in certain situations, but LLMs would get a lot more meaning out of casual common JSON if it just allowed comments, and it would also greatly benefit humans.

Between JavaScript's and JSON's mistakes, that's at least <DoctorEvilVoice>THREE BILLION DOLLARS!!!</DoctorEvilVoice> ;)

https://en.wikipedia.org/wiki/Tony_Hoare#Research_and_career

>Speaking at a software conference in 2009, Tony Hoare apologized for inventing the null reference:

>"I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years." -Tony Hoare

https://news.ycombinator.com/item?id=19568378

>"My favorite is always the Billion-Dollar Mistake of having null in the language. And since JavaScript has both null and undefined, it's the Two-Billion-Dollar Mistake." -Anders Hejlsberg

>"It is by far the most problematic part of language design. And it's a single value that -- ha ha ha ha -- that if only that wasn't there, imagine all the problems we wouldn't have, right? If type systems were designed that way. And some type systems are, and some type systems are getting there, but boy, trying to retrofit that on top of a type system that has null in the first place is quite an undertaking." -Anders Hejlsberg

Re:Show HN: LLMs can generate valid JSON 100% of the time by BoorishBears on Tuesday August 15, 2023 @05:59AM (#37132028)
I'm not saying use placeholder keys: the actual keys themselves serve as guidance.

Naming a key "nameBasedOnLocationIGaveYou" instead of "name", or "oneSentenceSummary" vs "summary", results in a meaningful difference.

You can even use that for formatted single-response chain of thought, like {"listOfStuff":[...], "whatDoTheyHaveInCommon": "", "whichOneIsMostImportant": ""}

Also remember, the LLM doesn't need valid JSON: I just straight up insert comments in the JSON in a non-compliant way for some of my prompts, GPT-4 and Claude are all smart enough to not hallucinate comments back at you. 3.5 might be pushing it if temp is too high (although even the nerfed API logit bias should fix that now that I think about it)

And sometimes to save tokens I describe a JSON object without using JSON: just structure it in neatly formatted markdown and even 3.5 can follow along

Re:Show HN: LLMs can generate valid JSON 100% of the time by DonHopkins on Tuesday August 15, 2023 @08:18AM (#37133044)
Oh, I see! I misunderstood that you meant using dummy keys to hold comments in their values, which some people have suggested as a work-around for there not being any comments in JSON.
Re:Show HN: LLMs can generate valid JSON 100% of the time by prempv on Tuesday August 15, 2023 @12:55AM (#37130347)
I've had the same experience as well. I suspect if it's due to large presence of HTML in the training data as part of codebases and online content
Re:Show HN: LLMs can generate valid JSON 100% of the time by MuffinFlavored on Monday August 14, 2023 @04:58PM (#37126717)
I wonder if the next iteration of OpenAI features is something like:

right now you can inject prompts that the LLM takes into consideration before the output

I wonder if you can make it have a "post" generation function that says like "keep re-trying in a loop (aka hallucinating with randomness) until the output message passes XYZ format/checks/scoring"

Re:Show HN: LLMs can generate valid JSON 100% of the time by padjo on Monday August 14, 2023 @05:29PM (#37127056)
It’s starting to feel like LLMs are to “classical” software engineering what quantum physics was to classical physics
Re:Show HN: LLMs can generate valid JSON 100% of the time by catlifeonmars on Monday August 14, 2023 @08:55PM (#37128809)
How so? I’m not quite following the analogy.
Re:Show HN: LLMs can generate valid JSON 100% of the time by antonvs on Monday August 14, 2023 @10:00PM (#37129265)
Just guessing what was meant, but quantum physics in some sense tries all possible paths before an outcome is selected.

The problem with that is that without a quantum computer, or without some sort of filtering, that process can take up to infinite time.

Re:Show HN: LLMs can generate valid JSON 100% of the time by padjo on Tuesday August 15, 2023 @06:50AM (#37132328)
Oh it was just a glib way of moaning about non-determinism making its way into software engineering. Much like how physicists had to make peace with the probabilistic nature of quantum physics.
Re:Show HN: LLMs can generate valid JSON 100% of the time by kristjansson on Tuesday August 15, 2023 @01:49AM (#37130648)
Why wait for OpenAI?
Re:Show HN: LLMs can generate valid JSON 100% of the time by thumbsup-_- on Monday August 14, 2023 @05:02PM (#37126751)
Yeah same thing. I have done the same with GPT-3.5. Simply ask it to output using provided schema only and give a few examples. Always outputs in provided json format
Re:Show HN: LLMs can generate valid JSON 100% of the time by orasis on Monday August 14, 2023 @05:15PM (#37126896)
What about using ChatGPT’s new function calling mechanism?
Re:Show HN: LLMs can generate valid JSON 100% of the time by superasn on Monday August 14, 2023 @05:22PM (#37126984)
That returns broken JSON a lot of the times too
Re:Show HN: LLMs can generate valid JSON 100% of the time by phillipcarter on Monday August 14, 2023 @05:21PM (#37126963)
This is what we do, but for GPT-3.5. And it doesn't need to be system messages either. We even have it emitting only JSON in a specific structure (except for when it fails to produce an output altogether). This is without the function calling model.
Re:Show HN: LLMs can generate valid JSON 100% of the time by nextaccountic on Monday August 14, 2023 @05:27PM (#37127035)
What about reprompting with a different temperature value?

If this works, how to select the optimal value? Maybe you can train a model that can excel at the task of querying gpt4 for valid jsons

Re:Show HN: LLMs can generate valid JSON 100% of the time by simonw on Monday August 14, 2023 @05:28PM (#37127042)
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.

Re:Show HN: LLMs can generate valid JSON 100% of the time by creatonez on Tuesday August 15, 2023 @03:57PM (#37138580)
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 by caesil on Monday August 14, 2023 @05:48PM (#37127224)
With ChatGPT function calling I get valid JSON 100% of the time from GPT-4 unless I have made some error in prompting.

The chief error is not providing escape hatches. LLMs look for a right answer. If you are feeding it some texts and asking it to return structured data about the texts, but then one of the texts is blank, it will be difficult to determine a right answer, so you get hallucinations. The solution is an escape hatch where one of the arguments is a `textIsMissing` boolean or something.

As long as you've accounted for these failure modes, it works flawlessly.

Re:Show HN: LLMs can generate valid JSON 100% of the time by reissbaker on Monday August 14, 2023 @07:18PM (#37128092)
GPT-4 is amazing, but the upside of smaller models is much lower cost. I get basically 100% accuracy on JSON modeling with GPT-4 with function calling too, but I will say that gpt-3.5-turbo with function calling is somewhat less accurate — it usually generates valid JSON in terms of JSON.parse not exploding, but not necessarily JSON following the schema I passed in (although it's surprisingly good, maybe ~90% accurate?). I use 3.5-turbo a decent amount in API calls because it's just a lot cheaper, and performs well enough even if it's not gpt-4 level.

I haven't gotten a chance to earnestly use the smaller Llama models yet in more than small prototypes (although I'm building a 4090-based system to learn more about finetuning them), but the little amount of experimenting I've done with them makes me think they need a decent amount of help with generating consistently-valid JSON matching some schema out of the box. This is a pretty neat tool to use for them, since it doesn't require finetuning runs, it just masks logits.

Re:Show HN: LLMs can generate valid JSON 100% of the time by BoorishBears on Monday August 14, 2023 @11:26PM (#37129813)
claude-1.2-instant came out last week and is doing extremely well at following schemas.

I'd say it's reached 3.5 turbo with the format following skills of GPT-4, which is powerful once you give it chain-of-thought

Re:Show HN: LLMs can generate valid JSON 100% of the time by selcuka on Monday August 14, 2023 @09:40PM (#37129136)
The premise of function calling is great, but in my experience (at least on GPT-3.5, haven't tried it with GPT-4 yet) it seems to generate wildly different, and less useful results, for the same prompt.
Re:Show HN: LLMs can generate valid JSON 100% of the time by ipaddr on Tuesday August 15, 2023 @01:49AM (#37130658)
You can change the randomness value to 0 and get the same output each time for the same text
Re:Show HN: LLMs can generate valid JSON 100% of the time by selcuka on Tuesday August 15, 2023 @01:56AM (#37130695)
I should probably re-test it, but I think it wasn't the temperature. The results were unusually useless.
Re:Show HN: LLMs can generate valid JSON 100% of the time by tomduncalf on Tuesday August 15, 2023 @10:16AM (#37134301)
In my experience (with GPT-4 at least), a temperature of 0 does not result in deterministic output. It's more consistent but outputs do still vary for the same input. I feel like temperature is a bit more like "how creative should the model be?"
Re:Show HN: LLMs can generate valid JSON 100% of the time by selcuka on Tuesday August 15, 2023 @07:30PM (#37140730)
One theory is it is caused by its Sparse MoE (Mixture of Experts) architecture [1]:

> The GPT-4 API is hosted with a backend that does batched inference. Although some of the randomness may be explained by other factors, the vast majority of non-determinism in the API is explainable by its Sparse MoE architecture failing to enforce per-sequence determinism.

[1] https://152334h.github.io/blog/non-determinism-in-gpt-4/

Re:Show HN: LLMs can generate valid JSON 100% of the time by caesil on Wednesday August 16, 2023 @04:07PM (#37152963)
GPT-3.5 is pretty much useless for reliable NLP work unless you give it a VERY proscribed task.

That's really the major breakthrough of GPT-4, in my mind, and the reason we are absolutely going to see an explosion of AI-boosted productivity over the next few years, even if foundation LLM advancements stopped cold right now. A vast ocean of mundane white collar work is waiting to be automated.

Re:Show HN: LLMs can generate valid JSON 100% of the time by andreygrehov on Monday August 14, 2023 @10:54PM (#37129631)
Meh... I asked GPT4 to return a sample PHP code inside of a random JSON. It failed the JSON linter from the very first try. I actually couldn't pass the validation despite many retries, eg follow up corrections. Not a single time it generated a 100% valid JSON, I eventually gave up.
Re:Show HN: LLMs can generate valid JSON 100% of the time by ipaddr on Tuesday August 15, 2023 @02:00AM (#37130717)
This worked with chatGPT: create a sample hello world in php

store that code in a json[object

code: { "php_code": "<?php echo 'Hello, World!'; ?>" }

Re:Show HN: LLMs can generate valid JSON 100% of the time by adamrezich on Tuesday August 15, 2023 @04:29AM (#37131505)
if you think that's bad, try to get it to generate Inform 7 games—Inform's natural-English-ish syntax completely throws all LLMs for a loop, consistently. it generates code that looks possibly correct (to an Inform newbie at least), but fails to compile far more often than not. I find this super interesting.
Re:Show HN: LLMs can generate valid JSON 100% of the time by karmasimida on Tuesday August 15, 2023 @02:16AM (#37130792)
I see grammar constrained generation for 2 major advantages:

1. It consumes fewer tokens, no need to add too many examples into the prompt.

2. It suffers less from the forgetting issue.

Another minor advantage is you can control precisely where your desired output to begin with.

But overall, those are nice perks not too substantial IMO.

Re:Show HN: LLMs can generate valid JSON 100% of the time by msp26 on Tuesday August 15, 2023 @06:01AM (#37132041)
>I can make GPT4 return valid JSON simply by providing examples in the system message. This works nine times out of ten

But you can do both. For my current use case of extracting information from articles, I have a json schema + one/two example articles along with their correct answers. This increases token costs but 3.5 is so cheap that it doesn't matter and for 4 you can use batching to decrease token cost per article.

Re:Show HN: LLMs can generate valid JSON 100% of the time by vsrinivasan on Tuesday August 15, 2023 @07:23AM (#37132555)
Can you please explain what is batching ? any pointers?