ChatGPT and Python API

How to use ChatGPT with Python API


Photo by Rolf van Root on Unsplash

ChatGPT model is currently using gpt-3.5-turbo, which transformer-based model which priced at $0.002 per tokens. (Read more about pricing here)

ChatGPT is an AI system that lets you ask any questions and will respond in human like conversation.

Photo by Levart_Photographer on Unsplash

If you are not a fan of theory want to get to the crux of the code, link for the code is here.

ChatGPT use-case:

  1. Provide code suggestions
  2. Draft Email
  3. Create ChatBot
  4. Translate the languages
  5. Much more…!!

What is GPT — 3.5 -turbo?


How to use the python API for ChatGPT?

Screen shot from OpenAI

2. Add billing information, one get $18 promotion credit to use and generate the API keys which required to use python API.
Note: Make sure to you copy the key

3. Install OpenAI in any env you want test this, you can use Google Colab

pip install openai

4. Once installed, need use the API key generated in step two and copied.

import openai
openai.api_key = "Use your OpenAI API key"

5. One get response from ChatGPT one can use method openai.ChatCompletion.create()

6. Method takes in two input

a. model
b. message or question to the model

completion = openai.ChatCompletion.create(
messages=[{"role": "user", "content": "Tell the world about the ChatGPT API in the style of a pirate."}]


Google Colab

ChatGPT — Google — Colab — here is the link

Note : Put your OpenAI api key to get response.





Data Scientist | ML-Ops| | |

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store