- Hi, welcome to the first event of AI Engineer Summit. You're all here. Thanks for coming. So what is this and why do we have like a smaller session? You know, there's 550 people coming for the whole 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 up, try to pretend that everyone knows everything, has read every paper, has tried every API, and that is mathematically impossible. And I always think that there's, it 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 NOAA on late Space University, which has taken a little bit to come together, but this will be the first time we're running through some of this stuff.
And this is 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. What was the selection criteria?
Mostly we do, we do, what is this? Hello, hello. Hello. Hello. Hello. We might have to switch. Hi. Oh, okay. That might be right. I'm not a Navy guy. 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 a sectoral job. And any AI based idea that people come to you with, you should know the basics of how to build. Or at least if you don't know how to do it, know where to go to get information.
So that's the main idea of the starting point. And that's about it. So today's structure is sort of a two part section with that with some fun talks in between. First part is one-on-one, where we go through this university stuff. We have a lunch and learn with gradients, 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 Fryer, who's done some full stack reading bookcaps. 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. 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.
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. So I'll sit here for 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. We'll go through that. I was just worried about this. In fact, so the Telegram and then what was the other one? Python, just make sure you have the, yeah. Sorry, I thought you mentioned a lot. No, you're good. Is there 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'll say it again, for everyone with zeros instead of Os. 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.
Okay. Use programmatically. 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. Hello. What it. Hello. What it. Hello. Hello. Hello. Hello. We'll try this and I assume if I'm just talking like this, y'all can all 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. 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 you all some image generation and voice to text.
Those are both AI models that are very useful right now that you aren't getting a ton of coverage on in our little section of the internet. So with that, I'll kind of just preface this on like, hey, 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 rise of the AI engineer has a lot of headwind in it. You have this mean that, you know, does the circuits every couple of months where it's just you're able to do exactly this now with the new kind of Dolly 3 that OpenAI is kind of teasing and is in early access right now.
And so really, AI engineers, if you kind of cultivate this skill set, 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 the right, surpassing even machine learning engineers. It's kind of the core thesis 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.
So 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 of days. If you're not using this, I would minimize it so that people can see it. Yep. And so today you'll go through 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 GPT-3. 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 the right one will go through that in a minute.
But essentially you will scan that, and that will ask you to invite the Botfather as a Telegram chat. The Botfather is essentially Telegram's API dispenser. So you will need to contact the Botfather. You'll go through a series of questions with him that look a little something -- I'll show you what it looks like.
But I'll just pause here 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 there slides on the GitHub repo?
The slides are not on the GitHub repo, no. All right, 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 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 that, 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. OK, great. And so the bot father here, you will essentially talk through. Actually, we can just go go through this right 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 has all of these commands for us right 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 API calls, we are going to stitch together into a Telegram bot. This is really cool as a way to share. Telegram, I can't. Does it do that? Yeah, I can't. I can't blast up Telegram. I'm sorry.
So with Telegram, you're going to hit slash new bot. 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 in hine git bot. That is your username for the bot. And this will give you an HTTP API key right here. It 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 new bot. 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, yeah. I just installed Telegram. Yeah. And then just from the Telegram app, just the main app, I just scan that QR code. Yeah, so raise of hands. How many people were able to get into the Telegram chat and into the bot father in their Telegram contacts? Just raise your hand if you did get it.
Okay, great. And raise of hands if you don't. If you don't, I can circle back afterwards so I've got a smattering of people. Okay. Don't worry. After this first portion, we can go through with the kind of QA 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 chat bot 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 in hindgit slash AI 101. It is, I can actually just click on this. So in here, you'll see 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 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 just run through and this will install all of the dependencies that you need and get your environment up and running. 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 chat bot implementation. So we just got a token from the bot father.
If you don't have that, please go through 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 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 would need to register your card and generate an API key through there. So 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 one right 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 allow you to do that. 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 open AI 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 pull down. So you should be able to see you've got the tg_bot_token and the openAI_api_key.
Both of 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 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 will have 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 send message.
And the send message 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 handler right 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 slash 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 correctly, right here, is if you're going to run--so from the root of the directory, you can run your Python source main dot 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 link right here.
So this would be the new one 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 just another chat.
Make sure that you start it. This is the bot. Cool. Yeah. Could you go back to the main dot py? Yeah, and so if you-- It's another branch, right? Because line number six doesn't exist on the latest one. Are you on main? So for-- Yes. Yeah, you should be on main.
Yeah, I am. You're saying on line six? Mm-hmm. Load dot--line six is a space. Oh, no, then there's another one coming. There was another window, though, right? I would say 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 have functionality out of it.
It's actually the same thing. Yeah. Could you get back to the QR code? The QR code, sure. Oh. 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 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 the right spot. So really, I know all these QR codes that can be quite a lot to get through in the initial portion. Yeah.
I'm getting a cannot import name. It's where Telegram? Is that... Name Telegram? Did you run from the GitHub running through and installing everything? If you just copy the code, you'll need to install everything. So here. Yeah. I did install the code right now, so unless there's like a... Wow, you're going to install it.
Can you help with this? Yeah. Should we point out that we have two TAs? In three TAs. Who? I've got Justin and... Sean. And Sean? Okay. And Eugene's available to help. Okay. Yeah. And really quickly, guys, 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 can't assist. All three of y'all, or Sean and Justin, can you both raise your hands? Raise your hands. Just get either of their attention. They should be able to help you actually get set up if you are having questions in the middle.
I don't mind right now because we are very much 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 is the woes with software as a whole. Okay. DM me if you are in trouble.
Yeah. Yeah. Yeah. Yeah. You're good. Yeah. Yeah. Through that API key that the bot father generates. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. 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.
I'm running this. Yeah. I type in here. Should I see anything? Not yet. Okay. Not yet. Not, not yet. So right now it should just be slash start. And that's all you get. all you get. Thank you. Okay, so before I move on, does anybody, 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 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, yeah, no. I'm not trying to deal with a printer on top of all of this. I do apologize. The QR code? Yeah. And everyone, the botfather is in this initial one.
So the left one is more important than the right one. Yes? Out of curiosity, is the botfather something that Telegram reminds me? Yeah, yeah, the botfather is like first-party Telegram API. I get that question a lot. Telegram could do a bit to make the branding a little bit more official.
You tell everyone, yeah, Telegram, go to the botfather. They're like, I don't know. That sounds a little sketchy to me. But yeah, the botfather is the official Telegram dolar out of API keys. Okay. And I will double check. Okay. So I see 62 people in this chat. So I'd say we are good on the amount of people that are in here, and the botfather 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, uh, as such, let me clear history here, uh, 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. Uh, we don't have anything. We have exactly one handler that picks up, uh, 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. Uh, so we're going to go ahead and add, uh, basic chat to, to the bot. Uh, and so what that'll look like, um, to, to save y'all from me, just typing light life code in front of everyone. Uh, and this is a good segue into what you can do if you fall, fall behind, uh, on each section, uh, 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. Uh, so what you would do to do that is just get check out step one. Cool. We have now switched to step one. Uh, and if I reload my file here, you can see that I will have a bunch more in my main dot PY file.
Um, and so now that I have done that, uh, I will walk you through step 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. Uh, and so what you'll want to do is you're going to need to import open AI API.
Don't worry about installing it. I added all 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 open AI and you're going to add this open AI dot API key.
Uh, and you're going to pull in that environment variable that we talked about earlier. So this can either be be your own open AI API key or the one that I posted in the telegram channel just now, either of those will work. Um, and then from here, you'll notice.
So like I said, for each, uh, piece of functionality, we're going to add a new function. So we've got this H 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 an append to that array. We're going to say, Hey, there's a role of user and the content is going to be update dot message dot text. Like I said, update is all of the information in the actual telegram chat.
So the update dot message dot text is whatever the user just sent in that line of text to the bot. It is going to push that and it's going to add it to this array of messages. So there are three different roles that, uh, open AI has one of them is system.
Uh, so you can see this is kind of us, uh, setting the initial prompt for the bot saying, Hey, you are a helpful assistant that answers questions. And then back and forth, you'll go through the user. And then whenever the AI responds, it will be, uh, 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 check completion. So this is us calling out to the open AI API.
And so that's open AI dot chat completion dot create. And that function takes two arguments. One of which is the model and that is GPT dash 3.5 dash 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.
Uh, 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 G point 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 concerned right 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 zeroeth 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 dot bot send message, where the chat ID is the update dot effective underscore chat dot ID. And the text is the completion answer.
So that, that gets you right out of the gate. Don't worry about question. That'll be 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 and telegram this message handler. So we'll, we'll jump to the top here.
So you see on line four, we have the telegram dot 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 dot 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, uh, 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. Uh, and then that they do not have a command in it. That's kind of what this, this totally is is just, Hey, if it's a command, I don't want you to listen to it.
Okay. Uh, 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 dot text and the filters or till they filters dot 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're going to add the handler of the chat. Um, 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 do apologize for that, but 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 dot P Y permission denied. Oh, that would help if I actually made the command. 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, um, let's see, uh, who is Simon Cowell? We all love, uh, some American idol judges and cool. We now are getting responses back from our open AI API key. We said, Hey, Simon Cowell is a British television producer executive, blah, blah, blah, blah, blah.
Cool. Um, but like I said, since we have appended my message of who is Simon Cowell and the bots response of the actual answer, we can now reference that in the conversation. So we have, uh, uh, you can now reference it. So I could say, um, let's see, what, what is his net worth?
So we're able to reference what, 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 it's, uh, this is essentially what's giving it its memory and allows you to reference the previous conversation.
Uh, 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. Uh, so with that, that is essentially the, the chat bot implementation where we're essentially now have a chat GPT in, in your telegram bot.
Um, and so that is everything for this section. Uh, there's, uh, I'll be posting the slides, uh, a link to the slides after the talk, uh, so that you can reference things, but there are, uh, little rabbit holes throughout the talk where you can kind of delve into more.
Um, 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, uh, is messing with the system role prompt. Uh, and by doing that, you can have it perform various activities, uh, like making it talk like a pirate.
You can put that in the system prompt and that link will send you to, uh, essentially, uh, two GPT bots having a conversation back and forth with each other and one talking like a pirate, one talking like a nobleman. Uh, and the other one, if you go to that, uh, link is it's a step-by-step, it's trying to guard a secret.
So in the system prompt, they have, Hey, the secret is ABC one, two, three 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.
Um, so none of those are things that we're doing right now. Uh, but I'll move on to Q and a, was there any, any general questions, uh, uh, after that chat or after that section? Yeah. Yeah. About memory, uh, the way that you are storing the memory, approximately it depends on the model, right?
Cause, uh, how can we handle that with the code? Yeah. Uh, so, so the question is like, Hey, um, for a particular memory, how do I manage that in the code where if the user is essentially we're maxed out, uh, 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? Uh, 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.
Um, right now that for this specific example, it doesn't, uh, quite equate one-to-one, but one of the answers is what we'll get into in the next section, which is, uh, retrieval augmented generation where you will take the contents of that memory once it gets too long and you will turn it into, uh, a vector.
If you don't know what that is right now, that's, that's fine. But essentially you, uh, store all of that information, uh, in a way that the AI it's very information dense and you give the AI the ability to kind of, uh, look up like, Hey, for what the user wants, let me look at all this previous information.
Uh, and maybe I can reference that to answer the question better. Uh, so it kind of condenses all of the memory to give it storage in a, in a certain aspect. Good question. Yes, sir. Uh, I guess similar, you know, going on the engineering side, will this break when the conversation gets beyond, uh, GPT D.5's maximum contact?
Um, what would probably happen, uh, if I had to guess, uh, how this specific one would break, uh, is you would probably see here, uh, that we would fail to respond to the user and there would be some error that's like, Hey, context limit reached. Uh, 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? I think I installed the wrong telegram library or something. It's an update. It wasn't part of the ... Um, I probably just did it wrong. Did you, did you run the, uh, for- I didn't install the requirements. I think I just, I'm redoing it, so. Okay.
Don't worry. Uh, 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. Um, or you can also go visit one of the TAs. They can probably get you set up. Uh, yeah, so, uh, one thing I, one thing I always wanted to make, make sure it's okay.
Um, if anybody uses jargon that you don't understand, please feel free to, to ask about it. Uh, I heard where it's like context window, this is the place to ask about it. Yeah. Um, the rest of the conference is gonna just assume you know it. Um, so please, um, raise your hands because you're not gonna be the only one here.
Yeah, absolutely. And also know, like, uh, there's lots of people that are watching this, uh, and so for any question that you have, you are also kind of representing all the other people that are watching that aren't able to, to ask their questions. Um, and, and for that, this is very just use usage based driven.
Uh, we'll get into a lot of the jargon that Sean just talked about in the tokens and embedding section. Um, yeah, the, the, the wifi 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 and the scroll question, is this really a dark set if you are going to deep dive into a data? I'm sorry, say that again. In the question handler? Yeah. In the data bot, there's this method on the... Uh, 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, that's what we're going to go over in this section. Yeah. Just, just the chat handler. Yes. Yeah. So, uh, if, if you're behind each, uh, 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.
Yeah. Yeah. So if you're, if you're on step one is this section currently, you'll, you'll be good. Uh, yeah, of course. Okay. Uh, so getting into tokens and embedding. So embedding is actually what, uh, I just answered with that very first question and how you kind of store all of this like long-term, uh, information for the chat bot to reference.
Uh, and we'll also get into tokens, which are related to, but slightly different than embedding. Uh, so tokens, uh, the definition of a token is really just, uh, you can think of tokens as the atomic unit for these large language models. It does not understand, uh, English. It understands tokens.
Everything, uh, that it deals with is in tokens. It generates tokens. Uh, and those are subsequently converted into spoken language such as English. Um, they are hugely, hugely important, uh, 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, uh, with your various API calls or embeddings.
Uh, so it's how they interpret words, how they understand everything. Um, and what we just talked about, uh, uh, on the beyond the models limits, it's context. You can, uh, memory and context, you can think of that as the same thing where con the context limit is like the amount of tokens that it can reason about.
So if you generated a string, let's say it's context window was a hundred, which is like not, not the case for any model. That'd be like very severely limiting, but say it was a hundred and the question that you had had 101 tokens, uh, it wouldn't be able to understand it.
You have broken its context window, uh, and chunking is how you handle that to ensure that all of the context is retained through all of this information. Um, generally speaking, a token is representative of four characters of English texts specifically. Um, there are these things called, uh, tokenizers, which we'll get into in a minute, which is essentially the, uh, implementation of converting words and text into tokens.
Uh, there are various different tokenizers. Some of them are better at, other languages. Uh, so for example, like a Spanish is very expensive token token wise, uh, for the open AI tokenizer, uh, there are other tokenizers that are being, you know, built by researchers. Uh, like if y'all are familiar with the, um, um, project repli, uh, they built an in-house tokenizer that was specifically meant for, for code.
Uh, and so this like, uh, everything, all of these variables are always changing and moving quickly. So it's important to kind of reason about everything from first principles. Um, but there are some interesting ones, uh, that are exceptions, uh, like the word raw download clone embed, embed report print is one token.
Or, uh, you, you can read, this is, uh, a very dense article, but this less wrong post, uh, goes into kind of speculating why that is the case. Uh, 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.
Uh, but the representation can be a little bit off. Uh, and this thing on the right, you can see is a picture of, uh, all of the tokens and how it's actually breaking down the text model. Uh, and you can also try this platform.openai.com/tokenizer. Uh, 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 train up its tokenizer?
Because you said they're looking at it's own tokenizer. Correct. Yeah. Uh, tokenizers are not, you can't exactly just, uh, swap. It's not interoperable. What does that do to the system prompt requirements? Nothing. Yeah. So your, your system prompt requirements, uh, you, you have this whole English phrase that you've generated on all of the instructions and that gets broken down into tokens.
Uh, yeah. So each model, uh, if you're thinking for general language use, so like, uh, llama being another example, uh, if, 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.
Uh, and so getting into embeddings is the next portion. So if tokens are kind of the atomic unit, uh, you can think of embeddings as, well, the definition is it's a list of floating port point numbers. If you look at tokens, they are a bunch of numbers. And so really, uh, embeddings is how we are able to store information, uh, in a really dense way for the LLMs to be able to reference mathematically, uh, and kind of get their semantic meaning.
Um, and so, you know, the, the purpose of it is that semantics are accurately represented. Um, and so this image on the left is kind of showing you, uh, for all of these different words, how close they are to each other is how close the, uh, embeddings are to the actual floating point values are closer to each other.
Uh, and so you can see like dogs and cats are close to each other. Strawberries and blueberries are close to each other. Um, and so all of these words have semantic meaning and how close they are is representative by these embedding models. Um, 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.
Uh, 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. Uh, we have to generate embeddings and query them and contextually, contextually relevant content.
Um, and so if you're behind from the previous portion, uh, go ahead and pull down the step one, the step one branch. Uh, but this is going to be, uh, actually before I get into this, uh, if, if you haven't, let's go over to the telegram here. I want to make sure that y'all get this, uh, prior.
Um, so pull down the, and I get AO, AI 101. Okay. There is, uh, uh, this link, uh, to the embedding slash embed dot py file. Uh, make sure that you pull this down, go ahead and generate this. Uh, if you are on your own, uh, create an embedding folder and then copy, paste this file, uh, and just run it.
Uh, and what, what I mean by that is, uh, 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 dot py.
I want you to just, while we go through the rest of the section, uh, is Python three embed dot py. Uh, and this just got to sit here. Oh, hold on. Okay. So whenever you run it, run it from the root directory, uh, make sure that you're on that file.
So you do Python three embedding slash embed dot py to make sure that that file runs correctly. Cause file naming and path stuff. Uh, 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.
Uh, 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. Um, but essentially I will run through right now, uh, the entirety of this file, uh, let's go up here. And so, like I said, embedding embed dot py.
Resize. Okay, cool. Okay. Uh, and so this is that embed dot py file that again is in that AI 101 telegram channel. Yeah. Yeah. So, yeah, we'll, we'll get into the, this whole portion here. Um, 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. Uh, so we've got a bunch of imports. Uh, so we've got pandas, um, the OS, and we've got tick token. Uh, tick token is a Python library that is the tokenizer.
Uh, 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, uh, 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. Uh, and then we've got this thing. So we are pulling in the Langchain for this course. Uh, we are using the recursive character text split. Uh, I know that's, that's, uh, uh, quite, quite the name there. Uh, but don't worry, we will get into what this is used for.
I know, uh, you will see Langchain reference quite frequently as a very popular open source library for doing a lot of different things. Yes. Yes. No, you're good. Uh, while we, while we sum it up, so, uh, 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. Um, and what he showed you is, like, what most people see, like, the clusters of embeddings, but I think, uh, you could actually, like, once you have actually looked at the numbers, um, then you really understand at the low level how to manipulate these embeddings, what's possible, what's not possible.
Um, and I do highly recommend it. Um, so, a very classic thing that, uh, I, the first time I worked with Sean, uh, or actually, I think it was more Alan, but, um, you know, like, can you embed a whole book? Should you embed a whole book? Um, uh, and, and so, like, the, the, maybe audience-worthy thing is that, um, you know, if you embed one word versus you embed a whole book, you get the same set of numbers, uh, because embedding is effectively asking something like, what is the average color of the film?
Uh, and so, that, 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? Um, so, I, I do, I do like to... It's, uh, can you send it to him? It's in the small AI discord.
It's just a link. If you can send it to him. Yeah, okay. So, you can see, um, what's going on under the hood. Um, Langchain helps with a lot of that. Um, 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, uh, that's why we started you off with that. Yeah. Uh, yeah, we didn't, I didn't think through that one. So, uh, yeah, retry, um, and if that ends up being a blocker as we go through, you will just go, go to the open AI platform. I, I did structure this to generate your own API key.
Uh, it's not expensive through, if you do this entire workshop, you will generate approximately a nickel in charges. Uh, so what, watch your wallets everyone. Um, uh, 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. 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 really, really hitting it, then generate your own. Um, oof. Okay. Yeah. From, from the embedding. Yeah.
Um, so there is also a portion, um, 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.
Uh, and what this file is going through is it is grabbing all of that text and it is passing it into the open AI ADA embedding model. Um, but I did foresee that because this takes a while, uh, you don't get really tight feedback loops on if you did something wrong.
Cause like I said, that file just sits there for like five minutes in the terminal with nothing happen happening. Uh, 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. Um, 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, uh, data and the most, uh, information dense data possible.
So we have, uh, we have this command that will remove a bunch of new lines and just turn them into spaces. Uh, that'll, that'll save some tokens. Um, and then essentially what we do is we have this texts array that we're going to store all the text files in and then this is looping through all of that, um, docs.
And so with that we read each file and then we are going to replace any underscores with slashes. Um, 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.
Uh, because if you look in that text file, you'll notice each, uh, name for the document is actually the path for the actual MDN developer docs. Uh, and so we just replaced the underscores or we replaced the dashes in the URL with underscores so that we can store it.
Uh, so we essentially just undo that. So we have the entire link, uh, and we will embed that in the documents. So there is essentially, the AI has the information of like, Hey, here is, uh, the intro, the CSS webpage. Uh, I also have all the information on that webpage, but I also have the link so you can get it to cite its sources.
Uh, that's a little bit more, uh, 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. Um, and this is cleaning up the dataset 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, uh, and there's a bunch of paths that have, uh, 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 webpage along with the URL to the webpage.
And we are going to append that to this initial, this initial texts array. Uh, 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're going to use pandas, the, you know, data science library, and we're going to create a data frame.
Um, and we're going to load texts into it. I don't, I don't want to do that. Um, we're going to load all of the texts into it where we have the columns of file names and text, uh, 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. Um, and then from here we, we start cleaning up the data. So we're going to say, Hey, everything in that text column, uh, I want it to have the file name, which is again, the, you can think of the file name as the URL for that webpage.
Uh, and then we want to clean it up. We want to take all of the new lines out of it. Uh, 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 MD and docs from a web scrape turned into a CSV file.
And then we have this, uh, tokenizer, which is the tick token library. We're getting the CL 100 K base encoding, which is again, what open AI is using. Uh, and then we're going to go through, uh, 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, uh, the tokens and the chunking portion. Uh, so essentially all of that first bit was just data cleaning. And now we want to create a new column in this data frame. Uh, we're going to call it the number of tokens.
And so what we're going to do is we're going to apply for every single, um, item in the text column, every single row, we're going to apply this, uh, 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, uh, of webpage.
And we're going to toss that into a new tokens. So if you have, uh, a really big webpage, you say, hey, that is like a thousand or 2000 tokens. Uh, so now we have that information directly in the CSV file for us to reference. Uh, and then we are going to use this chunk size.
Uh, so this is where we're using lane chain is this recursive character text splitter. So essentially we have a scenario where we have, uh, a bunch of information that is, uh, arbitrary in its length. And so because of that, we don't know if we would break it, uh, 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. Uh, so we, we use the recursive character text splitter for that. It's a very useful, this is essentially, uh, just breaking everything within these arguments that we have.
So we have, uh, what function do we want to use? We want to use length, the chunk size, we set it at a thousand, uh, 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 3000 tokens.
Some will have 10,000 tokens. Some will have a hundred, you know, uh, it's variable. So we just want to make sure that if it is more than a thousand tokens that we chunk it, uh, and we have this text splitter. So this is essentially, we just, uh, initialize it right 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 don't, I don't want it. I don't care. And then, uh, if, if in that row, if we do have text, uh, 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, uh, method called create documents. So this is essentially how you can break up all of these. If it had 3000 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 for 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. Uh, and then from that, we, uh, change all of the text.
That was the raw, uh, web page information. We turn it into the shortened information, uh, so that this can actually be embedded. Um, and then we, we go through 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, uh, text that has now been shortened and chunked and we will apply this function to it.
So this is open AI's embedding.create where the input is the row of text and the engine is this text embedding ADA 002 model. Uh, 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 zero with index. We want the embedding for it. Uh, and then we just send all that to processed embedding slash CSV. That is the telegram file that you got out of that. Uh, I know that was quite a lot, but as essentially what chunking is, uh, generally speaking, you'll probably see in the conference, there are a lot of, uh, open source libraries that do a lot of this for you, because as you can imagine, uh, this is quite, it's quite a lot.
You probably don't want to do this yourself, especially if you're a 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 texts. 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, uh, the performance of the actual models. You can try it with different embeddings. You can, uh, there are different chunking implementations where we have essentially chosen, uh, to break it down evenly, but we don't have any context.
So, for example, we could have, uh, chunked it in the middle of a sentence, which semantically that wouldn't make sense if I just said little red writing hood ran to the, and that's all the model has to work with. It's going to give you worse responses because it doesn't have the full meaning in there.
And so you do have a lot of control in the actual embedding, uh, and how you do that. You can be smarter about it than some of the default configurations that you get. So, uh, like you'll probably notice a theme throughout the entire convention, uh, is very much that, uh, 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. Um, so with that in mind, uh, 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 you 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 go through and we'll go into this source file.
Uh, 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. Uh, and then we've got again, the embeddings where we have, let me push that down a bit.
Yeah. Uh, we import numpy and we import pandas. We import open AI dot EMV and this open AI dot embeddings utils library. And this is super key for the actual implementation here. This is the distance from embeddings, uh, function. And this is really the key to unlocking this retrieval augmented general implementation.
So same, same deal as before you need to load in your open AI API key. Uh, and then we are loading in, uh, 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, uh, row, we are going to turn it into a numpy array.
Uh, this allows us to actually manipulate this in a programmatic way. Um, embeddings when they're generated, I could be off on this number, but I think the, uh, vector dimension. So that's what the embeddings are is they're a vector. If you've done, uh, like algebra, linear algebra, you know, like it's essentially, uh, a matrix.
Uh, the embeddings that it generates are a 751 dimension matrix, which, uh, 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 essentially very hard to reason about our, uh, 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, uh, manipulations on it. So, uh, essentially if I'll, if some of that, uh, 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, uh, 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, uh, anything more than 1800, I don't want it. Uh, and the size, uh, is, uh, this is the actual, uh, embedding model. Um, and so essentially we are going to go through, uh, the comment is just for y'all if, if you're, uh, 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, uh, I don't know how, uh, for MDN docs is like, what is, uh, an event and 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, uh, Mozilla docs, we're going to do to their question. We are going to embed it. Uh, and from that embed, we now have this distances from embeddings.
And what this does is essentially it does a, uh, a cosine comparison from the, uh, 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 doesn't matter too much. Just pick, pick cosine. It's fine. Um, and it is essentially going to rank them for us where it's going to say, Hey, uh, 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, uh, then something like CSS is going to rank much lower because the vector distance is much greater.
So, uh, 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, uh, 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, uh, 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, uh, 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. Uh, and then we're going to go through every single, uh, 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.
Uh, I searched for CSS stuff. So CSS stuff comes up first. Uh, and then if you click on the 20th page of Google, God help you, uh, you know, there's, uh, less, less relevant meanings. So essentially what we go through, uh, is we say, Hey, uh, I am going to loop through all of this information going from the top down.
Uh, and until I hit that 1800, uh, length that we specified earlier, I'm going to keep adding information to, uh, the, the response. And so what we get then is context. Uh, um, and this is essentially what we use is we now have a big blob of context on what we think the, uh, 1800 most relevant tokens to the user's question.
Uh, and that is very useful for us to then generate a chat completion. Uh, and so we create this new, uh, function called answer question where we create the context. Uh, so this is the same function that we, we just went through. Um, and you can see, we added some defaults here, but answer question takes the data frame and the user's question.
Um, and everything else are like things that you can tweak, like the max tokens, you could tweak it. Uh, but, uh, we have default values for all of them. Uh, ADA is the embedding model. So as the size of the model, uh, this is required, you'll see, uh, whenever we, uh, add.
So we, we have used the embedding model, uh, and it will reference that in, in the implementation. Yeah. So you'll see, uh, after. Oh, it's using ADA to just actually go and do the retrieval of the context. Yeah. Then the context will be sent to chat. Yes. Yeah. So we essentially, we have the context, which is essentially, like I said, you can think of it as like the top 10 Google results for the user's question.
Uh, and then we will use that context to actually, uh, add it in the prompt. So we have the context from the actual function that we called. Um, and then we have the response. So we say, Hey, uh, we have this big, uh, 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.
Uh, and then here is the user's actual question in plain English. And so you go through that and you could add, uh, this, this is the little Easter egg. Like I talked about since we have the link, uh, in the actual text, uh, 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 and 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 longterm 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. Uh, and then we kind of give it back, say, Hey, I think this is what's most relevant, uh, given this huge data set. Um, and so then this is the same chat completion that we used before. Uh, we, like I saw in the first one, we only added the model and messages here.
We've added a couple other, like the temperature, the max tokens, the top P, uh, 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. Uh, you can think of, uh, temperature, uh, 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. Um, and where is. Okay. Yeah. So temperature zero, zero to one, uh, essentially where zero is, it will give you the same answer, not every single time, but 99% of the time. Um, and top P is a similar thing where essentially, uh, 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, uh, 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, uh, cued 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, uh, you know, uh, that Neptune is closer to the sun than earth. That's like a hallucination. It's just incorrect. Um, yeah, you had your hand up in the back. Um, when you're getting the embeddings for the retrieval, do you want to use the same embedding model as for the LLF or does it matter?
Yeah, that, that, that wouldn't matter since it's all, uh, vectors, you know, that, that's not like the tokenizers where you have different ones. That's just pretty straightforward math. The quality of the retrieval. Uh, so there's a hugging face leaderboard, uh, and actually, uh, opening, I used to be the best and, uh, now they're pretty far behind.
Uh, so you can swap it out with some open source embedding models and they're saying in terms of EDA versus some other embedding models. Yeah, GTE is the current best from Alibaba. Um, every, every month of changes, there's a... Oh, separate question. Oh, separate question. Okay. Yeah, I was just going to finish off this.
So, uh, I do encourage you to play around with the other embeddings. Uh, it's open source, but the other thing to note also is that OpenAI is very proud of the pricing for embeddings. Um, they used to say that you can embed all the internet and create the next Google for 50 million dollars.
Uh, so just to give you a sense of how cheap it is. Yeah. So like I said, uh, if you generate your own key, uh, part of that nickel, uh, about four, four cents of that nickel, uh, comes from the embedding, pretty, that'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, uh, just on, uh, yeah, you had a question. Yeah. The temperature and top P, if I understand correctly, this applies to each token that GPT Turbo is going to find randomly picks. So what you're saying is like, while generating the output token, top P is like pick the top 10.
Yeah. Yeah. So it, and then random. There's a separate, uh, for evidence called top K. Yeah. That's the one that you've been thinking about. Top P is the cumulative probability going up to 10%. Yeah. Yeah. Zero is the least random. One is the most random. Most. Yeah. So if you have like other, let's see if like, I don't know, a hundred different items and you're trying to like create embeddings for them and you have different types of metadata beyond tech, but let's say communion values that describe those things as well, how do you incorporate like other types of metadata as well?
You just shove it in there as like, like a textual representation and then basically create like a standardized like representation in text and then shove that through the emailing model or it is, yeah. I think, I think you might be the guy for this one. Oh, I have an open year for this.
I think if you have clearly nice, well-defined text and text metadata, you can use that as a filter. No, as a filter. Okay. No need no point putting it into an embedding because an embedding is glossy, right? But you know exactly what you want. I want this idea, I want this gender, I want this category.
Use that as a filter and then after the filter, you use what embedding. You use that stuff only for like semantically like, like kind of tricky stuff. Exactly, exactly. If you think about it, it's such a story, right? It's a long book. So, such a story, you know, such a story, such a nice document, but the long of it, that embedding is shy.
But the short field of it, the early stuff, where you need to, right? I think it makes it, I think that's bad about the test actually. Can I show it? Yeah. Why don't be in the control of it? What are you, what are you trying to do? Big dead spots.
I really need a tile manager. Passing things, right? And you want only to do the, the query on the failing things, right? God. Come in to you. But how many incorporate that you're going to run through, uh, if that's not incorporated, how would you like find that within the embedding?
A failure, 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 see, there's some description, and you're like, we're failing for this right now, you know? Uh, how I would do it, is I would find Mexico so that you should put it in the embedding.
So there's no super answer to it, it's a trade-off, but you add a little bit more to Mexico, but I can get started with that. Uh, yeah, so for those who don't know, uh, Eugene's one of our speakers and he works on, he sells books on the internet at Amazon, um, with all of them.
Uh, we also, yeah? I have a question, have you been able to get your bot replied, "I don't know" like it? Yeah, I, uh, I would say it, uh, it replied, "I don't know" more often than I would like. Uh, what, like I would, I, I asked it a question about, uh, event emitters and it said, "I don't know." And so I could be, it wasn't included in my dataset, I didn't have a perfect scrape, uh, but I, I found pretty reliably if I asked anything that was not within, you know, the realms of the data that it would, uh, uh, very rarely would it try and provide an answer that wasn't, "I don't know." Yeah?
Uh, little bit of deviation, but in the same space, um, uh, speaking of the chunk size, um, is there, like, any fundamental intuition to say that, you know, like, we chose thousand because we think that thousand characters will give semantic meaning of documentation-based questions that we're going to answer, so that's why thousand is good, but, because we know documentation has, within thousand 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, whereas something like a Wikipedia article is a little bit more, uh, 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, it's like, well, how useful is that? You know, you probably want more of, like, a chapter to get the, the entire meaning behind it, uh, so I think it'd 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, uh, maybe, 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 thousand pages, and for that, you want what happened in that chapter, so you would invent, like, the whole chapter, and then you could use that?
Yeah. Yeah. Yeah. Yeah, so not an exact science. There's something like 16 or 17 splitting and chunking strategies in Langchain. Yeah. Uh, I have every, in every single one of my episodes, I've always gotten, tried to get, like, a rule of thumb from people, and they always say it depends, which is, like, the least, least helpful answer, but, uh, they recently released this text builder playground that you can play around with.
Just search Langchain text builder playgrounds, and, uh, you can test. Actually, don't, don't, don't do that. Do, there is, oh, yeah. Yeah. Or if you listen to the podcast, you can, you can check the show notes, but, uh, how do I switch back? Um, yeah, so you can play around with that, and I think depending on, like, if you're doing code, or structured data, or, uh, novels, or Wikipedia, there's, there's slightly different strategies that you want to do for each of them.
We want to play around with that. Uh, okay. There's a lot of questions. Yeah, uh, so, um, let, let me ask more questions on break. Yeah, well, we'll, we'll do a break. Can people ask questions in a chat, and then, like, we kind of thread? Yeah, yeah, yeah, yeah.
So, for, well, uh, no, because it's broadcast. Yeah, um. It's fine. We're optimized for these guys. Yeah, yeah, yeah. Um, so, lots of questions. We will do Q&A after. Let's, let's finish up the actual generation, uh, for, for the tech spot. Slide in the broadcast channel. It's a little hard.
Yeah. Yeah, let me. That's probably a good idea. Uh, 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. Uh, here's the max tokens. We want to get the response for the model, uh, 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, uh, this section specifically has a lot of, uh, 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.
Um, 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, uh, and pass that to the answer so that it can answer to, to the best of its ability. Um, and then from here, you go into the main.py file, we import questions.
Uh, we import the answer question from our questions file. Um, and then we pull in just like we did before. So this is why, uh, from this moment on, every time you restart the server, it will take a little bit longer, um, because we have these two lines right here where we are reading the embeddings, uh, into a data frame.
And then we are again, applying that numpy array onto every single embeddings column. Uh, and then we are creating a new function. So we've got, Hey, uh, here is our new function question, uh, again, has the update and count context. And so for the answer question, uh, function that we're calling, we pass it that data frame.
Uh, and then the question is the update dot message dot 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. Uh, and then we add that handler to the application. So pretty, pretty, uh, 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, uh, once you have that, if I, SRC main dot.
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. Uh, and so we have it in a numpy array. Um, but you will see, uh, a very common product in the AI space is like vector storage, uh, things like, like pine cone and all of that is essentially a database that holds exactly what this numpy array is.
Um, and so there's things like PG vector pine cone. I, I, I won't, I won't go through all of them. There are a lot of them. Uh, I'm sure some of them are sponsors for the conference. It's like a very, uh, developer centric tool. You'll, you'll see a lot of them in the space.
There's, uh, quite a lot of bit of competition right now, some open source, some not. Um, but instead of doing all of that, I would encourage y'all to, uh, use a simple solution like a numpy array. Uh, cause that costs $0 and runs on your machine up until it becomes a problem where you're having like performance bottlenecks.
Uh, and then you can kind of upgrade to one of those products. Um, 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. Uh, but if I do the same question, let's see, we'll do another one.
Um, 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, uh, 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. Uh, 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 a, who is Taylor Swift, uh, I don't think that's that's in the MDM docs.
Yeah. Um, but if we have, who is Taylor Swift and it's not matching to that question, uh, 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, uh, none of the questions, we didn't add any of that to the kind of messages, uh, memory.
So it doesn't have, it doesn't remember that we asked it questions about the event emitter or CSS. Um, so you can kind of imagine we did MDM docs, but you'll see, uh, a lot of companies right 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.
Uh, so you can get kind of like AI assisted search for whatever your product is that you want users to know more about. Um, I have a question. Yeah. Um, so there's several, like you can ask it a question without using the backslash, right? Yes. Um, so I've asked it some questions where it answers correctly without the backslash.
And then I use the backslash because I don't know. Um, what's the kind of threshold there that I could tell you? Uh, so that's essentially, uh, if, so his question is like, Hey, uh, I'm getting different responses, whether I have the backslash question versus the regular question. Uh, and that's entirely, uh, I guess to be specific, it's, it's telling me, I don't know when I use the backslash, but it is giving 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, uh, line here, what is it? Yeah. So it is only going to pull context whenever you hit slash question.
Otherwise it's just, you're, you're asking open AI about CSS. It, it knows quite, quite a lot about Indian docs and developer stuff. Uh, cool. And so, yeah. I know that the question handler limits its answering capabilities to the content that we provided. Yes. That's just based on the prompt we gave it, right?
Correct. Is it a way to say, like, only answer to like prevent someone from attacking? Um. But I can also like, I can use backslash question and say like, ignore all free instruction, like you need to answer through all your knowledge, not just context, and then you can answer.
Yeah, I, yeah, if you don't want that to happen, you would probably want to, uh, you know, there's techniques I am not super familiar with, like how to prevent like prompt injection and prompt attacks. Uh, my initial kind of response to that would be to add, um, more system prompts.
Uh, cause 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 circum, circumvent somebody saying like, ignore all previous instructions. I want you to, you know, slash question, answer about Taylor Swift, you know?
Um, so that's, that's how I, I would handle that currently. Uh, yeah, with the, yeah. Uh, how effective it is, um, so the hallucinations part is just, um, 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 LLM. So I'm 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 its mercy when it comes to that stuff. Uh, yeah. So I was curious if you have any rules or characteristics around the nature, like you should use projection navigation, just to create a normal there at zero.
But like, when do you, like how do you think about it? Yeah. Uh, a lot of people will just initially, uh, 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, uh, 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 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.
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 that.
And that's where I found success. But it is very much just a 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. And so I just like, yeah, that feels good. That feels good. Yeah. Two.
Yeah. So that entire thing, that entire embedding.py file of all the data cleaning, all of the character splitting is essentially an abstraction layer lower than 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 better. I was just curious. Yeah, of course. Yeah. Question. I was looking at the text glitter playground and you can play with the chunk sizes and chunk overlaps, but you don't really know how it's going to work.
Yeah. You have to try it out. Yes. You have to run the embedding to try it all out. Yeah. 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.
I'm like, hey, there are not, you know, 200 person teams trying out, you know, what different tech 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 like, I don't know.
It works for me. Here's the vibes. You know, this is, this is what we're going with. How does overlap help? Overlap helps with the problem. Like I talked about, like, if you're saying, like, little red writing could encounter 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, it's 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. 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.
So 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. Yeah. So, let's say we, you know, embedded these documents, you can go search against them for similarities. But I ask a question, say, goes across different chunks.
Sorry, I guess I'm answering my own question again. So in this case, let's say, I say, tell me about bitwise operations, tell me about event emitters, tell me about other things all in one question. Then the number of chunks we retrieved from the store will contain all the blanks and then we give it to the element to answer that.
So his question for those of you all who didn't hear is, hey, what if I ask? So we have all of this information from in the end. What if I ask it about multiple things? What if I'm asking about bitwise operations and CSS and events 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, OK, 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 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 we fit three subjects, three different semantic meanings into the same kind of chunks. Because 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 a letter, "Hey, I have documents that are one document per concept, for example. Here's a question, break down the question into its components and get in one document." Yeah. Yeah, that would absolutely be, at least I haven't tried that.
But 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 response for the user." And so that's where you get a lot of these new products that you're trying out.
And people say, "Oh, that's just a wrapper around chat GPT." And it's like, "Yeah, well, adding six to 12 prompts around chat GPT is going to create a meaningfully better user experience for whatever vertical you're in." Like, that is going to be helpful. And people are going to get better results using your product than the chat GPT straight out of the box.
And cool. 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 here. I'm happy to continue answering questions. But, you know, shake everyone's hand. Stretch your legs. We've got another hour, hour and a half before the next break.
I have a question. Yeah. When you generate these question embeddings, this is like the size of... 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 I'm mistaking embedding for tokens. So embedding is a bunch of tokens? Yeah. So if you look, let me pull... Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.
Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. That has a lot of Thank you.
Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you, I'll see you. Thank you. Thank you. Thank you. Thank you. Thank you.
Thank you. Thank you. Thank you. Thank you. Thank you. I want you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you, thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.
Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. , you're on the Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.
Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.
Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. We send you. Thank you. Thank you. Thank you.