3. OpenAI API Python - Earnings Call Summarization
Summary
TLDRThis video script offers a tutorial on utilizing OpenAI's API with Python to perform tasks like summarizing earnings calls. It guides viewers through installing the OpenAI package, obtaining an API key, and crafting prompts for the AI model. The script demonstrates the process of summarization by breaking down a lengthy earnings call transcript into manageable chunks and using the API to generate concise summaries for each part. The tutorial emphasizes the importance of understanding basic functionalities to build more complex applications and hints at future videos exploring real-time speech transcription and text classification.
Takeaways
- 🧑💻 The power of OpenAI is fully realized when developers interact with it through computer programs using its API, allowing dynamic crafting of prompts and processing of outputs for unique tasks.
- 📚 To start with OpenAI's API in Python, one must first install the OpenAI Python package using the pip command 'pip install openai'.
- 🔑 Users need to sign up on OpenAI's website to obtain an API key, which is essential for accessing the API and managing credits for the services used.
- 💡 OpenAI provides free credits worth $18 to new users, which is a good starting point to learn and experiment with the API without incurring costs.
- 💰 The API key is necessary for billing purposes, as using OpenAI's services will eventually require payment when the free credits are exhausted.
- 📈 OpenAI's models, such as Ada and DaVinci, have different costs associated with them based on the complexity and capabilities of the model.
- 📝 The script demonstrates a basic task of using the OpenAI API to answer questions and generate summaries, starting with a simple Q&A prompt and moving to summarizing earnings calls.
- 🔍 The script also covers how to handle large text data by chunking it into smaller parts to comply with OpenAI's token limit for processing.
- 🔄 The process of summarizing long documents involves breaking down the text, sending chunks to OpenAI for summarization, and then combining the results into a cohesive summary.
- 🛠️ The script provides a foundation for building more complex applications, such as those that process audio, text, and video, and perform natural language processing tasks.
- 🔮 The tutorial encourages users to explore OpenAI's capabilities further and consider building applications that can leverage its advanced features for various use cases.
Q & A
What is the main purpose of the video script?
-The main purpose of the video script is to demonstrate how to use OpenAI's API with Python to perform tasks such as summarizing earnings calls and to introduce viewers to the capabilities of AI for developers.
How can one start using OpenAI's API?
-To start using OpenAI's API, one needs to install the OpenAI Python package using pip, obtain an API key from the OpenAI website, and then configure the API key in the Python code.
What is the significance of an API key in using OpenAI's services?
-An API key is significant as it ties the usage of OpenAI's services to an individual account, allowing for tracking of usage and billing, ensuring that each user's credits are managed separately.
Why might OpenAI start charging for its services in the future?
-OpenAI might start charging for its services in the future because running cloud infrastructure and making machine learning models available as a service incurs substantial costs.
What is the initial credit provided by OpenAI for new users?
-OpenAI provides 18 dollars worth of credits for free to new users, allowing them to try out the service and learn how to use it.
How can the OpenAI API be used to summarize text?
-The OpenAI API can be used to summarize text by sending a prompt with the text to be summarized appended with 'tldr' to the OpenAI completion endpoint, which will then return a summarized version of the text.
What is the limitation on the maximum content length for the OpenAI API?
-The OpenAI API has a maximum content length limit of 4096 tokens, which means that very long texts need to be chunked into smaller parts before being sent for processing.
How can the text of an earnings call be obtained for summarization?
-The text of an earnings call can be obtained by using the Python requests library to fetch the text from a URL where the transcript is available online.
What is the approach to handle long texts when the OpenAI API has a token limit?
-The approach to handle long texts is to chunk the text into smaller parts, summarize each part individually using the OpenAI API, and then combine the summaries to form a complete summary of the original text.
What are some potential future applications of the OpenAI API as mentioned in the script?
-Some potential future applications of the OpenAI API include real-time speech transcription, text classification for sentiment analysis, and building full-stack applications that process various types of data like sound, text, and video.
Outlines
🚀 Getting Started with OpenAI's API
This paragraph introduces the capabilities of OpenAI's large language model, emphasizing the interactive experience provided by the Open AI playground and the powerful API available to developers. It outlines the process of installing the OpenAI Python package, obtaining an API key, and configuring the environment for API use. The speaker also discusses the cost implications of using the API, including the initial free credit and the potential future costs associated with extensive use, highlighting the balance between cost and the convenience of leveraging AI without training custom models.
📚 Exploring OpenAI's API Features and Prompts
The speaker delves into the various examples and prompts available within the OpenAI API, demonstrating how to interact with the API using Python to perform tasks like answering questions or summarizing text. The paragraph highlights the importance of crafting specialized prompts to achieve desired outcomes from the AI model. It also touches on the different models offered by OpenAI, such as Ada and DaVinci, and their respective costs, emphasizing the need to choose the right model based on the task's complexity.
🔍 Summarizing Earnings Calls with OpenAI
This section focuses on a practical application of the OpenAI API: summarizing earnings calls. The speaker describes the process of transcribing audio to text and then using the API to generate summaries with the 'TLDR' prompt. The paragraph discusses the limitations of the API in terms of content length and introduces a method for dividing the text into manageable chunks to overcome these limitations, providing a step-by-step approach to summarizing large documents effectively.
📈 Implementing Text Chunking for API Summarization
The speaker provides a detailed explanation of how to chunk long texts into smaller segments for summarization using the OpenAI API. This includes splitting the text into an array of words, dividing it into equal-sized chunks, and then joining these chunks back into sentences for summarization. The paragraph demonstrates the process of summarizing each chunk individually and then combining the summaries to form a comprehensive summary of the entire document.
🌟 Building Complex Applications with OpenAI API
In the final paragraph, the speaker envisions the potential for building more complex applications using the OpenAI API, starting from mastering simple tasks and gradually incorporating more sophisticated functionalities. The speaker expresses optimism about the future of AI applications and invites viewers to share ideas for specific use cases they would like to see demonstrated with OpenAI. The paragraph concludes with a teaser for upcoming videos that will explore real-time speech transcription and text classification, among other advanced topics.
Mindmap
Keywords
💡API
💡GPT (Generative Pre-trained Transformer)
💡Language Model
💡Summarization
💡Token
💡DaVinci Model
💡Earnings Call
💡Python Package
💡API Key
💡Machine Learning Model
Highlights
Introduction to the power of AI and its creative output through the Open AI Playground.
The capability of developers to interact with OpenAI through a powerful API for dynamic tasks.
Tutorial on getting started with OpenAI's API using Python for summarizing earnings calls.
Installation of the OpenAI Python package using pip and importing it for use.
Explanation of obtaining and securing an API key from the OpenAI website.
The necessity of an API key for accessing the service and managing credits.
Details on the cost associated with different OpenAI models and their usage in applications.
Demonstration of using the OpenAI API to answer questions like 'Who is the best investor of all time?'
The process of crafting specialized prompts for the OpenAI API to perform unique tasks.
Experimentation with different parameters like 'temperature' to control randomness in responses.
Method of handling text length limitations by chunking data for processing by OpenAI.
Technique for summarizing long earnings call transcripts by breaking them into smaller pieces.
Example of summarizing the first 1/6th of an earnings call document using OpenAI.
Collating summaries from multiple text chunks to form a comprehensive summary.
Final summary of an earnings call highlighting key financial figures and business insights.
Discussion on the potential for building more complex applications using the OpenAI API.
Invitation for viewers to suggest specific applications or tasks for future OpenAI demonstrations.
Transcripts
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
浏览更多相关视频
AutoGen Quickstart 🤖 Build POWERFUL AI Applications in MINUTES
Build Talking AI ChatBot with Text-to-Speech using Python!
Build Anything With ChatGPT API, Here’s How
How To Build Your Own AI With ChatGPT API
ChatGPT - OpenAI API w Excelu (za darmo)
End To End Youtube Video Transcribe Summarizer LLM App With Google Gemini Pro
5.0 / 5 (0 votes)