So now let's talk about AI, actually making our applications intelligent. So when we think this and think about adding AI, we might think right now at least about the generative part. So this is using the Vercel AI SDK, and it's actually a really, really good SDK if you don't want to talk to OpenAI or other model providers directly.
With the OpenAI SDK, this is an example in the back end where you would have a prompt, you would pass that prompt into stream text, use whichever model you want, and just return that as a streaming text response. Now on the UI side, there is a convenient hook called use completion from the AI SDK that gives you a bunch of things that you could pass into a form using that API URL, and the result, if we're making our apps more intelligent, is a way to do things like this. Just generate content, stream content, maybe have a chat bot.
But I actually really want to talk about really, really, really old AI. Because when I made stately.ai, when I thought of AI, I was thinking of the old school AI, the enemy video game behavior, like just how the ghosts in Pac-Man behave, which I realize they all behave a little bit differently. And so you could represent this using a state machine. State machines are one of the oldest representations of AI, and we see here that this is how a ghost behaves, more or less, in Pac-Man. Now I created xdate. First of all, show of hands, how many of you have used xdate? Or know of it? It's a lot of hands. I love it.
So xdate, if you don't know, it's a state management library for creating state machines. You pass in an initial state, and then your states really tell you what can happen in each state. And this is going to become important for some of the demos I'm going to show later. So in this case, if you're in the wandering maze state, two things can happen. You could lose Pac-Man or Pac-Man can eat the power pill, and depending on what's happening, you have a different objective. Either chase Pac-Man or run away from Pac-Man. And then you create an actor, and this actor is something that you could subscribe to and send events, and then you could listen to the state changes, start the actor, and then send events, and that's the xdate API in a nutshell.
Now I don't have too much time, but if you want to talk to me about xdate, then just say state machine three times and I'm going to randomly appear next to you. So that's how you can find me. But like I said, UI's applications are evolving now especially because of AI. This is make real by TL draw, which this is one of the things that really stretches the limits of what we could do with user interfaces and even generative AI. And really we're discovering that users want intelligent apps. Now they might not want those chat bots where it's like, all right, I don't care, just get me to a human, but they want AI to enhance their experience so that they could just do things faster. This is an example from ramp, where it's just a proof of concept, and there are actually real extensions that intelligently guide users and sort of act as that intelligent, you know, assistant. So what we're going to do today is we're going to use the AI SDK, and in order to go beyond just the generative example, we're going to be using what's called tool calling. So just to show you what this is, we have this generates text function. You pass in the model such as GPT4 turbo, you pass in a bunch of tools, and so in those tools, when you give it a prompt, the model can decide, you know what, I'm going to actually execute a tool because I probably need to do some certain task.
Comments