Using ChatGPT With Python

What is ChatGPT

ChatGPT is an Artificial intelligence (AI) language model created by the OpenAI organization. It is designed to generate human-like text based on the input given.

Generally, it’s a machine learning model trained on a massive amount of text data to generate a test that resembles human language. The training data includes a wide range of topics, allowing to generate text on various subjects, including but not limited to news articles, fiction, poetry, and technical writing.

When given a prompt, this uses statistical methods to generate text that continues the prompt in a way that is like how a human might write.

ChatGPT stands for "Chat Generative Pretrained Transformer". It uses a transformer architecture, a type of neural network designed to handle sequential data, such as text.

The company OpenAI launched ChatGPT on Nov. 30, 2022.

Getting Started with ChatGPT

  • Open https://chat.openai.com/auth/login?next=/chat
  • Do Sign up if you have not registered yet.
  • Log in.
  • Left Bar - left bar has recent search history, new chat, and some settings.


     
  • Home page examples - Has some build in search examples, capabilities, and limitations.


     
  • To start a new chat, type a question in the chat box and hit enter.


     
  • Here is the answer result.

Create an App using OpenAI

  1. Open https://openai.com/api/ and click get started.


     
  2. Create your account if it does not already exists, then log in.
  3. You have some QuickStart tutorials on building sample apps and some examples to help you understand.


     
  4. There are multiple ways to build an application.

  1. Go to the right side, expand Personal, and click View API Keys.


     
  2. Create a new secret key so the python application can connect to API. Click on Create new secret key button and copy the key somewhere safe.



    Note - Do not share your API key with anybody.

Create Python App

In this article, I am going to use Visual Studio code.

  • Open the command prompt.
  • Create a new directory using mkdir command.
  • Write code. Command to get in VS code.


     
  • Now add a new file in VS code and give extension chat.py.
  • Now open a new terminal to install OpenAI.


     
  • Write command in terminal.


     
  • We are good with setup; Now let’s work on the python file code.

Add the following line of code in the python file.

  • Api_key - which we have just created.
  • Model - Language model, which is used by ChatGPT also.
  • Prompt - User input to the AI for which the AI should then generate a response.
  • Temperature - Internal parameter for language model.
  • Max_tokens - Maximum length of the answer which will generate.

Let’s run the python file now to see the result.

You can also run this in VS code file.

Implement ChatGPT with Python

Conclusion

In this article, you have learned a brief introduction to ChatGPT and how to use Chat API with Python.