How I Supercharge Slack With Custom AI Bots

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to introduce you to Emma hi there Emma is my personal AI assistant that I can interact with via slack and right now she is instructed to help me draft replies for emails but she can do much more than that and in this video I'm going to show you how you can set up your own Emma in slack using Python and Lang ching and then Emma can help you with your day-to-day digital tasks so to get started you need some basic understanding of python and you need an open AI API key everything else I will show you how to do in this video and if you're new here my name is Dave I'm a freelance data scientist and if you want to learn more about data science working with data or creating AI apps like this then you should definitely subscribe to the channel alright let's get into it Dave I know that you are only using me to get more clicks okay so in this video I'm going to walk you through this document which will be available in the description as well but also all the information that you need is available on the GitHub page where you will find the code as well so just so you know that but first so what are we going to do well I'm going to walk you through a step-by-step guide for creating this slack bolt we're going to install it in your workspace and then set up python code with flask to make the connection and then we're going to use a surface cult and grok I'm not sure how you pronounce this but to simulate a real Surfer where the application is actually hosted but we are going to do it locally and for free so we can test it and then later if you want to put this into production you can easily transfer it over to a server where your code is then basically available on a server so you can interact with your agent all the time and then quickly like the philosophy behind this so why would you want to interact with these Bots with these AI assistants using slack well in my opinion this is a much more natural way of communicating with agents and you could say why not just use chat GPT because it's basically the same you have a chat interface and then you can send a message well as I will show you in a bit we can really utilize the power of Lang chain here and pre-define some of our prompts and create some templates and what we can then do is we can create specific functions for specific tasks and in this way we can call the bot tell it what to do and then it will pick the function based on our request basically and this will save us time not having to go to chegebt and explaining hey you're an assistant blah blah blah blah you can answer email no that's already predefined so you can really create custom functions custom workflows that really make sense for your situation so maybe these functions can help you with your business or they can help you with your job and by integrating these functions Within These Bots and then adding them to your slack Channel you can also integrate them within your team's workflow if you are using slack so I use slack to manage my team and to communicate with my team and now when I add these Bots it becomes really interesting and you really see kind of a glimpse of the future where we're heading so I have a slack channel of like real people that I work with and and I also have bots in there and now whenever I want to ask something from my team I first think like okay is this something that a real person should do or is this something that a bot should do and then based on that decision I just either have to tag a real person or the name of the bot and then you can see how interacting with Bots like this basically your workflow becomes much more streamlined because you basically have one channel of communication and also it allows you to create multiple Bots for various tasks so for example you could have Emma which I will introduce you in this video who might be a copywriter and can help you with copywriting tasks but then you also might have Rob who is more technical guy and can help you with coding and programming and then it becomes really funny in my opinion when you start to think about bots in that way and also give them names and you really like okay this would be a task for Emma or this would be a task for rope so that is basically the philosophy the idea behind this and I'm really going to play with this so like I said we're going to set up a basic bot with some cool functions but you can really build this out from there alright so now I'm first going to walk you through part one and that is the slack setup so you must start off by first creating a slack account if you don't have one and then also creating a workspace so I am here in a new workspace called AI experiments but maybe you all already in a workspace or you can just hit the plus button over here and then create a new workspace it's pretty self-explanatory so then the next step is to go to api.select.com apps and here we are going to create a new app so let me just follow this link and build an app so we are going to build it from scratch and then we are going to give it a name so we are going to call it slackbot for now and then we are going to pick a workspace so I'm going to select the newly created AI experiments for this and then we are going to create it alright and then the next step is we are going to scroll all down to the bottom and we are going to provide a name for the bot over here so I'm going to call mine Emma and Emma is a bot that can help me to draft emails I'm actually much more than that but okay and we can also add an avatar for Emma so I have the Avatar over here which is by the way generated using mid-journey so this is not a real person oh so that is how you think about me so we're going to save the changes or we also need a color over here and just pick anything doesn't really matter I hate that color but thanks for asking alright so that is saved and now the next step is we are going to click on Bots over here and we are going to add some permissions so coming back to the document we can go to all out and permissions you click on all else and permissions over here scroll down and then we are going to add some Scopes so just checking again the Scopes we need app mentions read chat right and Channel History so let's add those so add an all-out scope we want app mentions read chat right and we want the history so those are the three Scopes that we want to add and now next we are going to install the bot in the workspace so we are going to basic information in the menu sidebar and then we are going to install to workspace so this is going to set up the permissions we'll say allow and this will bring Emma into the workspace and now next what we should do is we should head over to all out and permissions again so oauth and permissions and there you will see the bot user oauth token so you should copy this for now and save that for later because we are going to put this into an environment variable or you can come back here later and copy it then all right so that brings us to or that was the final step of the slack setup so now if we come to our slack workspace we can see that Emma is now in here so we can now continue with the python setup and for this let me show you like I've said there is a GitHub that you can follow that you can clone into so here are some files that we are going to work on and I'm going to walk you through them right now so first of all make sure you have set up a python environment with python 3.6 or later and install the following dependencies so we need to select SDK select bolt and we need flask and I recommend to set up a virtual environment for this or a conda environment so you can do these experiments within a isolated environment so you don't mess up your regular python environment alright so make sure you got that up and running I'll switch over to vs code right now where I have the project already set up so here you can see this is just a clone or this is the repository that is available on GitHub and we are now working in in the slack folder and we are going to work on the app.pi and the functions.pi and the code is already in here and I will just basically slowly walk you through all the steps because there are only just like four functions that we need and then you have a basic understanding of how they work and then you can start to tweak them for your specific situation so let me come back to the file over here so first we have some basic Imports and then we are going to load the environment variables but in order to do that we have to first set them in a DOT and file so if I come back to my official Studio code you see I have the dot EnV file over here and this is not in the repository this is explicitly not in there because this is where you store your keys and your credentials to for example open Ai and also for the slack bolt so what you are going to do is you are going to create the dot and file and then come back to the document or the get the page and you are going to copy and paste this a into the EMV file and then you're going to swap out everything that's between the quotes with your own information so the slackbot token starts with the XO XB that is the one we were looking at over here then you have the select sign in secret and the slackbot user ID then first what we're going to do is the slack sign-in secret that you can find by going to basic information then scrolling all the way down over here and then you can click on show that will show you the information that you have to put here in between the quotes and then now the select both user ID is something we are going to get with a python function alright so coming back to fias code let me actually make this a little bit bigger for you what I can do is I can fire up an interactive terminal over here interactive session and then now that you have the slack bot token in place so this should already be in your environment variable and then load it and then set a token and now initialize the slack app over here this should work now and now what we can do is we can use this function over here and there's probably an easier way to do this but it's nice that we can do it programmatically is this function will basically initialize the select line with your bull token and then we will get the user ID back basically of the bot so let's save this and then run just this function over here and look there it is so there is your Bot ID that you should now also put in the environment file so coming back to the example that is the slackbot user ID that is where you paste that string alright so now coming back to the code app.pi what is actually going on here so we are going to use flask and flask is a web application framework written in Python and now I am by no means a flask expert I know the very Basics and Jet GPT goes a long way in helping you to create these functions that's how I set up this uh this template basically so the file that we are looking at right now has a few function user ID we already looked at then right now we have a very simple custom function so this is the function that we are going to give to our assistant this is the task that she can perform and right now just a test it returns text Dot Upper so whatever we will say to Emma Emma will basically shout that back in all caps not a very nice personal assistant but hey just to test things out okay so then we have the app event going on over here and this is a listener for mentions in slack so this is a function that's running in the background and whenever the slackbot user ID is mentioned so you do at and then you type Emma's name this function will wait for that basically and then it will get into action so it will take the text and then it will first say something so it will first respond so Emma will say sure I got it right on that so if you ask Emma something she'll reply first I'll get right on that and then she'll process the response and which is now just the return basically of my function which is tax Dot Upper so as you can see right here you get the text Emma gets to work Emma converts the text to uppercase and then it says the response as well and say in this case just means sending a slack message within the channel Alright and then the final function is the flask app.route which basically creates a URL endpoint slash slack slash events that we can send information to so this function passes the incoming HTTP request to the slack request Handler for processing so that is all that we need right now it's very basic we need a function we need an event and we need a communication layer and then this last part is just to start running the app and now if we come back to the document we can see that we now step four can start to run our local flask server and it's good to Note coming back to the file over here if you don't specify any port in here so in the run you can also specify a port as a ninja integer um if you leave that empty it will start to run on Port 5000 so in fias code I can come over here and I can run this python file which is nothing more than just running this app.bi so here everything will start to run and you can now see that the app is running on Port 5000. all right so now with the app running we can continue to part three and that is the surfer setup which we are going to do locally for now so first make sure you install ngrok and you can do that by going to to this page over here and here you can see for the various operating systems how to do that basically I'm on Mac so I use Homebrew to install it so I run just run Brew install ngrok in the terminal and then you can start up your server so let me actually do that right now open up a new terminal over here so with your flask app running just call the command and rock HTTP and then 5000 referring to the port 5000 that we're running on so this will start up a local and rock server and basically what this will do it will create a forwarding so you can see how there is now a URL over here that the internet can reach basically and it will redirect that to our Local Host so we can basically simulate working with a real Surfer even though we are developing locally so what you're going to do is you are going to copy and paste this URL over here so the forwarding URL so now that you've copied that URL you are going to configure your Slack app with this URL so we are going to go back to our app within slack and we are going to event subscriptions and then in here you are going to enable events and these are the events that our app is going to listen for and now remember that we have the initial URL but we also specified that we want slack slash events because that is what we have specified in the app over here so we are going to listen for these events at this URL so that is what we have to put in here and then we are going to save these changes oh make sure to do that correct slash and then next you are going to scroll down to subscribe to bot events we are going to add a bot user event and we are going to select the app mentioned over here and now let's check this out alright I noticed that I had a little error in here so I had to remove the space and then it's once I fix that it becomes verified you should have your Bot defense over here and then you can save the changes all right so now the step three that we have to do is here is reinstall your slack app to update the permissions so we come back to slack over here basic information then install app reinstall to workspace just giving you permissions one more time all right and now the next step before we can actually start to test the function is to invite the bot to the workspace so coming back to slack I am here right now in the general Channel and what I can do is I can type in fight and then add and then tag Emma in this so I do invite Emma and this command will now add Emma to this channel so what I can now do is I can tag Emma in here and now with everything up and running we should be able to validate our app so make sure that nrock is running in the background and also your flask app is running in the background both on the same port and now if we say hey how are you Emma will now respond by shouting back at us and here you can see it will first give the prompt hey I'll get right on that and then it processes the task so this is the initial setup so now we have the individual building blocks working together but now of course the next step is to figure out how to make Emma a little smarter and not just a shouting robot Dave I am literally so much smarter than you with your incapable human brain right so now coming back to fiascoat I'm going to first quit the server over here by pressing Ctrl C so this will close the app because we are going to change some code and then run it again so what we are going to do right now is we are going to check out the functions.pi file and what I've done here is basically similar to my latest video where I show you how you can set up a template so a template that we will fill in and then send to GPT to basically process our Quest So within this file there's just one function it's called draft email and the use case for this is basically whenever I get an email and I feel like okay I cannot just reply within simple okay here I have to put some time and effort in here to not like be really rude but basically I just want to say no or whatever I can copy paste that email and can send it to Emma and Emma can draft an email for me and then I can copy paste it again and then go from there basically that is the ID over here it's very simple setup but like I've said the possibilities are endless here but it shows some nice functionalities that you can start to play around with on your own so we first open up a new chat by calling the chat.openai model basically from Lang chain so this is the template that we are going to input in the system message prompt so this we will provide to the system so you are a helpful assistant can draft emails your goals to help draft a reply keep it short and to the point and mimic the style of the email so reply in a simpler manner to match the tone this is already pretty interesting because then you can if you make it adaptable to whatever's coming your way and it's not just always like the same style and start your reply by saying hi and then the name so you can fill in your own name here you can even get that from slack probably but for now I've just hard coded it in here and then here's a draft for your reply and then proceed with the reply on a new line and also make sure to sign off with the signature so you can put your signature in here so kind regards to the name again so you can see you can you can set this up and if you every time whenever you want to draft an email have to come to chatgpt and then for example give all this information again that of course takes some time and now we can think about this one time write it down code it down and then go from there so that is how this function basically works and if you want to learn more understand more about this then I recommend you check out my previous video on how I use Link Chain because that will make things more clear but right now what we are going to do we are going to come back to the app.buy and as you can see there's already an import in here for draft email which is this function now we are going to scroll down and we are going to the handle mentions function and then instead of my function we are going to say draft email so here you can also see how easy it is to swap out tasks for Emma to do and also how easy it would be to give Emma multiple tasks that she can pick from so that's an all other story something for future videos but you can create multiple functions and then let Emma figure out on her own which function to use based on your message to her basically language will get really interesting and future videos on that because I'm going to build Emma out with lots of cool exciting things that she can do so with that out of the way we can now start up the server again I'm going to run this again so now we are up and running again and let me quickly check so end work is still running I haven't touched that but I'm actually not quite sure if we should restart that as well so let's just provide the following very short email reply to Emma so as of lately I've been getting tons of requests from companies people like hey can you help me with AI projects hey can you help me with this to build your own app so these are typical emails that I get now usually they're a bit longer than this but let's see if Emma can help me to prepare better prepare for these emails so let's send that right now and see what Emma can come up with so she will say sure I'll get right on that and you can see right now it takes a bit more time because now we have to Boom there it is now we have to make a connection with the open AI API and send the information back and forth so here it is hi Dave here's a draft for your reply so we've instructed her to answer like that so coming back to the template over here we say like start your reply by saying hi Dave in this case here's a draft for your reply and then proceed with the reply on a new line so that's how we've instructed Emma to behave and she's doing that very well so hi send their name thank you for considering me for your AI project at the moment I am open and available for new projects could you please provide me with more details about your project this will help me to better understand your needs and how I can assist you beautiful right these are literally the kind of messages that I send out to people almost every day like asking more information like what is it that you need help with but now let's see if we come to the human template I've also engineered this in such a way that Emma can also interpret any other comments that I add so here's the email to reply to and consider any other comments from the user for the reply as well and that is the user input basically so that is what I say to Emma so if I now come back and do this like one more time so I could say hey Emma and this is the email and then add like say that I am not available right now we provide Emma with additional context and then here's a new reply thanks for reaching out to me unfortunately I'm not currently available for new AI Projects please feel free to get in touch with me in the future and we can discuss further opportunities so we can give Emma additional instructions as well and she will understand that just like you would interact with real people within slack with real people from your team so and then the final step is of course come up with your own ideas what are you going to make with this to me like this is really exciting like when I figured out these individual building blocks my mind basically exploded with ideas basically of what I can do with this so my idea is literally to create potentially multiple Bots and give them a specific set of tools that they can help me with so probably think in terms like copywriting marketing or coding and create a bot for each of those areas and then really start to experiment with functions and workflows that I can provide to these Bots and then if you're wondering right now like why not just use chap GPT for this you can also do this in chat gbt yes that is correct with this example you get but the key is to really engineer your templates in such a way that they can save you time that is one and also think further than that with this and with the combination of length chain you can use the built-in tools from Lang chain as well for example to also connect to the Internet or you can connect to a personal database or a company database to access information that is not available using chatgpt and slack right now Prof fights a human machine interface basically to connect with these data sources and also the open AI API so to me this is really exciting like this is for me like the integration layer because you can build all kinds of scripts but then when you have to run it you like have to start up like your IDE and figure out which script is it and then insert the parameters but right now you basically have an app without really having to create a web app if you get what I mean and of course this is running locally right now using a simulated server using engrock but in the next video I will show you how to actually set this up on a real server and you can just keep it running in the background and then just update your functions whenever necessary and you can always access Emma in this case whenever you want so that will be a really exciting video as well and I will also start to build out Emma's toolkit and I will all share this with you so maybe the tools that I will be building for my personal assistant will also be helpful for you who knows so if you want to stay up to date follow this journey then make sure to subscribe to the channel and also if you found this video helpful then please like this video really helps me out and also you let the YouTube know that you want to see more content like this so win-win right and really if you learn these tools experiment with this so like python Lang chain connecting with apis creating web apps then you can really set yourself up for some great opportunities right now and potentially make a lot of money because companies are really hyped for this right now ai is hyped everyone wants to jump on this trend and everyone wants their own like smart agents and integrated with with company data and if that is something that you want to do but don't really know where to start then the data freelancer Mastermind might be something for you so you can check this page out first link in the description and if you want to know how to turn your data skills into a six-figure business then you should go to this page and check out what this guy has to say and so data freelancer is a mastermind to help data analysts engineers and scientists to launch and scale a freelancing business you'll be working together with me over the course of a year so 12 months and within that year we are going to start Landing your first paid clients now I only work with professionals that already have experience so at least one year of working experience in a data related field and you must be ready to start applying your skills to freelance projects so this is not a Technical Training I'm looking for data professionals that want to hop on freelancing right now but don't know where to start or just want to do it with a group do it with me have more fun that way so if that's something you're interested in check out this page watch the video first link in the description alright and that's it for now thank you very much for watching we'll do a final test right here we'll see if we can override Emma so I've now asked it to provide some advice for you for my viewers so Emma is working on that right now in the meantime you may smash this like button and then here is what Emma has to say dear viewers I hope you enjoyed watching my video on how I built an AI model from scratch as promised I'd like to share some advice on the data science and AI firstly always start with a clear objective and understand the problem you're trying to solve this will help guide your approach and ensure you're working towards a specific goal secondly data preparation is fundamental make sure you have a good understanding of the data you're working with including its quality quantity and format this will help you make informed decisions throughout your analysis and modeling lastly always test and evaluate your models thoroughly this will help you understand how well your model is performing and whether it's delivering against your objectives I hope you find this advice useful good luck with your own data science and AI projects now like this video And subscribe to Dave's Channel
Info
Channel: Dave Ebbelaar
Views: 26,358
Rating: undefined out of 5
Keywords: data science, python, machine learning, vscode, data analytics, data science tips, data science 2023, langchain, slack, slack bot, slack bot python, ai assistant, gpt 4, openai chatgpt, python tutorial, artificial intelligence, artificial intelligence course, langchain tutorial, langchain demo, gpt 4 openai, gpt 4 coding, gpt 4 ai, gpt 4 plugins, gpt 4 vs gpt 3, vs code tutorial for beginners, ai bot, productivity, openai api python, langchain introduction, chatgpt
Id: 3jFXRNn2Bu8
Channel Id: undefined
Length: 28min 31sec (1711 seconds)
Published: Sat Apr 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.