ChatGPT-101

Rohit Ranjan
3 min readNov 19, 2023
GPT

This article can be referenced by AI starters or rather ChatGPT starters. It is aimed at giving a novice person, insights into the world of ChatGPT and how GPT integration can be used using Python to create awesome stuff.

Visit the OpenAI website (https://openai.com) to register and obtain the required API keys. Create account on OpenAI https://openai.com/

There are two ways to integrate ChatGPT into an application or website. First is direct API calls and Second is by using official Chat GPT libraries. The direct API call method gives more control and flexibility, while the official libraries provide more convenience with out-of-the-box functions.

Route to below links:

API: https://platform.openai.com/

ChatGPT: https://chat.openai.com/

Let’s Write some code to explore:

Install Dependencies*:

pip install openai

pip install python-docx

  • Use pip install for packages as per projects requirement (just python things!!!)
  • There are more such libraries and they can be used as per requirements of the project

I prefer securely using keys so for this code I did export variable and not hardcoded in script:

export OPENAI_API_KEY=skbKb********e1c607z*************XQE**kggUI******

To obtain API keys:

Get API keys generated here

To obtain org-ID used in the code:

Get value for openai.organization variable from here.

Get Org ID

Below code creates an interactive ChatGPT on console. Script for console ChatGPT (now supported on older openai version):

import os
import openai
openai.organization="org-iV********************ws"
openai.api_key=os.getenv("OPENAI_API_KEY")
openai.Model.list()
def generate_prompt_response(prompt):
response=openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=200,
n=1,
stop=None,
temperature=0.8
)
return response['choices'][0]['message']['content']
while True:
print ("Type 'exit' to close prompt!")
user_input=input("User: ")
if user_input == 'exit':
print ('Thank You.')
break
response=generate_prompt_response(user_input)
print("ChatGPT: ",response)

Supported on latest version of OpenAI:

import os
import openai
openai.organization="org-i*********************sws"
openai.api_key=os.getenv("OPENAI_API_KEY")
from openai import OpenAI
client = OpenAI()
def generate_prompt_response(prompt):
response=client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=200,
n=1,
stop=None,
temperature=0.8
)
return (response.choices[0].message.content)
while True:
print ("Type 'exit' to close prompt!")
user_input=input("User: ")
if user_input == 'exit':
print ('Thank You.')
break
response=generate_prompt_response(user_input)
#print ("Type 'exit' to close prompt!")
print("ChatGPT: ",response)

Below code asks for the path of audio file to be converted to text and stored in doc file. Sample Working Code (More Like Hello World of ChatGPT with Whisper)(now supported in older version of openai):

import os
import openai
import docx
from docx import Document
document = Document()
openai.organization="org-i*********************sws"
openai.api_key=os.getenv("OPENAI_API_KEY")
def transcribe_audio(audio_file_path):
with open(audio_file_path, 'rb') as audio_file:
transcription = openai.Audio.translate("whisper-1", audio_file)
document.add_heading('Audio Translate', level=1)
document.add_paragraph(transcription["text"])
document.save('demo.docx')
return transcription['text']

while True:
user_input=input("Insert Path: ")
if user_input == 'exit':
print ('Bye!!!')
break
response=transcribe_audio(user_input)
print("ChatGPT: ",response)

Supported in latest version of OpenAI:

import os
import openai
import docx
from docx import Document
document = Document()
from openai import OpenAI
client = OpenAI()
openai.organization="org-i*********************sws"
openai.api_key=os.getenv("OPENAI_API_KEY")
def transcribe_audio(audio_file_path):
audio_file = open(audio_file_path, "rb")
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
document.add_heading('Audio Translate', level=1)
document.add_paragraph(transcript)
document.save('demo.docx')
return transcript

while True:
user_input=input("Insert Path: ")
if user_input == 'exit':
print ('Bye!!!')
break
response=transcribe_audio(user_input)
print("ChatGPT: ",response)

Reference:

--

--