Workspace
Maximiliano Castelli/

Code-along | 2023-09-14 | An Introduction to GPT & Whisper with the OpenAI API in Python (copy)

0
Beta
Spinner

Code-along | 2023-09-14 | An Introduction to GPT & Whisper with the OpenAI API in Python

While chatting with the GPT AI is commonly done via the ChatGPT web interface, today we are looking at using the API. This is great for programming and automation workflows (some ideas later). As well as GPT, we'll also make use of Whisper, OpenAI's speech-to-text AI, and langchain, a programming framework for working with generative AI.

We'll cover:

  • Getting set up with an OpenAI developer account and integration with Workspace.
  • Calling the chat functionality in the OpenAI API and langchain.
  • Generating a transcript of speech from an mp3 file.
  • Summarizing a transcript.
  • Asking questions about the context of a transcript.

The solution notebook is available in the file browser as notebook-solution.ipynb. Use the Files tool in the left-hand sidebar to access this.

Before you begin

The instructions for creating an OpenAI developer account, and adding your OpenAI API key as an environment variable in Workspace are covered in getting-started.ipynb.

Task 0: Setup

We need to install the langchain package. This is currently being developed quickly, sometimes with breaking changes, so we fix the version.

The langchain depends on a recent version of typing_extensions, so we need to update that package, again fixing the version.

Instructions

Run the following code to install langchain and typing_extensions.

# Install the langchain package
!pip install langchain==0.0.286
Hidden output
# Update the typing_extensions package
!pip install typing_extensions==4.7.1
Hidden output

In order to chat with GPT, we need first need to load the openai and os packages to set the API key from the environment variables you just created.

Instructions

  • Import the os package.
  • Import the openai package.
  • Set openai.api_key to the OPENAI_API_KEY environment variable.
# Import the os package
import os

# Import the openai package
import openai

# Set openai.api_key to the OPENAI_API_KEY environment variable
os.environ["OPENAI_API_KEY"] = openai.api_key

We need to import the langchain package. It has many submodules, so to save typing later, we'll also import some specific functions from those submodules.

Instructions

  • Import the langchain package as lc.
  • From the langchain.chat_models module, import ChatOpenAI.
  • From the langchain.schema module, import AIMessage, HumanMessage, SystemMessage.
  • From the langchain.prompts module, import ChatPromptTemplate.
# Import the langchain package as lc
import langchain as lc

# From the langchain.chat_models module, import ChatOpenAI
from langchain.chat_models import ChatOpenAI

# From the langchain.schema module, import AIMessage, HumanMessage, SystemMessage
from langchain.schema import AIMessage, HumanMessage, SystemMessage

# From the langchain.prompts module, import ChatPromptTemplate
from langchain.prompts import ChatPromptTemplate

Task 1: Your first message to GPT

Let's start by sending a message to GPT and getting a response. This is the basis for all interactions with GPT.

Versions of GPT

We'll use the default model, GPT-3.5-turbo. If you want to use GPT-4. If you have access to GPT-4, you can use that instead by setting model_name="gpt-4" when you call ChatOpenAI(), though note that the price is 15 times higher. The model names are listed in the Model Overview page of the developer documentation.

Types of Message

There are three types of message, documented in the Introduction to the Chat documentation:

  • SystemMessage messages describe the behavior of the AI assistant. If you don't know what you want, try "You are a helpful assistant".
  • HumanMessage messages describe what you want the AI assistant to say. We'll cover examples of this today.
  • AIMessage messages describe previous responses in the conversation. We'll cover how to have an interactive conversation in later tasks.

Why are we using LangChain instead of just using the OpenAI API directly?

There are two benefits to using the LangChain framework.

Firstly, some of the code is a bit cleaner, particularly the code for extracting responses from the API.

Secondly, if you want to swap GPT for a different model at a later date (as you might in a corporate setting), it can be easier to do so if you use the langchain package rather than the openai package directly.

Instructions

  • Create a ChatOpenAI object. Assign to chat.
  • Create a list of messages. Assign to msgs_what_is_llm.
    • The first message is a system message with the content "You are a machine learning expert who writes for an audience of ten year olds.".
    • The second message is a human message with the content "Explain what a large language model is."
  • Pass your message to GPT. Assign to response_what_is_llm.
  • Print the response object and the contents of the response.
Hints

ChatOpenAI() creates an object used to chat with GPT. LangChain has classes to access dozens of LLM chat APIs, making it straightforward to swap out GPT for a different AI. The result can be called like a function to send a prompt to the LLM and get a response.

The messages are passed to the chat object as a Python list containing SystemMessages, HumanMessages and AIMessages.

# Create a ChatOpenAI object. Assign to chat.
chat=ChatOpenAI()

# Create a list of messages. Assign to msgs_what_is_llm.





# Pass your message to GPT. Assign to response_what_is_llm.


# Print the response


print("\n----\n")

# Print the response's content