In this app, maybe we just want to be able to ask a question, that's our user intent, and then get a set of results with a couple of venues and each of their descriptions as well. So that seems all good, but how would you go about trying to get one of these language models to produce data that you can use in this app?
The thing that you may have realized in trying to do something like this is you end up trying to often pamper the AI to give it in a specific format, and even once it's in that format, it comes up with natural language that's just not always going to be easy to parse. So for example, here's a prompt and response that took a couple of tries to get even reasonable, right? One of the things that you might notice is I end up trying to just give it a little bit of guidance on how I expect the answer to be, right?
This isn't so bad, right? But mostly because it's come up with a regular answer in a specific format. And so it's actually given me a format of list items. Each of them is numbered. And then between the venue and the description, I have a colon. Now, is the AI always going to come back with that schema or format? Not necessarily. A lot of these language models are non-deterministic. So you really can't count on this format. But even if you could count on that format, you can't always trust that the data is going to be uniformly parsable, right?
So for example, in this case, I have a list and I have everything in this format. What about trying to split the colon, right? You might try to say, let me just try to shave each of the numbers off and then split by the colon. But what if one of the items in your list has a colon inside of the title or something like that, right? You're basically trying to do natural language parsing at this point, right? And now you have a bug, and now you have to figure out how to be resilient against that. And so this ends up being a little bit, impractical for most people, right? It is very hard to parse natural language.
But many of you probably also realize that you can get the language models to respond in the form of JSON. And that's great, right? Now you actually have something that your app can easily just do a JSON.parse or whatever, and get the data that you need and work off of that. But that only really works for sort of simple examples, right? Here I was able to say, here's an example of the JSON that I want to get back, right? I have a venue and a description, a venue and a description. And the AI is pretty good at figuring that out. But it doesn't tell us about maybe optional properties, maybe the case where you have three different kinds of objects that you expect to be in a specific position, things like that. And so just giving examples would be impractical because you would get into these combinatorial explosions of all the types of things that you would want to actually provide. So examples aren't enough. What you need is something a little bit more.
And it turns out there is a format that does work out pretty well, for the most part, in our experience. And it's something that you're all familiar with here at this TypeScript conference, which is types. TypeScript types are actually a great format for describing the exact format that we want out of a language model and all the sorts of shapes that we're expecting. So types are actually really good at guiding the language model into the thing that we want, right? And you can actually take the types in an application, like the actual text of the types in your program, take a user intent, and craft a prompt that you can send into an AI service, into a language model. And that will provide you with JSON, You would say, give me a response in the form of JSON, here's the type that you should conform to when you provide that response. And so, now you're able to guide the language model.
But like I said, the language model isn't always going to come back with a response that is actually exactly as we expect, right? So maybe it comes back with JSON, but it's not of the format precisely, right? So we can always say, hey, you didn't get me correct in the JSON, try again. But you need something else that pushes an AI so that you can say try again as well. And that's the validation.
Comments