Python + OpenAI: Create your own custom GPT
- Alexandre Ferreira
- Feb 24, 2024
- 3 min read

Imagine being able to create your own GPT simply by connecting to the OpenAI API!
This possibility is more within reach than you might imagine. In this article, I will guide you through the process of connecting, allowing you to have your own ChatGPT.
In my last article, I addressed some of the advantages of having your personal assistant, and now, I would like to delve even deeper into this concept.
Technology used
PYTHON
STREAMLIT
API OPENAI
Motivation
For most of my needs, GPT-3.5 performs satisfactorily, but occasionally it demands more refined responses. Although $20 does not represent an exorbitant cost, I realized that I was not taking full advantage of the available potential.
Given my interest in studying Python, I decided to combine this knowledge with the intention of optimizing costs. Now, I pay only for the queries I make, especially when seeking solutions for more complex tasks. This approach provides significant savings, allowing for a more targeted investment in specific needs.
My YouTube Tutorial (in Portuguese)
Here is the video with instructions, and below is the written tutorial as well to assist you in your first code!
Step 01: OpenAI Account
The first step is to access the website from OpenAI and log in:
You can log in using your Google email or any other preferred method.
When signing up, you should click on the API field.
Step 02: Payment, don't worry!
You have the option to add credits for testing purposes, with the minimum amount set at $5.00.
This amount is more than enough to explore extensively. The only caveat is that unless you enter an infinite loop and forget, you will hardly exhaust this value quickly. Given the time that queries take, it is impractical to request an excessive amount in a short period. So, don't worry, there is no need to enter credit card information at this time.
To ensure even more control over spending, even for those who prefer to avoid any risk, it is possible to establish limits. Simply access Settings -> Limits, and set a monthly limit that you consider fair and comfortable. This measure provides safe and personalized management of the financial resources dedicated to queries
Step 03: Create your API Key
Go to the "API Keys" tab and proceed to create your API Key.
Important: Make a copy of the generated Key and store it securely. Later, it will not be possible to retrieve this information, requiring the generation of a new key in case of loss or need for replacement. Caution in preserving this code is crucial to ensure continuous and secure access to your API key.
Step 04: Let's get to work
This first program will be extremely simple, just to connect and test functionalities.
Importing Libraries
from openai import OpenAI
import streamlit as st
import os
from dotenv import load_dotenv
Loading API Key
# Load ENV
load_dotenv()
# Create client
client = OpenAI(
# This is the default and can be omitted
api_key=os.getenv("OPENAI_API_KEY"),
)
Creating a field and printing the response
st.title("Meu primeiro App")
pergunta = st.text_input("Escreva aqui sua mensagem: ")
if pergunta:
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=[{"role":"user", "content":pergunta}]
)
st.subheader("RESPOSTA")
st.write(response.choices[0].message.content)
Outcome!!!
Running the program:
streamlit run program_name.py
Yes!! Just like that, you connected and created your custom GPT.
Refine:
Gradually enhance your code, identifying precisely the necessary functionalities. Streamlit is remarkably flexible and offers a wide range of possibilities for customization.
When optimizing the code, focus on the specific features that meet the requirements of your project. Explore Streamlit's versatile capabilities, adapting them as needed to create an efficient and intuitive user interface. Remember that Streamlit's flexibility allows for an incremental approach, enabling progressive adjustments as your project evolves.
Conclusion:
In this technical guide, we meticulously outlined the process of connecting to the OpenAI API using Python and Streamlit to create a custom ChatGPT. We started by highlighting the accessibility of this implementation, then moved on to the practical motivation of saving resources by studying Python and making payments only for more complex queries.
By addressing credit acquisition, the importance of preserving the API key, and financial management through monthly limits, we provided a solid foundation for ongoing exploration. We concluded by encouraging you to progressively enhance the code, identifying essential functionalities, and leveraging Streamlit's flexibility to create a customized and effective solution. This guide, therefore, empowers enthusiasts to efficiently integrate into the OpenAI universe.
Comments