Exploring the process of integrating the CHATGPT API with Python

Tejas A
3 min readApr 10, 2024

--

If you’re interested in learning how to seamlessly integrate ChatGPT with your Python code, you’ve come to the right resource.

Hey, folks today we are going to discuss integrating chat-gpt API (by openai) into your Python code.

What is chat-gpt?

Image Source

I believe it’s safe to assume that most individuals within the technology sphere are familiar with ChatGPT and likely incorporate it into their daily routines. We’re all aware of the transformative impact ChatGPT has had on our lives. This blog aims to elucidate the process of seamlessly integrating the ChatGPT API with Python code, thereby enabling users to replicate the same functionalities typically accessed through the user interface.

We would like you to know that you have the following before proceeding further -

  1. Openai Account with credits added to your account.
  2. Python is installed on your PC with any editor of your choice.

Generating Openai key

To create the key you need to login to platform.openai.com and navigate to the API-keys section.

Once you land on the API-keys section, click on “Create new Secret key” and you see a screen like something shown below.

API key section
Creating a secret key

While generating the secret you have 3 options ALL, Restricted, and Read-Only.

  • ALL- Full permissions are set for the secret key. This is the default setting.
  • Restricted- Enables the user to set None, Read, and Write permissions for each endpoint. For example, you create an API key that specifically does not have permission to Read or Write to the /v1/assistants endpoint.
Permissions in API
  • Read Only-Read permissions are set for all endpoints.

Once you select the necessary permissions click on “Create Secret key”.Make sure you save the key as it's not accessible again.

Connecting to Openai using API-key and Python.

Installing openai library — run the below CMD to install the library.

pip install openai==1.16.2

Once the library is installed initialize the Openai key.


from openai import OpenAI
api_key = "your api key"
# the gpt model you want to use
llm_model = "gpt-3.5-turbo"

Prepare the message format. The API accepts inputs in JSON format.

A sample message input format -

messages =[{"role":"user","content":"Your are an AI assistent"}]

Now let's use all the information to build a code that helps to chat with the model.

from openai import OpenAI
api_key = "your api key"
# the gpt model you want to use
llm_model = "gpt-3.5-turbo"
client = OpenAI(
# This is the default and can be omitted
api_key=api_key,
)

messages =[{"role":"user","content":"Your are an AI assistent"}]
while True:
message = input("User : ")
if message:
messages.append(
{"role": "user", "content": message},
)


chat_completion = client.chat.completions.create(
messages=messages
,
model=llm_model,
)
response= chat_completion.choices[0].message.content
messages.append({"role":"system","content":response})# save the reply message by api for chat context
print(response)

Now let’s see the API in action-

Basic Chat

Congratulations! You’ve reached the conclusion of the blog. By now, I trust you’ve gained a basic understanding of how to utilize Python to interface with the ChatGPT API.

Hope you all enjoyed this blog if you want more of this please let me know in the comment section.

Stay updated with regular content by subscribing.

--

--

Tejas A
Tejas A

Written by Tejas A

Health Care Data Scientist at a Saigeware.

No responses yet