Build an AI Agent from Scratch

Fetching Dad Jokes

Scott Moss

Scott Moss

Superfilter AI
Build an AI Agent from Scratch

Check out a free preview of the full Build an AI Agent from Scratch course

The "Fetching Dad Jokes" Lesson is part of the full, Build an AI Agent from Scratch course featured in this preview video. Here's what you'd learn in this lesson:

Scott explains how to create a tool for generating a random dad joke. He demonstrates how to define the tool functions and their respective tool definitions using TypeScript and Zod.

Preview
Close

Transcript from the "Fetching Dad Jokes" Lesson

[00:00:00]
>> Scott Moss: We have an agent, it's looping, it's remembering, its calling tools, it's got all the things. It's just not useful because the weather is always 90 degrees, and that's just not accurate right now, so let's fix it and let's add some real tools. So let me go to my notes.

[00:00:21]
I don't think I actually have too many notes on this one, let's see. Yeah, it's really just a code. So what we're gonna do is, if you remember at the beginning, I showed you what this agent is gonna do as far as being able to generate an image, talk to Reddit and get random dad jokes.

[00:00:43]
So we're gonna make tools for those three things. To note on the image generation, I am using a model from OpenAI, it's called dall-e-3, it's their image generation model. You might have to turn that on in the OpenAI if you wanna use it, it's not required for this, you just won't be able to use this tool.

[00:01:01]
Just send something back and be like, nah, I'm not using that. So you don't have to use it but if you want the image generation, yeah, you have to go turn that on. Or you could go use something else, something from Replicate or Hugging Face, where they have tons of stable diffusion models that you could probably use right now publicly without a credit card or a name.

[00:01:22]
You can put that in there too. So whatever image generation tool you want, you can use. We're gonna use dall-e-3 because we already have OpenAI accounts at this point, might as well use it. Everything else is free and easy, no sign up. Okay, so we only gotta do a few things here.

[00:01:38]
We need to, one, make these tools. Two, we need to update our tool runner to know when to use these tools. Three, I'm just gonna create a little handy index file to export all of our tools into one place so we can pass them around as an array, super easy.

[00:01:58]
That's it, so typically what I like to do with my tools is I like to define the definition of the tool and the tool itself in the same file, and then I just export them. So let's start with the dad joke. So I'm gonna go into, here, I'm gonna make a new folder in the source, call it tools.

[00:02:21]
I'm gonna make a new file, call it dadjoke.ts.
>> Scott Moss: And all this one is gonna do is it's gonna use fetch, and it's going to fetch this URL right here that when you hit it, it gives you a random dad joke. You can actually just put it in the browser right quick and you can kinda see what you get back.

[00:02:45]
What kind of tree fits in your hand? A palm tree. So good, so let's do that. So I'm gonna import z from Zod. I have a type here called ToolFn that basically it's just an interface for, yeah, the tools. So come here, types, there we go. It just shows you the arguments or whatever.

[00:03:16]
So the first thing is the definition, so you can say exports const dadJokeToolDefinition. There's three parts on this, so you have the name, I'm just gonna call it dad_joke, any parameters. So even if you don't have any arguments for this, I highly recommend just putting empty objects. I've had bad luck in the past where, yeah, I forgot exactly why I started doing this, but I remember I started doing this and I got rid of a problem.

[00:03:45]
So even if you don't have any arguments, I highly recommend just putting an empty object here. If you're using it with Zod, I think it has little to do with OpenAI API, and more to do with how it translates Zod schemas over to the OpenAI API. And if it doesn't have a default, I think it puts something that the API doesn't like.

[00:04:05]
Maybe it's undefined, maybe it's null, I don't know what it puts, but it doesn't like it, so I just always put in the object here if I don't have anything. And then the last thing is just like, the description of this get a dad joke, however you wanna describe it.

[00:04:18]
Another thing that I do is I like to make some Args from the parameters, so I can type check my actual ToolFn. So I'm gonna use, I'm not sure if you've heard of this before, but you can do like z.infer, which is kinda cool. And it allows you to do like a typeof a Zod schema, so I can make a TypeScript type by inferring a Zod schema, which is kinda cool.

[00:04:43]
So if you don't really like TypeScript but you like Zod, you can just go make all your stuff in Zod and then infer it with TypeScript, and you get the best of both worlds. You get the runtime type checking, and you get the Dev time type checking, you get both and the build time touching, so you get all three of them if you wanna do that.

[00:05:04]
Just pretty cool.
>> Scott Moss: And now let's make that dad joke. So export const dadJoke, it's the interface of the ToolFn. The ToolFunction takes two generics, the first generic is, these arguments are here called toolArgs. So what is the type of this toolArgs? In this case, it's an empty object, so it means nothing.

[00:05:33]
And then the second one is like, what is this thing gonna return? I don't know why I even put this in here, it's always gonna be a string. You have to return a string in a tool, so this is kind of redundant, but yeah, it's gonna return a string, there we go.

[00:05:47]
And then I'm gonna import fetch from node-fetch.
>> Scott Moss: My God, can you stop? Thank you. Then I'm gonna say = fetch from there application JSON.
>> Scott Moss: Passing the dad joke, make sure it's JSON, and get back the joke
>> Scott Moss: Nothing too crazy here, we're not even gonna use these arguments, although the user message is here, I could pass a user ID.

[00:06:27]
If there were some arguments that this URL needed, I could have either that be arguments up here that this thing figures out, or if they're just coming from my database, I could just pass them in here. You have so many options, so many things you can do.
>> Scott Moss: Okay, cool, we got that joke.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now