ChatGPT and Python API
How to use ChatGPT with Python API
Introduction
ChatGPT an AI system that can interact with humans in normal conversational way. OpenAI released ChatGPT in November 2022, since then it has more 100 million active users, making it the fastest growing consumer application in the history.
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.
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:
- Build own application
- Provide code suggestions
- Draft Email
- Create ChatBot
- Translate the languages
- Much more…!!
What is GPT — 3.5 -turbo?
GPT-3.5-turbo is one the recent version of GPT-3.5 model and optimized for chat, whereas GPT-3.5 was optimized for text completion.
ChatGPT API
Generally GPT models consume unstructured text, which is represented to the model as a sequence of “tokens.” But with ChatGPT model use sequence of messages together with some metadata.
How to use the python API for ChatGPT?
- Create a account in the 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(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Tell the world about the ChatGPT API in the style of a pirate."}]
)
print(completion)
Google Colab
Here is the example Colab with can used to play with python ChatGPT API.
ChatGPT — Google — Colab — here is the link
Note : Put your OpenAI api key to get response.