Let’s Start Conversing with AI

Let’s Start Conversing with AI

Last edited time
Last updated January 26, 2024
Published
Jan 26, 2024 12:54 PM
Author
Fenix Wang
Tags
AI
ChatGPT
OpenAI
Welcome to the world of AI! I imagine the ChatGPT craze has drawn you in — and since you’re here with that curiosity, I’ll deliver what you seek! We’ll start with ChatGPT fundamentals and then explore OpenAI APIs for building your AI applications over the next few classes. Under the hood, they leverage the same GPT-3.5 language model as ChatGPT.
We’ll use public data and code to demonstrate best practices for leveraging large language models. So first, let’s set the stage — there is no need for expensive hardware to run the examples. Whether you are a product manager, an enthusiast, or not an engineer, online notebooks from HuggingFace, Kaggle, and Google Colab allow you to execute everything from any browser.
What incredible feats can these APIs achieve? How is Artificial General Intelligence defined? And how do models like GPT-3.5 differ from past deep learning NLP systems? We have much to uncover!
But first, some prep — signing up for access and setting up environments.

Obtaining an OpenAI API Key

To follow along, you’ll need an account to access OpenAI APIs — here’s where you can register.
notion image
Once signed up, expand the account menu at the top-right, choose API Keys, and create a new secret.
notion image
notion image
Be sure to store this securely for supplying in API requests later.
The free tier provides $5 in usage credits for this course and initial experimentation. For production workloads, consider upgrading to a paid plan.

Setting up a Local Jupyter Lab Environment

With the API key, let’s set up a development environment. We’ll use Python throughout this course to demonstrate applying AI. Engineers can install the latest Python from python.org. Python 3.7 will reach end-of-life in July 2023, so 3.10 is recommended nowadays.
To isolate Python versions, Conda environments are great.
We’ll create one for Python 3.10, install JupyterLab plus OpenAI and other libraries we’ll need:
conda create --name chap1_talk_to_ai python=3.10 conda activate chap1_talk_to_ai conda install -c conda-forge openai conda install -c conda-forge jupyterlab conda install -c conda-forge ipywidgets
Get familiar with these commands — we’ll use Conda/pip further to add more packages.
After installing JupyterLab, set the API key in environment variables and launch it. The interactive notebooks allow running code to experience OpenAI APIs first-hand.
export OPENAI_API_KEY=PUT_YOUR_API_KEY_HERE jupyter-lab .
Feel free to start a new Python 3 notebook to call APIs.
notion image

Executing Examples via HuggingFace

Assuming you have a HuggingFace account, under Spaces:
notion image
Click “Create New Space”
notion image
Fill out:
Name: chap1-talk-to-ai
License: MIT
SDK: Docker (blank template)
Hardware: Free CPU tier
Access: Public or Private
notion image
Once created, clone the space locally. HuggingFace provides a starter code to initialize the Docker project. Finally, commit and push changes:
Clone space
# HTTPS git clone <https://huggingface.co/spaces/fenixwang/chap1-talk-to-ai> # SSH git clone git@hf.co:spaces/fenixwang/chap1-talk-to-ai
Create a Dockerfile locally and paste the template
# read the doc: <https://huggingface.co/docs/hub/spaces-sdks-docker> # you will also find guides on how best to write your Dockerfile FROM python:3.10 WORKDIR /code COPY ./requirements.txt /code/requirements.txt RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt COPY . . CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
Commit and push to sync with HuggingFace
git add Dockerfile git commit -m "Add application file" git push

Using Kaggle Notebooks

With a Kaggle account: Click “+ Create” and launch a Jupyter notebook
notion image
Install libraries in the first code block:
!pip install openai
Then call APIs in the second block and run all:
notion image
We can see the response from OpenAI.

Using Jupyter Notebooks with Colab

First, log into Colab using your Google account. Then, on the homepage, click the “New Notebook” button to create a new notebook:
notion image
Like what we covered earlier in the Kaggle section, I created two Code Blocks and provided the same code.
notion image
Click “Runtime → Run all” in the top menu bar or use the keyboard shortcut Ctrl + F9 to run all the Code Blocks.
We get the same results as before. You can view the complete example here.

Coding Time

