Building a Chatbot with OpenAI, Shiny, & R - Part 3: Deploying our GPT Chatbot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome to part three of our series of building a chat bot in R using shiny and the openai API today is all about deployment I'll show you three different ways to deploy the app that we built in video 2. the first is to deploy it on posit cloud this is quite similar to posit connect if you've used that service before the second is shinyapps.io and the third is hugging face spaces let's Dive In all right so we're back in the chatbot project within our posit Cloud environment and we can go ahead and run the app to make sure that it's still working for us so we'll ask it to describe our studio in five words okay looks to be working well for us so the first thing we're going to do is publish to posit connect excuse me pause it Cloud connect is what I use professionally at work so it's certainly on the mind we'll go ahead and select this one here foreign [Music] here is our deployed app on posit Cloud we can test it out again so let's do our trusted what is two plus two and look it's not working okay I actually knew this wasn't gonna work because I had it happen to me before um what we're missing here is our API key remember that was set using our DOT R Environ file so we need to deploy that dot R and Byron file with our app as well okay so we can jump back over to our environment here and when we click deploy so another way you can do that is from this pane here you notice I I don't see you in a way to pick additional files well I can go and grab my DOT R Environ file in this case I have a project specific one if you are using one for your entire environment you might have to go kind of further up for the project level the easiest way to find this is using this use this so edit our Environ and you can specify either user which will cover everything or just for the project okay so we'll go back to our project and I'm actually just going to copy this dot our Environ file into the chat bot folder and then when we click deploy we now have the dot our environment file the available for us to select I'll go ahead and click publish okay here is our redeployed app we can ask it say what is a DOT RN Byron file this time it should work for us because it should have the environmental variable set for our openai API and there we have it we get a decently long response coming from the gpg 3.5 model and let's go ahead and bump up to four ask it a coding question and I'll say how can I edit that file using the use this package [Music] the gbt4 pack or model is a little bit slower okay so lots of details in here I'm going to skip those for now and you have now successfully deployed an app to posit cloud okay so now we're ready to deploy an app to shinyapps.io our second destination and I'm going to jump over to the website here for that again shinyapps.io prompting me to log in I'll use GitHub to do so and in order to connect to the either your local or Studio IDE or in our case posit Cloud we're going to want to go into the tokens section so you can click so I just created a new token here we can click show so I just clicked the show button and what it's asking us to do is to use the RS connect or rstudio connect package to establish the account information basically to make this connection for us we can copy this over here [Music] and jump back over into posit cloud if I run this code in the console we will now have this connected for us so you'll now see that we have shinyapps.o populated in our publish pane so if I if I did that a little too quickly for you click other destination from this deployment drop down that you see here so click that drop down other destination shinyapps.io and we're going to want to deploy the dot R environment as as well in this case and we'll just call this our chat bot there's some naming conventions for shinyapps.io so you can't add things like spaces we'll go ahead and click publish so I've deployed it before that was the warning that we got there was asking if I wanted to do this uh someone I was practicing I I went ahead and deployed an app there this is just redeploying that for us by the way um if you are creating your own connection to shinyapps.io you should you should keep this once this token secret much like I did in previous videos for the open AI API token I will be deleting this token at the end of this video so don't bother trying to copy it down uh so shortly we should see the deployment complete for us over to shinyapps.io [Music] you can follow along if you're interested by reading the the log there okay looks like this is open for us uh this time we'll ask it to let's say describe the creator of the Tidy verse in seven words [Music] all right [Music] let's see if the gbt4 version works like we did before [Music] all right so that seems to be working for us we can go to the shinyapps.io website and you can do some management of this if you want it I believe is open by default [Music] so that's worth confirming on your own yeah so this is public which is not true for the posit Cloud instance so it's it's important to pay attention to these different features when you're choosing a destination to deploy your app all right now on to the next one which is hugging face all right so we're ready to deploy to hugging face um back in our posit Cloud environment I will warn you before we get into the details here that this is a little bit of an advanced um approach uh one of the first things that we're going to do is um create a we're going to pull down uh the repository for the hugging face spaces so we're gonna Nest a get project inside of our project now to be clear I did not know how to do that so Google was helping me here we're going to use git sub module ad um and this is you can search around for this of course on your own um to read more about this but we're gonna Trust Google on this one and what I'm doing here is get get sub module ad and then we're going to put this in a folder called rjat Dash chatbot actually I'm going to say hf.chatbot all right so when we come into our main project we have all of the files within this HF chatbot page here and I'm going to go ahead and change our directory with CD in there so effectively we're working um we're working inside of here as well and what we need to do is replace a couple of these files um and I'll do that again in the terminal or I can do it from the r console so file dot copy and we want to take our chat bot slash app.r file and we want to paste it to the hf.chat bot app.r we'll need to say overwrite equals true then we're going to do the exact same thing with the helper and we'll just have this as the destination we don't need overwrite in that case and we're not actually going to use the dot R Environ file so we do now you can now see within the HF chat bot folder we have the app.r in this helper file just to validate yes here we go that looks good to me we need to open up the docker file I mentioned that this was a bit of an advanced topic and we're going to need to include all of the packages that are part of this project so in addition to the these listed here we need per uh we need glue we can get rid of GG extra and I think that should be it here oh of course we need HTT R2 that would have not been great um sorry about that I accidentally closed the window on us um okay so we're back in here this looks this looks good to me I meant to save it and I accidentally closed the window um all right so we're ready to um commit these changes and deploy we can do a little bit of cleanup actually into this folder as well I'll get rid of this file from the previous deployment we can go into the readme page to change the name for this and I'll say R chatbot as our name and maybe we can do a robot Emoji for this um and then yeah all of that looks good to me if you are I should have mentioned this at the beginning but if you're not following along I did make a dedicated video on how to deploy shiny apps to hugging face so I would encourage you to go check that out as well so this looks pretty good to me um what I am gonna do now is go ahead and add these changes I'll say git add Dash a git commit Dash M initial chat bot deployment and we will do git push to push the changes to this Repository all right it looks like it did not like our credentials so I'm gonna try to get paid to see if we have any um so [Music] we'll commit those changes see if this gives us any better but we'll add for in this case the same thing we said initial employment [Music] all right let's go ahead and push those changes [Music] I think that successfully pushed the changes to the GitHub repo but not to hugging face all right so this is a little bit of a [Music] excuse me a little bit of a pain [Music] to get this working I do I'm gonna use the same trick that we used maybe I'm not okay so for this sub module what we're gonna have to do is is the same trick I used in the last one and I'm going to replace my token here this one I do plan on reusing so I will pause the recording briefly I'm going to save this file with my token here and then we should actually be able to push to that Repository [Music] all right I mentioned that this was not the easiest way to do to deploy the the application although I anticipate it will get easier the terminal wasn't working for me it kept saying that it couldn't ask for the username my git tricked it looks like it didn't work this time to get config so what I ended up doing in this case was changing the working directory to the HF chat bot and then using the Gert package and then when I did git push it prompted me for my credentials that then were accepted now let's jump over to hugging face we should see some of these changes um coming in it had to okay good it would have had to rebuild um the container for us and I do not expect this to work we haven't told it anything about our API key all right and the grade out here or whatever the faded out is indicating to us that it's not working what I'm going to do is go down I'm into the settings for this [Music] um space and in here we can we can set our secrets so what I'll do is say open AI API key then in here I will copy the API key I'm not going to show that step just so I don't have to keep deleting API keys and then I'll click add new Secret all right so the secret has been added if we go back to our space that did trigger a rebuild as you can see here [Music] and this shouldn't take very long because we didn't actually change anything besides the Environmental um variables for this right the environment variables let's try this one more time [Music] didn't like something about this [Music] let's see [Music] okay let's see if we can get this to succeed for us [Music] try one more time with what is two plus two ask for a response [Music] with a few fault starts behind us here on getting this hugging face deployment to work I think this latest one might do okay the issue is that the secrets on hugging face are not exposed by default when you're using a docker-based deployment which is what we're doing so if we add this line that I just pasted into our Docker file this should expose that secret that specifically has to match the open AI API key there's some other helpful stuff from the documentation I'm not going to go through what each of them do for two reasons one I don't fully understand them and two this video is already long enough the next line here is setting the openai key by basically printing or using cat to grab this open AI secret open AI key secret and then writing it to a DOT R Environ file so this can work much in the same way as we did for the other deployments the reason we didn't just deploy it with the dot our Environ file is because this would be sitting in the repository so it would be exposing our secrets to anybody who had access to our space okay so with this in mind we can now save that I will go ahead and commit these changes and I'll say have Secret right to dot our Environ file push that change [Music] I don't love that it didn't say anything to us here oh whoops helps to [Music] looks like I had an extra letter there okay spelling is important especially when it comes to the terminal but I think we got there for the most part there's my chat GPT trying to figure this out jump okay there's the documentation that I was using and now we will actually go to the correct page this should be building yep um so it's going to be rebuilding our container based off the changes that we set there and here's the specific part where it's saving the openai key value to the dot our Environ file it's installing the packages all part of the build process of course and once this is complete uh if we're lucky this actually will work this time all right I it's unclear to me when it actually refreshes because the page seems to mostly look the same um so I I tend to kind of click around a bit I don't think this is quite the latest version yet okay let's see if this one works for us okay not a great sign there but maybe that was the container actually starting up [Music] darn looks like it didn't want to work for us [Music] hmm well this has now exceeded my uh I would say my knowledge and what what exactly is going on here um so I'll leave this open uh sometimes deployments don't go well I will have to learn from one of you of how to do this appropriately in the future I can go in here and confirm that this Yep this looks like it's doing what we would expect it to um just to validate that we do have the open AI API environment or secret here within our repository so um apologies we couldn't come to a successful end here I'll try it one more time just in case it hadn't actually reset on us quite yet yep no no luck and unfortunately some of the print statements I've tried to add in here to give a little bit of debugging aren't showing up in the log so this makes it even a little bit more difficult to debug as well so this will have to cut I'll have to answer this in a future video thanks for bearing in there for me but I think it's important to show that not everything is successful um all right [Music] I guess it's a bit embarrassing to say that we didn't get all three of our deployments to succeed but I'm gonna leave that there uh because that's reality sometimes not our all of our deployments are successful maybe in a future video I can learn from one of you and reveal the solution to make hugging face spaces work with secrets with a Docker deployment and shiny I hope you'll stick around for more videos in the future I'll try to have much many more successes than I do failures but I hope that you can learn from both thanks for sticking with me on this one and I hope to catch you in the next one [Music]
Info
Channel: James Wade
Views: 120
Rating: undefined out of 5
Keywords:
Id: odBkUdWlwUk
Channel Id: undefined
Length: 21min 22sec (1282 seconds)
Published: Mon Jul 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.