3. OpenAI API Python - Earnings Call Summarization

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
over the past several weeks millions of people have been experimenting with chat GPT and the open AI playground by typing in a specialized text prompt into a text area and clicking submit users are able to witness the power of AI right in front of their faces and see the very creative output that can be produced by this large language model but the real power of open AI comes when you are a developer who is able to interact with openai from a computer program openai provides a very powerful API that allows us to call gpt3 codex and Dolly from our computer programs allowing us to dynamically craft specialized prompts and process that output in order to perform very unique and interesting tasks and build applications that do whatever we want to do with AI without training our own models in this tutorial I'm going to show you how to get started with openai's API and Python and we're going to start with a very simple task which is summarizing earnings calls from there we'll be able to build more and more complex things but I want to start with a very basic task and get people up and running with open AI first and then we'll begin to build more and more complex applications if you've never written code to interact with the open AI API I've included a Google collab notebook down below where you can follow along exactly with what you see here execute the code and see how all this works so to get started with the open AI API from python the first thing we need to do is install the open AI python package and to do this you just run this pip command pip install openai and that's going to use the python package manager to install open AI once you see that the package is installed right there you see the check mark we need to import the open AI package very easy right the next thing you need to do is if you don't have an account on open AI already you need to visit this API page here and sign up and when you do that you'll be able to log in with your account so you sign up there I just used my Google account so it'll just log me right in and and what you're going to need to do is visit this view API Keys page right here and what you can do is create a new secret key so I'm going to click that and I'm going to delete this key right afterwards but you'll get this key and you can copy it right there to your clipboard and then you can paste it in down below and you can delete these if you're worried that someone gets it you can just delete them I'm going to delete that one right after this tutorial so what I'm doing here is configuring open AI to use this API key and so you can either set that as a string right here but since I want to show a an API key on here without showing it to you I realize I already did but what you can do is you can just import this Library called get pass if you run this it'll actually prompt it prompt you for it and I'm going to paste a new one right there that you've never seen before right I paste it in and I click enter it should be done and now open AI should have access to this API key now why do you need an API key what do you mean I've been using chat gbt and typing stuff in the Box and it's been free no once uh mentioned anything about an API key well I don't expect this free stuff to last forever when you sign up for open AI they give you 18 worth of credits free and this is enough to actually use it quite a bit you can probably run thousands and thousands of requests on this thing and learn how to use it and all of that but actually running all this Cloud infrastructure and making this machine learning model available as a service I think costs a substantial amount of money for them so I expect that over time that they'll begin to charge money for a lot of this stuff now is that worth it for you for me it personally is I have plenty of money and I'm willing to pay for things in order to accomplish tasks and to be able to train these models on my own would take so much time and buying like Nvidia gpus and all the things I would need to do this and running this service myself would cost a lot of money so if I can spend a few bucks a month and run all this these cool summaries and build an entire startup on top of it or do something that helps me financially then I would love to to use this and I'm happy to pay for it and I've actually already done that and I'm spending a few bucks at this point but I was able to get by I think you can get by for a month on just this 18 and learn this so definitely a sign up and it's definitely worth it and the reason there's an API key is so it can tie it to you and so you have your own credits and you'll get this uh billing screen if you haven't used it yet but there's this December usage and you can see this isn't my main account but it'll show the number of times you call it and there's a whole section where it shows how much it costs to call each one of these machine learning models so you can see generating all these images cost a couple cents and using these language models if you use this smaller model called Ada it's .0004 cents per thousand tokens and if you use the most powerful DaVinci model it's two cents per thousand tokens and so you can send it tons and tons of text and it'll charge fractions of a Cent and then over time you think if you built some startup that's worth millions of dollars on top of this and called it thousands of times this would add up over time right and so you know to come up with your your use case for it and decide if this thing is going to be worth it for you but for now it's absolutely free and you can try it out uh with no cost so I recommend trying it out and learning about this stuff and I think ultimately um there will be more and more versions of this maybe not run by open AI but but by someone else for instance stable diffusion I can run it on my MacBook right here and run it for absolutely free and it doesn't go through an API I can download it and run it right here and open AI whisper which I ran in the in the last video if you watch that that does live speech transcription that's absolutely free and we're able to just run it on our own machine but uh this latest DaVinci 3 Model right here I need to access it through the API and using chat GPT cost a lot of resources and so that's why there's an API key here so once we have the API key configured then we just need to call openai with one of these prompts and so I recommend if you haven't done so already I assume a lot of people here have already done this but if you click around they have these examples here and can look at some of the powerful prompts that can be sent here so they have like a q a where you can ask it a question they have a a text command they have natural language to open AI API there's a SQL translate where you can send in a database structure we'll play with that later so send it a database structure and have a query and maybe build some kind of a stock screener on top of that there's parsing unstructured data python to natural language so this takes code translates it in a to natural language so it tells you what the code does and so the first thing we can try is just this q a and it shows you what the prompt looks like and you can say open in the playground here and type this prompt directly in and so I can ask of you know who is the best investor right and as you probably know you can't just ask it anything especially about Finance it doesn't know like real-time price date or anything and so I don't want anyone to be misled into thinking you can ask in any Financial question that's going to give some great answer for you it turns down out a lot of those questions but it doesn't mean it's not extremely useful for financial tests so if I say who is the best investor of all time right it probably has an answer for that right if I click that it just says Warren Buffett just like that and or I can say who are the best investors of all time so who are the best investors of all time let's see if it understands multiple right George Warren Buffett George Torres Carl icon Ray dalio generally considered some of the best investors of all time so there's no real absolute there and it has some knowledge it's trained on all this text and knows you know these guys are investors that are highly acclaimed or widely praised right and so what we can do how do we call this from the open AI API this is one interface for openai but the powerful power as I mentioned is creating our own unique interfaces that allow the user to interact with and enter these prompts in interesting ways and so what we want to do is take this exact prompt right and send it over uh using python code and so we can do is using this open AI python package we can send this prompt and so same way we had this I'm highly intelligent question answering box ask me a question blah blah blah I can say who is the the greatest investor of all time and notice I have these Carriage returns in here back just and backslash n right and that's just a character chart so when I hit enter that's actually a backslash n so that's how I encode that in a string if you're a programmer you know this stuff already but I'm just beating it to death here and so uh prompt so there's our prompt and now we just need to pass that prompt to this open AI completion create and so that's a parameter so the prompt equals this prompt up here and we need to tell it an engine and so I mentioned all these models have a different cost associated with them right Ada is this cheap one DaVinci and DaVinci 3 is the latest one this came out like last week this stuff is changing so fast so new stuff is coming out like all the time so we start for free with 18 in free credit pay as you go uh choose your own model and so you can look at this and see how much those things cost and depending on the complexity of your task you might just need this most basic model so you should use just the cheap one but if you want something very Advanced you need to summarize or tell a very creative story maybe you'll use DaVinci and it'll come up with a better better answer so I'm using text DaVinci 3 right here but I might not have to so you can experiment with the different engines so you can do a text Babbage 001 for instance and so you can change these variables experiment with different prompts and I encourage you to play with the playground for a while figure out what this thing is capable of and how you might craft these prompts in order to come up with unique things right and so there's a variety of parameters here this temperature parameter you can experiment with and this controls the randomness of the response so you can have it give a real exact answer or you can have it be a little more creative a little bit more random the same with these uh parameters down here you can also control the maximum number of tokens so if you want the response to be longer or shorter right and so there's a variety of parameters that are all documented and we'll talk about these in more and more detail as we build more applications and so let's run this now we're going to send in this string as input and get a response from open Ai and see what it tells us so I'm going to run this right and I ask it the greatest investor of all time and you can see the response right here is this dictionary where choices usage tokens the model that's used and that's the text completion it says the greater greatest investor of all time is widely considered to be Warren Buffett and so the magic here is that we can vary this prompt and build it dynamically based on some variables based on a text file based on audio and we can generate this cool string whatever we want to send it and then parse this response and present it in a unique way in order to build a sophisticated applications right and so it responds to us that Warren Buffett is the greatest investor of all time and that's great and I've included a link here if you want to check out check out the difference between the different models and all of the different parameters and they're all in the documentation right there I just want to show you you know you could learn all this from the documentation but a lot of people like this channel because I showed an action and kind of explain it from the ground up give you the code and walk through it and that helps a lot of people see it visually so now that we've done the most basic thing you know we've successfully used the open AI API let's try to get it to do something a little bit more sophisticated so let's try a summarization task and so what I've done here in the last video we transcribed the audio to text so we transcribed the FED speech now in this video I'm going to use the text transcript of an earnings call and so you could build this huge data set of different earnings calls and extract information out of these earnings calls and so I've linked an earnings call that I've already done so we don't need to do that again and transcribe it from audio but this is the text of an earnings call and you can see it's quite long right quite long and if I click the raw thing you can see all the text right and where they say on Reddit whenever someone posts this big long wall of text and we don't have time for it too long didn't read they post tldr and it turns out you can send this exact prompt that's one of the examples in the open AI examples here there's this too long didn't read right and so if you look for summarization there's TLD are a summarization so you can add that to the end of The Prompt and so what we can do is do a an audio transcription a two text then take the text and then append a couple Carriage returns and tldr to it and then send that to the open AI API and instead of telling us a story or answering a question it will do a tldr summary now how am I going to get this earnings transcript into python well this one is on the internet here and so I'm just going to use the python request library to a request that URL and just get the text transcript right here and so if I import that I can retrieve that and here you see I have the plain text and so now I'll be able to get that plain text and append tldr to it and some Carriage returns and craft a special response and tell it to summarize this earnings call and so what I have here is a prompt variable and I'm going to pass it this transcript variable so this transcript is going to be inside I put a couple character terms and this tldr colon right there and this forms my prompt and so if I run this you'll see my prompt right there and if I were to expand this out you'll see the entire Big Blob of text here and this little TR tldr right here at the end so I know that seems pretty basic but these very basic building blocks are what are going to enable us to build more and more complex things so we need to master these very simple tasks and we'll be able to put them together to build these full stack applications in uh following videos and so we'll be able to build as some type of crawler that gets tons of audio and extracts a tons of text and builds a queue and sends it through open AI build summaries extracts does natural language processing uh pulls named entity recognition pulls company names and different numbers out of them and we'll be able to perform more and more unique tasks as we go so let's first see if we can summarize this text so we have the prompt now and we can just call open AI again with the same completion endpoint and send us this new send in this new prompt now if I run that what you're going to see is the multiple the model's maximum content length is 4097 tokens and so this returns an error it doesn't work we send out this huge wallet text and open an AI says no not going to do it and so we're going to need to learn how to take all of our different inputs and massage them and do different things with them in order to make it acceptable to open AI so it'll process them and so the first thing we're going to do here is chunk up our data so we can't send it a giant like thousands of pages of text and tell it to summarize Wikipedia all at once right that's too complex of a task to get an instant response like this and so we're going to take this long earnings call and chunk it up into pieces and this is the approach we're going to take so we have this long text transcript in a string and so since there's spaces that separate all the work words right here what I'm going to do is just divide this long blob of text into an array of words and so if I run this and split it on Spaces we'll get an array of words so good afternoon my name is Emma and so we have a python list I'm just going to show the first 20 words just to show what that looks like now what I can do here is since openai has this length limit here what I'm going to do is just divide up our text into different chunks here and so numpy has this array split function right here and so I'm going to send it this this giant list of thousands and thousands of words I'm going to say divide this up into pieces of equal size so I'm going to get six lists here and so you can come up with different rules on how you want to divide your text but for this particular one I just hard coded six and I said divide this into a six pieces you could divide it into paragraphs and do other things like that as well and so I call it numpy dot array split and I'm going to split this list of words into six smaller arrays and I'm calling this chunks and so if I run this and you see the chunks here here you see the first 1 6 of the earning calls the next one six and so forth so this is a list of arrays here right and so now we have this nicely chunked up and then now I can join one of these so I'm going to take the first array here so the first one six this list of words I'm going to join this back on spaces and just turn it back into the sentences and you'll see this is just it in paragraph format so this is the first 1 6 of sentences in that giant document right and so now let's see how one of these looks like let's summarize just the first 1 6 of the document so I'm going to get the sentences here and tldr and send it again and you'll see a summary of the first 1 6 of the document and so we can take the same technique and chunk up this into six pieces then summarize all six pieces and call open AI for each chunk of text and then we can collate and combine all these different chunks into a single final summary of the entire earnings call right and so if I go here and I run it you see the summary of that first one six and you'll see Simona welcomed everyone to the earnings call Colette Cress Executive Vice President and CFO reported revenue of 5.93 billion down 12 successionally sequently data center a revenue of 3.83 billion and so just a few sentences here we summarized that first uh that first section of the earnings call so that looks pretty good so how do we summarize all of it what we can do here since I've divided this into six chunks right we access the first chunk with uh chunks zero what I'm going to do is create a list of all the different summary responses and we're going to store all those summaries in this list so we're going to Loop through all the chunks we're going to take the sentences from each chunk we're going to tldr and call open AI then we're going to build this list of summary responses then we're going to join all those summary responses together and print out the full summary of the earnings call and so this is calling open AI six times with six different chunks of text we're going to get the summary put it all together and we'll have an earnings call summary just like that and that's our full Earnest call summaries and so we got that back and so I'm going to copy this into a text editor and let's look at it and see if it makes sense see how well open AI gpt3 understands the language of those many pages of text and so if you look at this gpt3 is very aware of all the different people that are talking in this earnings transcript so I have Simona Jankowski collect Cress and Jensen the CEO here we see a summary of the earnings right here just the the basic numbers we talk we see some information about the sell-through rate in gaming and then there's some comments on AI so the dynamic Computing environment so AI focus on productivity and the AI Enterprise software stack and uh Cloud gpus which I actually bought some Nvidia about a couple months ago a month and a half ago and so I'm kind of optimistic on the demand for a GPU use and their use in machine learning and so you can see some questions about hyperscale Cloud purchases and demand for these gpus in the future there's also some brief notes here that blockchain is not expected to be a major part of the company's business in the future because of the ethereum change to the proof of stake and so these miners aren't needing these gpus as much but you can see there's a huge demand coming in for gpus for AI machine learning purposes so there you go just like that we have a quick understanding of how to use the open AI API for a very specific task I'll be at a fairly simple one but this lays a very solid Baseline so that we can get started building more and more complex application some folks had said in comments in the previous video they couldn't even get the basic example off the open AI web page going so I'm hoping this helps those people get started and now we can continue to build on this and start building apps that accept sound text video URLs process them and perform perform more and more complex task and let our creativity run wild and so yeah I have a lot of good ideas in the pipeline and also let me know if there's anything specific you want to see with open Ai and I'll try to take that into account I already have like 10 to 12 other video ideas in my head right now and also a few people I just posted the open AI Whisperer video and a couple people are already asking me for like real time uh real-time uh Speech analysis of the FED speech and executing trades based on that which is completely expected because we have a bunch of Trader Types on this channel and so I'll show how to do a real-time speech transcription as well using whisper and talk about how we might classify text and maybe classify sentences as hawkish or dovish and things like that so text classification is something we need to do as well image classification all those types of things so thanks a lot for watching hope this was helpful for getting started with open ai's API and stay tuned got a lot in store for you thanks a lot for watching see you next time bye
Info
Channel: Part Time Larry
Views: 36,454
Rating: undefined out of 5
Keywords: openai, finance, python, api, text summarization, earnings call, nvidia, machine learning, trading, models
Id: LWYgjcZye1c
Channel Id: undefined
Length: 20min 53sec (1253 seconds)
Published: Wed Dec 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.