OpenAI Api Crash Course For Beginners | Financial Data Extraction Tool Using OpenAI API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today's video is going to be the beginners tutorial for open AI API which is the backend for famous chat GPT application using this we are going to build financial data extraction tool where you can copy paste the content from any news article related to companies Finance or reporting and it will extract key information such as companies name Revenue net income Etc when I was at Bloomberg we used to work on this kind of projects but we are using some traditional approaches in today's video we are going to use the most cutting-edge approach of using llm for this task the first step is creating account on openai.com you can click on login and create your account once the account is created you will go there and you need to click on API option go at the top view API Keys now I have multiple keys but you will see one key which starts with SK hyphen you need to save this we are going to make use of it going forward they give five dollar free credit uh so that should be more than enough for this project we will start our coding I have created Finance data extraction folder in my C code directory which is an empty folder and I've launched my pycharm where I am going to open that project so I will just say C code and then the name of that particular folder okay so we'll just say open so C code Finance data extraction tool and here it will create the empty main.pi file so I can just remove all of this and we'll start writing our code so I'll first go to my zen mode so that I can have a focus here okay so all it has is some empty function now we need to import open AI module if you are not installed this you can run pip install open AI and then first thing that you need to do is set an API key so here I will set my API key so it will be SK something and what I do is I don't keep my API key here so I can maybe create a new file here and I will just call it secret key so Secret secret key dot uh this one and open AI key so this is a python file where I'm creating some variable it will be called SK hyphen something okay and I'm not going to write my actual key so I will just close it I will pause the video and save that key so I saved that key in that file and here I will say from Secret Key import openai key and that key I will set here so I hope things are clear it's not a rocket science now we will call open AIS uh chat completion API and the way you do that is you will say open AI dot chat completion and here we are going to give some prompt okay I think you will say create you'll give some example prompt just to test things out so I would like to create some interesting prompts such as for example write a poem on Samosa only four lines please please this is very interesting I like Samosa and it can write poem on Samosa so let's do that so here I will create a function called poem on samosa and the prompt will be this okay so prompt will be this and in that API you need to specify a model by the way uh and if you go to open AI documentation you will find couple of models so here if you go to open a documentation click on documentation GPT in a chat completion API they are suggesting to use either GPD 3.5 turbo or gpt4 gpd4 is little costly but it gives you accurate result for complex tax if your task is not very complex you can use CPD 3.5 turbo because it is faster and it just saves you some time it is less costly as well so I will use this guy here okay and then what was the other argument by the way so the other argument was the solid we copy paste and explained so here you are giving a chat transcript or a conversation sample conversation here okay in our case all we need is write a poem on Samosa correct so I will just copy paste this thing here in the content and other things are not needed by the way so there are different kind of role system user assistant Etc but let's not worry about all those for now and just focus on just a simple task so write a poem on Samosa and whatever you get is my response now if you look at the response structure it has this element called choices which is a list so you need to get this zeroth element in that there will be another element called message then there will be element called content so we need to grab all of that so I will do that here okay and we can just simply print that and see what happens and from the main function I will call poem on Samosa let's right click and run the code awesome I'm getting water in my mouth poem is ready so my basic setup is correct now I will remove all this code because this is not necessary let's move on to our main business which is we want to write a function for financial data extraction and we need to First think what kind of prompt you are going to give to chat GPT or to extract that data well you can say things like this retrieve a company name blah blah blah from this article return the responses are valid Json string okay and then you can copy paste this article let me start with simple so I will say return retrieve company name Etc from this news article and we will say news article is this what is my news article well you go to Apple's earning article copy paste that thing here and let's see okay well I think it is doing something else it is using my previous thing okay retrieve company name Revenue net income from this article and here it is writing the code okay I mean I will open a new chat and I will say retrieve company name Revenue EPS from this article from this news article I mean you can say don't write code because I had some old contacts so it was kind of misbehaving at that point uh let's see see it works uh it told me whatever EPS Etc I can also tell it in which format I want because I'm writing a code in code rather than parsing this if it gives me dictionary or a Json string it will be easier for me to pass that so for that reason I gave it this kind of prompt saying a retrieve company name Etc from this article return the responses of validation string the format of the string should be this okay so let's modify this and say this and you'll notice that now I'll get a nicely formatted Json string which I can parse in my python code so this prompt is ready so I will uh save that prompt here as get prompt Financial I'll write a simple function you don't need to write a function as such but uh I believe it makes things little more cleaner so I wrote a function retrieve this from this article if you can't find information from this article don't make things up this is a very important you need to instruct llm what kind of behavior you expect from that otherwise it will make things up you need to also say Okay retrieve stock symbol for that company for this you can use some outside information I'm saying that to retrieve Revenue net income Etc you use the information from this article only but for stock symbol it can use its knowledge so you just read this prompt I did couple of tries and then I got this prompt you can pause the video and read it and by the way I'm hoping you guys are practicing along with me you're not just watching video this is not a Netflix movie it's like learning swimming if you can't jump into the pool you can't learn it so you have to practice along with me okay now I will write a function called extract financial data which takes the news article as a text and then whatever prompt I have I will append the news article so the prompt is this and after this you need to put your news article So when you say plus you're putting that news article so that makes your prompt and then the same open AI chat completion API whatever we tried with our samosa we are trying the same thing here and once you get the content the content will be string but in a dictionary format like this and if you want to out parse that in Python you might know that there is a model called Json and in Json you can say load string so load string will convert that string into a dictionary format and I have this habit by the way to put try and catch because if it's uh if if it's not a valid Json string then weird things can happen okay so that's why I always have this in a try accept uh and if let's say there is an error or something uh I want to return an empty data frame which looks something like this so the empty data frame will look something like this and I need to import pandas obviously Panda SPD so it is importing pandas and creating this empty data frame now once I have data from that data also I need to create a data frame so how do I do that so I will say part period.data frame and if you do data dot items it will use that to create pandas rows and columns and columns you can specify as columns is equal to I have two columns measure and values so what is measure and values so let me show you so the measure and values are this so measure is this values is this so this is a data frame two columns measure and value so that's exactly what I'm doing here once I'll run the code you will get it and you can return this data frame back as a as a string now now why we are doing data frame well we are going to use streamlit uh for writing the code in streamlit you can directly render UI for a data frame Let's uh test this code out in the main function here and I will say get not get extract financial data and the text will be what so for text maybe I will just use some simpler text such as this Teslas earning in newspaper in news in this format whatever 4.5 billion is a news article small news article and here you are getting your DF and if you want to convert DF to a string this is how you convert it and you can just print it so let's right click and run it perfect see company name Tesla whatever so it is working as expected uh it's curious it just worked on a first time maybe I have because I have practice usually things don't work on a first attempt you might have the experience but our code is ready and we can now create a streamlined base application so let's look into that let's create a streamlit app streamlit app I would like it to be in main.pi so what I'll do is I will move all of this code in a separate file and maybe we should call that file open AI helper and just Ctrl a x control V save it and here I will import streamlit as St if you're not installed stimulate folks just run pip install streamlit okay and streamlit let me tell you what stimulate is so financial data extraction tool let me run this and then then I will talk about it okay so I import this template I'm saying St dot title and you're wondering what this is okay let me show you so here you can run a command streamlit run dot Main py and let's see what happens see it created an App in seconds it's a UI you know it's a it's a front-end application so if you go to stimulate website it's it's a tool that allows you to build your data science applications very quickly because as a data scientist when you are writing code you don't have too much time to wrap things around in flask and fast API and writing some react uh react.js or whatever HTT HTTP CSS base front end so if you don't want to waste too much of your time writing a web code you just use streamlit and even in my company when we work in ethnic when we work with our clients on data science and machine learning projects for a client demo we always use streamlit because we have code return we have notebook but if you want to demo it in a nice nice UI quickly then streamlit is away so streamlined is something you use for proof of concept demo Etc and the benefit you you're just seeing it you can write code so fast and create this application in a matter of seconds so now I will go to my zen mode okay Enter the Zen mode and let's create an app so what I did is I talked to my good friend you know who is my good friend Chad GPT so in chat GPT I said create a streamlined app where on the left hand side I have a big edit box see left hand side I have a where is it okay so on left hand side you see here we will have big edit box where you can copy paste news article on the right hand side you will have a table and Below edit box you will have extract button I told it the exactly same thing below that I have extra button on the right hand side of a table where you put blah blah blah and it give me nice core see and if I just copy this code here and copy paste this code here save it go back to my UI rerun it see if I click on extract whatever it will fetch the information but it is putting that table in the bottom I need to say that okay please put the table on the right hand side so I can go here and I can say okay this and I think okay can you put the table on the right hand side right now it is below the news article and okay it will it will require some kind of trial and error and eventually it will get it so I'm going to just show you what I got in the end so let me just remove this thing and I will start from the beginning so I will do this here St dot columns now St dot columns is used for creating layouts in streamlit application and what I mean by layouts is this so let's say you you want to divide your screen where three out of five portion is on the left hand side for News Article 2 out of five portion is on the right hand side for your table so that's what this means okay so call one and call to these are like two screen areas you have created uh and if I have to explain you using let's say some some kind of pain tool let's say you have this screen where this particular area let's say you consider it as Phi units so three units see these three units you want to keep for your news article okay and the two units you want to keep for that table where you can have Revenue EPS Etc so that's what this is doing it's very simple and you will say here that width column one St dot title Financial or just data extraction tool I don't want to type the whole thing and St dot text area is used to uh you know put that big edit box and you can just say this you can also provide a height and then whatever news article you copy paste it will come into this and then you can have s t dot button call extract at the bottom just save this and let's see how our code looks so far so you can see you have this news article you have an extract button on the right hand side you will have a table so here you say when button is clicked what happens so this thing will happen when button is clicked it will go into if block okay so let's keep this thing as is and let's focus on creating the right hand side table and for that what I can do is I can create an empty data frame so let's create an empty data frame Panda's data frame and let's put that data frame here so you can say St data frame is equal to financial data TF and let's see how this thing works okay fine it is working I know it's not perfect so number one problem is it has this index 0 1 2 3 4 I don't want that you can remove that easily by saying hide index true and also the column width is not appropriate so I I talked to my dear friend chat GPD once again and it said that I should use this column config this column config where measure column is 150 pixel wide and value is also same width okay so again hit save rerun this looks good but it is not aligned so we need to move it little down once again chat GPT told me I need to create this kind of space using markdown BR is the that HTML tag which you can use to create that markdown and now my table is kind of in alignment with the news article all right now we need to do wiring we are like electricians we need to do some wiring so on extract something needs to happen Okay what will happen well you will call that function from open API or let's say I will just import open API Helper and I need to call that function extract financial data news article and it will give you the data frame back financial data frame DF back you will get that data frame okay so let's try this out and I'm just going to see if I missed anything here uh probably not let me see so I will exit from Zen mode I will check the open AI helper and PD dot data frame okay columns are okay perfect all right I have saved my code I can go back rerun my tool you can press R key also okay CR and news article I can copy here wonderful it's working folks EPS 1.5 to net income is 24.16 see 24.16 in net income very good and 94.64 is here perfect let's try some other article HDFC and here what you will notice is that let's see if numbers are correct so net profit is this now it knows that net profit and net income is same so this is the benefit of using llm it understands that these two terms are same uh if you're using some regular expression some manual traditional approach this will be a mess it won't have that understanding that net profit and net income is the same thing uh EPS so if PSA did not fetch because this article doesn't have EPS at least this chunk and uh revenue is 34 point something it's a Consolidated net revenue so it knows Consolidated net revenue is equal to this particular Revenue I can copy paste this uh Tesla's article it's just a very small one and you'll notice here I'm saying profit see it knows that profit is net income say 4.5 billion so these are the benefits of using LNM now we need to test some worst case scenario let's say high extract doesn't work but it is returning that empty data frame back that's all I had for this video folks you will find the link for the code in the video description below as I said before learning coding or AI is like learning swimming unless you try it it's not gonna be effective for you open AI is giving you five dollars free credit which is more than enough and you can use that to build this tool end to end it can look good on your resume for your upskilling NLP fill is booming and llms are kind of uh very hot right now it's a hot skill uh in terms of job market so I hope this video has given you some understanding on how these things work if you like this video you can give it a thumbs up it's it's only a one second work for you but for us it makes a big difference because it makes YouTube algorithm happy so give it a thumbs up if you like it or subscribe to our Channel or share this video with your friends who are learning AI if you have any question there is a comment box below thank you for watching thank you
Info
Channel: codebasics
Views: 21,631
Rating: undefined out of 5
Keywords: yt:cc=on, openai api tutorial, openai api tutorial python, openai api explained, financial data extraction, streamlit, streamlit tutorial, openai python, openai crash course, chatgpt
Id: xP_ZON_P4Ks
Channel Id: undefined
Length: 25min 16sec (1516 seconds)
Published: Wed Jul 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.