Power icon
Check our latest Product Hunt launch: 404 Error Hound!
Right arrow
404 Error Hound - Hunt down & fix website errors with AI power | Product Hunt
AI

A beginner’s guide to ChatGPT API using Python

August 2, 2023
4 min read
A beginner’s guide to ChatGPT API using Python

Introduction

ChatGPT, developed by OpenAI, has taken the world by storm with its extraordinary ability to comprehend and generate human-like text. While you may have already explored ChatGPT's user interface, we are here to introduce you to a whole new realm of possibilities by harnessing the power of Python alongside ChatGPT's API. In this blog post, we will delve into the fundamentals of using ChatGPT's API in conjunction with Python. By merging Python's versatile programming capabilities with ChatGPT's natural language understanding, you will unlock the potential to create advanced conversational agents, automate tasks, generate personalized content, and much more.

Setup

Account and Keys

First, you will need to create an OpenAI account and get the necessary API key. Log in to your account, click the avatar icon, and click on “View API keys”.

Click on “Create new secret key” and add a name. Copy the API key and keep it in a safe place.

This is the key you will use to authenticate and access the OpenAI API to use ChatGPT.

Python environment

Assuming you have successfully added Python to your system's PATH, it is suggested that you create a virtual environment by using the following command in your working directory: 'python -m venv env'.

Activate the environment by using ‘env\Scripts\activate’ in the directory you have created the virtual environment and use ‘pip install openai’ in the CLI to install the OpenAI library.

API calls

This is what the Python code for an OpenAI API call to the gpt-3.5-turbo (ChatGPT) model looks like:

There are a few things to note here.

  1. Role: This is the character of the model. In the above example, we have told the chatbot to be a “comedian”. We could change this and tell the chatbot to be an “action film director” or whatever we like. Based on the role, the chatbot gives different responses. See the results below for the same prompt but different roles. It is obvious that the first result is more joyful than the second.
  1. Content: The “content” is the message that we send to the chatbot, similar to when we type a message in the ChatGPT UI.
  2. Parameters: These are the parameters sent in the API call to adjust the response of the chatbot according to our preferences. Some details:
  3. ~Temperature (0-2): This parameter adjusts the creativity of the model. Higher values produce more creative responses, while lower values produce more stable and solid answers.
  4. ~Max tokens (1-4097): This parameter adjusts the length of the produced response.
  5. ~Top_p (0-1): This parameter adjusts the response diversity.
  6. ~Frequency penalty (0-2): This parameter adjusts the likelihood of the model repeating the same line.
  7. ~Presence penalty (0-2): This parameter adjusts the likelihood of the model talking about new topics.

Keeping a conversation

The answers of the chatbot can be appended to the “messages” list and can be used to send follow-up input or questions to keep the conversation going. An example can be seen below:

This code can be included in a loop to create a whole conversation. Be careful, every time an API call is made, the whole conversation is resent to the API, so the tokens needed are increasing, and therefore, so does the cost.

Conclusion

Using ChatGPT programmatically offers limitless potential. By combining Python's capabilities with ChatGPT's natural language understanding, you can build advanced conversational agents, automate tasks, create customized content, and foster AI-driven creativity.

In addition, this article has provided you with the foundation to start using ChatGPT in your Python applications. You can now explore and push the boundaries of what's possible and tailor the code to suit your specific requirements.

Similar posts

Read more posts from the same author!

Start your 30-day free trial

Never miss a metric that matters.
No credit card required
Cancel anytime