1. Learn
  2. /
  3. Courses
  4. /
  5. Working with the OpenAI API

Exercise

Creating an AI chatbot

An online learning platform called Easy as Pi that specializes in teaching math skills has contracted you to help develop an AI tutor. You immediately see that you can build this feature on top of the OpenAI API, and start to design a simple proof-of-concept (POC) for the major stakeholders at the company. This POC will demonstrate the core functionality required to build the final feature and the power of the OpenAI's GPT models.

Example system and user messages have been provided for you, but feel free to play around with these to change the model's behavior or design a completely different chatbot!

The openai package has been pre-loaded for you.

Instructions

100 XP
  • Assign your API key to openai.api_key.
  • Create a dictionary to house the user message in a format that can be sent to the API; then append it to messages.
  • Create a Chat request to send messages to the model.
  • Extract the assistant's message, convert it to a dictionary, and append it to messages.