Python Tutorial - Use ChatGPT API With Text-To-Speech! (Personal Assistant)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to fighter code in this video we'll continue our personal assistant we have the ability to talk to our computer and we can open programs and even search Google for different things we can open the browser but we want to incorporate now GPT we're going to incorporate GPT two different ways we're going to do it with the GPT 3 model and then we're going to actually incorporate chat GPT so we're going to have two different types of GPT we can say and then at the end of that we will actually get the computer to speak to us so we will be able to hear gpt's response let's go ahead and get started I'm just going to add another LF here in this case we're going to say GPT is following a similar pattern as open notepad search Google for good night computer I also want to make sure to import open AI this is going to be the actual module that gets us the ability to get into gpt3 air chat gbt now I'm gonna actually use the API key this is one of the first steps open AI key so how to use that we do open AI Dot API underscore key and this is going to equal one of your keys so how to get here you do platform.openai.com account API keys and this is basically just the API Keys section of your openai account now just kind of a heads up this is something that I will say costs money but you are able to use this for like some sort of trial period they give you a couple of dollars to use their API if you want to try it out so we can go ahead and delete these or create new ones but it's important to keep track of your API key because once you create it it will only show you the API key once so let's do an example create a new secret key this is the API key here and I'm only going to be able to see this once so I'll copy it for example if this is the API key that I'm using I am not able to actually open this back up and see what the full key is so it's important whenever you're using API key in this case that you kind of copy it and put it off to the side of course you can create new keys and whatever and you can delete new keys so it's not really that dangerous if you forget the API key you can just create a new one once we have that API key we're just going to paste it right in here as a string and then we're going to set up for GPT 3. got pt3 so there are a couple different things we do here we do model model model module engine not it there it is in this case we're going to do DaVinci because this is working with gpt3 not chat GPT query is going to equal something similar that we did when we're using our search for Google this is kind of splitting the string we're going to split it at the GPT section and we're going to take the last half of the string if this is kind of a little bit new please take a look at the other video the previous one in the playlist where we actually go into a little bit detail on how this split is working we're going to add a temperature temperature I feel like I get this spelled wrong pretty consistently equals 0.7 this is kind of the default values we can say and Max tokens 256. this is kind of how long uh the response is going to be how the maximum of character number of characters we want the response to be so this is kind of the default setup for that engine now what we want to do is we want to generate a response it's going to be response equals openai dot completion dot create and inside this create we're going to put some variables in here we're going to do a model equals text Dash DaVinci Dash 003 and that actually the text DaVinci that comes from right here so you can play around with different types of models for from open ai's GPD 3 section but we're going to use this one text DaVinci since it's kind of just a base gpt3 model so comma prompt we're going to add another variable this is going to be query so this is going to be actually the string now that we have spoken that is right here line 43 this is so this is actually going to be what we've said and this is going into the prompt temperature equals we're going to do our temperature variable and Max tokens is going to equal our Max tokens variable so at this point we have a gpt3 set up and ready to take in our prompt we're going to print the response Dot choices and this is going to be an array this is how GPT kind of sends back the data we're just going to strip all the white space off of this not response so let's go ahead and give this a try GPT tell me a fun fact so we were able to accurately get that we said GPT tell me a fun fact so when it says GPT we hook into this LF statement and we kind of break this string up to just say tell me a fun fact that is what we're actually going to send in this query tell me a fun fact and now we have a response let's go ahead and continue oh this right here was not supposed to be string trip GPT tell me a fun fact okay so now we were able on this time to get a response from gpg3 so the longest recorded Flight of the chicken is 13 seconds so now let's go ahead and use Chan GPT so what I want to do here is I want to actually see what the speech recognition grabs when I say chat GPT chat GPT so this is kind of going to be this is going to be what I want to code my my next LF statement for so we're going to do another LF and it's going to say chat GPT again we're following the same pattern in text Dot text Dot lower so we have the import for open AI we don't need to do that because we've already done that if you haven't go ahead and do the PIP install openai and do the import here we're going to do the openai dot API key again we're going to use the same API key and this is going to be handled a little bit differently we're going to have here a an array an empty array it's going to be named conversation conversation and this is just going to be kind of keeping track of what we are saying in our conversation with chai GPT so here's kind of the defaults of what we're putting in role user they have different examples but this is going to be kind of the basic setup to go with we're going to do the exact same thing we've done before where we have query but instead of this we're going to do chat GPT now the actual response is going to be a response equals open AI Dot chat completion dot create and in this create we I don't know why chat completion there we go and this create we're going to have our model equals GPT Dash 3.5 Dash Turbo comma and we're going to add one more this is actually going to be our prompt and with that we're going to have a uh response chat GPT response is a new variable we'll do output this is going to be the response variables similar to the gpg3 it's going to be kind of what we're looking for is going to be returned inside of an array so choices response choices zero message and the last one content and we'll just print this out so we can see what chat gbt response is so I'm actually going to change this after a couple of tests I have trouble with it recognizing open AI so I'm going to just use the word computer and that means here we also have to change this in order to just say to split whenever we see computers let's try it now computer tell me a funny catchphrase so we have here a response and let's just go ahead and run through it even though I had a break point there so we actually get a response from Chachi BT so in this case we have GPT using gpd3 and we have chat gbt using gbt 3.5 turbo so there is an issue with this way of coding for chat gbt and the problem is that this conversation gets lost whenever we lose this if Condition it's only exists inside this scope an example is I asked the computer who won the World Series in 2019 and I asked also a computer how did they win and I pretty much lost my context so what we're going to do here is we're going to take this conversation array and we're just going to move it pretty much towards the very top so we're going to keep this whole conversation in our python personal assistant so if we try this again computer who won the World Series in 2015. computer how did they win so now asking who won the World Series in 2015 and asking again computer how did they win we see that we actually have some context now that chat DBT is able to take this this phrase this prompt how did they win and they're able to associate Chachi BT's able to associate this prompt with its previous prompt which is Kansas City Royals won the World Series in 2015. now let's do one more thing let's incorporate the Pi TTS X3 this is going to be text-to-speech engine and what we're going to do here is we're going to create a function we're going to start with getting all the text-to-speech engine stuff initialized so that's going to be a variable called engine we're going to set that equal to Pi TTS X3 Dot init then we're going to set the rate of speech this is going to be how fast we want the speech to be foreign get the property First Rate and then we would be able to set the property what I found is this value is pretty realistic I feel this is like a good rate of speech how fast or how slow that the computer is talking we're going to set the voice this is going to be the same as the rate we're going to do a voices variable it's going to be equal to the engine dot get property and it's going to be a property called voices and then we're going to do engine.set property The Voice property we're going to set it to voices and I thought this was a just a normal voice so this is all just kind of making it a little bit realistic and now here is our function to speak so we're just going to say speak text and that's the uh property that we take in with that text we do our engine dot say and we give it the text we want to speak and we also are going to do an engine run and wait so that it kind of helps to stop like 20 different things for being said at once so now we have our function speak we're going to go all the way down here and we're going to do a speak chat gbt response we're also going to do it here too let's do a speak I don't know why I can't there we go speak this response too so let's go ahead and start with Chachi BT computer tell me why python is important to learn as an AI language model it would provide you below points one easy to learn compared to other programming languages python is an easy language to learn so we kind of get the idea um that one took a little bit longer but we have a lot more uh response text from chat gbt than previously so at this point we have a personal assistant that can use gpt3 it can use chat gbt and we also have the ability to speak from the computer from given text it can speak and we can hear our computer basically talking to us you can use the same speak if you want you can say things like in other different types of if statements you can say opening notepad and you can just use this wherever you want this is your personal assistant as long as you have use this speak function you can just pass in any string to it so we'll stop the video here in our next video we'll start to incorporate Spotify into our personal assistant seeing how we can control Spotify with python but thank you guys so much for watching if you have any comments or you're having trouble with any of this code please leave a comment down in the comment section if you enjoy any part of this video please drop a like please subscribe to keep track of this playlist and I hope to see you at the next video
Info
Channel: Bytes Of Code
Views: 4,145
Rating: undefined out of 5
Keywords: python, programming, coding, tutorial
Id: TxFzfNh3oS0
Channel Id: undefined
Length: 17min 35sec (1055 seconds)
Published: Sat Mar 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.