Parsing JSON Into zod schema using OPENAI GPT

I want to parse a JSON example https://store.steampowered.com/api/appdetails?appids=1702010 Into large advanced zod schema. Do you guys have any experience doing this with gpt openai package?? Seems like pretty normal use case but I can't find any good examples
33 Replies
iDarkLightning
iDarkLightning11mo ago
idk about AI but have you tried: https://transform.tools/json-to-zod
JSON to Zod Schema
An online playground to convert JSON to Zod Schema
Jaaneek
JaaneekOP11mo ago
I’m sorry but this is not even close. I want any data => json formatted according to zod schema 😄
iDarkLightning
iDarkLightning11mo ago
what do you mean
Jaaneek
JaaneekOP11mo ago
I want to parse any input into a JSON that has schema defined in zod.
iDarkLightning
iDarkLightning11mo ago
wdym "any input into a JSON" be specific here, give an example input
Jaaneek
JaaneekOP11mo ago
I guess you don’t understand how does gpt work, I want to put literally any string on earth and I want the gpt to output JSON from it. It’s pretty normal use case. For example v0.dev does it but without zod. Here is example input https://store.steampowered.com/api/appdetails?appids=1702010 I’m already doing it as well but the gpt is hallucinating a lot for me and Im looking for some good practices/tips/snippets
iDarkLightning
iDarkLightning11mo ago
your example input is JSON bud
Jaaneek
JaaneekOP11mo ago
Sorry @iDarkLightning I think you are not helping here, you don’t understand my question. If you are curious about this topic feel free to DM me but let’s not spam this topic so others will be more willingly to give answers
Strike
Strike11mo ago
Why are you using the raw API? There's node packages that make this easier
Jaaneek
JaaneekOP11mo ago
I’m using OpenAI npm package but I have trouble incorporating the zod schema into it, even with the examples @Strike
Jaaneek
JaaneekOP11mo ago
I’m using this one https://www.npmjs.com/package/openai @Strike
npm
openai
The official TypeScript library for the OpenAI API. Latest version: 4.24.7, last published: 4 days ago. Start using openai in your project by running npm i openai. There are 1427 other projects in the npm registry using openai.
Strike
Strike11mo ago
@Jaaneek no like I mean accessing steam API Or what exactly are you trying to do? I'm a bit confused
iDarkLightning
iDarkLightning11mo ago
give us an example of the JSON that the gpt would output if I gave it the string "I like web development"
Jaaneek
JaaneekOP11mo ago
1. I’m fetching the data from steam API 2. Im using this json from steam API as input to GPT 3. I Ask gpt to use data from steam API JSON and put it into JSON that is defined by my zod schema …. I want it to be generic so I can also fetch the data from other places and always have it in my zod defined JSON format
Strike
Strike11mo ago
I see but you don't only want it to work for the steam API?
Jaaneek
JaaneekOP11mo ago
Correct, I want to also scrape data from App Store, google store, other gaming platforms
iDarkLightning
iDarkLightning11mo ago
So you want
{
// ...random JSON here
}

to be put into

z.object({
data: z.string()
})
{
// ...random JSON here
}

to be put into

z.object({
data: z.string()
})
smth like that?
Jaaneek
JaaneekOP11mo ago
Yes, my zod schema is very large so I have trouble “forcing” gpt to follow it correctly
Strike
Strike11mo ago
I see Yes the problem here is: Chatgpt is a transformer It doesn't have contextual awareness, it essentially uses data to classify tokens and predicts what's next
Jaaneek
JaaneekOP11mo ago
Jus to let you know: I’m already doing this with portfolio-quiz.com where I parse resumes into JSON, but the zod schema there is much simpler so gpt has no troubles doing it
Strike
Strike11mo ago
Really?
Jaaneek
JaaneekOP11mo ago
Yes, I will show you the code. One moment
iDarkLightning
iDarkLightning11mo ago
There is no science to what you want to do
Strike
Strike11mo ago
While increasing temperature might make this better Ultimately the science behind GPTs prevents it from being perfect Even in your application you are still at risk due to the nature of transformers
Jaaneek
JaaneekOP11mo ago
Yes, so my question is how to make it work better, how should I approach it?
Jaaneek
JaaneekOP11mo ago
Here is snippet from portfolio-quiz.com. It takes any resume and parses it pretty well https://pastebin.com/nCpLAC3L I have quite few users and every portfolio looks well
Pastebin
const openai = new OpenAIApi(configuration); const response = awai...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Strike
Strike11mo ago
While you can try to make it better using prompt engineering, ultimately the problem is models are probabilistic Not deterministic Using a bit of randomness it tries to guess the next token based on the dataset which it was trained on (oversimplification but I hope you can see the problem) So what you can try to do for getting the model to fit your output is try to: 1. Prompt engineer 2. Change variables such as temperature
Circus
Circus11mo ago
Second this. Read the docs on prompt engineering on how to communicate so that AI will more reliably (but not guaranteed perfectly*) give the output that you're seeking. I get a pretty complex JSON created through these methods as well.
Jaaneek
JaaneekOP11mo ago
Can you give me any tips/direction? Samples?
Sturlen
Sturlen11mo ago
I don't know if it's exactly what you're looking for, but there is an article about using OpenAI to extract data based on a schema (it's in python, but it should be possible to do something similar in ts): https://medium.com/@jxnlco/seamless-integration-with-openai-and-pydantic-a-powerful-duo-for-output-parsing-fcb1e616167b
Medium
Seamless Integration with OpenAI and Pydantic: A Powerful Duo for O...
Today, OpenAI introduced a Function Call API so we’re going to dive into a much more structured and efficient way of handling output parsing when interacting with OpenAI. This method leverages the…
Circus
Circus11mo ago
https://platform.openai.com/examples The OpenAI discord also has a channel that guides you along to build a better prompt as well.
Isaiah
Isaiah11mo ago
You gave a misleading question then treated people like an idiot when they didn’t understand. I’m surprised others are willing to help you
Jaaneek
JaaneekOP11mo ago
I literally said to DM me because I wanted to avoid spam, I believe I was not hostile at all Im sorry if I was, it was not my intention
Want results from more Discord servers?
Add your server