Introduction
Welcome to the next skill of the Programming for Data Science course, Explore AI Language Models and OpenAI's ChatGPT! In this first video, we'll review everything we're going to learn in this skill, see you there!
What is AI?
From Wikipedia, "Artificial intelligence (AI) is intelligence—perceiving, synthesizing, and inferring information—demonstrated by machines, as opposed to intelligence displayed by non-human animals and humans. Example tasks in which this is done include speech recognition, computer vision, translation between (natural) languages, as well as other mappings of inputs."
In this video, we'll break AI down into smaller subsets and lay the foundation for working with each technology. See you there!
OpenAI GPT-3 Language Models
From Wikipedia, "A language model is a probability distribution over sequences of words. Given any sequence of words of length m, a language model assigns a probability to the whole sequence. Language models generate probabilities by training on text corpora in one or many languages. Given that languages can be used to express an infinite variety of valid sentences (the property of digital infinity), language modeling faces the problem of assigning non-zero probabilities to linguistically valid sequences that may never be encountered in the training data. Several modelling approaches have been designed to surmount this problem, such as applying the Markov assumption or using neural architectures such as recurrent neural networks or transformers."
What is ChatGPT and How Does it Work Under the Hood?
In this video, we'll use Seaborn to visualize a probability distribution of as language model to further explore the concept. I particularly like using other Data Science libraries to keep one foot in Data Science and the other in AI. See you there!
import seaborn as sns
import matplotlib.pyplot as plt
# Generate mock data for visualization
prompt = "The cat"
predicted_words = ["sat", "jumped", "ran", "slept"]
probabilities = [0.8, 0.1, 0.05, 0.05]
# Plot the data using Seaborn
sns.set(rc={"figure.figsize":(10,5)})
sns.barplot(x=predicted_words, y=probabilities)
plt.title("Prediction for next word given prompt: '{}'".format(prompt), fontsize=20)
plt.xlabel("Predicted word", fontsize=16)
plt.ylabel("Probability", fontsize=16)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.show()Prompts and Completions
In this video, you get to finally interact with ChatGPT and this time, programmatically using Google's Colab. I've included both the Google colab, an example text file for your key you'll need to modify and upload to Colab, and the code in case you want to use an IDE.
That being said, be sure to create your own api key in the OpenAI dashboard and modify the example my_key.txt file or create your own. All that needs to be in the file is the key, that's all.
!pip install openai
!pip install prettytable
import os
import openai
import time
from google.colab import drive
import prettytable
drive.mount('/content/drive')
with open('/content/my_key.txt', 'r') as file:
api_key = file.read().strip()
# Store the API key in an environment variable
os.environ["OPENAI_API_KEY"] = api_key
# Use the API key to authenticate
openai.api_key = api_key
# Define the list of available models
models = ['text-babbage-001', 'text-curie-001', 'text-davinci-002', 'text-davinci-003']
# Define the rate limit (e.g., 10 requests per minute)
rate_limit = 10
rate_limit_interval = 60
# Initialize a counter for the rate limit
request_count = 0
# Timestamp for the start of the rate limit interval
interval_start = time.time()
# Continuously prompt the user for questions
while True:
# Ask the user to enter a question
question = input("Enter a question (type EXIT in all caps to exit): ")
# Check if the user wants to exit
if question.upper() == "EXIT":
break
# Check if the rate limit has been exceeded
if request_count >= rate_limit:
time_elapsed = time.time() - interval_start
if time_elapsed < rate_limit_interval:
time_remaining = rate_limit_interval - time_elapsed
print(f"Rate limit exceeded. Please wait {time_remaining:.2f} seconds.")
continue
else:
# Reset the rate limit counter and interval start time
request_count = 0
interval_start = time.time()
# Ask the user to select a model
print("Select a model:")
for i, model in enumerate(models):
print(f"{i + 1}. {model}")
try:
selected_model = int(input("Enter the number of the model you want to use: ")) - 1
if selected_model < 0 or selected_model >= len(models):
raise ValueError("Invalid model number")
except ValueError as error:
print(f"Error: {error}")
continue
# Generate text
try:
response = openai.Completion.create(
engine=models[selected_model],
prompt=question,
max_tokens=2000
)
except openai.OpenAIException as error:
print(f"Error: {error}")
continue
# Print the generated text using prettytable
table = prettytable.PrettyTable(["Model", "Response"])
table.add_row([models[selected_model], response["choices"][0]["text"]])
table.max_width = 100
print(table)
# Increment the rate limit counter
request_count += 1Fun Examples to Try on Your Own!
Here are my favorite three examples for anyone getting started with the OpenAI API. Enjoy! 🪐
1. Corrects sentences into standard English without a subscription!
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="text-davinci-003",
prompt="Correct this to standard English:\n\nShe no went to the market.",
temperature=0,
max_tokens=60,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)2. Explain Complicated Code! In the code below, you are leveraging temperature and top_p which we'll cover in the next skill. Until then, have fun with this code co-pilot example!
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="code-davinci-002",
prompt="class Log:\n def __init__(self, path):\n dirname = os.path.dirname(path)\n os.makedirs(dirname, exist_ok=True)\n f = open(path, \"a+\")\n\n # Check that the file is newline-terminated\n size = os.path.getsize(path)\n if size > 0:\n f.seek(size - 1)\n end = f.read(1)\n if end != \"\\n\":\n f.write(\"\\n\")\n self.f = f\n self.path = path\n\n def log(self, event):\n event[\"_event_id\"] = str(uuid.uuid4())\n json.dump(event, self.f)\n self.f.write(\"\\n\")\n\n def state(self):\n state = {\"complete\": set(), \"last\": None}\n for line in open(self.path):\n event = json.loads(line)\n if event[\"type\"] == \"submit\" and event[\"success\"]:\n state[\"complete\"].add(event[\"id\"])\n state[\"last\"] = event\n return state\n\n\"\"\"\nHere's what the above class is doing:\n1.",
temperature=0,
max_tokens=64,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\"\"\""]
)3. Last but not least, convert movie titles into emoji 🎥🎬🖖
Back to the Future: 👨👴🚗🕒
Batman: 🤵🦇
Transformers: 🚗🤖
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="text-davinci-003",
prompt="Convert movie titles into emoji.\n\nBack to the Future: 👨👴🚗🕒 \nBatman: 🤵🦇 \nTransformers: 🚗🤖 \nStar Wars:",
temperature=0.8,
max_tokens=60,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\n"]
)Challenge
It's time to check your knowledge of everything you have learned so far. Answer the questions below and if you get any of them wrong, feel free to review the corresponding video above. You got this! 🎉
Knowledge Check
True/False: Deep learning models are based on artificial neural networks
Knowledge Check
True/False: A language model is a probability distribution over sequences of words.
Knowledge Check
What unit does ChatGPT use to make predictions?
Knowledge Check
True/False: When interacting with ChatGPT, it doesn't matter how you write a prompt, you'll generally get accurate, human-like output regardless.
View Transcript
Introduction
0:06<v ->Hello, and welcome.</v>
0:08In this skill, we're gonna get a chance
0:10to explore language models and OpenAI's ChatGPT,
0:15and what's really exciting about all of this
0:18is that this is gonna set us up
0:19for understanding machine learning,
0:21which is the next part of Programming for Data Science,
0:25Where we are in the course,
0:26so this is a skill so that you could take this skill
0:28and just learn about language models and ChatGPT,
0:33but this is also part of a course
0:35called Programming for Data Science,
0:37and what's cool about that is that we've learned Python
0:40and all the libraries for data science,
0:42and now we're gonna get a chance
0:44to sort of apply the programming to AI,
0:48right, and check out language models
0:50because it's super popular right now.
0:51So ChatGPT was released,
0:53and basically, everyone's talking about it.
0:56It's super exciting.
0:57AI is kind of like now everybody knows about it.
1:01Data scientists have been using AI in various capacities,
1:04computer scientists and so on.
1:06However, now it's kind of like out, right?
1:08Everybody knows about it.
1:10So let's take a look at everything
1:11that we're gonna learn in this skill.
1:16So first we'll check out what is AI and define that,
1:20and that's really important
1:21when we're talking about language models.
1:23And so this would be AI.
1:24And what we're gonna do in this first video
1:26is really talk about the other parts of AI.
1:28So this is a subcategory and another subcategory of AI.
1:32And this is just gonna us
1:33a basic understanding of what AI is,
1:36and that's what ChatGPT is.
1:37So we'll dive into what ChatGPT is
1:40in the context of AI, right?
1:42And then we'll dive a little bit deeper into language models
1:46and check out OpenAI's GPT-3 language models.
1:50And so what is this?
1:51Well, we have many models
1:54that are available to us through OpenAI,
1:57and we'll explore all of these.
1:59And each one of these models
2:00will have a different sort of specialty, right?
2:05So the first one is the most basic,
2:08and by the time that you get to the fourth one,
2:09it's the most advanced.
2:11And then we'll talk about how does it work under the hood,
2:13and that's really cool
2:14because you would think that it's really just super smart
2:18when, in fact, that's not what's actually happening.
2:20And we'll talk about that
2:21because it's making predictions, right?
2:24It's like you could think of it as like a letter.
2:26So C-H-A, and then
2:29what is the probability of this one being T, right?
2:32So that would depend on what happened before.
2:35And these are all predictions.
2:39So I wouldn't say ChatGPT is smart,
2:41but I would say it's kind of clairvoyant.
2:44Like, it can make some predictions,
2:46but it doesn't know anything else.
2:47It's just like, "There's is a very strong probability
2:50that the next word is cat."
2:52Like, that's kind of what's happening.
2:54And when you have CPU
2:55and GPUs allowing you to do this very, very quickly,
2:58then you can get these, like,
2:59amazing results from a chatbot.
3:02But when you break it down, it's pretty surprising,
3:04and that's what we're gonna talk about in that section.
3:07And then finally, how do we interact with ChatGPT?
3:10And that would be through prompts.
3:12And so what are prompts?
3:13Well, that's exactly what we're gonna talk about.
3:16And then finally, we have this challenge,
3:18and this challenge is just to make sure
3:19that you understand all of the concepts in the video.
3:23And if you don't, this is a great time to review those
3:26because everything is going to sort of build off
3:29of everything else.
3:30If there's some holes that you have in what AI is
3:32or what a language model is,
3:34by the time that we get to use it,
3:36there's gonna be some things that might not make sense.
3:38So just be sure to review those
3:40so that you get, like, the most bang for your buck.
3:42But at this stage, it should be pretty straightforward.
3:45All right, and so that's about it for this video,
3:48and I'll see you in the next video, What is AI?
3:51So until then, I hope this has been informative,
3:54and I'd like to thank you for viewing.
What is AI?
0:06<v ->Before we jump into ChatGPT and OpenAI,</v>
0:10and all that good stuff,
0:11which is like a lot of fun.
0:12Probably already checked it out
0:13if you're curious about it, but if not,
0:15what we're gonna do is just
0:17unpack what is AI so that we can get
0:19the most outta this, right.
0:20And in the next two skills,
0:22we're gonna build off of all of this knowledge.
0:24So let me just break down
0:25what we're gonna do in these three skills,
0:27and how they all work together.
0:28Normally we just go skill by skill,
0:31and we don't really connect them.
0:32And this is true in this case,
0:34you don't need to connect these three skills,
0:36but just wanted to give you a heads up
0:38in case you wanted to build something,
0:40and here's what it is.
0:42So this first skill,
0:43we're gonna talk about what is AI,
0:44and check out ChatGPT,
0:46and really get into like prompts,
0:48and how does ChatGPT
0:51make these predictions, right.
0:52They use something called tokens.
0:54So we'll dive a little bit deeper into that.
0:56And the next skill, we're gonna take ChatGPT,
0:59and Google Colab and then work with the API.
1:03So we can work programmatically with ChatGPT,
1:06and the different models, right.
1:08And then you start to wonder,
1:10"Wow, I can actually mix Python
1:12with ChatGPT,
1:14and things start to get really amazing.
1:16The power is really at that point
1:18just starts to just exponentially take off,
1:21and here's why.
1:23ChatGPT is making predictions,
1:24but when you pair it with
1:25a programming language like Python,
1:28then you can get them to work together,
1:29and then it's pretty amazing.
1:31So what we can do is in the third skill,
1:34we're gonna build an app
1:35using something called Streamlit.
1:37So you're gonna build an AI app,
1:39and launch it in that third skill.
1:42So there's a lot going on
1:43in these three skills, right.
1:45So this is programming for data science,
1:46but I'm giving you a little heads up,
1:48and showing you how you can build
1:49a data science app,
1:50and publish it to the web using AI.
1:54And so we'll talk about what is AI
1:56in this video.
1:58So when we're thinking about this,
2:00let's talk about it in the big picture, right?
2:02So AI generally is something
2:05that normally would be a task
2:08that a human would do, but now we can use,
2:11this is my attempt at a robot, right.
2:15So this is a machine.
2:16So normally AI is something
2:19that would normally be done
2:20by a human that is now,
2:22sort of done by machine.
2:24And that's the high level overview, right.
2:25So we're thinking about things like
2:27recognizing speech, making decisions,
2:30solving problems, right.
2:31AI has the potential
2:33to automate all of these tasks,
2:35but they're currently performed by humans.
2:38But robots are gonna be more efficient,
2:40and that's how it is, but it's not terrible,
2:42and here's why.
2:43It's the same thing with the calculator
2:45or first car when cars replaced horses,
2:48and things like that.
2:49The only difference is that,
2:50we just don't know what the future's gonna be.
2:52So it's, it's kind of exciting
2:53and sort of terrifying for everybody.
2:56But I'm sure it was the same thing
2:57with the calculator
2:58or any like technological breakthrough.
3:01So we're not talking about like
3:02the "Terminator" side,
3:03like the Hollywood version of AI.
3:05What we're talking about is the big picture.
3:07So AI is gonna help us automate tasks.
3:09And then so what do humans do?
3:11Well, humans can do things
3:12that humans are better for at, right.
3:15Humans aren't so great at repetitive tasks
3:17over and over and over like a robot.
3:19So it kind of makes sense.
3:20So let's dive deeper into this.
3:23Okay.
3:24So here we have artificial intelligence.
3:26So this is that main concept
3:27that we've been talking about.
3:29And AI would be like things like reasoning, act,
3:33adapt sense,
3:34so like human tasks.
3:37All right.
3:38Fair enough.
3:38So what is machine learning,
3:40and what is deep learning?
3:42So this is machine learning right here.
3:44So machine learning is a subset of AI,
3:47and these are the algorithms
3:49whose performance improves when you add more data
3:51over time, right.
3:53So it learns from data,
3:55without being explicitly told to do that.
3:57So this is very cool,
3:59and we'll talk a little bit about this.
4:01And when we're talking about machine learning,
4:03we have tests, training,
4:05and test data, right.
4:08And then we'll talk more about that.
4:10And then finally we have deep learning, right.
4:12So deep learning is a subset of machine learning.
4:15As it gets deeper, there are all subsets
4:17of each other
4:18using slightly different technology.
4:19For example, deep learning uses
4:21neural networks.
4:23Many, many neural networks
4:25and big data modeled on the human brain,
4:27and this is especially useful
4:29for when we're processing unstructured data,
4:32and other things.
4:33We'll talk about that in a more detail.
4:35Again, we're just trying to stay
4:37at the high level.
4:38So we know what AI is,
4:40it's the general term.
4:41Machine learning sort of uses test,
4:44and training data to learn.
4:46And deep learning uses
4:47these recurrent neural networks,
4:49these mini networks to refine,
4:51and get these products like
4:53deep fake videos, right.
4:54Which is pretty amazing, right.
4:56So AI we know is the larger domain,
4:58and machine learning and deep learning
5:00are both subsets of AI.
5:02And these tasks are usually done by humans.
5:05And one of the earlier
5:06types of AI is expert systems.
5:09And this was really cool
5:10because it was actually very early AI,
5:14but the problem was that it was very time consuming,
5:18and very complicated to build
5:19because you would get about
5:22a million instructions right or more.
5:25And imagine doing that by hand.
5:27So this was very, very time consuming,
5:30and complicated.
5:32The complexity was just very, very large.
5:35And what deep learning does,
5:36interestingly enough,
5:37it sort of automates that process.
5:39So there's a huge breakthrough,
5:40and that's why deep learning,
5:42and deep fake videos that you're seeing
5:44are just exploding and coming out
5:46because it makes that process of expert systems,
5:49and basically using neural networks.
5:51Many of them to sort of refine the output.
5:54And so we take AI and you have machine learning,
5:57and then you have deep learning,
5:58and you see how they all sort of
6:00kind of work together
6:01to produce these amazing results.
6:04And then machine learning,
6:05we talked about how it's an algorithm
6:07that helps us make decisions
6:09by using that training and test dataset.
6:12And then finally, the sub last subset
6:14of ML in this case is deep learning.
6:17There are other subsets,
6:18but these are the main ones.
6:19And it uses deep neural networks
6:22to learn patterns in complex data, right.
6:24So like deep fakes and even language models.
6:27All right.
6:28So that's about it for this video,
6:30and I'll see you in the next one
6:31when we talk about OpenAI GPT-3 Language Models.
6:35I hope this has been informative,
6:36and I'd like to thank you for viewing.
OpenAI GPT-3 Language Models
0:06<v ->Welcome back.</v>
0:08So in this video, we're gonna talk about language models,
0:11and if you're probably wondering what a language model is,
0:14think of it as a probability distribution of words
0:18and which is the most probable
0:20to come next in a sequence of words.
0:23And in this video, we're gonna take a look
0:25at OpenAi's GPT-3 models.
0:28But first, let's check out a definition from Wikipedia
0:31on a language model.
0:33Okay, so the definition,
0:34and we're just gonna look at this first one here,
0:36is, "A language model is a probability distribution
0:39over sequences of words."
0:41So basically what we just discussed.
0:43So, "Given any sequence of words, length m,
0:47a language model assigns a probability," right?
0:50So word one all the way through word m
0:52for the whole sequence, right?
0:54So, "Language models generate probabilities
0:56by training the text corpora in one or many languages.
1:00Given that the languages can be used to express
1:03an infinity variety of valid sentences,
1:05language modeling faces the same problem
1:07of assigning non-zero probabilities
1:09to linguistically valid sequences
1:12that may never be encountered in the training data."
1:14In summation, several modeling approaches have been designed
1:18to deal with this problem,
1:19such as applying Markov assumption
1:22or using neural architectures
1:24such as recurrent neural networks or transformers.
1:27Now, this might be like, okay, what is, (laughs)
1:30what is a language model again?
1:32And while some of this makes sense, some of it may not.
1:35It's gonna be a lot easier
1:36because we're talking about it in the scope of ChatGPT.
1:40All right, so without further ado,
1:41let's jump into OpenAI GPT-3 language models
1:46so that we can better understand what is a language model
1:49and what's happening with this probability distribution.
1:54All right, so what is ChatGPT?
1:57We said that, well, it's a language model
1:59developed by OpenAI.
2:02All right, so that's good, but what does GPT mean?
2:05So GPT
2:08is generative pretrained transformer, right?
2:12Which is a type of language model
2:14that's developed by OpenAI,
2:16and again, it uses deep learning techniques,
2:19so we talked about that, to generate human-like text.
2:23And how does it do that?
2:24In short, you have a massive amount of data,
2:27like big data, a lot of complexity.
2:30And so the GPT,
2:32these models are trained on this huge amount of data
2:35with a goal of generating coherent text
2:37that just shoots out.
2:39And it does this pretty much most of the time.
2:42And, again, we're talking about a clairvoyant,
2:44but not necessarily rational fortune teller, I guess, right?
2:48Word fortune teller.
2:49So we'll dive into this.
2:51But what are the models?
2:52So we've said, okay, we know what ChatGPT is.
2:55It's this language model.
2:57We don't know what a language model is totally,
3:00so let's talk about these different models.
3:02So there's more than one.
3:04For example, we have the first one.
3:07So text-ada-001,
3:09and this is Ada Lovelace.
3:13If you don't know Ada, big part of computer science.
3:15So in short,
3:16this model is the smallest and most basic model,
3:19and it's designed
3:20for simple language generation tasks, right?
3:22So if you're first starting out using GPT models,
3:25this is a good one, okay?
3:26So this is the starter and also the smallest.
3:29Then we have text-babbage.
3:32And this one is,
3:35and this one's named after Charles Babbage.
3:38Also, both mathematicians that work closely together
3:42and definitely research more about them.
3:44And this is just a more advanced version
3:46of this model right here.
3:48And what you're gonna get here
3:49is more sophisticated responses.
3:51And that's good, if you need more advanced capabilities
3:54compared to ada-001, so this would be a good choice.
3:58And then number three, we have curie, okay?
4:01Then we have text-curie-001
4:03named after physicist, Marie Curie.
4:06And again, this is gonna be a more advanced version,
4:09more sophisticated with more computing power
4:12compared to text-babbage-001.
4:14It's good for like writing articles, composing poetry,
4:17and even coding, right?
4:20We'll talk a little bit more about that.
4:22And the next one, which is,
4:24actually, let me just clean this screen up
4:26and then start again.
4:27And then number four, we have,
4:30and this one's named after Leonardo da Vinci,
4:32and it handles the most demanding tasks
4:35like writing entire books,
4:37composing complex software programs,
4:39great for demanding language generation tasks,
4:43and also for those
4:44who need more advanced power capabilities.
4:46Again, we went from the other two models, one through three,
4:50and then finally, we have this one,
4:51and this is just gonna be the most powerful.
4:54And the takeaway here is that, well, you have a choice.
4:57If you need something that's very, very basic,
4:59well, then you would use the first one, right,
5:01text-ada-001.
5:03Then you might sort of jump down
5:06based on what your needs are,
5:07and you think of it as a production application.
5:09So if you have a production application,
5:11you don't wanna use davinci-003
5:14if you're just doing something very, very basic.
5:16You wanna match that.
5:17And this is kind of like thinking about
5:19algorithm complexity, right?
5:21You don't wanna use something that's gonna be,
5:23that's gonna have terrible time and space complexity
5:26because it's gonna be more expensive and not very efficient.
5:29So in a sort of abstracted way, same thing here.
5:32So use the right model for the task, right, based on these.
5:36And we're gonna jump into these models in a lot more depth,
5:39but I just kind of at least wanted to talk about them
5:41before we started to like, check out the documentation
5:44and actually interacting with ChatGPT.
5:47All right, so that's about it for this video.
5:50I hope this has been informative,
5:51and I'd like to thank you for viewing.
What is ChatGPT and How Does it Work Under the Hood?
0:07<v ->So we've talked about all of these models,</v>
0:09but we still haven't really...
0:15Welcome back.
0:16So far we've talked about all of these instrument.
0:20Welcome back.
0:21So far, we've talked about all of these interesting
0:24language models, right?
0:26Going from the first one, which is the most simple
0:28and basic, all the way to DaVinci 003,
0:32which is the most advanced,
0:33but still, we don't know how ChatGPT works under the hood.
0:37So let's check it out.
0:39All right, so when we're trying
0:41to understand ChatGPT,
0:43we're really thinking about probabilities, right?
0:46So we have this word cat,
0:47and let's say that we're trying to predict the next word.
0:51So what could it be?
0:53So we talked earlier about predictions, right?
0:55So we're predicting characters, like if you said cat,
1:00what is the probability of that happening, right?
1:03So this would be at the character level.
1:08And we can do this at the word level, right?
1:10So like, what would be the next word in this sentence?
1:14Let's say that it's something to do with a cat.
1:17So what's the probability that it could be
1:21cat sat or cat jumped,
1:26or cat ran or cat slept, right?
1:30So what is the probability?
1:31I'm thinking cat sat because it seems like the most,
1:35it rhymes the most,
1:36it seems like the most probable in my mind, right?
1:39So like if we're pretending
1:40to be ChatGPT, I would choose that one.
1:42So this is the character level,
1:44and then there is the word level, cat sat.
1:49And then what ChatGPT uses is something called a token,
1:54which is kind of like a word chunk, right?
1:56So the the different levels that you can have
1:58for these predictions, and this is
2:00what ChatGPT uses is this token.
2:02And so what I did is used the seaborn to visualize this
2:07and here we have that.
2:08So I'll add this code above this video so
2:11that you can actually check it out
2:12and run it yourself if you'd like.
2:14But it's not the logic,
2:15it's just basically showing you the prediction.
2:17So the probability for sat is like almost one, right?
2:22So from zero to one,
2:23so one is like 100% and zero would be not.
2:26So all of these are less than 10% or less, right?
2:32So you can clearly see that, so cat sat
2:34and that this is what it's gonna do.
2:36Sometimes the other words will not be so far apart.
2:40So you might have these two words.
2:41So you can actually choose the first, second,
2:44or third option based on the probabilities, right?
2:47So in essence, that's what's actually happening, right?
2:50Words that the language model thinks
2:52that might come next, given a certain prompt.
2:55Okay, I said prompt and that's how we talk to ChatGPT.
3:00So now that we have an understanding of these prompts
3:03and character level, word level,
3:05and token level, which is like a word chunk,
3:08that's how it's predicting.
3:09It's predicting word chunk by word chunk,
3:12or token by token, based on these prompts.
3:16And that's what we're gonna talk about next, like how
3:19to use prompts to interact with ChatGPT.
3:23And until then, I hope this has been informative,
3:25I'd like thank you for viewing.
Prompts and Completions
0:06<v ->Congratulations on making it</v>
0:08to the final video in this skill.
0:10And so far we've covered a lot about ChatGPT,
0:13the different language models that we have available to us,
0:17and which one we should use based on different tasks,
0:20thinking about time and space complexity abstracted.
0:23And so now we're talking about prompts, right?
0:25This is the exciting part.
0:26So let's check out OpenAI.
0:29Register for an account,
0:31and then we'll start prompting ChatGPT.
0:33And I'll also share a Colab notebook with you
0:36so that you can actually interface programmatically
0:39using the API, and I'll show you how to do all of this.
0:44And again, we're not gonna dive super deep into this
0:46in this video because we just unpacked language models
0:50and ChatGPT, and what does it do?
0:52And that's exactly what we're gonna do,
0:54we're gonna build off of everything
0:56that we've done in this skill
0:57and use Colab to programmatically interact
1:00with the OpenAI API,
1:03so we can interact with ChatGPT models that way.
1:06All right, enough said, let's jump right into this.
1:10Okay, so here is Google.
1:12And I've searched for OpenAI's ChatGPT.
1:17and just make sure that you're logged in here
1:19so that you can copy this Colab notebook
1:22that I'm gonna share with you.
1:23But first let's go ahead and go to ChatGPT, here it is.
1:28"Optimizing language models for dialogue."
1:30So definitely a language model.
1:32So let's click on that.
1:34And what we wanna do here
1:35is you can just click on "Try ChatGPT," but you might need,
1:39I think I might be logged in, but if not,
1:41what you can do is just click on API and then log in.
1:48And at this point, if you don't have an account,
1:51then you can just sign up.
1:52And basically you would either use your email address,
1:56or you can use an associated Google account,
1:58which is what I'm gonna do.
2:00But I'm gonna go to Log In,
2:01'cause I've already created an account.
2:03So here is "Continue with Google."
2:05And I'm gonna choose this other one here.
2:09It's my test account.
2:12All right, so now we're signed up.
2:14And sometimes you might not be able to access the site
2:16because it's super popular.
2:18So just keep in mind that it might say something like,
2:22"Network is very busy," or something like that.
2:25Try again later.
2:26So just keep in mind that's pretty normal.
2:28But if you keep trying later
2:29you should be able to get into it.
2:32All right, so here is the API.
2:34And so here's some tutorials and some examples.
2:37And we'll dive into all of this in the next skill,
2:39but for right now, I just wanted to show you
2:42that like, for example, Text Completion,
2:45if we click on this,
2:46it'll take us through this whole,
2:49basically a completion is what you get back
2:51after you give ChatGPT a prompt.
2:55So you get this completion, the output, right?
2:58And this talks all about this, the prompt design,
3:01and basically here are the three main steps.
3:04All right, so this is what we wanna look at right here.
3:07And so let's say the first one,
3:10okay, so what is it saying here?
3:12So, "You can either use instructions or examples
3:15or a combination of the two.
3:16So if you want a model to rank a list
3:18of items in alphabetical order,
3:20or to classify a paragraph by sentiment,
3:22show it that's what you want," okay?
3:24So show and tell.
3:26And then here's this other one, is provide quality data.
3:30"If you're trying to build a classifier
3:31or get a model to follow a pattern,
3:34make sure that there are enough examples.
3:36In short, the model is usually smart enough
3:38to see through basic spelling mistakes
3:41and then still give you that response,
3:42but it also might assume this is intentional,"
3:45and also give you a weird output.
3:47And finally check your settings.
3:49And we'll talk about this in the next skill
3:51when we talk about temperature and top_p settings,
3:54and these are how to control how deterministic the model is
3:58in generating this completion, right?
4:00So we'll do that in the next one,
4:02but I just wanted to show you
4:03that there's a lot of information,
4:05and the documentation's pretty good documentation.
4:09All right, so how do we get started with ChatGPT?
4:12Let's go back, click on this logo.
4:16And I'm gonna go back to OpenAI.com
4:19and then just click on this "Introducing ChatGPT," "Try."
4:23Okay, so this is great.
4:24So this is exactly what I was trying to show you earlier,
4:28and now we can see that we have,
4:31that we're not able to check out ChatGPT.
4:34So here I am, I'm gonna make sure that I'm logged in,
4:40and then I'm just going to reload this.
4:43Log in.
4:48Normally, I don't have to do this kind of jumping around.
4:52Okay cool, it worked.
4:54So, "This is a free research preview."
4:57Let me make this a little bit bigger.
4:59"Our goal is to get external feedback
5:01in order to improve our systems and make them safer."
5:04And, "While we have safeguards in place,"
5:06for example, if you start to like ask things like,
5:10how do you make meth, or something that's illegal
5:13and basically like questionable,
5:16ChatGPT is not gonna let you do that
5:17and you might get your account canceled.
5:19So even when you're testing it,
5:22keep in mind that it can lock you out
5:24if you start to ask some weird stuff.
5:26So that's what that's about.
5:30"How do we collect data?
5:31Conversations may be reviewed
5:32by our AI trainers to improve our systems."
5:35So what that means is watch out with what you type,
5:39'cause somebody else might see it.
5:40"Don't share any sensitive information."
5:43That should be pretty clear, but definitely don't do that.
5:46And, "We'd love your feedback!"
5:48So this is important.
5:48"Our system is optimized for dialogue.
5:51So let us know if a particular response
5:52was good or unhelpful."
5:54And share your feedback here in the Discord server.
5:57So this is definitely encouraged,
5:58'cause you will help make ChatGPT even better.
6:03Okay, so now we have these prompts.
6:06So here's some example prompts,
6:07and the capabilities and some limitations, right?
6:10And one of those major limitations is right here.
6:14So it has a, "Limited knowledge of the world
6:16and events after 2021."
6:19Okay, fair enough.
6:20And so it, "May occasionally produce
6:23harmful instructions or biased content."
6:26And this is also important:
6:28"May occasionally generate incorrect information."
6:30So definitely keep that in mind.
6:33So let's try some things.
6:34So,
6:38"Tell me how to write a good prompt."
6:45All right, so here's our first prompt.
6:47"Tell me how to write a good prompt."
6:50Here we go.
6:53"Be clear and specific, be concise, be interesting,
7:00and provide context."
7:02So this is basically what we just talked about.
7:03"Use active language, avoid leading questions,
7:08consider your audience."
7:10That's pretty cool.
7:13This is very detailed.
7:14So this is a lot more information
7:16than we were looking at here.
7:20A lot more information than here,
7:22when we were looking at text completion, right?
7:25Here it was like telling us like three steps,
7:28you know, basic guidelines for creating a prompt,
7:30and here's eight, and they seem to be pretty useful
7:33and very easy to understand.
7:36Okay, the accuracy of these things,
7:38again, you have to test them yourself,
7:40because it's making predictions,
7:42as we talked about earlier, right?
7:44So how do we use the API?
7:46So this is pretty cool, but I can't really use
7:48different models, we only can just chat to it.
7:53So what are the other models that we can use?
7:56Let's go to Examples.
8:00There's a lot of really cool stuff here.
8:04These are really cool examples.
8:05So definitely check this out.
8:06Python Bug Fixer, nice.
8:11Okay, so I don't want to get lost here
8:13because it's like a kid in a candy store.
8:15This is like amazing stuff.
8:17So what I'd like to do is talk about the API.
8:20So in order to do that we need to go to the API.
8:23So I'm gonna click on my profile here,
8:25and go to "View API keys," right?
8:29So I've already created my key, you can't see that,
8:31but this is what you'd have to do.
8:33Once you create this key, you wanna copy that key
8:35and you won't be able to see it after you create that key.
8:38So just keep that in mind.
8:40However, you can delete it and then start again, right?
8:43So that's one option, right?
8:45So I already have mine and I have it loaded up in a notebook
8:48and I'm gonna show you how that works.
8:51All right, so I'm gonna copy and paste code,
8:52but I'm definitely gonna leave this for you.
8:54And what we're gonna do first is we have to install these,
8:57so PrettyTable and OpenAI.
8:59These are the two things that are not part of Colab.
9:02So we're gonna install them.
9:04We're using the exclamation mark
9:06to do this in the command line, so in the shell.
9:09So this is doing this in the back end.
9:12And once this is done, then we'll start to,
9:15I'm basically doing copy and paste,
9:17because I don't wanna spend too much time typing code.
9:21The reason for that
9:21is that's what we're gonna do in the next video,
9:24we're gonna actually like work with the API a bunch.
9:27But for right now, I'm just showing you this
9:29so that you can actually work with the other models
9:31because you can't do that with ChatGPT,
9:33but you can with Colab.
9:35And in the next couple of skills
9:37we'll get really deep into this.
9:38But for right now, we're just gonna create a playground.
9:40Okay, cool, that looks like it was installed.
9:42And so let me show you what I'm gonna do here.
9:45So first what I'm gonna do is,
9:47so we already have OS, OpenAI, time, and drive.
9:52We're gonna connect to drive in PrettyTable.
9:55And so this is gonna allow us to connect to that drive.
9:58But first what I'm gonna do
10:00is I'm gonna upload this text file,
10:04which contains my key, right?
10:07And it will be deleted when this runtime is terminated.
10:10So I'm doing this because you don't wanna just
10:13put your key right here, right?
10:15And expose it to everybody.
10:16So this is the best practice.
10:18And in the next two videos we're gonna talk more about this.
10:22Using drive to host your key
10:23is not the safest way to do that.
10:25But this is one way, if you're doing a teaching screencast,
10:29like I am, and just showing this.
10:31In case you wanna share your notebook
10:33but you don't wanna share your key,
10:34you could do it like this.
10:35However, if you're in a production environment,
10:37you definitely want to use environment variables.
10:40And we'll talk more about that
10:41when we launch our AI app into production, right?
10:45And we'll use Streamlit to do all of that.
10:47But for now, I'm just gonna show you
10:49one other way to do this.
10:51And so here is my key, let's continue.
10:55I'm gonna click on Copy Path.
10:57And I'm gonna put that path right here, okay?
11:00And then I'm gonna execute this block of code,
11:04and I'm gonna give it permission.
11:08And this is where I actually have that file.
11:13And let's talk about what's happening while it's loading.
11:15So I connected to, mounted to drive,
11:18and I said, hey, this is the file that I want you to read,
11:21and that is my API key.
11:23And now it's connected to Open API,
11:27and we can use that key, right?
11:29And that key is what we have here.
11:31So this is the API key that we're using.
11:35And you can always go to settings or usage,
11:37and you can see like the usage.
11:39So you can see that this is what I'm doing here.
11:41The first time I used it,
11:43I guess just right now when I connected, it cost a penny.
11:46I think that's what happened.
11:48So let's try it again.
11:49So now what do we wanna do?
11:50So as I said before,
11:52I'm gonna give you some models to play with,
11:55and let's just add those here.
11:56So these are the four models that we discussed earlier.
11:58So what we're doing
11:59is just saving those models as a list, right?
12:03Let's go ahead and run that.
12:04And there's gonna be a lot of code here
12:06that I'm not gonna talk about.
12:07I'll just briefly go over it.
12:09But, again, we're gonna do this in the next skill.
12:13So what is all this that I just pasted here?
12:16Well, the models we know.
12:17So here this is rate limiting,
12:20we'll talk more about this later.
12:22And I'm just gonna skip to what's more important.
12:24So here it's gonna keep asking you a question,
12:27and if you type "EXIT,"
12:29then it's going to allow you to break, right?
12:32So that's how you get out of the program.
12:34And this is the rate limiting.
12:36So you don't wanna exceed that.
12:37So this is what's checking for that.
12:40And then here it's asking the user to select a model.
12:43And so we have that model,
12:44and we already talked about that up here.
12:46Let me just show you, there, it's our models.
12:51And that's what we're doing.
12:52So where's the models? Right here.
12:54You select that model
12:55and then you get your response, right?
12:57And this is the max amount of tokens that we've selected.
13:00And again, we're gonna dive much deeper
13:01into all of this good stuff in the next skill
13:03when we're actually programmatically
13:05working with ChatGPT and the OpenAI API.
13:11All right, so let's go ahead and run this.
13:16All right, so let's go ahead and type a prompt.
13:17So, "Once upon a time" is a good one
13:21because it'll want to complete it, right?
13:23So this is kind of an easy prompt.
13:26And so let's type "enter."
13:28And now it asks us what we want to use, like which model.
13:32So I'm gonna say one,
13:33so we can see the difference between one and four, right?
13:35Because we talked about this one
13:37being the slowest and most basic,
13:39and this is being the most advanced, okay?
13:42So, "Once upon a time."
13:45Here we go.
13:46"Once upon a time there was a kingdom in progress.
13:49A kingdom was growing and developing
13:51and people were prospering and enjoying life."
13:54Hey, this sounds great.
13:55So, you know, there's nothing about terminators.
13:57Perfect, I'm happy.
13:59So let's do this again.
14:01And this is what I said, "Once upon a time."
14:04We'll do the same thing,
14:06and it just keeps asking us what we want to type, right?
14:09I guess it's in that loop.
14:10So we're gonna say "DaVinci,"
14:12so that's gonna be number four,
14:14which is not telling me the number here.
14:18So one, two, three, and four.
14:19Anyway, I know that it's four, so I'm gonna just hit Enter.
14:24And here it's thinking now.
14:27And this could be because of network traffic, right?
14:30Like it could be just because of that.
14:34All right, so let's stop, start it again.
14:39Okay, so now I'm gonna say the same thing,
14:41"Once upon a time."
14:44And we're gonna use number four 'cause it crashed last time.
14:47So, "Once upon a time,
14:48there was a princess who lived in a castle.
14:50She had long, beautiful golden hair."
14:52Okay, so this is clearly much more of a story, right?
14:55The other one was kind of weird about a kingdom.
14:57This one's like, okay, once upon a time
14:59there was a princess, there's golden hair.
15:01And like this is gonna be like more advanced,
15:05clearly more advanced, but definitely play with this.
15:08And so this is what I wanted to leave you with
15:10because I didn't wanna just leave you with this, right?
15:13And not have any options to use any other models.
15:17If I reload this, we're still at a penny.
15:19So you can do a lot of stuff
15:21and it's not gonna be very expensive.
15:23But definitely do protect your keys.
15:25And then I'm gonna just say "EXIT" to end this.
15:28And there we go, we've ended our connection,
15:30and so we're not gonna be using that API once you hit EXIT.
15:36All right, I hope you enjoyed this skill, I know I did.
15:39This has been something that it's been very exciting
15:41to talk about because now it's super popular,
15:44everyone's like, "Oh, I know about ChatGPT!"
15:47And what I always like to say is like,
15:50"Do you know about the other models,
15:52and about Google Colab?"
15:53So I'm finally able to share this with you,
15:56and I'm really excited about that.
15:57So all we have left is the challenge,
15:59which I'm sure it's not gonna be a problem,
16:01but if it is, definitely just review the videos
16:03to make sure that you understand the concepts,
16:05because in the next two skills
16:07we're gonna get deeper into Colab,
16:09and then we're gonna finish things up in that third skill
16:11by building an AI-powered app using Streamlit.
16:16All right, until then, I hope this has been informative.
16:18I'd like to thank you for viewing.
Team training path
Turn this skill into assignable team training
This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.
$708
seat / year