Build an AI Agent from Scratch

Integrate Reddit API

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 "Integrate Reddit API" 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 walks through how to create a tool that retrieves the latest posts from the NBA subreddit.

Preview
Close

Transcript from the "Integrate Reddit API" Lesson

[00:00:00]
>> Scott Moss: Let's go make the Reddit one. So originally, I was going to get the front page of Reddit, but probably not a good idea, especially today, [LAUGH] to get the front page of Reddit and have it. I had it doing stuff last night, and I was like, I can't believe it just did that.

[00:00:19]
That's kinda crazy. I don't even wanna know what that post was, for that image to get generated like that, [LAUGH] but that's kind of crazy. So, I mean, you can do the front page Redditt if you want, I'm just not gonna do, I'm gonna pick a subreddit. I think I'm gonna pick the, Dustin showed me the ore, the cute one was cut of the animals, but most of them were just images, so I need something that's like more text-focused.

[00:00:43]
So I'm gonna pick the the NBA one. There was a lot of text on there. So I'll just pick the NBA one. You can pick whatever subreddit you want. You can do the front page of Reddit, I'll show you how to do it. It's super simple. I'm just gonna do the NBA subreddit, that way I don't get in trouble.

[00:00:56]
[LAUGH] Cuz that front page is crazy. [LAUGH] You're not gonna do that. And we're gonna do the same stuff here as the import, was it fetched from no-fetch? There we go. So, exports const redditToolDefinition, we're gonna give it a name. I'm just gonna call it reddit. I mean, you don't have to do exactly what I'm doing here.

[00:01:22]
If you wanna call it a different name, give it a different name. If you wanna have it take some parameters, actually, I don't think you can have it take parameters, cuz the API that we're using takes no parameters, but you get my point. So I'm just gonna say, get the latest posts from Reddit.

[00:01:36]
I'm doing NBA, so I could just come in here and be like, get the latest NBA post from Reddit, but I'm not gonna let the AI think that. That way I don't have to ask very specific things in my chat like show me the latest NBA, just show me something from Reddit.

[00:01:49]
And then it doesn't know. So I'll do that, I'll make my Args again with my inference here. So z.infer, Reddit tool parameters, which is literally nothing. So I don't really have to make this to be honest, but it's just a habit. And then, let's actually make the function.

[00:02:07]
So we'll say, export const reddit = ToolFunctionArgs string>, oops.
>> Scott Moss: There we go, I need a comma there. And yeah, we'll get that data. So we'll call it Redditt, and this is where you can change it. So I'm going to change mine to the NBA, so I'm gonna say that, and then you need to do .json at the end.

[00:02:42]
So any subreddit you want /.json, like if I go through this right now, it's just gonna give me all the json.
>> Scott Moss: There we go. And it's the same structure no matter what subreddit you go to, even if it's the front page, it's the same structure. So this code will work no matter what.

[00:02:59]
So I'm gonna do that, I get the data. I could just send back all this data to the LLM right now, but it's a lot of data. Look at all this, look at all these tokens. These are all tokens that's gonna be, honestly, this might be more tokens than what the LLM can take.

[00:03:15]
So I'm not gonna send back all of this, right? So I wanna format this before I send it back. So that's what I have in my code up here, or wherever Redditt is. Here we go. I'm doing this, so I'm actually just gonna copy that because that's just a lot of work.

[00:03:34]
But yeah, I'm only gonna send back an array of these objects that have a title, a link, a subreddit, an author, and upvotes. Put whatever you want, whatever you want to be able to ask your LLM, put it there. If you think it's useful, put it there. You can do whatever you want there.

[00:03:48]
But these are the things that I thought were interesting, so I put them there. And then the last thing is, like I said, you always have to return a string. So I'm gonna return that. And I've always had better look when I format the JSON to the LLM.

[00:04:09]
Again, it's very human-like, even though I know it formats the string before it processes it and it does different types of stuff, it gets rid of spaces. But I've had so much better results when I stringify, I'll always put spacing on it. And if you don't know what this does, this means stringify it and with, spaces of two, so it's pretty printed out, and not just like in one line, like regular JSON.

[00:04:34]
Somehow the AI just reaches better, just like a human would, I guess. So I always, when I stringify it and I pass to the AI, it always just do that. You don't have to, it's not required.

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