Amazon Bedrock Tutorial for Beginners - Build an AI Chat App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you how to build a simple AI assistant from scratch using Amazon bedrock and anthropics Cloud V2 large language model by the end of this video you'll have a working chat bot like this where you can ask simple questions and input other variables I've chosen a language variable but you'll be able to customize this however you want maybe you want it to be the style of Shakespeare uh versus some other famous author you can input all these different variables into your prompt from this simple little interface and all this is going to be done using a python framework called L chain and the Amazon Bedrock service particularly using the Claude V2 model however what's great about this particular project is by changing a single line of code you could move between anthropic Amazon and a i21 lab models or coher models or any other models that show up in bedrock in the future as you please and before we jump in the code while you're here in Bedrock you need to go down to model model access and you actually need to manage model access and turn on access to whatever models that you want to use for this I personally asked for access to the anthropic clad models and I got access almost instantly After I filled out a form but you can use any of the models here for this exercise I am going to do my best to explain every concept along the way however if I use terminology that you don't understand or something's unclear just try to keep moving forward because I think getting handson and starting to experiment with this stuff is the best way to understand how it works even if you're a beginner even if it's a little overwhelming to start by the way this should only cost a couple of cents to test and build so very cheap to play with let's jump into the code all right so a few things up front uh this is a simple flat main.py file I've also before coding here in VSS code I've downloaded the AWS extension and I've logged in with a profile named Trevor you're going to have to log in via the AWS extension via some rooll that can be passed into if you want to do this on your local machine but the code we're writing could also be launched on an ec2 instance or something else where you could just apply a simple AWS role to accomplish the same thing first we're going to build the prompt and the prompt template for cloud V2 and then we're going to build that simple UI that we saw a moment ago with streamlet so to get started I'm going to pass some import statements here um all the code that I type today will be hosted on GitHub so check that out in the description if you are a little bit more advanced and you don't need a step-by-step walk through okay the important thing here if you take a look you need to have Bodo 3 installed streamlet installed and Lang chain installed on your system for me that looked like going to the terminal and typing pip 3 install Lane chain uh Bodo 3 Etc okay so if you hit errors make sure you have all of these installed via python next I just need to define the profile that I'm going to use to access this again this this is the one thing that's going to be very specific uh to you and I'm using a profile named Trevor again just to be clear that's because this is the profile I created when I logged in via the AWS plugin to my visual studio code so the only thing that will change in this code for you is this if you're developing on your local machine okay now I'm going to start actually writing the code to define the prompts and set up the application so one of the first things I need to do is actually create a Bedrock client so how you do that is really just by defining a bar variable I'm going to call mine bedrock client and now we're going to reference the Bodo 3 and I just want to basically set this variable to say like in anytime this is called uh reference the Bedrock service so that means the service name equals bedrock and the region name is USD one for me all right now I'm going to choose a model and I'm specifically going to be running the Claude V2 model so to Define that what you can do is actually go back into the Bedrock service in AWS and you'll see the model ID is going to be in this sample code for any model so I again if you wanted to experiment with something that isn't Claud V2 via the Bedrock service you would just go to that model and then pick out the model ID therefore my model ID is going to be that one from anthropic that I just copied again all you need to do now is change this variable if you want to use a different model experiment with that pretty cool all right now we're going to get into Lang chain so Lang chain is a python and JavaScript framework that is used specifically to build complex AI applications called Lang chain because you build chains and you can chain together prompts and other databases that you're going to reference or web scrapers like things that scrape Wikipedia or that pull information from various sites so Lang chain is something that if you're going to build an AI application you should probably just start by using that framework okay uh and so in link chain you often times are going to Define your model so I'm defining my model as a Bedrock model just as a reminder that was imported via the link chain llms Bedrock statement at the top here okay all right so for this I just need to give it a model ID which I already have equals model ID and I need to give it that client that I defined earlier too and you can also pipe in model keyword arguments okay so that is think of that as like settings that you can send in to the model okay so what I actually just did with the model keyword argument statement is I set the max tokens and this Max tokens can be moved up or down effectively to save cost this limits the length of inputs and outputs from the model and then temperature is another thing that you can set that effectively controls model creativity so I'm just putting these in here you can experiment with these and change them up a little bit to see how that changes the behavior of the application but these keyword arguments are just something you can send in to control your model whenever you're hitting the Bedrock API so that's my llm that's defined now I'm going to actually build my chatbot function okay so I'm going to start by defining my function and I'm going to name it my chatbot and in this chatbot I'm going to pass in two variables if you remember in that little web app I had I could select the language and then I could ask a question so I'm going to call those arguments that I'm passing in language and free form text because I want people to be able to really ask anything of the chat box all right so I started defining my chatbot and code whisper finished the code and it's the right code so I guess I'm just going to use what code whisper gave me download code whisper if you're learning code it is helpful so we're going to create a prompt template in Lang chain a prompt template is simply a way to structure a prompt that can be sent kind of like in this Cookie Cutter Way and so you if you're building applications you're going to reuse prompts over and over again and now I can just reference this prompt template in my function to keep sending the same prompt to an app so uh I'm going to make my input variables language in free form text if you remember earlier those were the inputs available within the app that I built and then the template itself the actual prompt template is instructions to Claud saying hey you're a chatbot you're in language X and I'm going to support a few different languages and then you can input the free form text now what's AES awesome about this function is you can now use this as a baseline to pass any argument into a prompt template if you want language is one option but again you might want it to be writing style or you might want it to be a type of response like a haikou or a poem or a song and the these are like fun things that you can now experiment with because you have this kind of basic uh code and I'll even say language is not the best feature to experiment with I'm going to change it from language to something else probably I may even take this out of my code entirely but I wanted to put both of these arguments in the function so you could use this as a template to continue iterating off of anyway got my input variables uh and I've got my prompt template set so everything looks good to me now I just need to build my chain so I got I'm going to say my Bedrock chain and this is where I'm going to go ahead and reference everything that I already did I'm going to go ahead and say hey with my llm chain my llm is the llm that Bedrock llm that I defined and my prompt is is the prompt that I set in this function now I just need to create a variable to save the response so I can actually output that in my app later so my response is going to be Bedrock chain and I'll be passing language as language and free form text as free form text all right so I've got the response I'm going to say return uh that response right now we actually have a functioning prompt that we can start playing around with so next I'm going to build that little web application so you can have a nice little visual interface for it but before I want to test this out with you so you can see it working so to test this out and make sure it's working I just want to define a print statement my chat bot I'm going to say I'm going to send it English and I'm going to ask it the question which country Tre has the largest economy and I'm going to run this okay so I just ran my code and there were some errors so I'm going to go back and kind of um fix these with you live we can do a little bit of live debugging here together so errors that I hit I forgot a comma always with the comma uh at the end of this I also actually misspelled this I had this as lowercase but it needs to be uppercase so if you were following along step by step you'll probably have to fix that or you'll hit an error if you're using this same uh same method and then finally in the keyword arguments this uh I had this set as a string in quotations but it needs to just be a number so uh clean that up if you hit an error and now I'm going to run this and I'm actually going to run this uh simply by running a print function on my chatbot so if you look down here I've said print I'm going to call my chatbot function and then I'm going to say um give me the language of English and ask the question who is is Buddha I'm going to go ahead and run this python file and amazingly as LMS will do it has responded with information it gave me information about the Buddha his history the timelines blah blah blah what you would expect an llm to respond to an English when ask that question that's pretty cool now we just want to build a very simple front end interface and I'm going to use what is currently one of my favorite projects called streamlet streamlet is this super super simple Python language that you can use to create little web applications uh to create this Stream app I'm going to go ahead and say I'm going to give it a title so really you're just defining these different sections and giving your user the opportunity to pass in variables so I'm going to Define uh the title we call it Bedrock chatbot I had my name trev's personal assistant but you can name this whatever you would like to call it this doesn't really affect the functionality and so really all I have to do here is create the op for people to put input the language variable and the prompt itself so language is equal to streamlit sidebar which you'll see what that looks like here in a second select box language and again code whisper is doing its thing it's giving me the option of English or Spanish I could actually add many more options here if I wanted to again language may not be the best example here this is just giving you something to pass into the prompt uh that you can experiment with maybe you have a better idea than I do on language all right maybe like writing styles or poems or whatever so anyway language is here looking good so the language is going to be set by the sidebar selector and then finally I just need to find a way to put in the actual prompt the free form text that somebody would type in to ask the model uh to do that I'm just going to say if language is set so basically once somebody selects the language then they're going to be given the option to input the free form text via the sidebar and I'm going to set a label to so it kind of prompts what is your question and just for the sake of it I think you would want to do this in real life I'm going to set the max characters to 100 so that way um I nobody could rack up costs for this by putting in a lot of characters I'm the only one using this so I I'm not going to exploit it and put in more than 100 characters this is probably what you would want to do to prevent some somebody from in putting massive uh context into the the application okay okay and here is the final statement this is really what makes it run in the web app is I'm going to say if the free form text is set so if somebody types in the free form text into the prompt then the I'm going to set a response to call the my chatbot function that I created earlier and then I'm going to pump in the language and free form Tech variables that again would be set via the UI that you're going to see here in just a second and then finally I'm just going to tell the app to write a response and that should be all she wrote I'm going to comment out my test print from earlier and now I'm just going to run this code and um what because this is a streamlet app I found that actually I needed to run it with a different command than just clicking the Run button here to launch the Stream app on the server so I found that I actually needed to run it like this where I would say python 3-m streamlet run file name in my case main.py all right it's likely that the folks watching along earlier caught this but I just tried to run my code and there's a typo it's right there uh the the the python helper making it very clear I said side bat needs to be sidebar I think everything is good now so I am going to attempt to run my streamlet run command again all right so it seemed like after I fixed side bat to Side Bar uh we should be working now let's take a look and now I actually have a app running so let's test this out English I can ask any question here what does GDP stand for it got our response it actually is telling us back what we sent into the model free form text and uh GDP which is awesome and then uh it's going to send the response back here now if I I just want to clean up one final thing here I'm going to go back into my code editor that text field here I just want to strip that out because I don't really in my app I don't really need people to see what they sent in I really just want the text to show up on the screen so I just need to go into my response here and I'm going to specify to uh only strip out the text piece so I'm going to save this and rerun my application with that command Python 3 m streamate run Pi all right so now everything looks good I should be able to ask it the same question here and by adding that little text piece to the code it should just strip out the response and show a more clean output so let's find out and looky there it's giv me a great answer to what is GDP and I now have the start of a chat poot very simple piece of code here that you can now iterate on and that I'm going to use to continue to build a more personalized assistant specifically what I plan to do is turn on some agents so that way the chatbot can go reference things like there's agents to reference Wikipedia and to do math and some other things like that so I'm going to bring in some agents to the chatbot to expand what it's capable of and I'm also going to create some custom instructions that are very specific to me so that way I can pass it in Via something called resource augmented generation or rag But ultimately what that means is just having a bunch of data that can be referenced in addition to whatever data the large language model was trained on so that's kind of the next steps for me I'll try to make YouTube videos about it if I continue to iterate on this and and get it working but for now I hope this has set you up with some very basic code go ahead and check out the GitHub repo in the description if you want the full picture of the final code that I am about to upload here in just a second thanks a lot and let me know if you have any questions all right peace
Info
Channel: Trevor Spires
Views: 18,250
Rating: undefined out of 5
Keywords: amazon web services, cloud computing, aws cloud, aws bedrock, what is bedrock, amazon bedrock, aws ai, aws architecture, aws cloud service, aws bedrock tutorial, amazon web services for beginners, amazon web services cloud computing, AI for beginners, beginner AI project, ai for beginners tutorial, python, langchain, anthropic, claude ai, claude, claude 2
Id: E1-mUfpeRu0
Channel Id: undefined
Length: 16min 58sec (1018 seconds)
Published: Thu Nov 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.