In the above code:
Lines 1–2: We import the openai
library to interact with the OpenAI API. We also import the os
module to access environment variables, which we’ll use to store the API key securely.
Lines 5–6: We set the OPENAI_API_KEY
environment variable to your actual OpenAI API key. We retrieve the API key from the environment variable using os.getenv("OPENAI_API_KEY")
and assign it to openai.api_key
so that the openai
library can authenticate API requests.
To run the code and observe the output, you need to replace "ADD YOUR API KEY HERE"
with your actual API key.
Line 9: We create a variable prompt
that contains the text we want the AI model to process—in this case, asking for a simple explanation of Educative.
Lines 12–18: We call openai.ChatCompletion.create()
to interact with the Chat Completions API and generate a response.
model="gpt-4"
: Specifies that we want to use the GPT-4 model for generating the response.
messages
: A list of message objects that represent the conversation history. The {"role": "user", "content": prompt}
represents the user’s input message to the assistant.
max_tokens=150
: Limits the response to a maximum of 150 tokens to control the length of the output.
Note: The number of words in 150 tokens depends on the language and specific content, but as a general rule of thumb, on average, 1 token is roughly equivalent to ¾ of a word or about 1 word in some cases, assuming typical English text. Therefore, using this approximation, we can say that 150 tokens is roughly equivalent to 110-150 words.
What are the limitations of GPT models?
Alright, let’s talk about the real deal with GPT models. They’re impressive—no doubt about it. They can generate human-like text, answer questions, and even help you write code. But, just like any tool, they come with their own set of quirks and limitations.
Factual accuracy? Not always. GPT is great at sounding right, but it’s not always being right. Sometimes, it confidently gives you information that’s flat-out wrong. Why? Well, GPT doesn’t “know” things like we do. It generates text based on patterns from data it’s seen, but it doesn’t fact-check. It’s not connected to reality—it’s just predicting the next word based on what it’s learned. So, if you want reliable info, you still need a human in the loop.
Another issue is bias. GPT models learn from the internet—all of it. And the internet, as we know, is filled with biases: cultural, political, gender—you name it. GPT can unintentionally pick up and reflect those biases. If you’re using it for sensitive tasks, this can be a problem. Developers can try to reduce these biases through fine-tuning and careful dataset selection, but it’s not perfect.
Also, Let’s not forget about size. GPT-3 has 175 billion parameters! That’s difficult to run. While this makes the model incredibly powerful, it also makes it slow and computationally expensive. For real-time applications, like chatbots or personal assistants, this can be a headache. You might experience delays (latency) when waiting for a response, especially when using bigger models like GPT-4.
By summarizing it all in a table, we can get the big picture