News for hackers. Stuff that matters.
Re:Native JSON Output from GPT-4 by jonplackett on Wednesday June 14, 2023 @05:03PM (#36332614)
It was already quite easy to get GPT-4 to output json. You just append ‘reply in json with this format’ and it does a really good job.

GPT-3.5 was very haphazard though and needs extensive babysitting and reminding, so if this makes gpt3 better then it’s useful - it does have an annoying disclaimer though that ‘it may not reply with valid json’ so we’ll still have to do some sense checks into he output.

I have been using this to make a few ‘choose your own adventure’ type games and I can see there’s a TONNE of potential useful things.

Re:Native JSON Output from GPT-4 by cwxm on Wednesday June 14, 2023 @05:25PM (#36332932)
even with gpt 4, it hallucinates enough that it’s not reliable, forgetting to open/close brackets and quotes. This sounds like it’d be a big improvement.
Re:Native JSON Output from GPT-4 by ztratar on Wednesday June 14, 2023 @05:35PM (#36333084)
Nah, this was solved by most teams a while ago.
Re:Native JSON Output from GPT-4 by bel423 on Wednesday June 14, 2023 @08:35PM (#36334899)
I feel like I’m taking crazy pills with the amount of people saying this is game changing.

Did they not even try asking gpt to format the output as json?

Re:Native JSON Output from GPT-4 by worik on Wednesday June 14, 2023 @09:25PM (#36335266)
> I feel like I’m taking crazy pills....try asking gpt to format the output as json

You are taking crazey pills. Stop

gpt-? is unreliable! That is not a bug in it, it is the nature of the beast.

It is not an expert at anything except natural language, and even then it is an idiot savant

Re:Native JSON Output from GPT-4 by jonplackett on Wednesday June 14, 2023 @05:50PM (#36333249)
Not that it matters now but just doing something like this works 99% of the time or more with 4 and 90% with 3.5.

It is VERY IMPORTANT that you respond in valid JSON ONLY. Nothing before or after. Make sure to escape all strings. Use this format:

{“some_variable”: [describe the variable purpose]}

Re:Native JSON Output from GPT-4 by SamPatt on Wednesday June 14, 2023 @06:17PM (#36333563)
99% of the time is still super frustrating when it fails, if you're using it in a consumer facing app. You have to clean up the output to avoid getting an error. If it goes from 99% to 100% JSON that is a big deal for me, much simpler.
Re:Native JSON Output from GPT-4 by jonplackett on Wednesday June 14, 2023 @06:22PM (#36333596)
Except it says in the small print to expect invalid JSON occasionally, so you have to write your error handling code either way
Re:Native JSON Output from GPT-4 by davepeck on Wednesday June 14, 2023 @06:45PM (#36333882)
Yup. Is there a good/forgiving "drunken JSON parser" library that people like to use? Feels like it would be a useful (and separable) piece?
Re:Native JSON Output from GPT-4 by golol on Wednesday June 14, 2023 @07:05PM (#36334103)
Honestly, I suspect asking GPT-4 to fix your JSON (in a new chat) is a good drunken JSON parser. We are only scraping the surface of what's possible with LLMs. If Token generation was free and instant we could come up with a giant schema of interacting model calls that generates 10 suggestions, iterates over them, ranks them and picks the best one, as silly as it sounds.
Re:Native JSON Output from GPT-4 by hhh on Wednesday June 14, 2023 @07:12PM (#36334178)
I already do this today to create domain-specific knowledge focused prompts and then have them iterate back and forth and a ‘moderator’ that chooses what goes in and what doesn’t.
Re:Native JSON Output from GPT-4 by andai on Thursday June 15, 2023 @04:05AM (#36337668)
That's hilarious... if parsing GPT's JSON fails, keep asking GPT to fix it until it parses!
Re:Native JSON Output from GPT-4 by golol on Thursday June 15, 2023 @07:34AM (#36338991)
It shouldn't be surprising though. If a human makes an error parsing JSON, what do you do? You make them look over it again. Unless their intelligence is the bottleneck they might just be able to fix it.
Re:Native JSON Output from GPT-4 by golergka on Thursday June 15, 2023 @07:48AM (#36339104)
It works. Just be sure to build a good error message.
Re:Native JSON Output from GPT-4 by golergka on Thursday June 15, 2023 @07:47AM (#36339096)
If you're building an app based on LLMs that expects higher than 99% correctness from it, you are bound to fail. Negative scenarios workarounds and retries are mandatory.
Re:Native JSON Output from GPT-4 by 8organicbits on Wednesday June 14, 2023 @07:15PM (#36334201)
Wouldn't you use traditional software to validate the JSON, then ask chatgpt to try again if it wasn't right?
Re:Native JSON Output from GPT-4 by girvo on Wednesday June 14, 2023 @08:04PM (#36334659)
In my experience, telling it "no thats wrong, try again" just gets it to be wrong in a new different way, or restate the same wrong answer slightly differently. I've had to explicitly guide it to correct answers or formats at times.
Re:Native JSON Output from GPT-4 by cjbprime on Thursday June 15, 2023 @12:23AM (#36336369)
Try different phrasing, like "Did your answer follow all of the criteria?".
Re:Native JSON Output from GPT-4 by whateveracct on Wednesday June 14, 2023 @09:33PM (#36335324)
It forgets commas too
Re:Native JSON Output from GPT-4 by bradly on Wednesday June 14, 2023 @05:42PM (#36333163)
I could not get GPT-4 to reliably not give some sort of text response, even if was just a simple "Sure" followed by the JSON.
Re:Native JSON Output from GPT-4 by rytill on Wednesday June 14, 2023 @06:15PM (#36333533)
Did you try using the API and providing a very clear system message followed by several examples that were pure JSON?
Re:Native JSON Output from GPT-4 by bradly on Wednesday June 14, 2023 @07:35PM (#36334378)
Yep. I even gave it a JSON schema file to use. It just wouldn't stop added extra verbage.
Re:Native JSON Output from GPT-4 by NicoJuicy on Wednesday June 14, 2023 @07:49PM (#36334514)
Say it's a json API and may only reply with valid json without explanation.
Re:Native JSON Output from GPT-4 by bradly on Wednesday June 14, 2023 @08:24PM (#36334818)
Lol yes of course I tried that.
Re:Native JSON Output from GPT-4 by dror on Wednesday June 14, 2023 @08:47PM (#36334981)
I've had good luck with both:

https://github.com/drorm/gish/blob/main/tasks/coding.txt

and

https://github.com/drorm/gish/blob/main/tasks/webapp.txt

With the second one, I reliably generated half a dozen apps with one command.

Not to say that it won't fail sometimes.

Re:Native JSON Output from GPT-4 by NicoJuicy on Thursday June 15, 2023 @05:35PM (#36346844)
Combine both ? :)
Re:Native JSON Output from GPT-4 by taylorfinley on Wednesday June 14, 2023 @08:02PM (#36334640)
I just use a regex to select everything between the first and last curly bracket, reliable fixes the “sure, here’s your object” problem.
Re:Native JSON Output from GPT-4 by avereveard on Wednesday June 14, 2023 @09:49PM (#36335449)
Pass in an agent message with "Sure here is the answer in json format:" after the user message. Gpt will think it has already done the preamble and the rest of the message will start right with the json.
Re:Native JSON Output from GPT-4 by reallymental on Wednesday June 14, 2023 @06:00PM (#36333376)
Is there any publicly available resource replicate your work? I would love to just find the right kind of "incantation" for the gpt-3.5-t or gpt-4 to output a meaningful story arc etc.

Any examples of your work would be greatly helpful as well!

Re:Native JSON Output from GPT-4 by SamPatt on Wednesday June 14, 2023 @06:14PM (#36333530)
I'm not the person you're asking, but I built a site that allows you to generate fiction if you have an OpenAI API key. You can see the prompts sent in console, and it's all open source:

https://havewords.ai/

Re:Native JSON Output from GPT-4 by devbent on Wednesday June 14, 2023 @10:24PM (#36335680)
I have an open source project doing exactly this at https://www.generativestorytelling.ai/ GitHub link is on the main page!
Re:Native JSON Output from GPT-4 by ignite on Wednesday June 14, 2023 @06:25PM (#36333643)
> You just append ‘reply in json with this format’ and it does a really good job.

It does an ok job. Except when it doesn't. Definitely misses a lot of the time, sometimes on prompts that succeeded on previous runs.

Re:Native JSON Output from GPT-4 by bel423 on Wednesday June 14, 2023 @08:32PM (#36334880)
It literally does it everytime perfectly. I remember I put together an entire system that would validate the JSON against a zod schema and use reflection to fix it and it literally never gets triggered because GPT3.5-turbo always does it right the first time.
Re:Native JSON Output from GPT-4 by worik on Wednesday June 14, 2023 @09:22PM (#36335236)
> It literally does it everytime perfectly. I remember I put together an entire system that would validate the JSON against a zod schema and use reflection to fix it and it literally never gets triggered because GPT3.5-turbo always does it right the first time.

Danger! There be assumptions!!

gpt-? is a moving target and in rapid development. What it does Tuesday, which it did not do on Monday, it may well not do on Wednesday

If there is a documented method to guarantee it, it will work that way (modulo OpenAI bugs - and now Microsoft is involved....)

What we had before, what you are talking of, was observed behaviour. An assumption that what we observed in the past will continue in the future is not something to build a business on

Re:Native JSON Output from GPT-4 by travisjungroth on Wednesday June 14, 2023 @09:52PM (#36335462)
ChatGPT moves fast. The API version doesn’t seem to change except with the model and documented API changes.
Re:Native JSON Output from GPT-4 by whateveracct on Wednesday June 14, 2023 @09:33PM (#36335321)
No it doesn't lol. I've seen it just randomly not use a comma after one array element, for example.
Re:Native JSON Output from GPT-4 by LanceJones on Thursday June 15, 2023 @12:39AM (#36336458)
Yep. Incorrect trailing commas ad nauseum for me.
Re:Native JSON Output from GPT-4 by thomasfromcdnjs on Wednesday June 14, 2023 @09:40PM (#36335383)
Are you saying that it return only JSON before? I'm with the other commenters it was wildly variable and always at least said "Here is your response" which doesn't parse well.
Re:Native JSON Output from GPT-4 by travisjungroth on Wednesday June 14, 2023 @09:57PM (#36335505)
If you want a parsable response, have it wrap that with ```. Include an example request/response in your history. Treat any message you can’t parse as an error message.

This works well because it has a place to put any “keep in mind” noise. You can actually include that in your example.

Re:Native JSON Output from GPT-4 by lmeyerov on Wednesday June 14, 2023 @10:44PM (#36335824)
Yeah no
Re:Native JSON Output from GPT-4 by sethd on Wednesday June 14, 2023 @07:53PM (#36334559)
I like to define a JSON schema (https://json-schema.org/) and prompt GPT-4 to output JSON based on that schema.

This lets me specify general requirements (not just JSON structure) inline with the schema and in a very detailed and structured manor.

Re:Native JSON Output from GPT-4 by seizethecheese on Wednesday June 14, 2023 @08:04PM (#36334660)
In a production system, you don’t need easy to do most of the time, you need easy without fail.
Re:Native JSON Output from GPT-4 by pnpnp on Wednesday June 14, 2023 @08:13PM (#36334730)
Ok, just playing devil's advocate here. How many FAANG companies have you seen have an outage this year? What's their budget?

I think a better way to reply to the author would have been "how often does it fail"?

Every system will have outages, it's just a matter of how much money you can throw at the problem to reduce them.

Re:Native JSON Output from GPT-4 by jrockway on Wednesday June 14, 2023 @08:16PM (#36334754)
If 99.995% correct looks bad to users, wait until they see 37%.
Re:Native JSON Output from GPT-4 by muzani on Wednesday June 14, 2023 @08:50PM (#36335000)
It's fine, but the article makes some good points why - less cognitive load for GPT and less tokens. I think the transistor to logic gate analogy makes sense. You can build the thing perfectly with transistors, but just use the logic gate lol.
Re:Native JSON Output from GPT-4 by sheepscreek on Wednesday June 14, 2023 @09:18PM (#36335209)
The solution that worked great for me - do not use JSON for GPT to agent communication. Use comma separated key=value, or something to that effect.

Then have another pure code layer to parse that into structured JSON.

I think it’s the JSON syntax (with curly braces) that does it in. So YAML or TOML might work just as well, but I haven’t tried that.

Re:Native JSON Output from GPT-4 by bombela on Wednesday June 14, 2023 @10:41PM (#36335803)
It's harder to form a tree with key value. I also tried the relational route. But it would always messup the cardinality (one person should have 0 or n friends, but a person has a single birth date).
Re:Native JSON Output from GPT-4 by rubyskills on Wednesday June 14, 2023 @11:28PM (#36336054)
It's also harder to stream JSON? Maybe I'm overthinking this.
Re:Native JSON Output from GPT-4 by sheepscreek on Thursday June 15, 2023 @09:00AM (#36339827)
You could flatten it using namespaced keys. Eg.

    {
      parent1: { child1: value }
    }
Becomes one of the following:

    parent1/child1=value
    parent1_child1=value
    parent1.child1=value
..you get the idea.
Re:Native JSON Output from GPT-4 by jacobsimon on Wednesday June 14, 2023 @10:43PM (#36335821)
Coincidentally, I just published this JS library[1] over the weekend that helps prompt LLMs to return typed JSON data and validates it for you. Would love feedback on it if this is something people here are interested in. Haven’t played around with the new API yet but I think this is super exciting stuff!

[1] https://github.com/jacobsimon/prompting

Re:Native JSON Output from GPT-4 by golergka on Thursday June 15, 2023 @07:43AM (#36339064)
Looks promising! Do you do retries when returned json is invalid? Personally, I used io-ts for parsing, and GPT seems to be able to correct itself easily when confronted with a well-formed error message.
Re:Native JSON Output from GPT-4 by jacobsimon on Thursday June 15, 2023 @09:05AM (#36339879)
Great idea, I was going to add basic retries but didn’t think to include the error.

Any other features you’d expect in a prompt builder like this? I’m tempted to add lots of other utility methods like classify(), summarize(), language(), etc

Re:Native JSON Output from GPT-4 by throwuwu on Wednesday June 14, 2023 @10:22PM (#36335652)
Just end your request with

‘’’json

Or provide a few examples of user request and then agent response in json. Or both.

Re:Native JSON Output from GPT-4 by clbrmbr on Wednesday June 14, 2023 @10:26PM (#36335697)
Does the ```json trick work with the chat models? Or only the earlier completion models?
Re:Native JSON Output from GPT-4 by throwuwu on Wednesday June 14, 2023 @10:31PM (#36335727)
Works with chat. They’re still text completion models under all that rlhf