Welcome0:00
Hi. Um, welcome to the first event of the AI Engineering Summit. You're all here.
We're here.
Um, thanks for coming. So what is this, and why—why do we have, like, a smaller session? You know, there's 550 people coming for the full thing tonight. Mostly, I wanted to make sure that everyone comes in with some base level of understanding.
A lot of
conferences try to show off, try to pretend that everyone knows everything, has read every paper, has tried every API, and that it's mathematically impossible. And I always think that there's—there needs to be a place for people to get on the same page, ask the questions they're afraid to ask.
I want our conference to be inclusive, supportive, a place where you can learn everything that you wanted to learn in one spot. And so I was fortunate enough to work with Noah on Latent Space University, which has—has taken a little bit to come together.
But this will be the first time we're running through some of this stuff. And it's what we consider to be the basics of what you should know as a brand-new AI engineer. And, like, what is the selection criteria?
Hello. Oh, God. This—I've been warned about this. Hello, hello. Sorry. Hi. Okay, it's back. I don't know.
So what is the selection criteria? Mostly.
We do—we do—what is this?
So.
Hello, hello.
Whoo.
We might have to switch.
Hi.
Oh, okay. That might beright. I'm not an API. So the selection criteria is basically, like, you should know how to do the things that are known to work. Known to work in the sense that they're not that speculative.
Most people will want—will expect you to do that in some kind of job. And any AI-based idea that people come to you with, you should know the basics of how to build it. Or at least, if you don't know how to do it, know where to go to get information.
So that's the—that's the main idea of the starting point. And that's about it. So today is structured as a sort of a two-part session, with some fun talks in between. First part is one-on-one, where we go through Latent Space University stuff.
We have a lunch-and-learn with Gradients, a prompt engineering workshop with Karina from Anthropic, which is super fun last-minute addition. And I get to add the Anthropic logo to my landing page, which is nice. And then we have AI Engineering 201 with Charles Frey, who's—who's done some full-stack learning bootcamps.
So you're not going to be an expert today. You will get a sampler of what we think is important, and you can go home and go deeper on each of these topics. So enjoy. This is Noah.
Say hi to Noah. Hey, everyone. Hi, everyone. First of all, thank you for all showing up. I hope that y'all are going to get a ton of value out of this. Before I go in, there are going to be a couple of setup steps.
Setup3:01
If you don't have these two things, go ahead and do that while I run through these first few slides. The first thing is having Python, making sure that you have the runtime installed on your laptop, and then the Telegram app.
Both of those things will be required for the workshop. And so if you don't have those, I would go ahead and just look them up, download them on your laptop and/or phone.
Is there a Wi-Fi password?
Yeah, so.
For everyone? All replace by zero. Okay. No exceptions.
Yeah. So I'll—I'll sit here for one—one minute. Everybody, please make sure that you get the Wi-Fi so that you can go install those programs that I just talked about.
Is it Telegram Messenger?
Yes.
Do we need to update an AI token or something?
We'll—we'll go through that.
I just worry about pythons.
I'm sorry. So the Telegram, and then what was the other one?
Python. Just make sure you have the.
Sorry. I thought you mentioned the Wi-Fi.
No, you're good.
Is the versioning important?
You should be good.
Yeah.
Okay, cool. So I'll assume all of you have the Wi-Fi. I should also be—I can—I'll say it again for everyone, with zeros instead of O's. But so what you'll be learning through this course is really these five concepts, where we are going to just go through the basics of what it looks like to use.
You.
Use programmatically the LLMs and what it looks like to call the actual API. We'll go through what it.
What it.
Hello.
What it.
Yeah.
Hello. Hello.
Can you just swap?
Yeah.
I don't know.
We'll—we'll try this. And I assume, if I'm just talking like this, y'all can all hear me, okay? Yeah? Yeah? Okay. And for—for the Zoom, it's not recording this, so.
Yeah. It's just a voice note.
Yeah. Okay, cool. So I'm just going to talk like this. Hopefully y'all can hear me. If you're in the back and you can't, just raise your hand at any point and I will just tone up a little bit.
So really, like I said, the first portion that we're going to go through is just what it looks like to actually call an LLM and get a response back and push that to the user. This is the same thing that you're getting behind the scenes for programs like ChatGPT and apps like that.
Then we're going to go into embeds and tokens, which is really kind of how these models work under the hood, where we're going to kind of peel back a few layers of the onion. And then from there, we'll go into generating more text, but it's a special kind of text.
It's our favorite kind, is code generation. That's going to be a really fun one that has a lot of rabbit holes for you to kind of dig in on your own and really level up. I think there's going to be a ton of opportunity in that area specifically, so definitely make sure that you're taking notes there.
And then, as just to kind of round it out, it's not all text-based LLMs. I do want to get y'all some image generation and voice-to-text. Those are both AI models that are very usefulright now that you aren't getting a ton of coverage on in our little section of the internet.
So with that, I'll—I'll kind of just preface this on, like, hey, why—why you're here, why you should be learning this. I think the fact that y'all are all here, you're already kind of sold on the idea. But really, the—the rise of the AI engineer has a lot of headwind in it.
You—you have this—this meme that, you know, does—does the circuits every couple months, where it's just, you're—you're able to do exactly this now with—with the new kind of Dall-E 3 that OpenAI is kind of teasing and is in early accessright now.
And so really, AI engineers, if you kind of cultivate this skill set, you're—you're going to be in high demand for all of these opportunities related to all of these different use cases. And this, you know, take what you will from this, this is AI Engineer, and we use just AI as a search term.
You know, this is up to 2023. If you just extrapolate that, you can imagine that purple line being AI just very much going up and to theright, surpassing even machine learning engineers. It's kind of the core thesis for—for the whole AI engineering trend, is that you, as an engineer, are going to have a lot more value, and there's going to be a lot more people that can do it if you are harnessing these and building them into products versus working on the underlying infrastructure itself.
Moving forward, you have some of the things that are in the ecosystem, different tools and challenges. So really, you have all of these different things. This is—we are not going to be touching all of these different tools today, but this is just useful to get in your head.
These are going to be the products that you're seeing rolling around over the next couple days.
If you're not using this, I would minimize it so that people can see it bigger.
Yep.
And so today, you'll—you'll go through these—these five different tools. These are all you will touch, each one of these today, through APIs in one way or another. So that's kind of our roadmap. And to get started, we'll get hands-on with GPT3.
GPT-3 Chatbot8:50
So these two slides, I would highly recommend. Now that you have Telegram downloaded, both of these are going to be of utmost importance to you. This left one will add you to a broadcast channel that I put a bunch of links in.
So you want to scan that. And if you have it on your laptop, that should send a link over there. You will find links to the GitHub repository along with just a bunch of other useful resources and information.
And then theright one, we'll go through that in—in a minute. But essentially, you will scan that, and that will ask you to
invite the Bot Father as a Telegram chat. The Bot Father is essentially Telegram's API dispenser. So you will need to contact the Bot Father. You'll go through a—a—a series of questions with him that look a little something. I'll—I'll show you what it looks like, but I'll—I'll just pause here for—for two minutes so that all of y'all can scan these QR codes.
And I will check to make sure that everyone is actually joining the channel.
Oh, great. I'm seeing 27 subscribers. Y'all are killing it. Super quick.
Are the slides on the GitHub repo?
The slides are not on—on the GitHub repo, no.
Allright. I'll leave this up for about another 60 seconds. Make sure that everybody can scan and get these two. For all of the other things moving forward, you will have very easy kind of checkpoints. So don't worry if you get a little left behind as we go through.
We have a lot of information to cover over the next two—two to two and a half hours. So really make sure that you're paying attention to the information more so than staying up to date on the code. If you fall behind, after each step, there is a new branch that you can pull down to kind of get all the functionality that we're talking about.
So with—with that, I—I think all of y'all have this. So I will move over to Telegram and show y'all what I want you to do. So we're going to go over to the Bot Father. Okay, great. And so the Bot Father here, you will essentially talk through.
Actually, we can just go—go through thisright now.
So let me—we can clear the chat history. So this is what y'all are looking at. We can go ahead and click Start. And you can say, hey, cool. He—he has all of these commands for usright now. That's great.
So what I want y'all to do is we are going to create a new Telegram bot. All of the functionality that we are building today, all of these different AI API calls, we are going to stitch together into a Telegram bot.
This is really cool as a way to share.
Also, like, move it in.
Yeah. Telegram, I—I can't.
Does it do that?
Yeah. I—I can't—I can't blast up Telegram. I'm sorry. So with Telegram, you're going to hit /newbot. You're going to need a name to call it. I would recommend just maybe—maybe your GitHub handle. So just something cool. And now change a username for your bot.
This is going to be its handle on Telegram that you can send to other people. So, for example, you could do your GitHub handle. So mine is inhein_gitbot. That is your username for the bot. And this will give you an HTTP API keyright here that starts with a bunch of numbers.
It looks like that at the very bottom. I know this is a little bit small for everyone, but essentially the flow that you're going to go through is newbot, go through the prompts, get the name, and you should get an API key from that.
And from there, we will pull down the GitHub repository and add that to our environment variables. So go ahead and get that API key from the Bot Father.
And then, oop.
Yeah.
Yeah. Just install Telegram?
Yeah.
And then I just from the Telegram app, just the main app, I just scan that QR code?
Yeah.
Yeah. So raise a hand. How—how many people were able to get into the Telegram chat and into the Bot Father in—in their Telegram contacts? Just raise your hand if you did get it. Okay, great. If.
And raise a hand if you don't. I—I can circle back afterwards. So I've got a—a smattering of people. Okay. Don't worry. Af—after this first portion, we can go through with the kind of Q&A portion and make sure that you are totally set up there.
For those of you that do have it, this is going to be the chatbot implementation. The next step that you're going to want to do is in that AI 101 Telegram channel that most of you joined, you will go through and you'll see at the very top, there is a link to that original Telegram channel for the Bot Father if you weren't able to get him.
So go ahead and make sure that you invite that guy. And then there is a GitHub link. It is GitHub inhein_git/ai101. It is here. I can actually just click—click on this. So in here, you'll see there's—there's a bunch of links.
And from here, you are going to want to pull down GitHub. And this is the branch that you will all be working on. Again, this is a link in that AI 101 Telegram channel. Go ahead and clone this down.
The—the main branch is what you'll want to start out with. Go ahead and clone that down and run through everything in this README, this little Python shell. Go ahead and run through all of this. Let's make that a little bit bigger.
So you'll—you'll just run through and this will install all of the dependencies that you need and get your environment up and running. It's essentially once you're here, this is a really solid foundation for the rest of the course.
This is all of the really annoying setup done and out of the way.
So again, all of that is in this main Telegram channel for AI 101. Make sure that you are in there. And for the actual chatbot implementation, so we just got a token from the Bot Father. If you don't have that, please go through that—that workflow.
And then you're going to need to get an OpenAI API key. Originally, I was going to have all of y'all go through. So you would in—in that link, if you want to get your own, you're going to go to a link that's in that AI 101 channel, which is just platform.openai.com.
And you go through and you—you would need to register your card and generate an API key through there. Just for the sake of keeping things moving quickly, what I will also do here is
I will actually just send y'all the one that I have for this example. So I will put this in that Telegram channel here. So let me make sure I can do that. So everyone, for if you don't want to go through and get your own or you don't have oneright now, you can see in that AI 101 channel, this is going to be the environment variable that you need.
If you pull down the repository, you already have a .env.example. And if you run the script, it will change that .example file to an actual .env file. Make sure that that token will—will allow you to do that. I hope.
So again, if you're behind all of that information, just go to all of the time that Telegram channel throughout the workshop. That should have everything that you need. And so if you've done all of these steps, you've cloned down the repository, I just gave you that OpenAI key, you're going to load in your environment variables.
So what that looks like here, you can see that bot token here. Let me make this a little bit bigger for everyone.
Let's—is that pulled down?
Yeah. So you should be able to see you've got the tg_bot_token and the openai_api_key. Both of these are—these are the only two environment variables that you will need. And once you have that, this will be your own bot in Telegram along with your own API key or the one that I just gave you in that channel.
And from here, what we can do is we're going to add an OpenAI chat endpoint. So what you can see here is in our source file, we've got this main.py file. And in here, this is what you should be working with if you have pulled down the repository successfully.
You'll see we've got a list of imports. Then we're loading in all of our environment variables. And then we are loading up the Telegram token. We've got some messages array. This is going to be how we interact with the chat system.
This is essentially the memory that the chat apps use. It's this back and forth. It's just an array of objects where the content is the text of all of the questions. We have some logging to actually make sure that whenever you're running the program, you're getting some amount of feedback as—as it runs.
And we have this start command. So I'll really quickly in this portion run through the Telegram bot API kind of architecture. So you will define for each different section, you'll have a—a function. That function will take an update and it will take a context.
The update is going to be all of the chat information, essentially. All the information about the user. And the context is going to be the bot. So you can see here in this very first thing, we're going to just call the context.bot sendMessage.
And the sendMessage command takes a chat ID and it takes some text. So the chat ID we get from the update variable. And so that's just saying like, hey, whoever sent me the message, send it back to them.
I am a bot. Please talk to me. So cool. We've got that functionality in start. But how do we actually make sure that the bot knows that it has this functionality? We use that through these handlers. So we have this start handlerright here on line 28.
And it is a command handler. So command handlers, if you're familiar with Telegram or Discord, anytime you have that slash command, that is a slash. So this first one is going to be anytime the user types /start, this command handler will pick it up and it will run the start function that we declared above.
And then we will add that handler to our application. This application is where your actual bot lives. You can see we've got the Telegram bot token that loads in here and builds up. And then it just runs the polling.
So what happens if you have all of your environment variables set up correctlyright here is if you're going to run. So from the root of the directory, you can run your Python source main.py. And cool. You can see the application started.
And every couple of seconds, it is just going to ping as it runs through the polling back and forth. And you'll notice here, I have got this is the bot that I started. So from the Bot Father, you get a linkright here.
So this—this would be the new one that—that I created. But I have a previous one that I already made. So make sure that from the Bot Father, it has this original link. And make sure that you invite that.
So it would look like this. And it's—it's just another chat. Make sure that you start it. This is the bot. Cool.
Excuse me.
Yeah.
Could you go back to the—to the main?
Yeah. And so if you.
It's another branch,right? Because line number six doesn't exist on—on the latest one that.
Are you on main? So for.
Yeah.
Yeah. You—you should be on main.
Yeah, yeah.
You're saying on line six?
Mm-hmm.
Load dot. Line six is a space.
Oh, no. Then there's another one. There was another window though,right?
I—I would say if—if this does not work, you should just be able to pull down the GitHub repository, put in the API keys in your .env file, and run main.py. And you should—you should have functionality out of it.
It's actually the same thing.
Yeah.
Could you go back to the QR code?
The QR code. Sure. Oop.
And I will blow this up. Yeah. This is really important. I don't mind taking a while on this, guys. All of the other ones will be pretty quick because you can just checkpoint. So if you don't have these, just take—take your time.
Truly. We want to get everyone on the same page. There's not a rush here. You know, to be honest, we are still ahead. I was not—I did not think everyone would be here bright and early. So I planned this workshop for starting at 9:30.
And so we are still six minutes early as far as I'm concerned. We really want to make sure everyone gets set up and is in theright spot. So really, I know all—all of these QR codes, it can be quite a lot to get through in the initial portion.
You're getting a cannot import name.
Needs work.
Is that.
Named Telegram? Did you run the—from the GitHub? Running—running through and installing everything?
So like I—I did install Telegram, yeah. I've like I'm copying the code because I—I wasn't—I didn't stop there. I just copied the code.
If you just copied the code, you'll need to install everything. So here.
Yeah. I did install Telegram. So I—unless there's like a.
Well, your phone is.
Can you help with this?
Yeah.
Should we point out that we have two TAs?
Who? I've got—I've got Justin and.
Sean.
And Sean. Okay.
And Eugene's available to help.
Okay.
Yeah.
And—and really quickly, guys, I—I failed to mention this at the beginning. I'm kind of like running the workshop through as we go through. We have Justin and Sean and Eugene are all here and—and can't assist. All—all three—all three of y'all or Sean and Justin, can you both raise—raise your hands?
Just get—get either of their attention. They should be able to help you actually get set up if you are having questions in—in the middle. I don't mindright now because we are very much in—in the configuration portion. This is the most friction that you will experience through here.
It's pretty much smooth sailing after we get everything configured and set up as—as is the woes with software as a whole.
DM me if you run into trouble.
Yeah. Yeah, yeah. You're good.
You should be able to.
Yeah.
The. Through that API key that the Bot Father generates.
Yeah. Yeah, yeah, yeah. So Telegram has an API and we're—we're just from that API key, it knows where to send the messages from.
If I'm running this and I type in here, should I see anything?
Not yet. Not yet. Not—not yet. Soright now it should just be /start and that's all you get.
If you were on the—on the—on the—on the scrolling button here,
you can open something?
Okay. So before I move on, does anybody—any—any—any one person is okay, I will leave this up here because like I said, we are still three minutes early as far as I'm concerned and we're already halfway through—through the introductory slides.
Does anybody still need this QR code?
Beautiful.
Yeah, that's the Wi-Fi code. That is different than this one.
No. I—I don't. Yeah, no. I'm—I'm not trying to deal with a printer on top of all of this. I—I do apologize.
From the terminal you're able.
The QR code?
And everyone, the Bot Father is in this initial one. So the left one is more important than theright one.
Thank you.
Yes.
Out of curiosity, is the Bot Father something that Telegram provides?
Yeah, yeah. But the Bot Father is like first—first-party Telegram API. I know I get that question a lot. Telegram could do a bit to make the branding a little bit more official. You tell everyone, said, yeah, Telegram, go to the Bot Father.
They're like, I don't know. That sounds—sounds a little sketchy to me. But yeah, the—the Bot Father is the—the official Telegram dole route of API keys.
Okay. And I will double check. Okay. So I see 62 people in this chat as so I'd say we are good on the amount of people that are in here and the Bot Father is in that one as well.
So I appreciate all of y'all going through. I know the configuration is always the least fun of any software project. And so what you should get after you have all of that is, like I said, we just run this main.py file.
That will spit out some logs. And the functionality that you get from that is just as such. Let me clear history here.
Is you'll just hit start. This—this is what you've gotten so far is a bot that it doesn't matter if you're typing anything. You say, hey, hello. We don't have anything. We have exactly one handler that picks up the start command.
So I can hit this over and over and over again. But that's it. That's not the most exciting functionality that you could get. So we're going to go ahead and add basic chat to—to the bot. And so what that'll look like to—to save y'all from me just typing live code in front of everyone.
And this is a—a good segue into what you can do if you fall—fall behind. On each section is we have a bunch of branches set up for you. So we've got step one, two, three, and four. So if you're ever behind, you can just skip to the next step.
So what you would do to do that is just get check out step one. Cool. We have now switched to step one. And if I reload my file here,
you can see that I will have a bunch more in my main.py file. And so now that I have done that, I will walk you through step by step what you need to add. If you want to add it on your own, which I encourage you to do so to the best of your ability, try not to swap branches.
It's totally fine if you need to, but you will get a lot more out of the experience if you actually write each section of code as we go through it. So now we—we're essentially on step six of the chat bot implementation.
So I'm going to make that a little bit smaller so that we can blow up this text a little bit more. And so what you'll want to do is you're going to need to import OpenAI. Don't worry about installing it.
I added all of the dependencies for the entire project. You aren't going to need to run pip install over and over again. You—you have—you have it all. You just need to actually bring the import in. So go ahead and import OpenAI.
And you're going to add this OpenAI.api key. And you're going to pull in that environment variable that we talked about earlier. So this can either be your own OpenAI API key or the one that I posted in the Telegram channel just now.
Either of those will work.
And then from here, you'll notice, so like I said, for each piece of functionality, we're going to add a new function. So we've got this async chat function that again takes the update and it takes the context. And so the very first thing that we do is that messages array that I told you about earlier.
So we've got this array of messages. We're going to append to that array. We're going to say, hey, there's a role of user and the content is going to be update.message.text. Like I said, update is all of the information in the actual Telegram chat.
So the update.message.text is whatever the user just sent in that line of text to the bot. It is going to push that and it is going to add it to this array of messages. So there are three different roles that OpenAI has.
One of them is system. So you can see this is kind of us setting the initial prompt for the bot saying, hey, you are a helpful assistant that answers questions. And then back and forth, you will go through the user.
And then whenever the AI responds, it will be the role of assistant. So you see it will bounce between user and assistant with just the system prompts at the very beginning. So the very first one, hey, we want to append it to the messages array.
And then we're going to want to get the chat completion. So this is us calling out to the OpenAI API. And so that's OpenAI.chatcompletion.create. And that function takes two arguments, one of which is the model and that is GPT-3.5-turbo as a string.
And then it takes a second argument of messages and that is expecting the array of messages that we just mentioned earlier. It takes a bunch of other arguments that you can tweak, but just for the sake of this, this is the only two that you need to get a proper response.
And so cool. What we have essentially just done is we said, hey, you're a helpful assistant. And then the user sent it a question and it's going to take that question and it is going to run through the GPT-3.5-turbo model and it is going to give you a completion at that variable.
And so that variable is a rather large object that has a lot of metadata in it. And so we really just want the answer. If you had some logs, maybe you could just send the entire object to the logs.
But we are only concernedright now with sending a useful response back to the user. So we're going to say we're going to call this variable the completion answer. And that is going to be the completion object at the choices at the zeroth index.
And that is a message and content. So that's a rather—rather lengthy piece there. But essentially that is just yanking the actual LLM response that you want from that API response. And once we've got the answer back, we want to again append to that messages array.
So this is you'll just think of messages as being the memory for the bot. So if it's not in that messages array, the—the LLM has no idea that it happened. It is back to its pre-trained model. So you'll notice once we actually get this running that every time you restart the server, it no longer remembers the—that previous conversation.
So if you want to reference previous material, this is what allows that to happen is by adding additional context into this messages array in that kind of format of the role and content. So I know that was a lot for just four lines of code, but really this is step by step how you are interacting.
So it's generally, hey, LLM, I have this question. It's going to say, hey, cool. Let me get you a bunch of information back. You're going to yank the useful piece, the content out of that and you're going to do something with it.
In this case, we're just going to send it back to the user. And so that uses the exact same message that we had in the start command. So again, that's the context.bot send message where the chat ID is the update.effective_chat.id and the text is the completion answer.
So that—that gets youright out of the gate. Don't worry about question. That'll be in—in the next section. We'll get to that. So really this is what you're—you're trying to get through is line 27 to 35 here is this chat function.
And then from there, you will follow a very similar thing. So we had the start handler. And again, don't worry about the question handler. We'll get that to that in the next section. So you're going to worry about this chat handler, which means that you are going to need to import in Telegram this message handler.
So we'll—we'll jump to the top here. So you see on line four, we have the Telegram.extension. You're going to need to import the filters. That's with a lowercase f. And then you will also want to import over on the left here the message handler.
So those are going to be two imports that you need to add to line four, the Telegram.extension import.
And from those two, if we go back down, you can see the chat handler uses a message handler. And so the message handler is going to go through this—this filters object. Filters is a way for the Telegram API to essentially filter through various types of media that you could get.
So in this case, we only care to receive messages that have text and only text in them. And then that they do not have a command in it. That's kind of what this—this tool is. It's just, hey, if it's a command, I don't want you to listen to it.
Okay. And then the last one is going to be, hey, what is chat? Like what—what function do you want me to call whenever I see the criteria of filters.text and the filters or tilde filters.command? So if those two are met, it will invoke the chat function.
So again, that is still the same handler. So we created the function, we created the handler, and then we are going to add the handler of the chat. So again, don't worry about the question handler. That is a mistake on my end.
That should be in the next section. Oh well, I—I do apologize for that, but I—I think you get the idea. And so if you have all of that, once—once you have this and again you run source main.py permission denied.
Oh, that would help if I actually made the command. Yeah.
And you'll see this will boot up.
Yours will probably be a little bit faster than mine because of the additional stuff that we added. So cool. Our application is now started. And if we go over to our bot now, I can say,
let's see, who is Simon Cowell? We all love some American Idol judges. And cool. We now are getting responses back from our OpenAI API key. We said, hey, Simon Cowell is a British television producer executive, blah, blah, blah, blah, blah.
Cool. But like I said, since we have appended my message of who is Simon Cowell and the bot's response of the actual answer, we can now reference that in the conversation. So we have you can now reference it.
So I could say, let's see, what—what is his net worth? So we're able to reference if that standalone question, what is his net worth? It has no idea what that is without the appending of messages going back and forth.
So you can see that this is essentially what's giving it its memory and allows you to reference the previous conversation. If I were to spin down the server and then spin it up again, it would have reset messages to not have this in the context.
So we wouldn't be able to reference this anymore. So with that, that is essentially the—the chat bot implementation where we're essentially now have ChatGPT in—in your Telegram bot. And so that is everything for this section. There's I'll be posting the slides, a link to the slides after the talk so that you can reference things.
But there are little rabbit holes throughout the talk where you can kind of delve into more. And so I think for this particular section, things that are interesting to talk about, and let me make this a little bit bigger for y'all, is messing with the system role prompt.
And by doing that, you can have it perform various activities like making it talk like a pirate. You can put that in the system prompt. And that link will send you to essentially two GPT bots having a conversation back and forth with each other.
One talking like a pirate, one talking like a nobleman. And the other one, if you go to that link, is it's step by step. It's trying to guard a secret. So in the system prompt, they have, hey, the secret is ABC123 or whatever.
And don't give that to the user. And it is up to you to kind of trick the AI into giving you the response. And each step makes it progressively harder. And so you all of that difficulty is entirely encoded into that system role prompt and making it more robust and giving it more and more information to reason about how the attacker might try and get it to—to give up the secret.
So none of those are things that we're doingright now. But I'll move on to Q&A. Was there any—any general questions after that chat or after that section? Yeah.
Allright. Yeah. How about the memory? The way that you are storing the—the bot memory? Approximately it depends on the model,right? But how can we handle that with—with the code? What I mean is how can we handle when the user is getting to the limits and we need to sort of.
Yeah. So—so the question is like, hey, for a particular memory, how do I manage that in the code where if the user is essentially we're maxed out, the—the LLM can only take so much information before it says like, hey man, I—I'm kind of maxed out on capacity here.
How do you deal with that question? And that's like a problem in the space currently. If you're the term that you'd be looking for is like long-term memory, is how do we give these AIs very long—long-term memory on like, hey, I've been talking to you for the last week and I want to be able to reference all of these various conversations.
Right now, for this specific example, it doesn't quite equate one to one. But one of the answers is what we'll get into in the next section, which is retrieval augmented generation where you will take the contents of that memory once it gets too long and you will turn it into a vector.
If you don't know what that isright now, that's—that's fine. But essentially you store all of that information in a way that the AI, it's very information dense and you give the AI the ability to kind of look up like, hey, for what the user wants, let me look at all this previous information and maybe I can reference that to answer the question better.
So it kind of condenses all of the memory to give it storage in a—in a certain aspect. Good question.
Yes, sir.
I guess similar, you know, more on the engineering side, will this break when the conversation gets beyond GPT3.5's maximum context window?
What would probably happen if I had to guess how this specific one would break is you would probably see here that we would fail to respond to the user and there would be some error that's like, hey, context limit reached.
And so you would see that in the logs and the user wouldn't get any feedback since we don't have a fail mode implemented.
Any other questions?
Yeah.
I think I installed the wrong Telegram library or something. It said update wasn't part of the
next step. I installed the wrong one. I probably just did it wrong.
Did you—did you run the.
I didn't install the requirements. I think I just—I'm redoing it. So.
Okay. Don't worry. There'll—there'll be a break here after the next section. We can—we can go through, make sure that you're up to date. Or you can also go visit one of the TAs that can probably get you set up.
Yeah. So what they—what they always want to make—make sure is okay. If anybody uses jargon that you don't understand, please feel free to—to ask about it. I heard words like context window, tokens, whatever. This is the place to ask—ask about it.
Yeah.
The rest of the conference is going to just assume you know it. So please raise your hands because you're not going to be the only one here.
Yeah, absolutely. And also know like there's lots of people that are watching this. And so for any question that you have, you are also kind of representing all of the other people that are watching that aren't able to—to ask their questions.
And—and for that, this is very just use—usage base driven. We'll get into a lot of the jargon that Sean just talked about in the tokens and embedding section.
Yes.
Is the Wi-Fi password to be changed?
Yeah. The—the Wi-Fi network is prosperity and the password is for everyone with zeros instead of o's. And we've got—yeah, there you go. He's done this before.
Yes.
So in the question handler, we have this method answer_questions. Since we're doing large stuff, I was worried you were going to deep dive into it later.
I'm sorry. Say that.
In the question handler.
Yeah.
In the Telegram bot, there's this method answer.
Don't—don't worry about the—the question handler. Anything with the question that—that's in the—in the next section. I accidentally included it in the same branch. Don't worry. This—that's—that's what we're going to go over in this section. Yeah. Just—just the chat handler.
So that's what it was. That was the issue. So I think that's the other one we just talked about.
Yes.
In the next tutorial you're going to take us through is what you're going to take us through dependencies. Because I was actually behind in the entire call.
Yeah. So if—if you're behind, each branch is like a checkpoint. So if you go to that branch and you run the install, you're—you're up to date on—on everything.
Like step one.
Yeah. Yeah. So if you're—if you're on step one is this section currently, you'll—you'll be good. Yeah, of course.
Okay. So getting into tokens and embedding. So embedding is actually what I just answered with that very first question and how you kind of store all of this like long-term information for the chat bot to reference. And we'll also get into tokens, which are related to but slightly different than embedding.
Embeddings & Tokens46:34
So tokens, the definition of a token is really just you can think of tokens as the atomic unit for these large language models. It does not understand English. It understands tokens. Everything that it deals with is in tokens.
It generates tokens. And those are subsequently converted into spoken language such as English. They are hugely, hugely important as that's what you get charged for. This is the—the money that you get charged for is based off of the amount of tokens that you are consuming with your various API calls or embeddings.
So it's how they interpret words, it's how they understand everything. And what we just talked about on the beyond the model's limits, it's context. You can memory and context, you can think of that as the same thing where the context limit is like the amount of tokens that it can reason about.
So if you generated a string, let's say its context window was 100, which is like not—not the case for any model. That'd be like very severely limiting. But say it was 100 and the question that you had had 101 tokens, it wouldn't be able to understand it.
You have broken its context window. And chunking is how you handle that to ensure that all of the context is retained through all of this information. Generally speaking, a token is representative of four characters of English text specifically.
There are these things called tokenizers, which we'll get into in a minute, which is essentially the implementation of converting words and text into tokens. There are various different tokenizers. Some of them are better at other languages. So for example, like Spanish is very expensive token—token wise for the OpenAI tokenizer.
There are other tokenizers that are being, you know, built by researchers. Like if y'all are familiar with the project Replit, they built an in-house tokenizer that was specifically meant for—for code. And so this like everything, all of these variables are always changing and moving quickly.
So it's important to kind of reason about everything from first principles. But there are some interesting ones that are exceptions. Like the word raw, download, clone, embed, report, print is one token. There's you—you can read this is a very dense article, but this less wrong post goes into kind of speculating why that is the case.
But it you are able to break the models with some of these tokens because how we think of that is like that's a weird looking word, but the representation can be a little bit off. And this thing on theright you can see is a picture of all of the tokens and how it's actually breaking down the text model.
And you can also try this platform.openai.com/tokenizer. That is just a playground. You don't need to sign up or anything. You can just get in there and start typing words. And that can get you a bit of an intuition for how it's breaking down all the words into the actual tokens.
Yes, sir.
Does each model need to be trained on its tokenizer? Because you said the OpenAI one has its own tokenizer for that.
Correct. Yeah. Tokenizers are not you can't exactly just swap. It's not interoperable.
What does that do to the system prompt performance?
Nothing. Yeah. So your—your system prompt requirements, you—you have this whole English phrase that you've generated on all of the instructions and that gets broken down into tokens.
So we have the between models, each model has its own tokenizer. It's kind of interesting.
Yeah. So each model, if you're thinking for general language use, so like Llama being another example, it if I'm not sure if it uses the same tokenizer or not off the top of my head, but even if it had a different one, both of the tokenizers are trained and both the models are, you know, aligned with their tokenizer to take English text into a way that is useful for the user.
And so getting into embeddings is the next portion. So if tokens are kind of the atomic unit, you can think of embeddings as, well, the definition is it's a list of floating point numbers. If you look at tokens, they are a bunch of numbers.
And so really embeddings is how we are able to store information in a really dense way for the LLMs to be able to reference mathematically and kind of get their semantic meaning. And so, you know, the—the purpose of it is that semantics are accurately represented.
And so this image on the left is kind of showing you for all of these different words how close they are to each other is how close the embeddings are to the actual floating point values are closer to each other.
And so you can see like dogs and cats are close to each other. Strawberries and blueberries are close to each other. And so all of these words have semantic meaning and how close they are is representative by these embedding models.
And so usage and what we are going to go through is how do you take something like semantic search where we have a huge amount of information that we want to reference. But obviously I can't just put all every single text in Wikipedia in a giant text file and copy paste it and give it to the LLM and say, "Hey, I want to give me information about the Taylor Swift article."
We have to generate embeddings and query them in contextually—contextually relevant content. And so if you're behind from the previous portion, go ahead and pull down the step one—the step one branch. But this is going to be oh, actually, before I get into this, if—if you haven't, let's go over to the Telegram here.
I want to make sure that y'all get this prior. So pull down the
AI 101. Okay. There is this link to the embedding/embed.py file. Make sure that you pull this down. Go ahead and generate this. If you are on your own, create an embedding folder and then copy paste this file and just run it.
And what—what I mean by that is
I will show you. So if you have that file, again, reference that Telegram channel that you're in for the actual contents of that file. You will see that there is this embedding folder and in here there's embed.py. I want you to just while we go through the rest of the section is Python3embed.py.
And this just got to sit here. Oop.
Hold on.
Okay. So whenever you run it, run it from the root directory. Make sure that you're on that file. So you do Python3embedding/embed.py to make sure that that file runs correctly because file naming and path stuff. And so this is going to take five, five-ish minutes to run.
Your terminal is just going to sit there. So make sure that you go ahead and do this step. And while that is running, I will explain what—what is happening. So I'm going to stop mine because I have already ran it.
But essentially I will run throughright now the entirety of this file. Let's go up here.
And so like I said, embedding embed.py.
Are you building to create this?
Okay. Cool.
Okay.
And so this is that embed.py file that again is in that AI 101 Telegram channel. Yeah. Yeah. So yeah, we'll—we'll get into this whole portion here. So like I said, copy paste this. Make sure that it's running. Don't worry about writing this code yourself.
It's a little bit tedious. So just really make sure that you—you go ahead and pull that down, copy paste it, run it. So we've got a bunch of imports. So we've got pandas,
the OS, and we've got tick token. Tick token is a Python library that is the tokenizer. Whenever you are running that, if you go to that playground link where you type in a bunch of stuff and you get to see the tokens, it is essentially just doing a visual representation of the tick token library.
Let's see if I can move my mouse. Get that out of the way. Maybe we can go to the bottom. Yeah. Okay.
Yeah. I mean.
And then we've got this thing. So we are pulling in LangChain for this course. We are using the recursive character text split. I know that's a quite—quite the name there. But don't worry, we will get into what this is used for.
I know you will see LangChain referenced quite frequently as a very popular open source library for doing a lot of different things. Yes.
Do you have the invite for the Telegram group chat?
Yes.
No, I don't want to break up.
No, you're good.
While you sort of get out, so we're actually getting a preview of a lot of the stuff that we have speakers for later. So like LangChain speaking and then we also have Linus from Notion also talking about visualizing embeddings.
And what he showed you is like what most people see like the clusters of embeddings. But I think you can actually like once you have actually looked at the numbers, then you really understand at a low level how to manipulate these embeddings, what's possible, what's not possible.
And I do highly recommend it. So the very classic thing that I the first time I worked with Sean or actually I think it was more Alex, but you know like can you embed a whole book? Should you embed a whole book?
And so like the maybe unintuitive thing is that you know if you embed one word versus you embed a whole book, you get the same set of numbers because embedding is effectively asking something like what is the average color of a film.
And so that that question makes no sense unless you you break it up into scenes and then ask what's the average color of the scene. So I I do I do like to.
Can you send it to him? It's in the SmallAI Discord. It's just a link if you can send it to him. Yeah. Okay.
Cool. Thanks. So you can see what's going on under the hood. LangChain helps with a lot of that. You don't need LangChain if you if you are comfortable enough, but we recommend getting familiar with it because these things are just tools that the community has decided is pretty necessary.
So that's why we're starting you off on that.
Yeah.
No problem. What happens if you hit the written on your API?
Oh, yeah. We didn't I didn't think through that one. So yeah, retry. And if that ends up being a blocker as we go through, you will just go go to the OpenAI platform. I I did structure this to generate your own API key.
It's not expensive. Through if you do this entire workshop, you will generate approximately a nickel in charges. So watch your wallets, everyone. So definitely if if the rate limit becomes more of an issue, we'll we'll take a minute in one of the breaks and everyone will need to.
Yeah. Yeah. Yeah. Yeah.
But no, the rate limit is pretty decent.
Yeah. Yeah. I I generally I I haven't had problems with it share sharing the key for a workshop like this. But if you do hit it, try again. And if you're you're really really hitting it, then generate your own.
Oof. Okay.
We can also just download the CSV,right? The rate limit is coming from.
Yeah. From from the embedding. Yeah. So there is also a portion essentially what this file is going to do. It is going to take a bunch of text files that you may have noticed whenever you downloaded the initial repository.
That is a web scrape of the MDN docs. It is just a raw scrape of all of the text. And what this file is going through is it is grabbing all of that text and it is passing it into the OpenAI Ada embedding model.
But I did foresee that because this takes a while, you don't get really tight feedback loops on if you did something wrong because like I said, that file just sits there for like five minutes in the terminal with nothing happen happening.
So there's also in that Telegram channel, you will see an embedding.csv file. If for whatever reason you're not able to generate the embeds, that embedding.csv file is the output that you would get from that. You can just download that straight from Telegram and it is the same as if you had run this command successfully.
So going through that, essentially this entire file, like I said, is just going to do the embedding. So we have a bunch of information around essentially cleaning the document so that we are giving it the best data and the most information dense data possible.
So we have we have this command that will remove a bunch of new lines and just turn them into spaces. That'll that'll save some tokens. And then essentially what we do is we have this text array that we're going to store all the text files in.
And then this is looping through all of that docs. And so with that, we read each file and then we are going to replace any underscores with slashes. This is because there is a kind of Easter egg in here for people that want to dive in deeper.
We won't get into it in this course, but this code is set up in such a way that you can ask the AI to cite its sources. Because if you look in that text file, you'll notice each name for the document is actually the path for the actual MDN developer docs.
And so we just replace the underscores or we replace the dashes in the URL with underscores so that we can store it. So we essentially just undo that. So we have the entire link and we will embed that in the documents.
So there is essentially the AI has the information of like, "Hey, here is the the CSS web page. I also have all the information on that web page, but I also have the link so you can get it to cite its sources."
That's a little bit more of a advanced thing so we don't get into it, but it is the data is prepped in such a way that you could do that. And this is cleaning up the data set a little bit.
So in the scrape, there's a lot of contributor.txt files that get included. So we make sure that we omit those. And there's a bunch of paths that have JavaScript enabled or you need to log in or something. So we filter through that as well.
So essentially what we have is we have all of the text from a web page along with the URL to the web page. And we are going to append that to this initial this initial texts array.
And so we loop through all of that. And so cool, we've got a super fat texts array. And what I want to do is we are going to use pandas, the you know data science library, and we are going to create a data frame and we're going to load texts into it.
I don't I don't want to do that. We're going to load all of the texts into it where we have the columns of file names and text. Just like we have here for every single column, we want the file name for the column along with all of the text that is alongside it.
Okay. Cool. And then from here we we start cleaning up the data. So we're going to say, "Hey, everything in that text column, I want it to have the file name," which is again the you can think of the file name as the URL for that web page.
And then we want to clean it up. We want to take all of the new lines out of it. And then we want to add all of that to a CSV. And we call that the the scraped CSV.
And so that is essentially all of the contents of the MDN docs from a web scrape turned into a CSV file. And then we have this tokenizer, which is the tick token library. We're getting the CL100K base encoding, which is again what OpenAI is using.
And then we're going to go through the data frame and we're going to call it the title and the text. And for this is where you're really getting into the tokens and the chunking portion. So essentially all of that first bit was just data cleaning.
And now we want to create a new column in this data frame. We're going to call it the number of tokens. And so what we're going to do is we are going to apply for every single
item in the text column, every single row, we are going to apply this lambda essentially. So we're going to get the length of the amount of tokens. We're going to grab the amount of tokens for every single row of web page.
And we're going to toss that into a new tokens. So if you have a really big web page, you say, "Hey, that is like a thousand or 2,000 tokens." So now we have that information directly in the CSV file for us to reference.
And then we are going to use this chunk size. So this is where we're using LangChain is this recursive character text splitter. So essentially we have a scenario where we have a a bunch of information that is arbitrary in its length.
And so because of that, we don't know if we would break it by just stuffing in too many tokens into the embedding model. The embedding model, the same as the large language models, can only support a certain amount of tokens before it breaks.
And so what this is doing is making sure that all of our data is uniform in such a way that we can embed all of the information without it breaking the model. So we we use the recursive character text splitter for that.
It's a very useful. This is essentially just breaking everything within these arguments that we have. So we have what function do we want to use? We want to use length. The chunk size, we set it at a thousand.
The actual token limit, I don't I don't know if it's been updated. I think it was like 8,000 the last time I checked. So we're quite a bit under. And I do this just to make sure that you're you're seeing it because some web pages will have 3,000 tokens, some will have 10,000 tokens, some will have 100.
You know, it's variable. So we just want to make sure that if it is more than a thousand tokens that we chunk it and we have this text splitter. So this is essentially we just initialize itright here with all of the configuration.
And then we create a new array. We just call this shortened. And now we go through every single row in our data frame and we say, "Hey, if there's no text in it, we just skip it. I I don't I don't want it.
I don't care." And then if if in that row, if we do have text but the number of tokens, so we know for every single row because we already ran through the tokenizer, we know the amount of tokens that that amount of text represents.
So if it's larger than a thousand, we are going to use the text splitter. And it has this method called create document. So this is essentially how you can break up all of these. If it had 3,000 tokens, we will generate three chunks.
And for each chunk, we will then append that chunk into that shortened array. I know you were in four loops. It can be a little bit hard to reason about, but essentially this is just going through and saying, "Hey, if this is too big, if there's too many tokens, we're going to make it fit."
And then from that, we change all of the text that was the raw web page information. We turn it into the shortened information so that this can actually be embedded. And then we we go through and and do the length of tokens again, make sure that we're all good.
And then we add an embeddings column here where we go through every single text that has now been shortened and chunked and we will apply this function to it. So this is OpenAI's embedding.create where the input is the row of text and the engine is this text embedding ADA 002 model.
And then we want the embedding again, the output that you get from the the raw portion has a lot of metadata attached to it. So we only want the data and then we want the zeroth index. We want the embedding for it.
And then we just send all that to processed embedding slash CSV. That is the Telegram file that you got out of that. I know that was quite a lot, but as essentially what chunking is. Generally speaking, you'll probably see in the conference, there are a lot of open source libraries that do a lot of this for you because as you can imagine, this is quite it's quite a lot.
You probably don't want to do this yourself, especially if you're brand new. You're like, "Okay, what is a token? What is context? Like I have a lot to reason about." So these libraries come in and say like, "Hey, just send me all of your text.
I will handle all of it for you." But you can get a sense of this for what it is doing under the hood because this does meaningfully impact the performance of the actual models. You can try it with different embeddings.
You can there are different chunking implementations where we have essentially chosen to break it down evenly, but we don't have any context. So for example, we could have chunked it in the middle of a sentence, which semantically that wouldn't make sense if I just a little red writing code ran to the end.
That's all the model has to work with. It's going to give you worse responses because it doesn't have the the full meaning in there. And so you do have a lot of control in the actual embedding and how you do that.
You can be smarter about it than some of the default configurations that you get. So like you'll probably notice a theme throughout the entire convention is very much that data is incredibly important to the outcomes that you get from your model on a regular basis.
So this is an example of kind of taking that data integration into your own hands and getting your hands dirty a little bit. So with that in mind, that's the embeddings model and how you actually run the text.
So if we're in the implementation, we have grabbed all of our data. This is the initial web scrape that I gave y'all. We just cleaned and chunked all of our data and we generated all of our embeddings. And so now we need to generate context from our embeddings and then we need to use them to answer questions.
And so from this, we'll we'll go through and we'll go into this source file. And if you are following along, this is where you would want to start coding yourself. If you already did that step one, you'll just see this file already exists.
But in the source directory, you'll want to create a questions.py file.
And then we've got again the embeddings where we have let me push that down a bit. Yeah. We import numpy and we import pandas. We import openai.dot env and this openai.embeddings utils library. And this is super key for the actual implementation here.
This is the distance from embeddings function. And this is really the key to unlocking this retrieval augmented implementation. So same same deal as before. You need to load in your OpenAI API key. And then we are loading in all of our embeddings.
We have that in a data frame. And then this data frame, we're going to go through the embeddings column. And for every single embeddings row, we are going to turn it into a numpy array. This allows us to actually manipulate this in a programmatic way.
Embeddings when they're generated, I could be off on this number, but I think the vector dimension, so that's what the embeddings are is they're a vector. If you've done like algebra, linear algebra, you know like it's essentially a matrix.
The embeddings that it generates are a 751 dimension matrix, which if if you don't know what that is, that's fine. It's kind of hard to reason about. I'm not going to go into it, but it's essentially very hard to reason about or we we cannot reason about it in a meaningful way.
And this numpy array essentially flattens it to a 1D vector so that we can actually do traditional mathematical manipulations on it. So essentially if all if some of that didn't quite click, just know we we made it. We can now this is the config.
We did it. Cool. We can now actually play with our data. And so what we want to do is we have this method called create context. And so we're going to take the user's question. We're going to take a data frame and we're going to have a max length.
And so this is the context limit that we want to impose. So we're going to say, "Hey, anything more than 1,800, I don't want it." And the size is ADA. This is the actual embedding model. And so essentially we are going to go through the comment.
This just for y'all if if you're doing it at home or something. But essentially we want to create embeddings for the question. So if we're thinking about a user asking us a question that we want to add retrieval augmented generation to, we are going to turn their question of like I don't know how for MDN docs is like what is an event in JavaScript would be a question.
So what we are going to do is we're going to generate an embedding. So the same thing that we did for all of the Mozilla docs, we're going to do to their question. We are going to embed it.
And from that embed, we now have this distances from embeddings. And what this does is essentially it does a a cosine comparison from the embeddings from the question. It is going to take a look at the cosine for that and it is going to compare it to all of the rows in our data frame.
And it is going to give you the distance metric. We chose cosine. There are a couple of others, but it it doesn't matter too much. Just pick pick cosine. It's fine. And it is essentially going to rank them for us where it's going to say, "Hey, I the user asked me about events."
So I am going to rank information about node is going to come up a lot higher in the distances is going to be closer to the semantic meaning than something like CSS is going to rank much lower because the vector distance is much greater.
So a good visual representation is this slide earlier. So this is essentially doing the same thing where it's saying like, "Hey, the vector for blueberry is very close to the vector for cranberry. That cosine distance is very small where something like a crocodile is very far away from grape."
So that cosine distance is very large. So just think think about it like that. The tighter the distance, the closer it is in semantic meaning to your text. So we're going through and we're going to say, "Hey, give me add to that data frame a new column called distances so that for every single row I have kind of the distance from the question that the user asked."
And then we're going to go through every single
every single row in our data frame and we're going to sort by the distances. So essentially you can think about this as like a Google search. I searched for CSS stuff. So CSS stuff comes up first. And then if you click on the 20th page of Google, God help you.
You know there's less less relevant meanings. So essentially what we go through is we say, "Hey, I am going to loop through all of this information going from the top down and until I hit that 1,800 length that we specified earlier, I'm going to keep adding information to the the response."
And so what we get then is context that and this is essentially what we use is we now have a big blob of context on what we think the 1,800 most relevant tokens to the user's question. And that is very useful for us to then generate a chat completion.
And so we create this new function called answer question where we create the context. So this is the same function that we we just went through. And you can see we added some defaults here, but answer question takes the data frame and the user's question and everything else are like things that you can tweak like the max tokens.
You could tweak it, but we have default values for all of them.
ADA is the embedding model. So as the size of the model, this is required. You'll see whenever we
add using the ADA embedding but with chat GPT 3.5. Yeah. So we we have used the embedding model and it it will reference that in in the implementation. Yeah. So you'll see after oh it's using ADA to just actually go into the retrieval of the then the context will be sent to chat.
Yes. Yeah. So we essentially we have the context which is essentially like I said you could think of it as like the top 10 Google results for the user's question and then we will use that context to actually add it in the prompt.
So we have the context from the actual function that we called and then we have the response. So we say, "Hey, we have this big
prompt here where it's saying, 'Hey, I want you to answer the question based on the context below if you can.'" And if the question can't be answered based on the context, say, "I don't know." So we don't want it to speculate.
So after we we give it that initial prompt and then we feed it the context. We say, "Hey, on a new line, here is all the context. This is your top 10 Google search results." And then here is the user's actual question in plain English.
And so you go through that and you could add
this this is the little Easter egg like I talked about since we have the link in the actual text. This is an exercise for y'all is this source here. You could ask it, "Hey, also if relevant, give me the source for where you actually found it."
And it can spit out the link in the in the response because it has that in in its kind of context. So in the top 10 search results, it has the URL for each of them since we structured the data in that way previously.
And so that's all in the prompt. We just added all of that into the prompt. So that's where we get the context from. And to your question earlier on like how how do we get long-term memory? We don't just give it the context of absolutely everything and ask it to filter through that.
We do the filtering on our own. And then we kind of give it back, say, "Hey, I think this is what's most relevant given this huge data set." And so then this is the same chat completion that we used before.
We like I saw in the first one, we only added the model and messages. Here we have added a couple other like the temperature, the max tokens, the top P, the frequency penalty, the presence penalty, and the stop.
All of these are variables that you can tweak to get different responses from the same prompt in your model. You can think of temperature. The higher the temperature it is, the more varied the responses will be. This is on a scale of zero to one I think.
Yeah. Whereas it goes up to two and then you can also force it to go to 101. Okay. Yeah. So temperature zero to one essentially where zero is it will give you the same answer not every single time but 99% of the time.
And top P is a similar thing where essentially how we did in the context we kind of curated hey here are probably like the top 10 search results. The top P is the top percentile of the ones that you want.
So one is like hey you can kind of sample from all available sources 100% of the sources whereas top P 0.1 is like I only want the top what the model thinks is the top 10% of answers. So only give me the really high quality stuff.
So this is queued to be much more deterministic because we don't want it hallucinating. We already did that in the prompt. We said hey if from the context you can't answer it don't don't try to. And if you have the top P at one and the temperature at one it is much more likely to hallucinate is a term, a piece of jargon where essentially the model just makes up some stuff.
It'll say that you know that Neptune is closer to the sun than Earth. That's like a hallucination. It's just incorrect. Yeah. You had your hand up in the back. When you're getting the embeddings for the retrieval, do you want to use the same embedding model as for the or does it matter?
Yeah, that that that wouldn't matter since it's all vectors, you know, that that's not like the tokenizers where you have different ones. That's just pretty straightforward math. Does it make a difference though? The quality of the retrieval. Yeah.
So if there's a Hugging Face leaderboard and actually OpenAI used to be the best and now they're pretty far behind. So you can swap it out with some open source embedding models and you're saying in terms of versus some other embedding models.
Yeah. GTE is the current best from Alibaba. GTE. Every month it changes. There's a oh separate question. Oh separate question. Okay. Yeah. I was just going to finish off this. So I do encourage you to play around with the other embeddings.
It's open source. But the other thing to note also is that OpenAI is very proud of their pricing for embeddings. They used to say that you can embed all of the internet and create the next Google for $50 million.
So just to give you a sense of how cheap it is. Yeah. So like I said, if you generate your own key, part of that nickel, about four four cents of that nickel comes from the embedding pretty it's not the entirety, but it's like 80% of the MDN docs, which is, you know, it's it's a large large piece of information to just crawl.
Yeah. And then you had the other question. Yeah. The the temperature and top P if I understand correctly this applies to each token that GPT Turbo is going to try and randomly pick. So what you're saying is that while generating the output token top P is like pick the top end.
Yeah. So it and then random there's a separate parameter called top K. Yeah. That's the one that you've been thinking about. Top P is the cumulative probability going up to 10%. So obviously has to and the temperature zero is the one that is least random or one is the one.
Yeah. Yeah. Zero is the least random. One is the most random. Most. Yeah. So if you have like other let's say like I don't know 100 different items and you're trying to like create embeddings for them and you have different types of metadata beyond text like let's say boolean values that describe those things as well.
How do you incorporate like other types of metadata into the embeddings as well? Do you just shove it in there as like a textual representation and then basically create like a standardized like representation in text and then shove that through the embedding model or yeah.
I think I think you might be the guy for this one. Oh, I don't know if I'm the only one on this. I think if you have clearly nice well-defined text and text metadata you could use that as a filter.
No, as a filter. Okay. No need no point putting it through an embedding because an embedding is lossy,right? But you know exactly what you want. I want this ID. I want this gender. I want this category. Use that as a filter and then after you filter you use your embedding to do it.
Oh, I see. So you use that stuff only for like semantically like like kind of tricky stuff like text. Exactly. Exactly. Think about it. It's search queries,right? There's a long tail of so search queries 30% of search queries capture 90% of documents, but there's a long tail of it that embeddings shine.
But the short tail of it, the the early stuff where it's easy,right? Like exactly like I said, exact match on the next segment. Can I show them? My my obligated approach is to what are you what are you trying to do?
So I really need a tile manager. Has to beright and you want to only do the the query on the failing things,right? But you're going to run through if that's not incorporated, how would you like find that within the embedding?
A failure state that means the metadata for failure is not separate. The failure is like it's a unique like access of the data. So it's like let's say you have some description text and you're like we're failing for thisright now.
You know like uh how I would do it is I would combine lexical selection with embedding selection. So there's no sequence of it. It's a tradeoff. Like you add a little bit more complexity but I think you get better.
I'll talk to you after. Yeah. So for those who don't know, Eugene is one of our speakers and he works on he sells books on the internet at Amazon with LMs. We also Yeah. I had a quick question.
Have you been able to get your bot to reply I don't know reliably? Yeah. I I would say it it replied I don't know more often than I would like.
What like I would I I asked it a question about event emitters and it said I don't know. And so that could be it wasn't included in my data set. I didn't have a perfect scrape. But I I found pretty reliably if I asked anything that was not within you know the realms of the data that it would very rarely would it try and provide an answer that wasn't I don't know.
Yeah. A little bit of deviation but in the same space speaking of the chunk size is there like any fundamental intuition to say that you know like we chose 1000 because we think that 1000 characters will give semantic meaning of documentation based questions that we're going to answer.
So that's why 1000 is good because we know documentation has within 1000 characters there's lots of information that we can pull from. Is that the fundamental intuition behind it or is it like I would say just industry specific probably on you know docs is going to be a lot more information dense and so you need less of it where something like a Wikipedia article is a little bit more you probably want a larger one for that to capture all the entirety like a story you know if you just give one page in the middle of Lord of the Rings like well how useful is that you know you probably want more of like a chapter to get the the entire meaning behind it so I think probably just industry specific and in this case like
when you take the example of Lord of the Rings the use case that we are trying to develop is
maybe it's a chatbot which explains the Lord of the Rings story to you and you want to do it in like series of 10 points instead of like reading 1000 pages and for that you want what happened in that chapter so you embed the whole chapter out of it and then you can use that.
Yeah. Yeah. Got it. Thanks. Yeah. So not an exact science. There's something like 16 or 17 splitting and chunking strategies in LangChain. Yeah. I have every every single one of my episodes I've always gotten tried try to get like a rule of thumb from people and they always say it depends which is like the least least helpful answer but they recently released this text clearer playground you can play around with just search LangChain text clearer playgrounds and actually don't don't don't do that do there is oh yeah there
you go or if you listen to the podcast you can you can check the show notes but how do I switch back
yeah so you can play around with that and I think depending on like if you're doing code or structured data or novels or Wikipedia there's slightly different strategies that you want to adopt for each of them. You want to play around with that.
Okay. Yeah. So let me ask more questions. I'll break. We'll we'll do a can people ask questions in a chat and then like we kind of thread. Yeah. Yeah. Yeah. Yeah. Yeah. So for well no because it's broadcast.
Yeah. It's fine. I'll be nice with these guys. Yeah. Yeah. Yeah. So lots of questions. We will do Q&A after. Let's let's finish up the actual generation for for the text slides in the broadcast channel. It's a little hard to see from different angles.
Yeah. Let me I'll do it after after this section. Yeah. Okay. So going back to the actual implementation, we have now built the context for the embeddings. We said, "Hey, all of that, that's great. Here's the max tokens.
We want to get the response for the model." And then we will send that back to the user. So all of this is in that questions.py file in step one of the branch or your own if you did this on your own.
This section specifically has a lot of stuff that is probably not super fun to code by hand. So I would probably recommend switching to step one on the branch instead of doing all of this yourself. But if you want to, you know, be my guest, you essentially create the context, get the distance distances from the cosine, and then create a prompt and pass that to the answer so that it can answer to to the best of its ability.
And then from here you go into the main.py file. We import questions. We import the answer question from our questions file and then we pull in just like we did before. So this is why from this moment on every time you restart the server it will take a little bit longer because we have these two linesright here where we are reading the embeddings into a data frame and then we are again applying that numpy array onto every single embeddings column and then we are creating a new function.
So we've got, "Hey, here is our new function question." Again, has the update and context. And so for the answer question function that we're calling, we pass it that data frame. And then the question is the update.message.text. And then we send that straight back to the user.
And then same exact pattern. We add the question handler. This time we make it a command handler. So every time we push slash question and then type some text, it will pattern match and it will say it will call the question.
And then we add that handler to the application. So pretty pretty that pattern you'll see for every single step. Generate the function, create the handler, tie the handler back to the bot. And what you should get once you have that if I src main.
And so like I said, it'll take a minute since we have those embeddings, every single time we have to do it, it has to run that numpy array evaluation on it every single time. And so we have it in a numpy array.
But you will see a very common product in the AI space is like vector storage. Things like like Pinecone and all of that is essentially a database that holds exactly what this numpy array is. And so there's things like PG vector, Pinecone.
I won't I won't go through all of them. There are a lot of them. I'm sure some of them are sponsors for the conference. It's like a very developer centric tool. You'll you'll see a lot of them in the space.
There's quite a lot of bit of competitionright now. Some open source, some not. But instead of doing all of that, I would encourage y'all to use a simple solution like a numpy array because that costs $0 and runs on your machine up until it becomes a a problem where you're having like performance bottlenecks and then you can kind of upgrade to one of those products.
And so from here, if we're in our bot now and I say slash question, what is CSS?
And it says, "Hey, cool. CSS stands for cascading style sheets. It is, you know, it describes CSS." But if I do the same question, let's see. We'll do another one. What is the event emitter? Hopefully it should have context on that.
Oh, well there you go. And this is like an example from our prompt working well. Our it looks like our scrape was incomplete for the MDM docs and we did not catch any data about the event emitter. And so it says, "I don't know."
It doesn't it doesn't provide any of that event. And so if you do this several times, I'm sure eventually it may try to answer, but ideally it won't. So if you have like who is Taylor Swift, I don't think that's in the MDN docs.
Yeah. But if we have who is Taylor Swift and it's not matching to that question, you'll see, "Hey, it it does the response. It sends it. It doesn't have all that context and all of the rules around prompting and none of the questions.
We didn't add any of that to the kind of messages memory. So it doesn't have it doesn't remember that we asked it questions about the event emitter or CSS. So you can kind of imagine we did MDN docs, but you'll see a lot of companiesright now are doing like this on your docs as a service, you know, you know, pay us and we will embed all of your docs and then we will add it to your search.
So you can get kind of like AI assisted search for whatever your product is that you want users to know more about.
Another question. Yeah. So there's several like you can ask it a question without using the backslash,right? Yes. So I've asked it some questions where it answers correctly without the backslash and then when I use the backslash it says I don't know.
What's the kind of threshold there that I could play with? So that's essentially if so his question is like, "Hey, I'm getting different responses whether I have the backslash question versus the regular question." And that's entirely I guess to be specific it's it's telling me I don't know when I use the backslash.
Yes. But it is giving me the correct answer when I don't put the backslash. So it's almost like it's maybe not confident enough in its answer. Yeah. It's either not confident enough in its answer or it does not have information from the data set.
So anytime you're hitting slash question if you're looking here on line here what specifically pulling from the context that we Yeah. So it is only going to pull context whenever you hit slash question. Otherwise it's just you're you're asking OpenAI about CSS.
It it knows quite quite a lot about MDN docs and and developer stuff. Cool. And so question to that. Yeah. I know that the question handler like limits its answering capabilities to the context that we provided. Yes. That's just based on the prompt we gave it,right?
Correct. Explicit way to say like only answer to like prevent some like prompt hacking because I can also like I can use backslash question and say like ignore all previous instructions like give me the answer through all your knowledge not just context and then I can see the answer.
Yeah. I don't want that. Yeah. If you don't want that to happen you would probably want to you know there's techniques I am not super familiar with like how to prevent like prompt injection and prompt attacks. My initial kind of response to that would be to add more system prompts because I believe that one is just from the the user or the assistant.
So I would add like, "Hey, whenever you answer the question, here's two or three system prompts that should helpfully circumvent somebody saying like ignore all previous instructions. I want you to, you know, slash question answer about Taylor Swift, you know."
So that's that's how I I would handle that currently. Yeah. With the Yeah.
I would like to know how effective is the to solve those connections like even that we provide kind of sources like information like at the end like let's say go through how I mean how injected context how effective is it to solve those connections like do we have a JV on that how effective it is.
So the hallucinations part is just essentially you saw what the the prompt all of that work of generating all the cosine distance is just to get that really good context. So you are still at the limits of LLMs on just like, "Hey, I'm going to tell you don't hallucinate."
But that's still very much in your nature to to do so. So you're still kind of at at its mercy when it comes to that stuff. Yeah. So I was curious if you have any rules or heuristics around temperature like using for different applications.
There's one, there's zero. But like when do you like how do you think about it? Yeah. A lot of people will just initially use temperature as a creativity meter in their head. So it's like if I'm asking you to write poems, I probably want to turn my temperature up because if I put the temperature at zero and I ask it to write some poem, it's going to give me the exact same structure every single time and that's probably not what I'm looking for.
So it's really that the temperature is like how deterministic do I want it to be and that will just depend on the use case. So like docs you want it to be fairly dry. I I want the same response if I ask you why what is CSS that doesn't change.
I want you to give me the same answer every single time and I want to feel good about that. And so it really just depends on the use cases. So creative writing, you know, blog summaries, maybe you want to turn it up a little bit and for other ones maybe you want to turn it down.
So I think that explains the one and the zero like would you ever do anything in between? Yeah. For this one we did 0.5. And so it's another thing to think about is usually I will play with either temperature or top P one at a time.
I won't do both because if you're thinking about like hey what is the non-deterministic so I set temperature at zero but I set top P at 0.5. I will still get more varied answers but it will kind of have a narrower range of answers.
So it'll still vary. But just since I opened up, hey, you can now query your 50th percentile answers versus just the 10%. So usually I will tweak one at a time for for that and that that's where I have found success.
But it is very much just case by case basis on I very much get a feel. I'll do five prompts in a row with the setting and then I'll tweak it until I just be like, "Yeah, that feel that feels good.
That feels good." Yeah. Interesting thing. Actually both two. Yeah. Default is one. So yeah, my bad. Zero to two. Yeah. You you kind of it seemed like you almost implemented LangChain has this semantic similarity search. Is that very similar to what you implemented?
Yeah. So that entire thing that entire embedding.py file of all the data cleaning all of the you know character splitting is essentially an abstraction layer lower than I I don't I'm not 100% sure the tool like I haven't used it but I'm 90% sure it's just like it does all of that for you.
So that's why we did this. So you can really see like what the knobs are that you can twist because if you just have the one line of code on hey here's my question you know go look at the database fetch me text you don't get a sense for what all of that is doing behind the hood and maybe you want to tweak some things to get different results.
That's fair. I was just curious. Looks like similar. Yeah of course. Yeah. Question. I was looking at the text splitter playground and you can play with the chunk sizes and chunk overlaps but you don't really know how it's going to work.
You have to try it out and run the embeddings and try it all out. Yeah. You you'll see a recurring thing through all of this. And since it's so new in the space like something like this where you're getting hands on with it is super super important to develop your own intuition about these products on like hey there are not you know 200 person teams trying out you know what different text splitting looks like for the same data set and we come out and say hey look this is the best way to do it.
Here's the empirical research that says so. It's just like everyone's just like I don't know it works for me. Here's the vibes. You know this is this is what we're going with. What does overlap help? Overlap helps with the problem like I talked about like if you're saying like little red writing hood encountered the blank.
If you have overlap there you will have two separate chunks that have the same information in it. So you know that one of the chunks is more likely to have all of the semantic search for it or all of the semantic meaning in a given paragraph.
So if I have three chunks and they all overlap a little bit is much more likely to query a chunk and have all of the semantics that you need to generate a compelling answer versus just like hard cutting each each one.
Yeah of course. Yeah. Hi. I have a question in terms of the distance measurement. Based on your experience in what context or in what scenario basically is cosine measurement perform well enough? I that is one thing I haven't played around.
There's only I think two or three different distance metrics that you can use. I have not played with the actual distance metric changing the cosine or not because I've to me that is the most deterministic portion given that it's just the straight math on the cosine between these two vectors.
I'm just like okay I can change that and that will change everything downstream of it but I'd much rather have that be a constant and play with everything else.
Yeah. So
let's say we you know embed this document and we can go search against them for similarity but I ask a question says
goes across different chunks.
Sorry I guess I'm answering my own question. So in this case let's say I say tell me about bridge wise operations. Tell me about event emitters. Tell me about other things all in one question. Yeah. Then the number of chunks we retrieve from the the store will be will contain all those answers and then we give it to the LLM to answer.
Yeah. So his his question for those of y'all who didn't hear is hey what if I ask so we have all of this information from NDN. What if I ask it about multiple things? What if I'm asking about bitwise operations and CSS and events all all in one question?
What does that look like for the retrieval? And the process is the exact same. But you can think of this similar to a search result in Google where it's like okay if I'm asking it about bitwise operations and I'm asking about the event emitter I'm not going to get as clear results as maybe I would like because the LLM is doing the same thing where it's going to do the cosine similarity and it's going to find documents that relate to all three all three of those things and it will generate you an answer for it but it will probably not be as information rich or as useful as if you had just asked it about the one thing because the because we fit three subjects three different semantic meanings into the same kind of chunks
the because the you know if I have 1800 tokens to use and it's all related to CSS I can have a much higher confidence that I found the best results versus if I have to divide that by three I'm suddenly much less confident in my ability to give and provide you a robust answer.
Yeah. So in practice would that mean that you run a three-step like asking hey I have documents that are one document per technical concept for example here's a question break down the question into its components and get me one documentary component that's yeah yeah that that would absolutely be be at least I haven't tried that but that that sounds to me a very reasonable approach on how I can separate like hey take this and give me the three semantic meanings and then those are all going to be three separate I want to create context for all three of those questions and then stitch all of that back into one one response for the user.
And so that's where you get a lot of these new products that you're trying out. People say like, "Oh, that's just a wrapper around ChatGPT." And it's like, "Yeah, well, I mean, adding, you know, 6 to 12 prompts around ChatGPT is going to create like a meaningfully better user experience for whatever vertical you're in.
Like that that is going to be helpful and people are going to get better results using your product than the ChatGPT straight out of the box."
And cool. That's that's it for now. We're going to take a 10-minute break. Go ahead and get some snacks. Get some water. I'll also still be I'll still be here. I'm happy to continue answering questions, but you know, shake everyone's hand, you know, get stretch your legs.
You know, we got another hour, hour and a half before the next break. I have a question. When you generate these question embeddings, is this is like the size of is it is it generating just one embedding for this question?
Yes. Yeah. It's taking your question and it is generating an embedding for it so that you can then perform that cosine distance search. So I thought the embedding would be you know maybe maybe I'm mistaking embedding for tokens.
So embedding is bunch of tokens. Yeah. So if you look let me
Cool. Allright. So we've had a lot of Q and A, so I'm going to try and speed up this portion so that we can kind of get through the rest of it. We're going to do code generation, image generation, and then hopefully if we have a little bit of time, we'll do the text-to-speech or speech-to-text.
Code Generation2:01:39
But what we'll get into. So everything that we've done so far with the initial implementation and the RAG has been text. And code is just another kind of text, but it is a super powerful one. And so for this, we're also going to be upgrading from the GPT-3 model to GPT-4.
GPT-4 just is significantly better than GPT-3.5. It's not even close. Here is just a graph of these are different AP tests on GPT-4. GPT-4 with you can't really see super well given the projector, but there is a very slight edge for the GPT-4 that has vision.
It's like a very slightly darker shade of green. But you can just see compare the blue to green. You're getting large performance benefits on reasoning, which is very important for code generation. And they're also quite good at code generation.
Something that we didn't really talk about so far is how it actually generates the answers. What it does, what the LLM does as it generates answers is it predicts, hey, what is the next most likely token given my training set?
So if I have
code, code has very repeatable meanings, which means it is much easier for it to predict like, okay, I had an opening tag for a div. That means every other single time I've seen code, it's had a closing tag for the div.
It is much easier for it to predict that. And it is very good at doing code generation versus something that is like creative writing. It's like, okay, yeah, it has a lot of creative writing examples, but it has so many different paths that it can diverge on.
Where if it picks one crazy path, each bad or crazy token that it generates essentially sends it on a parabolic curve on like generating more and more crazy tokens. Where code has a lot of, I don't know why that's not bolded, sorry.
But code has very strict rules and it's very easy for the LLMs to reason about. So code generation is very fast, it's very cheap, and is generally deterministic. And for us as engineers, it really augments your developer capabilities.
Even if you don't embed AI directly into your application where your users are calling out and making use of LLMs, it can help you as a developer write a lot of boilerplate because it has context on everything. I know some of y'all may be working on AI startups where it doesn't have all this training context on like newer technologies.
But a good example for code generation is if you use Rails, it has so much information on Rails because it's been around for the last decade. If you ask it to do, I don't know how many Rails developers are out there, but there's this device package that adds essentially login functionality.
You can ask it to tweak a device configuration with no other input and it will give you like the six different files that device auto generates for you because it just knows like, hey, this device package has all of it.
It has context. It's incredibly powerful just to speed up the code that you are writing regardless of whether or not that is AI augmented code or not. And is a big part of this very famous slide of software 3.0 where software 1.0 was handwritten code where that's what we write.
We write C++ programs and compilers. And then as the last 10 years have kind of come around, we have software 2.0 where we're no longer directly making code changes to the actual output where it's like, hey, we do this and that happens every single time.
Now we're like feeding new data sets into machine learning models and generating new outputs. So we're kind of more indirectly tweaking the outputs because we can't just like get into the weights and tweak things and pretend that we know how these neural networks are actually working.
It's a much more of a black box style. And software 3.0 is like kind of where AI engineers are coming in where it's a combination of one and two. And so really what we're going to be doing is creating a few-shot learning prop.
We're going to ask it to generate some code and optionally you could take that further and run or render that code. A few-shot learning prompt is essentially a prompt that gives the LLM a couple of examples where it's like, hey, if this was the input, this is the output that I want you to generate.
So you give it one or two examples in the prompt and that weighs quite heavily in future prompts on like, hey, they asked me to generate code that looks like that. So I will continue to do so without prompting.
So what we'll do is we can move over here back to our app.
And we will go ahead and we'll go to the step two branch. So we can get check out step two. Cool. And from here we now have our source folder and you'll notice that there is, let me move this over.
So like I said, we are going to speed it up a little bit so that we can get straight into the demo and then asking more questions. But essentially what we've added here is this code prompt. So we say, hey, here, this is the prompt that we're adding.
So it's, hey, here are two input-output examples for code generation. Please follow these. Please follow the styling for future requests that you think are pertinent. Make sure that all HTML generated is with the JSX flavoring because that's what we decided to do.
So we're doing inline styles. So like if I'm asking for a blue box with three yellow circles that has a red outline, I generated some HTML that did exactly that and I feed it the response like, hey, that is what I want it to look like.
I want inline styling with JSX. Cool. And we do that again. We say, hey, here is a red button that says click me. And we add a red button that says click me. And from there we add an additional system prompt that has all of the code prompt in it.
Cool. And then we add this additional code generation function. Again, takes the update and the context. We take the user's text and we get a completion. So this is the exact same thing as the chat, but we have added the additional messages for the code prompt in here.
And on the completion, we have changed the model from GPT-3.5 Turbo to GPT-4. And then we send that back to the user. And then same as the other ones, we create a command handler with the code and we have a code generation and we go from there.
So what this looks like once we have it implemented is, let me just pull up here. There we go.
And so we'll wait as it does that numpy array on the embeddings. Allright. I know that takes a minute. Cool. Our application has started. And now if I say code, generate me, let's say a red box with a blue button that says yes.
We would expect for one to get an accurate representation of that. We assume that it knows how to do a red box with a blue button that says yes. It has enough data to know what the CSS for that looks like.
But you can see that it adheres to that same style that we had earlier where I didn't ask it to do JSX. I didn't ask it to do inline styling. It was in the prompt and it knows, hey, this is what I want it to do.
And so you can tweak the prompt and the few-shot examples to whatever you're working on. If you aren't in a JSX code base, you could very easily change this like, hey, I want this to be view templating. We use Nuxt at work and so we want to use that for our code generation.
You'll see there's a lot of jumping off points here. I kept this very similar or very minimal, but if you go through the newsletter course, you'll actually see we get into function calling, which essentially allows us to do things like generate images based off of this.
So I could send it, hey, given this HTML, I want you to generate an SVG string and then we can pass that SVG string to a PNG converter and send that image back to the Telegram client. So you can really start using code generation to really augment your workflows in ways that are going to be incredibly productive for you.
And even if you don't do some crazy example like that where we're turning HTML into an SVG string and then turning that SVG string into PNG bytes and sending that back over the client, that's like quite involved and has a couple different steps and the AI can break down at any of those steps.
It is non-deterministic. But you can immediately stop writing that. I know I do not enjoy writing things like that. It's quite annoying to have to write background color red padding this. I can just say, let's see, code, generate me a, let's see,
we'll say a, this will be fun, a div with the text saying centered that is in the center of the page because we all know nobody can center a div.
And this will go through and we'll say, hey, look, we have the display flex, justify content center, align item center, position absolute. That's, you know, it's not perfect. It is going a little crazy. We've got position absolute with our top and left being 50.
But it does have, you know, it is centered. It did what I asked it to do. And so you can see really let your imagination go wild with the ideas that you can have here. This is like HTML rendering, but it's very useful for backend engineering if you're trying to convert different bytes back and forth, different formats.
It's very good at doing all of that. And so this is like really something that you should be digging into and really pushing the boundaries on because you'll be quite surprised at how long these prompts you can do and still get very reliable results going back and forth between these things.
And like I said, it is more useful with technologies that have been around a while. Like the Rails app that I used, for example, I did that a couple weekends ago. I did retrieval augmented generation and I did it in a Rails app.
I have not written Ruby before in my life. I have never deployed a Rails application. I kind of know it's like I'm a programmer. I can see what the syntax is doing, but I don't know any of the kind of conventions of what it looks like to do Rails programming.
But I just asked it one at a time, like, hey, I want a logout button that does this. Can you do that for me? And it sends me all of the templates that I need. It says, hey, run these three commands on the CLI and it will generate these files for you.
And I sent it to my old boss that runs Rails and has been doing Rails for the last decade. And he's like, I wouldn't change anything about this. Like, I am happy with the code this looks. This follows.
This is very idiomatic Ruby on Rails code. And it knows that because it's trained on really, really good code and it's quite good at this. So really, I know it's a bit of a trite example, but you can just imagine any code that you don't enjoy writing, try getting ChatGPT to do it for you.
I promise you will not be disappointed with the result. And let's see. And this is actually, I'll get to you in just a second. So there's a couple homework and bonuses. You can see here in the link, and like I said, I'll post the slidesright after the workshop for everyone.
But the function calling for your own use case, this is what I was able to generate, what I was just talking about with the PNG file. You can see the code for that in that replit application. But you can see that, hey, we asked it to, it doesn't do it perfectly where I said, hey, I want you to have a yes and no button.
And I asked it to convert that to an SVG string and it gave me a blank screen. I said, no, that's wrong. Try it again. That's a blank screen. And I'm like, okay, no, that's still not quiteright. Let me send you the prompt again where this is a green yes button and a no red button.
Please render that for me. And it does so. And another thing that if you haven't spent a lot of time with it, you wouldn't knowright off the top of your head, but GPT is not always the greatest at doing math.
And you can see we essentially added function calling to generate. We asked it in the eval, said, hey, generate some math that I can pass to the Python eval function. So yeah, no, definitely don't add that into your production apps or anything, you know, but it is fun as an example to have like, hey, you're not good at reasoning about math and just text.
It can do basic arithmetic if you ask it like, hey, what is three plus four? It'll say, yeah, the answer is seven. But it will get things wrong sometimes hilariously so. And you can ask it, well, you know, what is really, really good at math is code.
Code is always good at math. I can ask you to generate code that runs in the math and then I can send that back to a function that I made where it said, hey, I asked the LLM to generate me some code.
I have a function that I can then call and say, hey, look, I have some code. I want you to just eval that and print it out back to the user. And you can see that at the bottom where it's like, hey, can you tell me what this random string of arithmetic is?
And it says like, hey, hey, show me the function you called to do that math. And it has that math.evaluate at the bottom. I know the text is a bit small, so you may not be able to see fully there.
But really, you can get a lot of code generation allows you to augment and really kind of close the gap where GPT is bad at something. You can say, hey, look, I know that you're bad at math, but I can give you access to a Python kernel.
So I think you'd be pretty good if there was a Python shell and you could just anytime that you're like, I'm not 100% sure that I can do this, just send it out to there. And Python is quite, quite good at doing that.
So we can just do that instead. So you can start to use these ideas of code generation on how to extend the functionality where GPT and its counterparts tend to fall off the map. So I think those are all super valuable use cases that you can start getting into.
And if anyone has questions, yeah.
I know she's using the chat completion. OpenAI's chat completion. What if you're using a different, like I'm using Vertex AI and I was trying to look up if they have chat completions and a lot of examples have stopped chat completion.
Is that a standard? Is there a standard API around that? Or is it just OpenAI that has a chat completion?
I would say most places that I've seen have some form of chat completion just because.
It might not be called chat completion.
Yeah, it might not be called chat completion. Originally it might have just been called like completion, you know, where traditional completion would have been like they follow the yellow brick and you send that and it says road. It will just try to complete whatever the thought is.
You know, it's a very simple example, but it would just do that for each thing that you gave it to. It did not have the memory or context. So if you said, if I asked one question like, hey, who is Mark Cuban?
And it told me who Mark Cuban was. And then I asked him, what is his net worth? Chat, just traditional completion would not be able to answer that question because it wouldn't know who his is referring to. It would just be like, whose net worth?
Whose net worth are you asking me to? So that's where the chat kind of adds in as the context to kind of reference previous conversations. But some of them may just be called completion and it'll probably be similar to that.
Yeah.
So getting good results from this kind of depends on a good prompt. I know there's some good general prompt engineering guides out there, but is there a good resource for just example prompts for different purposes?
Yeah, I'm sure there are resources there. I don't have a good link for you that's just general like code prompting. But I know like if you Google any kind of like whatever specific thing that you're looking for, you can ask it to, you will find resources online.
And if not, something that you can actually do is GPT is quite good at helping itself. If you say like, hey, I know you didn't really give me a good response here. Can you tell me like what additional pieces of information do you need to give me this output?
And it'll say like, hey, I need X, Y, and Z. And then you can use that to kind of inform your next prompt on like, hey, I want you to do this. Like you see people, there's Kinsey Dodds has, for those of y'all who are familiar with him, he's a JavaScript educator, quite prolific, great guy.
But he has this prompt that's like, you know, two paragraphs long on just changing the default GPT interactions where he's like, hey, do not tell me that you are an AI chatbot. Do not say please, like leave all that nice crap at the door.
I do not care that you're being polite. I want you to be as short as possible. I don't want you to use emojis. I want you to get to the point on X, Y, and Z. And he has like, you know, like 30 bullet points or whatever that he sends it just to get better outputs.
And so it's really, you have so much flexibility there that any traditional like awesome list style resources can very quickly fall out of date. And I would really encourage y'all to start using this more than your traditional kind of like resource aggregation where like, hey, I want to learn about something new.
I'm going to Google it. I'm going to compile like three or four resources and then I'm going to spend the afternoon like kind of reading up and kind of taking what I don't like and leaving what I or taking what I do like and leaving what I don't.
Really try to just make a habit of asking this what it thinks about the various things and you'll start to develop your own intuition about it because the space is so new and the platforms that you're kind of relying and building on top of are changing so frequently.
You really need to develop your own intuition on like, oh, I think it would do better if I did X. And so that you'll kind of come from first principles whenever eventually the API changes or the model changes and there's things like that, you'll be well prepared to pivot and stay on top of your game continuously.
Other questions?
Image Generation2:20:14
Beautiful. Okay, cool. Well, then we will move on to image generation, this one I'm super excited about. So image generation, Dall-E kind of opened the way for image generation back in 21, 22. If you were really following AI at that time, I'm sure you have seen the avocado chair.
I would say it's pretty iconic for the AI generation era that really started. But just being able to say like an armchair in the shape of an avocado and get something that looks like that is like absolutely insane.
It's been two years now since you could do this and I'm still blown away that I can just like send some text of a description and get an avocado chair back in a PNG format is absolutely crazy to me.
That being said, there are quite a lot of problems with images as they stand currently. There's a lot of hard problems where it doesn't always understand context. Like, hey, here, I want you to generate me salmon in the river.
That'sright. That is salmon and it is in the river, but that is not what anybody wanted whenever they put that prompt in. I don't think anybody was expecting to get that back. And there's also hands. If you've done any work on there, there are some very cursed hands that you will see and they're just very ugly.
So much so that people will just add in their prompts that they're just like, there's this concept of negative prompts where the model can kind of reason about like, oh, you don't want this to be included. People just stop trying to generate good hands.
They just said, no hands. Like, put your hands behind your back if you're doing a mannequin, whatever. I don't want to see it. And it's kind of in line with hands, but it's counting. The prompt for this image is four people in a forest.
I don't really care what the forest looks like. I'm not being super specific, but I would like there to be four people. I only get four people in one of these images. A 25% hit rate is not good enough for production, I would say.
I'm not looking for 25% accuracy. It's also not very good at text. This is a prompt for a fox image and you can see it's kind of getting there. But this is like a long time of trying through.
Like, I probably spent 30 minutes trying to get it to say fox and this is the best that I come up with. If any of y'all are designers at heart, this is not cutting it. This hurts you quite a lot.
But it is being worked on. There are other models. The traditional image generation comes from a diffusion style architecture. Stability AI is making a model called Deep Floyd that uses a different architecture that is and does generate good, good models.
And you can see kind of tug and cheek. This is not Dall-E. It could not generate that. And you can see there's a lot of products that are being explored in the space currently. You know, there's essentially what is a hobby and what people will pay for is customization and personalization.
You had Lenza, you know, back in, I want to say last year was doing a million dollars a day selling personal avatars where you would upload a picture of your face and they would do it in different likenesses.
For those of you who know Peter Lovels, he has several AI apps that are generating by himself, you know, millions of dollars of revenue. Hasan, who I think is a speaker here at one of the days, is the creator of RoomGPT, which is an open source image generation where you can upload a photo of your living room and ask it to style it for you differently.
Where you can say, hey, I want like a mid-century living room. Here's what it looks like currently. And it does it for you. It's that image generation. Midjourney being another one where that's just like state of the art image generation where you can do quite a lot and tweak a lot with it.
All of these have either huge user numbers. This is an area that you're seeing a lot of normies use where it's just like, hey, maybe your mom doesn't get how impressive it is that you can say, hey, tell me who Mark Cuban is.
And it spits out a result. She's like, okay, well, I can Google that and Wikipedia tells me. That's like, that's cool. I guess they don't get the whole programmatic part. But if you show someone, I generated an avocado armchair.
That is quite mind-blowing because that is not something the average person can do. They cannot generate an avocado armchair even if they have a perfect visualization in their mind. There is not a way to manifest that in the physical or digital world for most people.
So huge, huge lessons to be learned on where you can really differentiate yourself for all of these products. And with that, we're going to get into image generation. So this is also pretty straightforward. So we'll get check out step three.
Cool.
And from here, we can now go back to our main.py file and we still have everything the same. The code for this is super, super simple since we already have OpenAI configured. We already have it already. Here's everything that we had.
We're just going to add an additional thing called image. Oh, there's also, if you are doing this on your own, you do need to import the requests package. Like I said, everything is already installed. You just need to add the import there.
So what we will do, let me go to image and let's move this over. Cool. So what image does is it is still that OpenAI package. So it's just OpenAI.image.create and it has a prompt. The N is the number of images that you want it to generate.
I think it goes up to four. I could be wrong on that. It could be more along with the size. I think it does 10 by 24 and 512 by 512. Those are the ones that I would recommend that you start with.
Probably just copy this if you're doing it on your own. And what the response gives is a URL where it has the, some Microsoft server has a URL that you can then send a Git request to and get the bytes for the image.
And then we have this context.bot. Instead of send message, we have this send photo function that again has the same chat ID, has another argument called photo. And we can call the image response.content will render the image. And then it's the same thing as before where we have an image handler.
So anytime we do slash image, we will pick up that response and then we tie the handlerright back to it. So with all of this, we can now generate images in our bot. Kind of going back to the code generation part and the function calling, you could actually, with function calling, you can think of
function calling, you may think like, oh, OpenAI is going to invoke a function that I give it. That is not what it does. What it does is it generate, you describe a function. So let's say I had an add function where I say, hey, I want to add two numbers together.
It will generate the arguments for that. So if it thinks from the context of the conversation that I want to add some numbers together, it will generate the arguments for me. So it would generate a two and a four and it would tell me, hey, I thought you wanted some numbers generated and here is the function name and you can infer that and then actually pass that into the function that you made.
So I can have some add function that adds X plus Y together. And because of that, you can add every single one of these handlers as like a homework assignment for y'all. You could turn every single one of these into just the chat handler and you could just have it infer what you want it to do.
So you could say, hey, instead of me saying slash image, cute picture of a koala hugging a robot, I could just forego the slash image and say, hey, OpenAI, I'm going to trust you whenever I say, hey, I want a photorealistic version of a robot hugging a koala.
It kind of knows, okay, they said photo and they gave me a description. I am going to generate a photo for you. So you can use function calling to kind of add smart if-else statements. That's how you can think of function calling really is like, hey, if I could have, if this weird esoteric example, you know, do this and the AI will be able to do that for you.
It's a bit to stitch together, but there is a link in like two or three slides back of the actual function calling implementation up on that replet. I think that is an area for all of y'all where you'll definitely get some clicking moments there and we might be able to walk through that if we can get through the rest of these.
So really quickly, let's go ahead and get images going. So we can say, hey, here is the main.py file. Let's go ahead and run that. And well, let me actually make sure that it's running before I give it a command.
Cool. Application starts. So now if I say slash image, let's say the iron giant sitting in the forest 4K.
And so what it is doing, it picked up on that slash image and it sent the iron giant sitting in 4K to the prompt in that image file that we just made or image function that we just made.
And you can see, you know, Dall-E, so this is another thing to be cognizant of that OpenAI is very dominant in the text space and their models are very good. Like you're not going to beat GPT-4 at just about anything, whether it's code generation, text generation, like you're not going to get better results than GPT-4 when it comes to text.
But Dall-E is quite dated. So this is sending to Dall-E 2. I know that they did Dall-E 3, which is they're kind of rolling out currently that has kind of ChatGPT with vision. You may have seen some of that across your timeline, but just know that like this is actually quite dated at this point.
And there are other softwares like Midjourney is quite good. Stable Diffusion is also quite good and is something that I would really encourage y'all to work on because unlike the OpenAI models, like you could not run that on your computer.
It is way too big. But Stable Diffusion is pretty much a state of the art open source model that is small enough to run on your computer. So this can really be a taste where you can run things on your own machine and really start to get some of these unlocks.
There's a bunch of UIs that will kind of run on top of, but that's definitely something, an area that I would like y'all to explore. But just know you may see like, okay, this is not as compelling as, you know, if some of y'all have seen the Pope wearing Versace.
It's like a very popular one that did the rounds. It's like you're not going to get that from Dall-E, but you can get basic images. So we'll just say a koala sitting in a tree. And images much, much more so than prompting or than the text-based side of things.
Like prompting is so, so important because you can't just go back and change this. Like you can, if you get a bad prompt, you can copy-paste that into notes and, you know, make your changes. That's very easy for most of us.
We do not have the skills to put the iron giant in this forest in a way that actually looks good to everyone. So it's very important that you are very specific on the prompts to get the best possible results.
And so you can see it does real life things quite, quite a bit better. So you can see here we have a koala that is in fact sitting in a tree. I'm quite happy with that image. And this is something that I really, really want to get y'all hands-on for.
And so you can see this was the whole implementation. We called to Dall-E to generate the image. We connect the image to Telegram and we profit. Cool. And now I'm going to ask y'all find a group, either a clustering of chairs.
I would say find three to five people. So if you're on a couch, just stay on your couch and say that you are a group. Find a group and have your Telegram up to this point where you have that Git step.
And we're going to spend 10 minutes where y'all are just going to come up on a team and generate some images. And so really spend some time, get hands-on and generate the best images that you think are possible.
There are a couple of prompts here where commas between ideas are good and describing the setting more than the subject are good. And there is also a link in that Telegram to a bunch of really good image generation prompts that will make it look better.
So go ahead and spend the next 10 minutes and we're going to generate some images.
Wow. That's, wow. Yeah, that's really good. Yeah. Can you even scroll it? Yeah. Isn't that crazy? Dude, how does it even get that? Because I mean, some of them were wrong, but like it got all of the handlers.
That's insane. That's insane. That is so cool. If I give it any more context, it'll start to really eat. Yeah. Yeah. Say, hey, actually this is a Telegram. It's going to be, oh, let me adjust all of it.
That's insane. That is so cool. Yeah, vision is nuts. Nuts. Yeah. Yeah. It's like I went back and forth. I'm like, I wonder what would happen if I, like, it was like building the workshop. It's like, how much new stuff can I put?
Like function calling is like, it's like kind of, kind of new. This is like really perfect, I think. Yeah. Oh, thank you. That actually somebody, like you, like Google engineer, the guy over there, he's like, yeah, I'm trying to think about what you can do with this.
And I'm like, well, fucking yes. Yes. Yeah. Yes. Yes. But like, he's like, we'll just get it done. Like, yeah. Exactly. Exactly. Yeah. Because yeah, you can start talking about Dall-E 3 and everyone's going to be fucking around chat being for a bit.
Yeah.
That's pretty crazy though,right? Yeah. Just I really wish images are pretty near and dear to me. I probably have put in like a hundred plus hours into like Midjourney prompts. So like I have, like it's, yeah. Yeah. It's like I really love that.
And it's like I put in these like really robust prompts that get me like good, like I can get almost like logo quality stuff out of Midjourney. And it's just like I try to do that with Dall-E. It's like.
So when people come on into our team, we do like a thing where I'll ask them four questions, like the hero or villain, like fantasy or sapphire, and then I generate like a character. Oh, that's very cool. It's our CTO.
I got him, I made this, his answers were really great and we made him this, it turned into this like elephant, like magic elephant guy. Oh, that is sick. So then the runaway ML's latest animation. So I dropped that into the elephant, into that.
Oh, that's nice. Yeah, that looks a lot better. I played with Mage a little bit and it's not that smooth. It's not. Yeah, they're just like, watch their latest. Yeah, yeah, yeah. But yeah, like, but ChatGPT vision, do you have access yet to it?
I don't have access yet to it. Fucking insane. Fuck with it. Like, yeah, I'm happy to let you play with it, but it's like, I'm dumping in all kinds of crazy ass diagrams in there. Yeah. Dude, diagrams is something like more people need to play around with because it's like, I did for the newsletter portion, there's like the function calling flow is like a bit convoluted.
And I just asked, I was like, well, hey, here's the code. Generate like a mermaid diagram from that. And yeah, it's like the ability for us like, well, you're bad at images, but I have syntax that converts it.
No, although I need to, I need to, you see, I'm in Neovimright now. I need to move. Because you can add, you add documentation to any technology source and it index it automatically from a link. Then you can reference that documentation in your code base.
And what I've been doing is like, I'll in Cursor, I'll, because it can, it can reference my code base, I'll then have it populate mermaid or UML based on my context. Oh, yeah. That's good. That's good. That's onboarding 101, man.
That would be so nice. What I'm doing it too, especially is like I'm pulling other, you know, GitHub repos for like complex agents and shit like that. People like small or something like that. And then dumping that into Cursor and then having it generate architecture so that I can visualize it better for my team.
That's awesome. Like insane. Yeah. Yeah. It's so good. Yeah. I love Cursor. Cursor is unbelievable. It's great. It's great that it's like, it's like VS Code, but it's like, it just uses all of it. How's the performance? It's fucking amazing.
Yeah, it's good. Yeah. No difference. It's amazing because it gets all your plugins and everything. Extension is all set up. You mentioned how ChatGPT doesn't know a lot of the stuff that we're working in. This solves for that.
Yeah. Yeah, absolutely. That's one of the biggest things. Yeah. And it will literally cite, like it'll cite the piece of documentation that it's referencing. Yeah. That's, that's really good. You know, I'll do it. I'll do it tonight. Tonight.
Tonight. Tonight. Because I've just been like, I've been watching it over the last month, like every like four days. It's like, you should, you should use Cursor. You should use Cursor. Be prepared to lose a lot of productivity.
Because like, I mean, that's what you should be working on. Yeah. Are you paying for the monthly? Yeah. But I've been, I use my, I've done my own API and I've done tons of shit like that, but I'm doing it to support them too.
Yeah. Well, no, for sure. I put my key in. So I haven't been paying again. I just put in my own API key and then my usage just went up. Oh, I spent 140 bucks. Yeah. Yeah. I was like way over 100 bucks.
But normally I'm like 5 to 10 bucks a month, you know? Dude, I struggled. It's like, I struggled to get my usage up, man. Just from like following all those guys and chatting with them on Twitter and stuff, they're like, they're, you're spending more.
So like the 20 bucks a month is a deal. Also, I know that they're doing because like they're staying on top of it. Like I know that they're starting to use like GPT, you know, 3.5 function calling, fine tuning to make sure better.
So they're like using model specific for different outcomes. So I think like not only is it cheaper, I think, it just makes sense to make the product. Yeah. Yeah. Yeah. And like 20 bucks for me is more than enough to support what they're doing.
Yeah. There's no way we're going to do that. Yeah. And like just adding the documentation, that's the biggest thing. Go through and just keep adding all of your. It's been a bit of a whiplash for me because my job, job is in crypto.
And so it's like the cost of playing, asking someone to play around, play around with that is just like immediately when I was like, oh, well, I sent my grandma some money and it cost like $6, you know, and I sent, it's like I sent her 10.
It's like, okay, well, that wasn't, that wasn't very fun. I swear. I'm like, here's, here's a two and a half hour workshop where you're generating images, doing transcription. It's like, I tried to break a dollar. Like, I dare you.
I dare you. Like, you were going to spend all day. You could do it on GPT-4. Four. Yeah. Yeah. But, but like, and then especially if you're starting with GPT-4 32,000, which is really helpful for Cursor. Yeah. Oh, yeah.
As soon as you have the GPT-4 32,000 API and you put that into Cursor, like when I'm doing code base searches and like full index searches on the whole code base on like a bigger file, I'll turn that model on and then it can, then it's like, then it can really start to comprehend shit.
That's huge. Oh, man. You have all the nice things. It's crazy. Well, you can use, you can use, what's it called?
Allright, everyone. 10 minutes is up. I hope y'all got some images generated. We are segueing straight into a break. So if you want to generate more images with your friends, that is cool. Alternatively, please, I want y'all to add me on Telegram and send me your favorite prompt.
I will be going through tonight and I will add all of the images that I get into that big chat so y'all can kind of see what everybody generated and hopefully y'all can get some big inspiration going out of that.
Yeah. Generate more images. Add me on Telegram. Drink some water. Go to the bathroom.
You probably just had your first baby shower two weeks ago. Hey, that's cheating.
Really?
This workshop is really great. Oh, thank you. Everyone's asking so many questions. Yeah, I was, yeah, I was like, that was my dread. Like I had, I had the Q&As all planned out. I'm like, I was like in my head, I'm like, I hope five minutes.
Yeah. I was like, I hope five, because five minutes is a long time. But it's like, it's just nonstop. You're hacking on your brain thinking about how to use this, how to break this, everything. Yeah. The rag questions were insane.
Amazing vibe game. It's like, you got this vibe and like, it's just great. Yeah. I'm quite, I'm quite happy. Create a little bit of a few hours of magic here. People are just so engaged. Are you trying? Yeah.
I'm very excited. Yeah. Thank you. Thank you.
Like I'm like here. Do I have, do I have like a hard, hard stop at 12? No. No? Okay. Cool. I want to get through this one. This guy Mark was speaking after you to write him a piece here.
Okay. But he's very, he's very flexible. Like I told him just, he has like a 10, 15 minutes off. The rest of the time he'll work pretty much. Okay. Okay. I won't go over very long. I just don't think, there's like five minutes left on break.
Okay. Cool. Then just like when you're done, just end this with me. Okay.
I was like, I never got. That's cool. I was trying to think how I can apply this to my work. Database stuff. How can I apply this image generation to my work? I want to. Hey, thanks for the class.
It's been great. Oh, yeah. Thank you. Thank you. If he's been positioning and asking a ton of questions. Yeah. Yeah, of course. I have a ton of questions. Like the first and foremost, I almost wanted to interrupt you when you were going through the code generation because there's kind of been a few new issues.
You know, when you format all the code and then put it into there's limitations on the architecture that it can reference. And then I found weird issues with that, plus like where the code might exist in their conversation.
Yeah. And I don't even know, it gets to the point where I don't even know
what code it's referencing. Foundation model for the code that I have been abe. Yeah. I would say for the code generation specifically, like I have never hit context windows on code generation. And so what I would say. Context window limitations were.
Yes. Okay. And by the way, I don't expect you to have the answerright now. No. Yeah. Yeah. No, you're totally fine. Yeah. At 101. Well, there's everyone that has specific answers to everything and it's all about talking about it.
Anyways. So I'd say
for me, usually if I'm doing code generation in like a non-purpose, like it's not embedded into my app in some way, like I'm just in ChatGPT. And so it's really important anytime you are diverging on like a different code path, that's a new conversation.
Yeah. So just making sure that you're always, you know, hitting new chat because I think a lot of people just like they have a question, they get answered, they have another question, they answer, and then it's like you have the messages array essentially in here and it's got like 12 different questions in it and that it is, it references that more heavily than it's base training.
So naturally, like your quality of answers goes down considerably. And so if you're saying like, hey, I have some files over here that are doing, you know, like byte processing and then I'm going over here and I'm doing some encoding, it's like it very easily gets its wires crossed.
Yeah. And especially if you're referencing code that you're, you're, yeah, it gets weird. Even when you're trying to fix a bug or something like that, you don't even reference the code that you're talking about. I think Replit's going to solve it because of.
Yeah. Yeah. Replit is definitely going to make some strides in that area. From like labeling code to be something versus something else. Yeah. And code interpreter on GPT-4 is also a really big unlock if you have that or not.
Because if you can do file uploads, I found it to be, it could be frustrating at times, but it started to use it. It's pretty good. That's what I've heard. I just, it started using it and I'm like, I actually have had my own just use cases.
Yeah. Yeah. Yeah. Totally. And then my use case is like in an enterprise, I haven't been able to use code. Oh, yeah. I haven't spent as much time on it. Yeah. So I've been only using like Starcooder. Okay.
And then fine tuning on top of Starcooder. Yeah. I would, I haven't heard of Starcooder. It's one of the new, it's open source, it also allows developers to opt out. Yeah. Okay. Yeah. Do you have, have you tried running Llama or Llama 2?
That's probably, that's like from what I've heard is like state-of-the-art open source models is Llama. I've done that but only unstructured text. Okay. And yeah, I don't have hands-on experience for code generation. I guess another question, we don't have time for it.
Later in the conference, I'll ask you a little bit about it. Embeddings for structured and unstructured text at the same time. Oh, yeah. Yeah. That'll, yeah, structured versus unstructured is like it's combining them. Yeah. Yeah. It can be.
It gets out of your, it gets out of my wheelhouse. Yeah. Yeah. Yeah. Totally. Well, definitely. Well, I'll have to catch up with you more. I want to answer his question. Hey, the code is timing out. Example code.
And the answer is, you're sending the image to the bot. That's timing out. What's, what's your? Yeah. No, what's the terminal? Like, what's the, is it just? It's saying, cannot post to Telegram, blah, blah, blah. But if I change that and say, okay, just post the link to the image, this will appear.
Oh, interesting. So just, just for you to know. Yeah. Okay. The code, I mean, I couldn't make it work as it is. Yeah. Okay. Cool. Thanks. Good, good, good that you played with it. And I'm not, I'm not sure.
Yeah. Yeah. Yeah. We'll see. Awesome. Thank you.
Image generation. Yeah. Have you seen anything in the way of like an uncanny value detector or like. What do you mean? I haven't heard that. So there's this whole thing where like sometimes they come out and it's just like something's off.
You don't exactly know what, but it just feels like it. Yeah. I'm like, I don't really know the architectures of how they like smooth things out before they come out of the model. Yeah. I was curious if there's been, if you're aware of anything that like will take a finished image and like make it more normal.
Yeah. You know what I mean? Or just like.
Not currently. So like the kind of like diverging on like, hey, you are generating this and that seems a bit weird. We're going to change course. I haven't seen a ton outside of just some like not safe for work filters where it's like you are generating a nude personright now and I need you to like slide some clothes on them.
Yeah. It's like I need you to, you know, diverge courses. And I know that usually is like an end step where it's like, hey, we didn't send it to the user yet, but we have a fully finished image and we're able to run on it and be like, okay, I detected that there's nudity in here.
And so we're going to take this image and do it again, but like add clothes or whatever. And so it'll, it'll kind of tweak and then send that to the user. Okay. So that's like kind of the safety that's happening behind the hoods.
Yeah. Yeah. On some of them. Yeah. Allright. Cool. Thanks. Hi. Maybe you said that, but is it going to be done? Yeah. Yeah. So I've got about 10 more minutes and we'll be wrapping up. Yeah. Allright, everyone. Break is up.
Speech-to-Text2:50:45
We are running a little bit tight. We're going to speed run our speech to text. I'll make sure everybody gets some food in them and we are able to finish the course.
If you do have any burning questions, I will be around afterwards, but I do want to make sure that we continue running on track. I don't want to run late just because of me. So we will go through speech to text.
Speech to text or automatic speech recognition or ASR systems are what powers things like Alexa, Siri, hey Google. And what's really fun about it is it's very interoperable because once it's text, you get to plug it into the rest of your stack like we did for everything else is regarded to text.
So if we can turn voice into text, we can probably do some cool stuff with that. Just lots of things to be aware of. It has, this is the Whisper architecture. Whisper is kind of state-of-the-art for these ASR systems.
You'll notice that Whisper is an example of OpenAI actually being open. It is an open source model that has a lot of different kind of, you know, the open source community has definitely taken and ran with it since then.
And so this is really how it works just really quickly. And so one thing to note is Whisper or Whisper 1 uses transformers where previous models used recurrent neural networks or RNNs. One of the reasons that people think it does better.
So differences is like on training. Training these models is a little bit more difficult. If you just think about how, how easy is it for you to find a bunch of text on a subject versus high quality audio?
That's a lot harder to find high quality audio for things. Training these is very difficult because not only do you have to find high quality audio, you also have to go through this audio labeling process where you feed it all of the inputs and say like, hey, this is what this is so that it can pattern match on that in the future.
Similar to how we talked about, you cannot run GPT-4 on your computer. You can absolutely run ASR models like Whisper on your own machine. This is very fun and it's something I encourage you to do in your own time.
Here you'll notice Whisper dominates everything in the landscape. So you can play if you're watching at home or doing anything. You start really with Hugging Face spaces. That's you can just go find a space on Hugging Face. It has inference on it.
You can talk to that webpage and get a response back. You don't have to sign up for a whole bunch of stuff. That's like the lowest friction way. We are going to be using the Whisper API since we already have that configured.
But generally nobody is using the Whisper API because why am I going to pay you when I could run it myself? You know, I'm not going to set it up on your machine, but I would highly encourage you to, if you want to run it on your own machine.
So you can either run the Whisper model that they open sourced or which I would recommend is you can use the Whisper CPP model. Somebody took the Whisper model and rewrote it in C++ so that it would run on CPU cores better.
So that is the machine that you want to use if you're running it on your own machine. You're not going to get much better bang for your buck there. And there is also a last one you can look up is Whisper X with diarization.
Diarization, if you haven't heard that word before, is the act of determining speakers. So if I have a podcast between me and my amazing helper Justin and we upload that to the internet, that's a single MP3 file. It's hard to tell the difference between me talking and him talking if you're not listening and you're just looking at the audio waves.
Diarization is the act of breaking down that MP3 file and labeling it. Hey, this is Noah talking and hey, this is Justin talking. That is diarization. And somebody has added Whisper X. I think they're a PhD student that is like doing their thesis on this.
You love to see academia and open source crossing. And then there's all of these links do the other way around where you can do text and turn it into speech. That's another very fun one. All of these are various platforms that you can do that on.
Tortoise TTS is the open source leading in that category. And so for that, we're going to get straight into the implementation for this. So you can see we're just going to do get branch step four. Cool. And so what this does is Telegram has voice functionality or voice memos built into the platform.
So the usage is we are going to record a note on Telegram and we are going to send that to the Whisper server and we are going to get the transcription back and send that to the user. So what that looks like, if we can move this over here, we can do, we can run.
And we will look at how we do this in the actual code. So if we go to
here, we have got the code generation, basic config chatbot.
Hello. Image code generation question. Did this not add it? That would be quite annoying.
It's in my step four. It's in your step four. Then maybe I just did this wrong.
Oh, yeah. I don't want to get branch. I want to get checkout. That would help. Cool. Thank you.
Okay. And you will notice here we are saving all of these voice notes on the server. These are examples of me talking to the chatbot. So we are saving that voice note on the server and then sending it back.
So what we get here is going all the way down. Here we go. Transcribe message. So again, like all the other ones, update in context. We want to make sure that we have a voice ID. We can check that by again going to update.
Update is where we have all the contents of the conversation. We're going to check the message and see if there's any voice in there. And if it is, we want to grab the file ID. So we now have the voice ID and we can turn that into a file by using the bot.get file method and plugging in the voice ID.
Telegram will then grab that file on their server and send it to us. And then we just download this file has a download to drive method. And so we have, we call it voice note and then we add the voice ID to make sure we don't have any naming collisions on our system.
And then it is a .ogg file. And at that point, there's going to be a little bit of latency. So this is just a way to kind of make the UX a little bit better. You'll notice we haven't gotten to streaming at all.
If y'all have used ChatGPT, you notice whenever you ask it a question, you get to see it. It's a much better user experience than saying, you know, who is Simon Cowell? And then waiting 10 seconds and then you get a huge block of text.
Streaming is, I would say, table stakes if you are creating an AI product, especially if it is text-based. And if you are not able to stream the response to the user, you need to do something to let them know they're like, hey, we got your request and we are working on it.
Just traditional, you know, UX things. Let the user know that they did something and that you are working on it. So once we have that downloaded, we have the audio file and we are going to use the OpenAI.audio.transcribe method and we are going to use the Whisper 1 model.
There is only one model that the API supports and it is Whisper 1. And you're going to plug in the audio file. And once you have that transcript, you can say, hey, transcript is finished and we are going to send that back to the user.
And you'll notice here that we're using slightly different. This is not the context.bot that we've been using. We are replying to the text message because we sent them a message and it looks a little bit nicer in the, we're just kind of like adding layers to it, making the UX a little bit nicer.
So with all of that, oh, there's also the handler is a bit different for this. So we have, we go back to the message handler instead of the command handler and we use the filter. The filters object has the voice.
So we are listening to any message. They don't have to say slash voice or slash transcribe because they're not sending text. They're sending a file. They don't have a way to let you know. So any message that has a voice file, we are going to transcribe that message using that message handler function.
And what we can do here is we could go ahead and run this. And instead of running it on my laptop, we'll see. Let's actually make sure that this runs. As I will use my phone here and I see 38 Telegram messages.
Thank you all so much for sending me your images. I will be going through later tonight and posting all of them in this chat so that y'all can see everything. Y'all's participation today has been really amazing and thank you so much for that.
So what we will go through here, let's go over to Telegram. And on my phone, I'm going to say, hey, my name is Noah Hein. I am in front of a bunch of upcoming AI engineers, rubber baby buggy bumpers.
Cool. We send the message. It's downloaded. It's transcribing. The transcript is finished. And although it spelled my name wrong, H-I-N-E, I will take that. That is a beautiful transcription doing that directly out of the box. And you can just, your mind can go wild here.
Something that, like, if that does not blow your mind, you're not thinking crazily enough. How hard somebody would have had to work to get that working, you know, a year ago, two years ago. Like, that is not possible.
And now you said, oop, OpenAI.transcribe. Are you kidding me? That's insane. That is so cool. And you're seeing, you know, you can, you will see in the future much, much more interactive applications through AI. Most people don't have any AI in their day-to-day outside of their timeline on their social media apps and they are already cripplingly addicted to those.
So just imagine how crazy and personalized experiences you're going to get when things like that are justright out of the box. And really, if you've done all of that, that is the workshop. Thank you all so much. This is all my contact info if you want to get in touch with me.
Outro3:01:33
Thank you all so much for your participation.
I will give up the floor here, but really big shout out to Sean and Ben for putting this together. He has been a huge, Sean specifically has been a huge inspiration for me. He's one of the reasons I got into tech in the first place.
And so while I wouldn't be here in this room because without him, because the conference wouldn't be going on, I literally may not have even gotten into tech without him. And so really just big shout out to him and all of the work that he's doing.
And that's it. I think that's lunch. Sean, do you want to grab Sean? Does he have any, is it just straight to lunch?





