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
# Update the typing_extensions package
!pip install typing_extensions==4.7.1
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 theOPENAI_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 aslc
. - From the
langchain.chat_models
module, importChatOpenAI
. - From the
langchain.schema
module, importAIMessage
,HumanMessage
,SystemMessage
. - From the
langchain.prompts
module, importChatPromptTemplate
.
# 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