Next comes the exciting coding part! The overall project structure is as follows:
notion image
In the project, I created a requirements.txt file to manage the Python packages used:
fastapi==0.104.0 uvicorn==0.23.2 openai==0.28.1
Then, under the app path, I create a code file main.py and paste it into the following code, remembering to replace your API key instead of <PUT_YOUR_OPENAI_API_KEY_HERE>. Of course, I encourage you to try it out yourself! 😁
from fastapi import FastAPI import os import openai import json app = FastAPI() # notice: replace <PUT_YOUR_OPENAI_API_KEY_HERE> to your API Key os.environ["OPENAI_API_KEY"] = '<PUT_YOUR_OPENAI_API_KEY_HERE>' openai.api_key = os.environ.get("OPENAI_API_KEY") COMPLETION_MODEL = "text-davinci-003" prompt = """ Consideration product: LEGO Star Wars 2023 Advent Calendar 75366 Christmas Holiday Countdown Gift Idea with 9 Star Wars Characters and 15 Mini Building Toys, Discover New Experiences and Daily Collectible Surprises 1. Refine the product title, keeping the text within 15 words.. 2. Provide 8 selling points for this product in Amazon. 3. Describe this product's information in Amazon.. Output the result in json format with three properties: title, selling_points and information. """ def get_response(prompt): completions = openai.Completion.create( engine=COMPLETION_MODEL, prompt=prompt, max_tokens=512, n=1, stop=None, temperature=0.0, ) message = completions.choices[0].text return message @app.get("/") def read_root(): return {"message": json.loads(get_response(prompt))}
Now, you can start running the code locally. Open Terminal and run the following command to install project dependencies:
# install dependencies pip install -r requirements.txt
Next, run this command to launch a web service listening on the root path and returning OpenAI API results:
uvicorn app.main:app --host 0.0.0.0 --port 7860
Finally, open your browser and enter http://localhost:7860. You will see the results returned below:
{ "message": { "title": "LEGO Star Wars Advent Calendar 75366", "selling_points": [ "9 Star Wars characters and 15 mini building toys", "Discover new experiences and daily collectible surprises", "Perfect Christmas holiday countdown gift idea", "Buildable calendar with 24 secret compartments", "Includes a foldout playmat for creative play", "Features iconic Star Wars characters and vehicles", "A great way to get into the festive spirit", "Ideal for ages 6 and up" ], "information": "This LEGO Star Wars Advent Calendar 75366 is the perfect Christmas holiday countdown gift idea. It includes 9 Star Wars characters and 15 mini building toys, as well as 24 secret compartments to discover new experiences and daily collectible surprises. The set also includes a foldout playmat for creative play, featuring iconic Star Wars characters and vehicles. It's a great way to get into the festive spirit and is ideal for ages 6 and up." } }
The full project code can be viewed on my HuggingFace Space page
At this point, you must have many questions. In this example, I stated requirements, and OpenAI could return the information I needed according to my prompts. Most incredibly, it returned well-structured JSON data based on my keyword cues. Let’s reveal the answers one by one.
First, this product was randomly selected from the Toys & Games category on Amazon
notion image
In the code above, I called OpenAI’s Completion interface and then posed three asks:
  1. I wanted to shorten the original product title length to 15 words.
  1. Provide eight selling points for this product.
  1. Finally, supply summary information about the product.
Lastly, I also told OpenAI that I hoped the returned format would be JSON, with the above three requirements denoted by the keys title, selling_points, and information.
OpenAI fully understood my needs and returned a JSON string meeting my criteria. In the process, it did several very different tasks:
The first was renaming the product title as requested.
The second was comprehending the semantics and generating pertinent responses, returning the number of selling points I stipulated.
Third was providing a concise product description.
If you find this unbelievable, let’s look at another example.
prompt = """ One of the most useful benefits to having an Apple Watch is its ability to ping your iPhone. Phones are notoriously easy to misplace, forcing you to waste time searching for them when you're just about to head out the door. Fortunately, the Apple Watch has a button you can press that causes your iPhone to make a noise so you can find it. This Apple Watch feature is relatively well-known. What's less well-known is that you can also make your lost iPhone flash its light to help you find it more quickly. This feature was originally intended to help people who are deaf or hard of hearing locate their phones, but there's no reason everyone else can't benefit from it as well. Please use typical NER to tag entities in the above paragraph and return them as a JSON object. """ print(get_resp(prompt))
{ "entities": [ { "text": "Apple Watch", "type": "Product" }, { "text": "iPhone", "type": "Product" }, { "text": "Apple Watch", "type": "Product" }, { "text": "iPhone", "type": "Product" }, { "text": "deaf or hard of hearing", "type": "Group" } ] }
I provided it a paragraph of tech news text, then used AI powered named entity recognition (NER) to extract and annotate entities based on my criteria. As you can see, the returned results fully meet my asks — drawing out and labeling entities from the news snippet.
In the two examples, a lot goes under the hood, including text generation, reasoning, NER, and more. In traditional machine learning, these capabilities require a standalone model. Creating a single model to deliver these functionalities implies implementing a sophisticated multi-task learning architecture — an enormously complex engineering feat likely out of reach for any team smaller than dozens of people.
OpenAI has not revealed parameter counts for GPT-3.5 or GPT-4 models. Available third-party estimates indicate GPT-3.5 has 175 billion parameters, while GPT-4 has 1 trillion parameters. GPT-3.5 has a context window of 4096 tokens versus 8192 tokens for GPT-4. Greater parameters can be understood as more knowledge capacity and diversity of responses. Larger context windows support longer continuity for conversational scenarios needing sustained context.

Recap

By now, I believe you have gained some intuitive understanding of OpenAI’s capabilities. Your account and API keys should also be ready. Whether running locally or using online platforms like Colab and Kaggle, you have tried executing or modifying source code and seeing the outputs.
notion image
OpenAI’s GPT-3.5 large language model empowers you to tackle any natural language processing task with this single model without needing to train specialized models or fine-tune them for particular tasks. In the past, we had to train individual models, sometimes with custom tuning for specific applications. But that is no longer necessary in the era of large language models. This greatly lowers the barrier to leveraging AI to solve problems. No matter how convenient NLP tools or frameworks were before, building something that worked well still required teams of subject matter experts. There were also the challenges of effectively combining models and preparing volumes of training data.
With large language models, we only need to learn to “ask questions” to develop “smart” products. That is the purpose of this book.
If you want to learn more, follow my book “ChatGPT’s Guide to AI Mastery.”