================================================================= TITLE: Untitled DATE: 2024-10-08 (October 08, 2024 at 11:46 AM) DURATION: 01:20:13 (4813 seconds) WORDS: 16,449 words SPEECH ID: 26XPVAWW4PBT3NWV ================================================================= --- AI SUMMARY --- live kit, environment variables, deepgram API, OpenAI API, function calling, vision capabilities, chat context, voice assistant, speech to text, text to speech, hosted playground, virtual environment, Python script, real-time API, Twilio integration --- FULL TRANSCRIPT --- [00:00] 0: Okay, take a look at the source code. It's a very, very simple source code. Using live kit I have, I think, 139 lines, including extensive comments that I added, just so you don't get lost. So this is actually pretty simple to follow. So before we run this the source code. Make sure you follow the instructions in the readme file. Just very simple. You have to create a virtual environment, install the libraries, and you need to set up a few environment variables. First you need livekit environment variables. Go to livekit, create an account. It's free. You're going to be able to grab these three environment variables, and you need to set them up. Then you need an API key for deepgram. We're using deep gram to turn my audio, or my voice, into text. It's going to be free as well. And finally, you need an open AI API key, because I'm going to be using GPT four. Oh, for this demo. After you do that, here is how you run the assistant, and after the assistant is running, and I'm going to show you how to do that. I'm going to be using the hosted playground that livekit offers to connect and give access to the assistant, to my webcam and my microphone. You could build your own user interface if you wanted to live. Kit has plenty of examples of how to do that. I'm not going to do that. I'm just going to use the hosted playground for simplicity. So let's go to the assistant here. Now, one thing that I want to mention before I start going down every single line of code the way this assistant works high level is I want to be chatting with the assistant, giving the assistant only access to my voice, or basically the text generated off of my voice. I don't want the assistant to get access to the webcam, because that will be a little bit expensive if every request that I send the assistant contains an image from my webcam. I'm going to be sending a ton of data for no reason. Instead, what I want to do is only offer the assistant access to the image whenever the assistant requires that image to answer a question. That way, we can limit the amount of information we send over the network, and we make this interaction way more fluid, way faster than if we have to send the image all the time. So imagine I asked the assistant tell me a joke. I don't want to have to provide an image to the assistant, because the image is not relevant for the assistant to tell me a joke, or if I want the assistant to answer how much is two plus two? I don't want to send an image, but if I ask the assistant, Hey, am I wearing glasses? Look at my webcam. I want the assistant to be able to grab an image from the webcam and look at it in order to answer the question whether or not I'm wearing glasses. So how do we do that? Which is going to be very simple. Basically, we're always going to chat just with text, except when the assistant thinks it needs an image. So how do we do that? Using Function calling. Remember, we have the ability this model support the ability to call functions outside of the assistant's purview. So whenever we ask the assistant, hey, look at an image, or look at my webcam. We want the assistant not to answer the question, but to respond back with a call to a function saying, I think you need to call it function. This is the name of the function that you need to call, and this is the context, or the text that triggered that call to the function. If we do that, we could capture that in that function, we could resend back the request to the assistant now with an image. So that way, that extra round trip will allow us to save a ton of traffic to the assistant. So to explain how I did that, let me show you this assistant function here, very, very quick. It's a class, and this class is supported by livekit to implement any function calling is super cool, super easy to understand, so it has to obviously extend the function context class, and here I can specify different functions that my assistant will have access to call, okay, my LLM will have access to call. You do that through this callable AI, callable, sort of like decorator here, and you specify the description, and remember this description, the agent will have access to this description to decide whether or not to call this function. Okay. In this case, the description is, hey, this should be called whenever you're asked to evaluate something that will require vision capabilities, for example, an image, video or the webcam fee. So we are going to pass this part of the prompt behind the scenes. This is going to go as part of the prompt to the LLM, and the LLM will look at this and if it notices that the request it requires vision capabilities, the assistant will answer back not with a message, but by asking us to call a function. And the function that's going to be called is going to be this one here, so I'm calling here image. So that is the way this is going to work. By the way, you can use function calling to do all sort of cool stuff. Let's say you want an assistant to be able to do complex math equations, right? So, instead of letting the LLM hallucinate some stupid answer, you can have that LLM to call notice, you know, realize that you want to solve a math, a complex math equation, and call a function in your code where you can actually solve the equation and give the answer back to the LLM. So that's the way, or one of the ways function calling can help. All right, so in this image here, you're going to notice that the only thing that I'm doing internally is storing a metadata variable that's called user message. So whenever the assistant or the LLM realizes that it requires vision capabilities to answer a question. It's going to call this function. I mean, it's not going to call directly the function. It's going to answer back saying, please call the function on the platform lifegate is going to call this function, and this function is going to receive the original message, the user message, the user message that triggered the function call, and we're going to store that user message in a metadata variable. That's it. That's the only thing. It will become clear how I'm using that in a few seconds. Okay, I just wanted to get this out of the way, because that model is something very different from what I did before, and it's super cool. All right, so with that out of the way, let's take a look at the main entry point, which is right here, what I'm calling. Or the thing that matters is that I'm calling a function that's called entry point where everything happens. So within that entry point, I'm going to go to that entry point here. Within that entry point, first I'm just printing the room name that we're connecting to, and then I'm creating a variable that creates a chat context, and this is what's going to allow us to keep a conversation with the LLM, okay. And I'm going to initialize that chat context with a system message. This is how you inject personality into your LLM, okay, through this system message. In my case, I'm saying, Hey, your name is alloy, and you're witty and you're funny, and you should answer with your answers. That's it. That is my chat context. I'm initializing it like that. Now I'm defining my GPT, my LLM, I'm using GPT for, oh, it supports text and it supports images, which is very important for what I want to show. And then I'm creating an assistant. I skipped over a couple lines here. I'm gonna get there in a second my voice assistant here, I need to define a bunch of properties for my assistant to work. First, a voice activity detector, which is gonna allow us to just detect whenever there is sound. And I'm using silero, I think it's pronounced like that. Don't kill me, please. It's open source. You can look it up. Celera is going to help us do that for the speech to text conversion. I'm going to be using deep ground, which is amazing. Again, I'm using the free tier. You need an API key for that to work. For the LLN, I'm using the GPT four. Oh, that I just defined for the text to speech. So turning text into audio to play through my speakers, I'm using the OpenAI API, and that's the definition that you see right here. Notice that I'm specifying that this is going to be using the alloy voice. Then I'm defining the class that's going to help us with the function calling, and that is the assistant function that I just explained. And finally, I'm passing the chat context variable so the initialization or the conversation that this voice assistant is going to keep with an LLM. That's it. That's my assistant. After that, I need one more variable to define the count, the chat manager. I'm going to call it chat. This is just going to keep that chat going, and I'm initializing it to the room. There are three functions, internal functions defined here. I'm going to go one by one when it's time. So I have an answer function, and I have a couple of events, or two more functions that are decorated with this weird thing is, I'm going to explain what they are in just a second. Skipping over those. I'm starting the assistant. Then I'm just sleeping here for a few moments. I basically want the assistant to have a second to breathe, catch on, connect, do everything that it needs to do, and then I'm going to play Hi there. How can I help you this calling say, basically plays that audio through my speakers. Okay, and that is the initialization of everything here. Notice that I'm passing allow interruptions in true meaning that at any point in time while the assistant is speaking, we can just interrupt it, which is awesome. Finally, just finalize what happens in the main thread. We have a while loop that is going to happen, or is going to just work, while we are connected to a room, and inside that audio loop, I'm going to be getting a video track, okay, from that room. This is a very simple function. Let's go to get video track so you can see actually what's happening. So the video track. First, it creates a variable here, video [2786:13:20] 0: that track, and just break the loop and just return that so pretty I'm about to show you today with [2836:05:20] 1: you're holding what looks like a can. I'm holding [2844:37:20] 0: a magazine, and there is to call this function. Keep the system function here. Very, very quick. It's a class, and this class is supported. Start, All right, so let's take a look at the source code. You need an open AI if it has played, basically the text generated off of my voice. I don't want the assistant to get access to the webcam, because that will be a little bit expensive. Every request that I send the assistant contains an image from my webcam. I'm going to be sending a ton of data for no reason. Instead, what I want to do is only offer the assistant using function calling text, except when the assistant thinks it needs an image. So how do we do that? Using Function calling? Remember, we have the ability this model support the ability to call functions outside of the assistance purview. So whenever we ask the assistant, hey, look at an image or look at my webcam, we want the assistant not to answer the question, but to respond back with a call to a function saying, I think you need to call it function. This is the name of the function that you need to call, and this is the context or the text that trigger, that call to the function. If we do that, we could capture that in that function. We could resend back the request to the assistant, now with an image, so that way that extra round trip will allow us to save a ton of traffic to the assistant. So to explain how I did that, let me show you this assistant function here, very, very quick. It's a class, and this class is supported by livekit to implement any function calling. It's super cool, super easy to understand. So it has to obviously extend the function context class, and here I can specify different functions that my assistant will have access to call, okay, my LLM will have access to call. You do that through this callable AI, callable, sort of like decorator here, and you specify the description, and remember this description, the agent will have access to this description to decide whether or not to call this function. Okay, in this case, the description is, hey, this should be called whenever you're asked to evaluate something that will require vision capabilities, for example, an image, a video or the webcam fee. So we're gonna pass this part of the prompt behind the scenes. This is gonna go as part of the prompt to the LLM, and the LLM will look at this, and if it notices that the request requires vision capabilities, the assistant will answer back, not with a message, but by asking us to call the function. And the function that's going to be called is going to be this one here. I'm calling here image. So that is the way this is going to work. By the way, you can use function column to do all sort of cool stuff. Let's say you want an assistant to be able to do complex math equations, right? So, instead of letting the LLM hallucinate some stupid answer, you can have that LLM to call notice, you know, realize that you want to solve a math a complex math equation, and call a function in your code where you can actually solve the equation and give the answer back to the yellow line. So that's the way, or one of the ways function calling can help. All right, so in this image here, you're going to notice that the only thing that I'm doing internally is storing a metadata variable that's called user message. So whenever the assistant or the LLN realizes that it requires vision capabilities to answer a question, it's going to call this function. I mean, it's not going to call directly the function. It's going to answer back saying, please call the function on the platform lightgate is going to call this function, and this function is going to receive the original message, the user message, the user message that triggered the function call, and we're going to store that user message in a metadata variable. That's it. That's the only thing. It will become clear how using that image, you say, Okay, I just wanted to get this out of the way, because that model is something very different from what I did before, and it's super cool. All right, so with that out of the way, let's take a look at the main entry point, which is right here, what I'm calling, or the thing that matters is that I'm calling a function that's called entry point where everything happens. So within that entry point, I'm going to go to that entry point here. Within that entry point, first, I'm just printing the room name that we're connecting to, and then I'm creating a variable that creates a cat context, and this is what's going to allow us to keep a conversation with the LLM, okay, and I'm going to initialize that cat context with a system message. This is how you inject personality into your LLM, okay, through this system message. In my case, I'm saying, Hey, your name is alloy. You're witty and you're funny, and you should answer with your answers. That's it. That is my chat context. I'm initializing it like that. Now I'm defining my GPT, my LLM, I'm using GPT four, oh, it supports text and it supports images, which is very important for what I want to show. And then I'm creating an assistant. I skipped over a couple lines here. I'm going to get there in a second my voice assistant here. I need to define a bunch of properties for my assistant to work. First, a voice activity detector, which is going to allow us to just detect whenever there is sound. And I'm using silero, I think it's pronounced like that. Don't kill me, please. It's open source. You can look it up. Silero is going to help us do that for the speech to text conversion. I'm going to be using deep ground, which is amazing. Again, I'm using the free tier. You need an API key for that to work, for the LLM, I'm using the GPT for O that I just defined for the text to speech, so turning text into audio to play through my speakers, I'm using the open AI API, and that's the definition that you see right here. Notice that I'm specifying that this is going to be using the alloy voice. Then I'm defining the class that's going to help us with the function calling, and that is the assistant function that I just explained. And finally, I'm passing the chat context variable so the initialization or the conversation that this voice assistant is going to keep with an LOL, that's it. That's my assistant. After that, I need one more variable to define the content chat manager. I'm going to call it chat this is just going to keep that chat going, and I'm initializing it to the room. There are three functions, internal functions defined here. I'm going to go one by one when it's time. So I have an answer function, and I have a couple of events, or two more functions that are decorated with this weird thing is, I'm gonna explain what they are in just a second, skipping over those I'm starting the assistant. Then I'm just sleeping here for a few moments. I basically want the assistant to have a second to breathe, catch on, connect to everything that it needs to do. And then I'm gonna play Hi there. How can I help you this calling say, basically plays that audio through my speakers. Okay? And that is the initialization of everything here. Notice that I'm passing allow interruptions in true meaning that at any point in time while the assistant is speaking, we can just interrupt it, which is awesome. Finally, just finalize what happens in the main thread. We have a while loop that is going to happen, or is going to just work, while we are connected to a room. And inside that audio loop, I'm going to be getting a video track, okay, from that room. This is a very simple function. Let's go to get video track so you can see actually what's happening. So the video track, first, it creates a variable here, video track, and then it goes through every participant connected into this room. That participant is going to be me through the playground that I'm going to be showing you. For every participant, I'm going to be grabbing every single published tracks. So for example, audio and video track. So if I have a microphone that's going to be a track, if I have a webcam, that's going to be a track. So I'm grabbing every single video track, and then I'm asking, Hey, if that video track is a remote video track, set this variable to that track and just break the loop and just return that so pretty much high level, what this function is doing is grabbing the first webcam that's connected to that room, and that is going to be the video track that we're going to use for the assistant to see what's happening around me. If you have multiple participants, you will have to change this function a little bit, but for now, this is just fine. All right, let's go back. So after I grab that video track, it's a webcam. Every frame that gets generated is going to be an event, and this is what's going I'm going to do here. So for every event generated by that track, I'm capturing it. You can see here, just wrapping that track into a video stream, I'm grabbing that frame, and I'm storing that frame in this variable that I defined before this latest image. So the end result here is that any point in time whenever I need access to my webcam, I can just use latest image, and that is going to be guaranteed to have access to the latest image published by the web track. One thing that is very important here to keep in mind, many people talked about this and they're confused. These assistants do not support video that is just an illusion. When you see these assistants working and they answer questions from a webcam or whatnot, it looks like they actually see they actually have access to video. They don't. They only receive images and text. So what I'm doing here is just making that illusion possible. I'm capturing a webcam, grabbing those frames from the webcam, and whenever I want to answer a question from one of those frames, I sent the image and the text, and it looks like the assistant has full access to my webcam. It does okay. So hopefully that's clear. This is the main track of the program. Everything else happens through events. A couple events here that we want to take care of. First event is called a message. We see this event gets triggered whenever there is a new message that we have to answer to or we want to send to the chat. So whenever I speak and the voice assistant captures my voice and turns that into text. It generates this on message receive event, and obviously this chat message here will contain the text that we want the assistant to answer. So whenever I get this event, I'm basically going to just call the function answer, and we're going to see that function answer in just a second, but basically I'm passing to that function the message, which is the text that I want the assistant to respond to, and I'm going to be saying, Hey, don't use an image, because this was just a normal text. There is no indication that the assistant requires vision capabilities to answer this question to respond to this text. So don't use an image. And there is a second event which is on function called finish. This event is going to happen whenever a function called finishes execute it. Remember that I talked at the beginning of this assistant function class, that our assistant is able to realize, hey, I think I might need vision capabilities for this. So instead of answering this question, I'm just going to call it function. That is what's going to happen when we call this function here. And remember, internally, we're storing the message that triggered that function call in a variable called user message. After this call is done, an event is going to get triggered, and that event is called, function calls finished. So we're going to capture that event, and we're going to grab the variable that we just stored as metadata. We're going to grab it. Remember, this is the original request, the original request that I spoke and made the assistant believe that it needed vision capabilities. Now it's right here in this variable, and now I'm just going to answer that, but this time I'm going to specify that, yes, I need an image. See that extra round trip. I asked the assistant a question. The assistant said, Hmm, I think I need an image to answer this. So it's going to call a function in my code, or basically respond with a function call in my code, and then what I'm going to do at that point is, oh, I'm just gonna send back an image to the assistant. Then same request, but now with an image, so now the assistant has everything it needs to answer the question, Does that make sense? Yeah. So these are the two functions, the two events, and both of them are calling this function, which is called Answer. Very simple function. Okay, so look at this. The answer function receives the text and it receives the Boolean indicating whether or not we want to pass an image. The way I pass an image or don't pass an image is just by creating a dictionary and saying, Hey, if you're saying that I should use an image and there is an image that it captures. So if you don't have a webcam, this doesn't blow in your face. I'm just going to be adding a property, adding an attribute to this dictionary called images, and I'm going to be passing an array with a chat image defined by the image here, you know, with the image the latest frame captured from my webcam. So this is what I'm going to be passing here, and now I can take, I can use the chat context and append a message. This is just going to be a collection of messages that I'm going to keep, adding, adding, adding, adding. Here I'm going to be passing a message to this chat context. The role is going to be user, because it's the user, the one that sending this message. It's not the system. The text is just the text that I received, and I'm going to be passing the list of arguments here, which is going to be either images or nothing. So I need to use an image. The images are going to get passed here. After passing that, after updating my conversation, my chat context, with the new message, I can just call chat from the LLM that I define and pass the conversation. So this is going to get sent to the LLM. The LLM is going to realize that the latest message is from the user. So it has to do something. It's going to basically answer. I'm going to capture the stream, the answer stream, right here, and I'm going to ask the assistant to play that stream through the speakers. That's it. That's pretty much the whole deal. So let me show you how to run this, so you can do it yourself. So you're gonna execute this, right? So Python assistant.py, which is the name of my file, and you're gonna call start. So I'm gonna do that, and this should now be running. It's funny that this is telling me the region is France. That's because I created this system before on that region. I was in France when I created it for the first time. Okay, so this is the playground, and there is a link in the readme file that you can open this playground. Just need to make sure you connect to the same room. Just very straightforward. So I'm going to click Connect, and from that point on, I'm going to be giving access to the agent, to the webcam and to my microphone. So let's see how this goes. Let's click Connect. Hi [6995:28:00] 1: there. How can I help? Hi, [7002:24:00] 0: what's your name? [7008:00:00] 1: I'm alloy. Nice to meet you. [7018:24:00] 0: Awesome. Alloy. Can you look at the webcam and tell me how many fingers I might show you. [7039:49:20] 1: You're showing five fingers. [7049:25:20] 0: That's awesome. Can you tell me what color is my watch? Your watch is blue? That's great. I'm holding something on my hand. Take a look at the webcam and tell me what it is. [7096:21:20] 1: You're holding a comic book. So [7106:29:20] 0: as you can see, that's pretty awesome stuff. I hope you enjoyed it if you really liked this video and you want more content like this, just please click. on the left [7151:28:00] 2: AI voice agent using the new open AI real time API. So open AI recently introduced the real time API, and this is super valuable for speech to speech experiences. So as you saw on a phone call before, I was able to dial a number and I was able to speak with an AI assistant to book in a time for me to get a car service done. Now, even though a lot of people are saying that this pricing is actually pretty expensive for the voice to voice API, I think that this pricing is going to come down, probably relatively soon. And when it does the demo that you just saw, it was basically using Twilio to generate a phone number and then to make that connection with the real time API. And that's a direct competitor to tools like varpi or bland it might not be as good as in the back end to kind of create those experiences. But as you can see, using OpenAI now you can create a chat assistant. You can plug it into your emails to automatically reply to emails. You can use the API to do completions and process files, for you to review data, for you to analyze your Excel sheets. And now you also have a voice API, so if you wanted to, you can use OpenAI for your ecosystem of AI integrations and all of your AI needs. So while it might be a little bit expensive and maybe not as usable or easy to use as the different AI tools, it's kind of looking like the trend is going towards OpenAI creating a full service experience where people that know how to code and also don't know how to code can really integrate AI into their workflow. So this is pretty exciting, and yeah, it's a little bit expensive now, but I think it's going to come down in the near future. Now, the final thing I want to point out is, on that demo call, pretty much as soon as I finished speaking, I already had a response coming back to me from the API, and that's thanks to the real time API, then you create a persistent WebSocket connection. So for real time conversations, for real time communication, like live chat or now this voice API, the WebSocket is the main mechanism that lets you communicate instantaneously. So let's say a regular API call where you're speaking with the OpenAI assistant. It's like you have to send the postcard to communicate. I've got to send the postcard to you. You've got to wait for it to be delivered to you, then you get it, then you write your next response, and then you send the postcard to me, and I've got to wait that for that delivery time. The website removes that delivery time by pretty much creating a portal between you and me, where as soon as I send a message, you immediately get it. There's no lack there's no downtime. So that's how this communication is real time by using this website. And another thing to point out is that you can still do function calling with this API. Now, coming back to that demo that I just showed you guys, I created a GitHub repo where I'm going to show you exactly how to download that into replet, how to deploy it, what kind of settings change probably takes you, like, five minutes total. And then when I show you how to create your Twilio account and actually add a number, and then, like, everything that you need to do to plug those two things in together so that within the next 510 minutes, you can actually go on your phone and then call your own AI assistant. So watch this entire video. That's all going to be explained, but just quickly, I'm going to show you what the functionality is of the assistant that I created. So as you saw before, it actually takes inbound phone calls. So we use Twilio, we generate a phone number? We plug that phone number into the API, and then you can call it on a phone and then speak with it. Then I've prompted it to capture some customer information, so customer name, the availability, and then whatever service work the customer needed. And as you can imagine, using that prompt, you can adjust this to whatever your business needs are as well. And then one thing that I haven't shown you yet is that I actually plugged it into make.com so that I can get that data from my phone call and then do whatever else I want with it. Like in make.com you already know you can plug into like any of your other tools, do absolutely anything you want, create different flows, automations. So I'm basically sending those details to make.com and for my example, I'm just updating a Google Sheet, and that's what I've got here. I've just got three columns, I've got name, I've got the availability, and then I've got some notes as well. And yeah, this was that most recent one. So I wanted to book an appointment for 1pm on Tuesday to have a regular service, and then check my breaks. And as you can imagine, you're going to have a lot of different use cases. As long as you can get that data into make you can do pretty much anything you want to do. And then finally, we have session management, so this can handle simultaneous phone calls all at the same time. I think there are some limitations in the API at this stage, where, if I'm not mistaken, the max phone call length you're allowed to have is 10 minutes or 15 minutes, which I don't think you kind of cross over that. But then also, if you're on like a tier one tier two account, there's, like, a limitation on how many phone calls you can have at one time. And then the API has rate limits anyway, but anyway, this is probably going to get outgrown really quickly. So if you're watching this video, you might have a couple of these speed bumps to start with, but then moving forward, it's going to get, you know, you're going to get a lot more breathing room. But really, I don't think you're going to run into any problems. But yeah, this session management lets you handle multiple phone calls at one time. So if I have a phone call on Twilio and then three other people having the same phone call, the information on that replicate server is not going to get mumbled up, jumbled up, so my name isn't going to be used for that third person, and that third person's car service isn't going to get muddied up with whatever I need to get done. It's just going to be all separate calls and separate sessions. So yeah, this is actually doesn't seem like much, but this is actually really cool to have, especially if you want to launch this starting today for your business. Now we're going to be using replit to basically take that GitHub repo, we're going to run it on replit, and then we're going to be, yeah, basically all the calls are going to be routed to replit. And finally, I have the GitHub repo here, which we're going to be going through in a second. I'll also explain all the code, and once again, I'm going to show you how to plug it into Twilio. So it's going to be super easy. Actually, the Twilio side of things is going to be very, very easy. Even if you have just a brand new account, you can get this working straight away. Okay, so just quickly. How did I build this out? So I first found Twilio is YouTube video like the day of or the day after, the real time API was introduced, and Twilio created a repo to write this in JavaScript. So they deployed this using node, and which is my favorite language. You can also, I think they've got a repo for Python as well, but you can watch this video where they explain the code in detail, just breaking it down, step by step, like what each part of the code means. I'll also explain in my code, which was built off this code, what each of the parts means. But if you do want more details, come to here for sure, the only thing is that the guy that's on there in this video made the code on his personal computer. He didn't deploy it on a server like rebel. Now they have an extra step in here where they have to expose their code to the internet so that the Twilio number can target that specific code, and that's why they're using this thing called ngrok. But we're not using this because on replet, we're going to expose it directly to the internet. So we're actually saving a couple steps here as well. So this is Twilio. Is GitHub repo. Just want to give credit where credit's due, because this is, again, what I started with, and what I actually used to build out my code. So yeah, definitely jump into here. Check this out. This is the main file, the index.js and come into here and look at the code, and you basically see that my code is very, very similar to this, just has a few different things. And then my repo is over here. I basically used a fictional automotive store, so bots automotive and I made a couple different features into here to basically capture the user's information and send it across to make.com there are some key differences between this code and the Twilio code, but just like upgrades things that I wanted to increase the functionality. Okay, so how do we actually launch this in replit? Okay, so over in replit, I'm already logged in, but if you don't have account already, just sign up. You can sign up for free. Otherwise, it's 20 bucks a month. That's the plan that I'm on for replit core. And I've got heaps of things running in replet, heaps of things that I deployed. I build all of my clients projects in here. And whenever I build that code that has to be available online, then I only use replet. This is like the very beginner friendly way to serve code online. Okay, so I'm going to go to create a REPL, and then I'm going to go to Import from GitHub, and then I'm going to go to from URL. So you guys will be doing this as well. You go to from URL, and then when I click on this green button, and we're just going to copy this button here. So let's click this copy to clipboard back in replet. Let's paste it in, and let's go import from GitHub. All right. So when you import from GitHub, pretty much all the code that I have on GitHub is going to be transferred across into this repo. So let's go confirm and close. And as you can see on the left hand side, all the files that I had in the GitHub are now transferred across into this replica. So all I had to do was pretty much just press that confirm button, and now I'm like, 95% complete, which is a few more things I need to do to get this up and running. And before I do that, I'm just going to pull this bottom screen and bring it to the right hand side. So now my interface is a little bit cleaner, and I'm going to go back to GitHub. So scrolling down, over here, I've got a command to run, and that's npm install. That's going to install all the dependencies that I need for my app in order for it to run. So I'm going to click here and copy this command and back in rep, I'm going to go across to shell, and I want to paste this command in and hit enter also. Now I have all the dependencies installed. So what is the dependency? Now, if I go to this package, dot json file, I've already listed out all the dependencies that we need for this app over here. So that's why, when we ran this command, it looked into this file, and it basically installed all this stuff so that we can actually run this app. Now the last thing that we need to do is create a new file which is going to be dot env. And in the dot env file, we're going to put our open AI API key, so I'm going to go back to index.js and our variable for the open AI API key is this, so back in the dot env file, I'm going to paste it into here and put an equal sign. And now in my open AI account, I'm going to go to dashboard, and then I'm going to go to keys, and I'm going to create a new secret key. I'm going to call this Test dash, Bart, create secret key. Copy this hit Done, and I'm going to paste it into here. All right. Also hit, run, perfect. This is the message that we're expecting to be printed when this is available online, and this is the URL that we're going to be using right now in this test development. And I'm going to show you how to actually deploy this more permanently later on. But for testing, we'll be using this URL so it's already exposed to the internet. And yeah, pretty much everything here is done for us. I will say that right now in this index file, we are using my preset stuff, so bias, automotive and some other things that I've configured here, but we're just going to set up Twilio now test out the phone call, and I'm going to come back into here and explain the code for you. So this is Twilio is website, and again, we're going to be using Twilio to generate that phone number for us so that we can then plug into our AI agent to go to twilio.com and then create an account by clicking Start for free. But I'm just going to click Login because I already have an account. Now, if you create an account for free with Twilio, you already get a phone number, and I'm pretty sure it's a US based phone number, and even though I'm in Australia, actually had a US phone number as well. So yeah, I think the default is going to be a US phone number, and you get like, 15 bucks of credit. So you can actually run this demo by using a free account. Didn't have to pay for anything just yet, but you cannot add a new number until you upgrade your account. And I think it's a deposit like 20 bucks. So I've actually turned off my US number, and I've upgraded my account because I wanted to get a couple numbers from my AU. So then click on your region where your phone number is, then click on phone numbers, then click on manage, and then click on active numbers. Mine's already popped down, so I'm going to click on active numbers. So your default phone number, if you just created an account, should have the voice capability. So you'll be able to see this little phone icon. If you don't see the phone icon for whatever reason, just you might have to, like, delete that number or repurchase a new number, but we need this in order to actually make this work. And yeah, I'm pretty sure, by default, you should already have this in your free number. So then all we need to do is click into one of these numbers. On this top one, and then you see a page like this, and then click into configuration. So again, this is my au number. I think the U number had different options here. The names were slightly different, but they do the exact same thing, just make sure you choose the one that has webhook, and I've got webhook over here. So what we're pretty much doing is saying for this phone number, when someone calls that phone number, so when a call comes in, we want to make a webhook request across to a certain URL with a certain HTTP type. So this is like making an API call, and we're making this API call to our rep let server so that we can communicate with our real time API. So all we need to do is go back to replet and then over here we copy this URL, then back in Twilio. Let's paste it into this first option here again, this is for when a call comes in. Now we're not done just yet. We still need to report something at the very end of here. So back in replet Now go to your index.js file if you're not already there, and then scroll down a little bit until you see this. So incoming dash call to just to highlight it like this so you can see it. And we need to append this incoming dash call on the end of this URL that we put into Twilio. So I'm just going to copy incoming dash call back in Twilio. I want to paste it in here. So why do we have to append this little bit at the end? So this URL is actually just targeting this app in general, and over here, this forward slash incoming dash call is basically saying, hey, Twilio, when you're sending your phone call to us, yes, we're sending it to this app, but then we want to plunk it into here. So from this code onwards, we want to insert your Twilio call that you're forwarding and then do everything else with it. And again, I'm gonna explain the code in a second. That's why we're appending this to the end of the URL in Twilio. So now that we're done, we have our URL with the incoming call appended to the end. Make sure that we're using the post request. And now let's hit Save configuration. Awesome. Now that everything is saved, let's go back into replet, and we've already got this deploy. Got this deployed. So again, I actually just hit run here, but if you haven't hit run, I'm just going to stop this to show you, just hit run here. And now we've got this running, and it's available to access by the internet. So I want to make a phone call, and we're going to be looking at these logs over here. So let me just make this bigger, and I'm going to be getting logs of whatever's happening in the code. So the first line should say incoming phone call, and then probably get some different logs into here, like the transcript of the conversation as it's happening, and a bunch of different stuff. So let's actually give this a try. Okay, so let's actually give this number a call. So I'm just going to plug this in directly into my phone, and let's give it a call. Hi, you have called Bart saw at [10882:02:40] 3: Commerce center. How can we sell [10892:10:40] 2: it? Hi, I want to schedule a service from my car. Of [10912:31:59] 3: course, I'd be happy to help with that. May I have your name please? [10927:44:00] 2: Yes. My name is Bartholomew, [10941:52:00] 3: thank you, Bartholomew, what type of service or work does your car need? [10965:20:00] 2: I want a full service, and I also want a car wash. [11001:57:20] 3: And you also want to r sorry, could you please complete your request? It seems like it got cut off. [11032:37:20] 2: I would like my car to be washed as well. [11047:01:20] 3: Absolutely we can add a car wash along with the full service, when would you be available to bring in your car? [11080:58:40] 2: How about next Thursday at 11am [11099:38:40] 3: next Thursday at 11am works perfectly. We'll see you then. Bartholomew, if you have any other requests or need to reschedule, feel free to let us know. All right, [11145:14:40] 2: see ya. So as you can see, we've got a bunch of logs here. For example. This is an agent response. This is exactly what the AI agent said to us this little bit here, which is session underscore. This is the unique session ID variable which lets us have simultaneous phone calls without them colliding. So if I keep scrolling up a little bit more, then I've got the user message, which is what I said Thursday at 11. So how about Thursday at 11, and we're essentially creating this transcript of the conversation so that at the very end, I've got the entire combo in text, and I can plug it into a completions API call and parse it, get different data from it, and then send across to make.com but as you can see, this phone call was successful. The only thing that we didn't do was we had an invalid URL for our webhook. So if I scroll up here, I actually didn't put my make.com URL into here. So across the make.com I've just got a web hook and then a Google Sheets integration. So I'm going to click on this, I'm going to copy this address clipboard back here between the quotation marks. And now, since I've made this change, I actually have to resave this, so I'm just going to hit stop, and if I hit run. Now the server is live again with the updated URL. So I'm going to test this out in a second, but let me just go over the code and just explain what's happening. So let's expand this out a little bit more. So again, watch the Twilio video, because it will give you a good idea of all the nuances of this code. But at high level, what we're doing is we're setting the system message for the AI receptions. So as you can see, I basically said, Hey, you're an automotive assistant. Just have a nice conversation with the user, get some of their information, like their name, their availability, and what kind of service work they want to have. Ask one question at a time. And then over here, I'm setting the actual voice that we're going to be using for this. So we've got the alloy selected. And if you go to your OpenAI account and click playground, then click real time, you can see there's a couple of voice options here. So if you drop down, yeah, there's obviously this alloy eco and shimmer, and you would just plug them directly into here. So you just choose your favorite one, and it's got to be in lowercase. Then from my use case, I just insert my webcam URL that's going to go cost to make.com and over here, we're logging all the events of the phone call. So it costs an open AI account. You go to API reference, and you go to client events, and then to server events. These are all the events that happen on the actual phone call that we're having with our assistant. So each of these little events will basically contain some different information, and this is what I use to obtain not only the user transcript, but also the agent transcript. And again, for my use case, I wanted the entire transcript and text so that I can actually use that and send a cost to make and get different details and do different things. So then over here, we actually have our first message that we're sending across to the user, which is, hey, you know, the phone call is successful. You know, how can we help you? So this is like the initial message that we send to the user when they make a phone call. And then, as you saw, I said something like, Hey, I want to make an appointment. And again, that's that first message coming into that incoming dash call endpoint that we appended into Twilio, and then scrolling down. A little bit more from here, we get a little bit more complex, but we're basically setting up the main configuration for the AI agent. This stuff over here, the session variables you'll remember from this over here. So this session dot update event. This is basically what we're doing here. Ours is a little bit shorter because we don't have, we don't have tools in here. And actually, one thing that I introduced into this code that wasn't in Twilio code was this input, underscore audio, underscore transcription. So we're using the Whisper dash one model to transcribe the conversation as it's happening live. So yeah, the Twilio GitHub code didn't have this actually took me a little while to figure out why I wasn't able to get the transcript of the audio. Yeah, you need to enable this to be true, so you can use the Whisper model to transcribe your audio into text. And that's what we're doing here. We're using the Whisper model, but yeah, we are setting our API key. We're setting the real time equals v1 parameter here as well. This is our actual real time API, WebSocket address, and over here, this is where I introduced some transcript handling. So like we saw in the console before, we had user message and Agent message, that's thanks to this additional code where we're taking the actual audio, we're converting it into the text transcript, and then we're going to be using that at the very end, once the phone call has ended, to send that across to make.com So scrolling down a little bit more. So over here, I'm actually making a chat GPT completions API call. I'm using the structured outputs schema, and I'm sending the system message to be Hey, extract the name, availability and special notes from the transcript that want to provide to you for the user message, we're actually sending the transcript itself. So that's all the messages that I'm saying, all the AI agent messages, all that is inside this transcript variable. And then we create this schema that basically creates a structured response back to us that contains the customer name, the customer availability and any special notes. And these are all just strings, so they just like straight things. Then it's going down a little bit more over here, we're creating that webhook across to make.com we're taking the webhook URL that we set at the very top of this code, we're making a post request, and we're sending the stringified payload. So that payload is essentially these three variables, and then those variables are going to be landing right into this web hook here, and then after they land, I have a Google Sheets module which basically takes the variables directly out of here and places them into the name availability and notes sections of my Google Sheets, which is over here, the name availability and notes. So I'm going to hit one once, and I'm going to quickly make another phone call, and I'm going to show you exactly how the varies to show up into here. All right, so I just finished my call, and then over here we have our inbound payload of customer name availability and social notes. This time I want a full engine rebuild and then a turbo upgrade package. And then over here, we basically insert those variables into our sheet, and now looking at our sheet, I'm booked in for next Tuesday at 1pm again, for that full engine rebuild with the twin turbo upgrade. I'm pretty sure I'm gonna go for All right, so now that we've actually gone through the code and seeing how it works, you can paste this into chatgpt and say, hey, I want to make a couple of edits, you know, I want to change the system message, or, you know, the webhook URL, or actually, I want to send it to a completely different program. So you can put it into chatgpt. It'd be super helpful for you to keep the code in [12820:00:00] 4: the background. However, it also has some agent functionality, and it can interact with different Python functions or the outside world in really, whatever way we set it up. So if I unmute my mic, you can see how it works. Hey, is Tech with Tim one of the best programming YouTubers? [12887:01:20] 1: Tech with Tim is a popular programming YouTuber known for his clear and informative tutorials. Many people find his content helpful. [12927:17:20] 4: Can you change the temperature in the living room to be 23 and in the bathroom to be 27 the [12952:26:40] 1: temperature in the living room is now set to 23 grease cells, and in the bathroom it is set to 27 grease cells. [12981:30:40] 4: Can you tell me what the temperature is in the bedroom? [12993:30:40] 1: The temperature in the bedroom is currently 23 Celsius. [13014:18:40] 4: So there you go. You can see that this actually has additional functionality, and I've set it up so that it can interact with some different temperatures, keep track of the temperature in various zones, and actually change that temperature as well. Now obviously, there's a lot more complicated apps we could build with this, but let me show you how we get started so that you can extend it and build something really cool. So let's get started by understanding the technology we'll use to build out this app. Now for this project, we'll be using live kit. Now, live kit is the sponsor of this video, but don't worry, they're free to use. They're open source, and they're actually what powers open AI's voice mode. So I'm showing you this because it's actually was already used for some of the largest companies in the world, companies like Oracle, eBay, Rumble, character. Ai, obviously, OpenAI actually use this, and the reason for that is it provides ultra low latency streaming of not only voice, but also video. So you can see that live kit is really the real time cloud. It allows you to build and scale voice and video applications, and the reason so many companies use it is that it's open source, so you can actually deploy your own live kit server and just use their protocol and their code, and you don't need to pay for anything. Or you can use the live kit cloud. You can deploy with them and allow them to handle the infrastructure, and that's what we'll do in this video. But don't worry, there's a free tier. You don't need to pay for anything. Anyways, this is super cool. I recommend you check it out from the link in the description, and obviously we'll be using it here to build out this AI agent. There's all kinds of different languages supported in a bunch of different libraries and SDKs, and you're going to see how easy it is to build this voice assistant out in Python in this video. So let's hop over to our code editor, get livekit set up, and then start building this app. So I'm in my code editor now, and the first thing we'll do is create a virtual environment to install our different dependencies. So to do that, I'm going to type python three, dash, M, V, E, N, V, and then I'm going to type A I. Now that's the name of our virtual environment. If you're on Windows, you can simply replace python three with Python and it should make that virtual environment for you. The next step is to activate the virtual environment. So we're going to type source, the name of our virtual environment, which in this case is AI, slash BIM and then slash activate. Now if you're on Windows, I'm going to put two commands on the screen, one for PowerShell and one for CMD or bash, which will show you how to activate the virtual environment. Now that it's activated, we can install the different dependencies that we need. Now we're going to be using a live kit, so we need to install some packages related to that. So we're going to type PIP three install, and then the first thing we'll install is live kit agents. Then we're going to install the live kit plugins. And then this will be open AI, because we're going to use open AI for this video, but you can use anything you want, like a llama or, really any other LLM provider. We're then going to install the live kit dash plugins, and then this is going to be celero, or however you pronounce that. Now this will be used for voice activity detection so we know when the user is typing. And then we're going to install the Python dot env module for managing our environment variable file. So let's press enter here and get this installed in the virtual environment. So now that our dependencies are installed, we're going to create a few files that we need. So let's go inside of the folder that we're working in in VS code, we'll make a dot env file. We'll make a python file so we can go with main.py and then we'll make another file we'll need later, which will be api.py now we're going to need a few different secrets here. For example, we need to know where to connect to our livekit server. We'll talk about how that works in a second, and we'll need things like our OpenAI API key. So let me quickly stop here and break down what we're about to do. We're going to be building an AI voice assistant. Now this voice assistant is going to work by connecting to something known as the livekit server. Now the livekit server is going to be hosted by livekit, but if we wanted to, we could host it ourself using their open source code. Now this live kit server is what's responsible for the transmission of data. So it's going to take, for example, the voice data that comes from our clients or our front end and send that to our back end or our AI agent, where we can then process it and then come up with some kind of response. So the server is kind of an integral part here, and inside of this environment variable file, we're going to put a few different kind of keys or tokens that we need to connect to that server. Then we're going to have a front end. Now the front end is like the user facing application. That's what you saw in the demo, where it shows us a preview of our video. It kind of gives us the text logs of what's the agent is saying, and that's actually pre made by livekit. Now, obviously we can build our own front end, but just to save us some time, here, we're going to use one provided by livekit that allows us to really quickly test our application. So quick summary, we have a front end. We're a client that connects to the livekit server and then our agent. So the AI we're going to build in this video here will connect to the server as well. The server is going to handle the transmission of data between those two different components, and that's kind of the main nice advantage here of using live kit, it does really low latency transmission of data, so we can get responses back super quickly. So what we need to do is now go to the live kit website and create a new application. So I'm going to press on try live kit, I'm going to make a new account, and then I'm going to create a new app. Now I already have an account and I already have an application, but let's make a new one, so you can see what the process looks like. So please go to the link in the description, make a new live kit account, and then we'll make a new application, and I'll show you how this works. So I'm just going to go down to the bottom left, press on Create New Project. I'll give this project a name, and then I'll show you where we get the connection keys that we need. So once you create your project, you'll be brought to a page that looks like this. Now notice that you can actually look at some of the documentation for creating a client and a server if you just press on any of these different languages or frameworks. So if you wanted to make a React client for this app, just press on React here, and it actually shows you exactly how to do that using live kit and some of their pre built components. Same thing for the server, but obviously this video will walk you through that, so you don't need to click into these unless you're curious and you want to check them out. Now what we want to do here is get our connection keys. So what I'm going to do is go to settings, and from settings, I'm going to press on keys. Now from key, I'm going to create a new key, and I want to give this some kind of description, so I'm just going to call this tutorial. To call this tutorial, because I'll delete it afterwards. Now it's going to generate this key for us that we need to copy these three pieces of data into our environment variable file. So let's start with the web socket URL. Let's go here and let's type in the correct variable. Now it's really important that you type this correctly, so just follow along with me. The first variable we're going to have is the live kit underscore URL, and we're going to make that equal to a string, and we're going to copy in this W or WebSockets or a URL. Now the next variable that we'll have will be the live kit underscore API, underscore secret. Let's make sure we spell secret correctly. Okay, that looks good. And then after that, we're going to have the live kit, underscore, API underscore key. This will be equal to a string as well. So let's now go and grab our secret again. I'm going to delete this after so don't think that you can copy it. I'm going to paste that right here into the secret, and then I'm going to grab the key, so the API key like that, and paste that for the API key. Okay, so make sure you have these three variables, and then we're almost done. But we do need to get our open AI API key. So we're gonna make one more variable here. This is gonna be open AI, underscore, API underscore key. This will be equal to a string, and now we're gonna go to the open AI website, wrap that, and then we're done with the different secrets that we need. Okay, so I'm on platform.openai.com/api [14984:37:20] 4: keys. I'll leave this link in the description, and this is where you can make a new key. Now, I believe you do need credit card information on OpenAI for this to work. They may have changed that recently. This might have a very, very small cost to use. I'm not exactly sure if there's a free tier right now, because they keep changing things, but the point is, I can press on create new secret key. Let's give this a name. We can call this something like live kit. And then we're going to copy this key and paste that in the variable. Same thing. I'm not going to use this afterwards, so don't feel like you can steal it from me. I'm going to delete it after the video. Okay, so I've just pasted that in here to open AI key. And now that we have that, we're done with the secrets, and we can actually start writing some code. And by the way, the reason we need all of these things are so that we can connect to the live kit server, so that when we eventually use that front end client, we actually have a way to transmit our information to the AI that we're making right now. Don't worry, this will all come for full circle, sorry, once we start writing some code. Okay, so let's go inside of main.py here, and I'm just going to start coding this out. We're going to make a very simple chat bot, and then I'm going to show you how we can connect this to some agent functionality. So the first thing we're going to do is import async IO. After that, we're going to say from dot env, sorry, import load underscore dot env. While we're at it, we'll call this function load dot env, and what that does is simply load this environment variable file for us that we have access to those variables. Next we're going to say from live kit dot agents, and we're going to import the auto subscribe the job context, the worker options, the CLI and the LLM, okay, now let me just make this a little bit larger so we can see this next we're going to say from live kit, dot agents, dot voice, underscore assistant. We are going to import the voice assistant. And by the way, you can make a lot more things with live kit than voice assistants. As I mentioned, you can actually stream video as well in extremely low latency, and a lot of companies actually use live kit for doing things like live streaming. Next, we're going to say from live kit dot plugins, and we're going to import OpenAI and celeria, which is why we installed them before. Now, there's a bunch of other plugins, and if you wanted to use something like olama, you could install that plugin, and then you could utilize that here. Lastly, we are going to actually create kind of a function here. So we're going to say async def, and then this is going to be called entry point, and this is going to take in CTX, and we're just going to do a variable annotation here of job context for now I'm going to put inside of here pass, and then I'm just going to write kind of the main line call here. So I'm going to say, if underscore, underscore, name is equal double, equal sign to underscore, underscore, main underscore, underscore, then we are going to call this function. So to call that, we're going to say CLI, dot run underscore app, and we're going to use worker options. And for the options, we're going to say the entry point, underscore FMC, which stands for function, is equal to the entry point. Okay, so what I've done here is, I've just made a function. This is an async function, because we're using async IO, and then what we're going to do is say, Okay, if we run this Python file directly, we're going to use the CLI, which comes from livekit, we're going to run a new application. That application is going to be a worker, and that worker is going to be this particular function. We'll talk more about how workers work later on, but this is where we can kind of write the code that triggers our AI voice assistant. Okay, so bear with me here. There's just a little bit more code to go until we have a functioning voice assistant. So next I'm going to say initial underscore, CTX, which stands for context, is equal to LLM, dot, chat context, dot, append. And this is where I can actually add in some context to kind of start my AI voice assistant. So just like you could give kind of a templated prompt to, you know, something like open AI or some LLM, that's kind of what we're doing right here. So we're going to say role is equal to system, and then we're going to say text is equal to and we can give this a description of what we want this assistant to kind of act like, or how we want it to work. And let me make sure I have a comma here so we could say, hey, act like you're someone from the 1800s or, you know, respond in this specific format or whatever we want it to do. This is where we're going to put that information. So what I'm going to do is just copy in some context. Here I'm going to do a set of parentheses, and then I'm going to copy in this it says you are a voice assistant created by live kit. Your interface with users will be voice you should use short and concise responses and avoid usage of unpronounceable punctuation. Okay, so that is the initial context. Obviously, you could change that if you want. And by the way, all of this code will be available from the GitHub link in the description, in case you want to copy any of this in. So next we're actually going to connect. We're going to say await, CTX, dot connect. And we're going to say auto underscore subscribe is equal to auto subscribe dot audio only. Now, pretty much what we're doing here is specifying that we just want to connect to the audio tracks. We don't care about the video right now, but obviously you could connect to the video and the audio, or just the video that this is specifying, hey, we want to connect, but we just want to subscribe automatically to all of the audio tracks that we have. Now let me just code out a little bit more, and then I can kind of explain to you what this means, because I understand this is a little bit confusing, but we just need some more code before this will make sense. So next thing we're going to do is actually define our voice assistant. So we're saying we want to automatically subscribe to the audio tracks. Now we need a place for our audio tracks to actually be handled. So we're going to say assistant is equal to voice assistant. And inside of here, we need to specify a few properties. So the first thing we're going to do is VAD. Now vav is voice activity detection, which is specifying what kind of model or what we're using to detect if the user is speaking or not. So we know when to kind of cut them off and send the message over to our AI. Now this is where we use celero, or however you pronounce this. And this is going to be slithero dot, VAD, dot. Load, okay. Next, we're going to do stt is equal to open AI dot, s TT. This is speech to text. We're going to use open AI speech to text. Next, we're going to say the LLM is equal to open AI dot, and then LLM in all capitals. And here is where you could actually replace these with different things, right? So you could use a different speech to text model, a different LLM, and then next, we're gonna have text to speech. You can use a different text to speech model, okay? You could replace all of them here. And you can find how to do that from the live kit docs. Now, lastly, we're gonna provide the chat underscore CTX, which stands for context. And that's going to be the initial context that we have. Okay? So now that we have that, we're going to say assistant, dot start, and we're going to say CTX, which is our context, dot room. Now the way that these voice assistants work in live kit is they can connect to a room, so we can have multiple rooms going at the same time, and these voice assistants can connect to one of them, all of them, they can kind of scale infinitely. And what we're doing is saying, hey, we want to connect to the room that's being provided by this job. So really, the way that this works is our agent is going to connect to the livekit server. The livekit server is then going to send the agent a job, and when that job is sent, it's going to have a room associated with it. So what we're doing is saying, Okay, we want to subscribe to the audio inside of that room, and we want to start the assistant inside of this room. So that's kind of how that's working. Now next, what we're going to do is we're just going to wait one second, so I'm going to say await, and then async IO dot sleep, and I'm going to sleep for one second, and then I'm going to say await, and I'm going to say assistant. We spell assistant correctly, dot say. And I'm just going to give this some text. I'm going to say, Hey, how can I help you today? So this is how you can actually just manually send a message with the assistant. If we wanted to say something specific, well, we could just say exactly that assistant, dot set, then we're going to say, allow interruptions, and this is going to be equal to true. So it allows us to interrupt that welcome message with anything else that we want. Okay, so believe it or not, that's actually everything that we need for making a basic voice assistant. Now you might be saying, Okay, that's cool. That kind of makes sense. But how do we actually use this Okay, so there's a few steps here. Let me explain how we start using this assistant. Okay, so the first step here is to run our agent. So to do that, we're going to type python three, the name of our Python file, which is main.py, and then we are going to say, start. Okay, so that's how the CLI thing kind of works. It's handling this command. So when we say start, well it's going to start the agent force. So let's go python three main.py start, and this should start the agent, and we should get some output coming here in just a second. Okay, so you can see that the agent has started. So now that the agent has started, what we need to do is actually set up a new room, and this will then trigger the live kit server to send a request here to this agent, so the agent can connect to it and then can start listening for any of the voice requests or any of the voice messages that we send. So the way that we do that is the follow. First of all, I have a link in the description to the live kit agent frameworks kind of GitHub repository, and this is specifically for Python, and it will give you some more information on how all of this works, but what it will also specify here is a URL for the hosted playground. Now the hosted playground is what I showed you before in the demo, and this is something that we can use to connect to our agent without having to build our own front end. So what I'm going to do is click on this URL here, and it's going to bring me to this agents, dash playground.livekit.io. [17548:58:40] 4: Now you're going to have to sign into live kit in order for this to work. You can see that I'm already signed in, and I'll leave this link in the description, so you can just click on it rather than having to type it in. Now what we're going to do is press on the project associated with the agent that we're building. So in this case, I believe I called it AI voice tutorial, so I'm going to click on that and I'm going to connect to this project. Now, when I do that, it should connect my video as well as my audio, and I can turn off my video if I want. So let's do that, and you can see that my microphone is going now notice that it will show me, let me just mute this for a second, that I have a room connected true. I also have an agent connected, and it also shows me the room that I'm inside of. And if we go back here, we can see that we've actually connected to this row. So it should, in a second here, if I turn on my microphone, actually allow me to talk and get a response from the agent. So let me do this. Hey, how's it going? Okay, sorry for the cut there, but I just actually disconnected and then reconnected, and now my agent is connected, and it's working, and and you can see it's picking up what I'm saying. So as soon as I stop talking now, it should actually grab all of this audio, send it to my agent, and then give me some kind of response. Okay, so you can see it handled that audio. It's going to take a second, obviously, and then it should give us some kind of response. [17882:24:00] 1: How can I assist you further? Perfect. So I just muted [17892:32:00] 4: my mic because obviously it's weird when I'm commentating and trying to talk to the assistant, but the point is now this is connected. So it shows you, right the room, the participant, the room connected, the fact that the agent is connected. If you're getting any weird errors, sometimes you can just disconnect and then reconnect, and then the agent will pick this up. Also, obviously you can shut off the agent by hitting control C on your keyboard and then rebooting it. But in my case, if this work. You can see it said, Hey, how can I help you? And then it grabbed all of our text, converted that into text. So grabbed our speech, converted into text, sent that to the agent, got the agent's response, and then turned that into speech, which is then spoken here by this kind of audio. What do you call it? Bubble window, whatever that live gets provided in this front so that is all great. We can play with this. Ask it anything that we want, and now we have something like open AI's voice mode. But I want to go a step further and show you how we actually add some agent functionality. So how do we allow this AI to handle different state, turn on lights, handle temperature in different rooms, connect to APIs. I'm going to show you how to do exactly that. So let's go back to our computer and start coding out that agent functionality. Okay, so I'm going to pop over to this api.py, file, which is where I'm going to be building the kind of agent functionality, just so it's separate from the main file. Now what we're going to do to start is import enum. We're also going to say from typing import annotated, and we're going to say from live kit, dot agents, import LLM. And then lastly, we're going to import logging, just so that we can print some things out if we want to. So first, I'm just going to create a logger. I'm going to say logger is equal to logging dot, and this is going to be get logger, okay? And then we're going to say this will be called the temperature control, so we can just give this a name, so that we can see what it looks like when we're logging different information. And then we can say, logger dot set level. This is the level at which we're going to log. And we'll say, logging dot info, so that this way, we will see in the logs any information messages that we decide to log. This is just because if we try to print using this agent, we're not actually going to see the output. Instead, we need to use this log. Now what I'm going to do is just set up a fairly simple agent that can keep track of the temperatures in different zones, and those zones we're going to define with enums so that we're able to actually access them and use them from the agent. So this is just a simple example. You can extend this and change it quite a bit, but this kind of illustrates the functionality. So I'm going to say class zone, and this is going to be enum, dot enum, okay. This is built into Python, by the way. You don't need to install this. What I'm going to do is make some enums. So I'm going to say living room is equal to living room, okay, and we'll just copy the rest of them in just to save ourselves a little bit of time. Okay, so here we have the bedroom, kitchen, bathroom, office. Obviously, you can make these zones anything you want, and they don't necessarily need to be rooms. I'm just doing this as if we're going to allow the AI voice assistant to, like, control our thermostat. Okay, then we're going to make a class. We're going to say a class is the assistant function, okay? And this is going to inherit from the LLM dot function context. Now basically what we're going to do is make a Python class, and any functions that we define inside of here that have a specific decorator can actually be called by the LLM, and the LLM will decide which function to call based on the description that we give them. So first of all, I'm going to make an initialization so I'm going to say, define underscore, underscore, init. We're going to take in self, we're just going to annotate that this returns none, and the first thing we're going to do is call super dot, underscore, init, underscore, underscore, so that this parent function here, or parent class three, gets set up correctly. Then we're going to say self dot temperature is equal to an object. And again, I'm just going to copy this in. All we're going to do is assign each enum or each zone a particular temperature. So we're going to say, hey, the living room is 22 The bedroom is 20 The kitchen is 24 these are just some random initial temperatures so that we can change them and keep track of those temperatures when the AI is running. Okay? So now that we have that, we're going to make our first callable function, and this is simply going to get the temperature from a particular zone. So we're going to say@llm.ai underscore, callable. Now this is a Python decorator that specifies the function that we're about to write can be called by our LLM. So if we want one of the functions inside of here to be callable, then we need to define that. Now for this, we're going to pass a description equal to and then this is just going to be whatever the function actually does. So you want to provide a good enough description that the AI is able to kind of distinguish between which functions it should use when. So we're going to say, get the temperature in a specific room. So the LLM will be able to see this description and know, okay, I want to use this function to get a particular temperature. Next, we're going to say, define, get temperature. And inside of here we're going to take in self and then a zone. Now it's important that for our parameters here, we annotate them using Python typings, so that the LLM knows the correct data to pass to the function, so that it works properly. So I'm going to say zone is equal to annotated, and I'm going to specify that this is zone. And then I'm going to say LLM dot type info, and again, I'm going to pass a description so that the LLM knows what this parameter should be. So I'm going to say, LLM dot type info, and the description of this parameter is the specific zone. Okay, so that's the annotation of this, and I need to what is the issue here, put my colon, and then I can start typing. Okay. So now, inside of here, the first thing I'm going to do is just say logger dot info, and I'm just going to say, Get temp, and then dash zone, and then I'm going to do percent s and I'm going to pass the zone. Okay, just so we're going to print out, hey, the LLM called this function, and it called it with this zone, then we're going to actually get the temperature. So we're going to say the temperature is equal to self dot, and actually we're going to say this is underscore temperature. So this is a private variable. So let's change this to underscore temperature, and we're going to grab the zone of the zone. Now this might seem a little bit weird, but this zone right here is going to be a string, so what I'm going to do is convert that into the correct enum. So if you do like the name of the enum, and then you surround a string here, it's gonna give you, like this kind of version of the enum. And Python, I don't really know a better way to explain that, so that when we actually try to get the temperature, we don't get any kind of type errors, or what do you call it, key errors here. Anyways, this is required. Just trust me, I've tested this before. Okay, then we're going to say return, and we're going to return an F string, and we're going to say the temperature in the zone is, and then whatever the temp is, and we're just going to put a c here so they know that this is in Celsius. Okay, so all we did here is we wrote a function that gets the temperature in a specific room. We annotated the parameters that the LLM knows what to pass, and then we just logged, okay, the LLM called this function so we can see that we got whatever the temperature is in that zone, and then we returned it. So now that we have that, obviously we want to use this. So let's go to main.py and let's import it. So we're going to say, from API import the assistant function. Now we just need to set up a context for this. What do you call it? Kind of assistant or agent functionality? So we're going to say FNC underscore CTX is equal to the assistant function. We're just going to call that so it gets initialized, and then we're going to pass to our assistant the FNC underscore CTX is equal to the FNC CTX. Okay, so that's literally it. All we have to do in order to provide the voice assistant with some functions that it can call is create a class. This class needs to inherit from this class right here, from the live kit agents framework. We then just need to provide one of these functions here, you can provide multiple which I'll show you in a second, which is@llm.ai callable, give the description, annotate the parameters, and then it can use this. So now, whenever the voice assistant feels that it needs to use that function, it will use it and then give us some response. So let me turn off this agent. I'm just going to quit by hitting Ctrl C a few times, and I will restart it. So python three, main.py and then start. Then we're going to go here, and I'm going to disconnect and just reconnect. So the agent will kind of be triggered to connect to this room again. [20184:42:40] 1: Hey, how can I help you today? [20192:26:40] 4: Okay, so the agent connected. Can you please tell me the temperature in the living room and in the bedroom, the [20221:36:00] 1: temperature in the living room is 22 Greek cells, and in the bedroom it's 20 Greek cells. There [20246:40:00] 4: you go. It worked. So you can actually see that the agent can call the function multiple times, which it would have done in this situation. And if we go here to our logs, we should be able to see the logs of it actually doing that. So it's a little bit difficult to see here, but if I go maybe temp, you can see, yes, it says, Get temperature, and it called zone living room, and then get temperature, and it called the zone bedroom. So it used those functions, and that worked properly. Sweet. So now I just want to add one more function that allows it to actually change the temperature, and then that will be pretty much all that we need. So I'm going to say@llm.ai underscore, callable, and I'm going to give this a description. I'm going to say the description is set the temperature in a specific room. Okay, for this one, we're going to say define, set underscore temperature. And we're going to do the same thing. So we're going to say self, and then we're going to take in the zone. So let's just copy this parameter right here. And as well as the zone, we're also going to have the temperature that we want to be set. So for the temperature, we're going to need to define that parameter as well. So let me go temp colon, and this is going to be annotated. And for the type here, this is going to be an int. And then we're going to do the type info. So just copy this and change the description. Okay, let me just save this. Okay, I just want you guys to be able to read this. Okay, so let's go temp annotated int, and we're going to say the temperature to set. Okay, perfect. And then inside of here we can kind of copy what we did before. So we're going to say logger dot info, and we're going to say set temp, dash zone, and then this is going to be percent s, and then we'll pass the zone. And then we're also going to say the temp is percent s, and we will pass the temp. We are then going to say self, dot underscore temperature, and with square brackets, we're going to say zone. And then zone is equal to the temperature. And we're going to return an F string and say the temperature in the zone is now temp, and then C, okay, perfect. So I just added another function. Now we didn't need to make this related to temperature. We can make this do anything that we want, and all the functions that we want to be callable, we can just put inside of here. Anyways, we can test this out. So same thing. I'm just going to stop the agent and then restart it. So say Ctrl C a few times and then restart the agent. I will disconnect. Make sure that this connects and is good. Okay, Let's reconnect here, and let's try this out and see if it can set and get the temperatures. Okay, so our agent is connected. Let's wait for the welcome message. Hello. How can I assist you today? Great. Can you please set the temperature in the living room to seven degrees and in the bedroom to nine degrees? [21058:24:00] 1: The temperature in the living room is now set to seven degrees and the bedroom is set to nine degrees. Anything else? [21091:44:00] 4: Can you tell me what the temperature in the bedroom and the bathroom is [21117:41:20] 1: the temperature in the bedroom is nine degrees and the bathroom is 23 degrees. What else can I do for you? [21146:13:20] 4: Great. So you can see the AI is working. It's actually using those functions. If we go back here, we can see that we have get temp, get temp, set temp, or set tamo, I guess because I spoke that incorrectly, and you could probably see from this short kind of code example how we can make this a lot more advanced. [21239:28:00] 5: okay. so video enter, let's start with the video. okay, so how many have a bad karen, cloud editors, okay, cloud based id and already have my pass market me both a church a cloud based id ism, for example, after they cover repellent, subscribed, ing conceptual cloud resources for user, the cloud, CPO cloud memory co user. For example, JSON Maraba separco files, editor and mobile person operating terming on them so replica.