Instruction 01

ChatGPT

ChatGPT is an AI language-learning model based on GPT models developed by OpenAI. The full theory and math behind creating GPT models is beyond the scope of this course, but here’s a simple explanation of how they work:

  • GPT models accept a text prompt as input, specifying a question to answer or a task to perform.

  • They optionally accept additional context, which is information related to the prompt describing how the model should act.

  • They use millions or even billions of neural networking nodes, which are mathematical functions, and weights, which are probabilities, to determine a reasonable response, or so-called chat completion, for the given prompt and context.

  • The resulting responses are non-deterministic. Meaning that even if you provide the same inputs, the response will be different nearly every time.

Wow, that’s a lot of jargon! Here’s an example to make it clearer:

A curious user wants to know more about the world and asks ChatGPT for help. He first tells ChatGPT, “Act as a scientist, yet keep answers short.” He then asks, “What color is the sky?” ChatGPT replies, “The sky is blue.”

Wow, that’s amazing, right…!? Okay, I can tell you’re not impressed. Let’s break each step down so you can understand how awesome this is:

  1. The context is: “Act as as scientist yet keep answers short.” ChatGPT uses this to help determine how to respond. You could’ve told it, “Act as a computer programmer and explain everything,” and you would’ve received a different response.

  2. The prompt is: “What color is the sky?” ChatGPT uses this as the starting point for a chat, wherein its responsibility is to complete the next part of the chat, given what’s most likely to come after this.

  3. The chat completion is: “The color of the sky is blue.”

Even if you ask again with the same prompt and context, you’re likely to get a slightly different answer because the GPT model is non-deterministic.

Unlike previous generations of “chatbots,” ChatGPT is not using if-else statements under the hood. Rather, it uses a complex set of nodes to determine what response is probable as a completion to the inputs. This means that it can not only answer questions about the sky, but it can answer questions about computer programming, law, medicine, accounting and more! It can even “learn” new concepts by training and fine-tuning the model with new data sets.

Limitations of ChatGPT

While ChatGPT seems magical and almost intelligent, it does have some limitations. To start, it isn’t connected to the internet. This means it doesn’t have access to breaking news, weather, etc. Instead, it relies on training data that can be out of date. Hence, if you ask it about Swift, it may not know about the latest APIs.

Providing more context and longer prompts is a great way to get better responses, but it’s a double-edged sword. ChatGPT can misinterpret prompts, context, and training data, and this can lead to wildly incorrect responses. It can even be influenced by its own responses, which influence the overall chat context, leading to compounding of inaccuracies based on false pretexts.

You can only provide a limited amount of context. For example, you can’t dump the entirety of Wikipedia into its context. However, the latest models do have a larger context capacity, and OpenAI is constantly increasing the context limits.

Collectively, these limitations lead to so-called hallucinations, wherein ChatGPT generates a reasonable sounding response, but it’s entirely made-up and inaccurate. Fortunately, there are several ways you can minimize hallucinations, which you’ll learn about later.

Using ChatGPT

OpenAI makes it easy to interact with ChatGPT through RESTful APIs. You’ll specifically learn about the chat completions API in this course, and they also provide several other similar APIs.

Before you can make RESTful calls to the API, however, you need to register for OpenAI and generate an OpenAI API Key. Unfortunately, OpenAI does not offer any sort of free or developer tier, so you’ll need to purchase at least $5 worth of tokens.

What are tokens? In layman’s terms, you can think of a token as “a piece of a word,” and one token equals roughly 0.75 words in English, though other languages may have more or less words per token. GPT models tokenize text, meaning both inputs and responses are transformed into a token form that’s usable by the node network. If you’re curious about how this is done exactly, see this page for details.

Go to this page to register for or log in to ChatGPT. Then, set up a payment method, purchase tokens and generate an OpenAI API project key. Your key will be 54 characters long and start with sk-proj-.... Your key will only be displayed once, so make sure you copy and save it while it’s shown.

OpenAI also suggests you declare your API key in an environment variable called OPEN_API_KEY, so you can use it in Terminal commands. If you use the default Terminal on Mac, you should add this to your .zshrc file, so it’s available every time you open a new Terminal.

Here’s how to do this:

  1. Open Terminal and enter this command to ensure your .zshrc file exists:
touch ~/.zshrc
  1. Enter this command to open .zshrc in Xcode:
open -a Xcode ~/.zshrc
  1. Add this line at the end of the file:
export OPENAI_API_KEY={PASTE YOUR KEY HERE}

Make sure to replace {PASTE YOUR KEY HERE} with your actual OpenAI API project key.

  1. Save and close .zshrc, and then enter this command to reload the file in Terminal:
source ~/.zshrc

With this done, you’re now ready to make your very first request to ChatGPT! Copy and paste this command in Terminal and press enter:

curl https://api.openai.com/v1/chat/completions \
 -H "Authorization: Bearer $OPENAI_API_KEY" \
 -H "Content-Type: application/json" \
 -d '{
 "model": "gpt-3.5-turbo",
 "messages": [{"role": "user", "content": "What is the OpenAI mission?"}]
 }'

Fantastic! Now go over what this means:

  • curl allows you to make networking requests in Terminal.
  • https://api.openai.com/v1/chat/completions is the URL for the chat’s completion API.
  • -H allows you to specify an HTTP request header.
  • You pass in your API key via Bearer Token Authorization, which is declared per the exact format shown.
  • You tell the endpoint that you’ll be passing application/json data in the body.
  • -d allows you to specify the request body, and you pass in a JSON dictionary.
  • There are two required fields in the dictionary:  model and messages.
  • model lets you specify which GPT model to use, more on this below.
  • messages is an array of dictionaries, wherein each dictionary must contain role and content fields.
  • role must be user, system or assistant. Again, more on this below.
  • content may be any string.

You should’ve received a response like this:

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "created": 1722169866,
  "model": "gpt-3.5-turbo-0125",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The mission of OpenAI is to ensure that artificial
          general intelligence (AGI) benefits all of humanity. They
          aim to build safe and beneficial AGI to advance scientific
          discovery, promote economic fairness, and mitigate existential
          risks. Additionally, they are committed to conducting and
          publishing cutting-edge research in the field of artificial
          intelligence."
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 14,
    "completion_tokens": 63,
    "total_tokens": 77
  },
  "system_fingerprint": null
}

Here are the important parts of the response:

  • id is a unique identifier for the response.
  • created is the date the response was created.
  • model is the model that was used to generate the response.
  • choices is an array of messages representing the responses that were generated.
See forum comments
Download course materials from Github
Previous: Introduction Next: Instruction 02