ChatGPT Python: How to use ChatGPT in Python Project

ChatGPT is no doubt a new buzz in the tech world, which is not going to end in the near future. Day by day, its craze is increasing with the addition of more power and features.

Microsoft has recently introduced 365 Copilot, which is a combo of Openai and Windows tools for better productivity in tasks. You can now use AI power in MS Word, Powerpoint, Excel etc.

People are trying lots of things with this Openai flagship product. One such thing is importing chatGPT services in a Python project. In this post, we will see how to connect chatGPT with Python and use it in your project.

Working with ChatGPT and Python

Firstly, I am sure you have Python installed in your system. If your system doesn’t have, you need to install Python. Here is the post in which we wrote about Python basics, you can learn how to install Python and PIP.

To use ChatGPT in Python, you will need to obtain an API key and install the OpenAI API client. The API key can be obtained from openai website. Here is the link.

Let’s create a Python project using ChatGPT API

mkdir chatgpt-project
cd chatgpt-project

Open your terminal and type the code above. In the above two lines of code, we have created a new folder named ‘chatgpt-project’ and moved to that directory for working in it. You can choose the name of your choice.

Now we will open this directory folder in VSCode to work in a better way. I hope you have VSCode in your system. If you don’t, you can continue in the terminal. Type the below code in the terminal to open the current directory in VSCode:

code .

The above command ‘code .’ will open VSCode with your folder in it. Now we need to install the OpenAI API client and all of its dependencies in order to use it in our project. Run the below code in the VSCode new terminal:

pip install openai

Now create a new Python file named ‘’ in the above ‘chatgpt-project ‘ folder by clicking the ‘+’ sign in VSCode. You can choose the Python filename of your choice.

Inside the file, type the below code:

import openai

#OpenAI API key 
openai.api_key = "API_KEY"

#Model and prompt setting
model = "text-davinci-003"
prompt = "What is Python?"

#Getting response
completion = openai.Completion.create(

response = completion.choices[0].text

In the above code, we first imported the openai module. Then we used the provided API key. Further we have decided the model and our question in the form of prompt. Here we have asked the chatbot ‘What is Python?’. You can change the question according to your choice. Here we have used the ‘text-davinci-003‘ model which is the newer and more capable model, designed specifically for instruction-following tasks.

Now, its the time to run this Python file in the VSCode terminal. Make sure you are still in the same directory of ‘chatgpt-project’. Run the below code to run the Python file:



Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. 

Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together.