Introduction
In this blog post, we are going to discuss how to build a simple chatbot using Python and OpenAI’s GPT-4.1-nano model. We will go through each line of the Python code, explaining what it does, and show the expected output. By the end of this blog, you’ll understand the basics of how to use the OpenAI API and how to create a chatbot using Python.
from openai import OpenAI
OPENAI_API_KEY = "your api key here"
client = OpenAI(api_key=OPENAI_API_KEY)
messages = [{"role": "system", "content": "You are a helpful assistant."}]
while True:
user_input = input('You:')
if user_input.lower() in ['quit','exit']:
break
messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="gpt-4.1-nano",
messages=messages
)
reply = response.choices[0].message.content
print('Bot :',reply)
messages.append({"role": "assistant", "content": reply})
Explanation of Each Line of Code
from openai import OpenAI
This line of code imports the OpenAI library, which is needed to interact with the OpenAI API.
OPENAI_API_KEY = "your api key here"
This line defines your OpenAI API key. You need to replace “your api key here” with your actual OpenAI API key.
client = OpenAI(api_key=OPENAI_API_KEY)
This line creates a new OpenAI client using your API key.
messages = [{"role": "system", "content": "You are a helpful assistant."}]
This line initializes the ‘messages’ variable as a list containing a single message from the ‘system’ role that says “You are a helpful assistant”.
while True:
user_input = input('You:')
if user_input.lower() in ['quit','exit']:
break
This code creates an infinite loop that asks for user input and breaks if the user types ‘quit’ or ‘exit’.
messages.append({"role": "user", "content": user_input})
Here, the user’s input is added to the ‘messages’ list.
response = client.chat.completions.create(
model="gpt-4.1-nano",
messages=messages
)
This code sends the list of messages to the OpenAI API, which responds based on the conversation so far. It uses the ‘gpt-4.1-nano’ model.
reply = response.choices[0].message.content
print('Bot :',reply)
messages.append({"role": "assistant", "content": reply})
This code fetches the bot’s response from the API’s reply, prints it, and adds it to the ‘messages’ list.
Expected Output
The output will be an interactive chat where the user can input text and the bot will respond based on the user’s input and the conversation history. The user can exit the chat by typing ‘quit’ or ‘exit’.
Conclusion
In this blog post, we have explained how to use Python and OpenAI API to build a simple chatbot. We hope you found this guide useful and now have a better understanding of how to utilize OpenAI’s powerful language model in your Python projects.
FAQs
1. Where do I get the OpenAI API key?
You can get it from your OpenAI account. Go to the OpenAI website, sign in, and there you’ll be able to generate your API keys.
2. What is the ‘gpt-4.1-nano’ model?
‘gpt-4.1-nano’ is a language model developed by OpenAI. It’s a smaller and faster version of the GPT-4 model.
3. Can I use other models instead of ‘gpt-4.1-nano’?
Yes, you can use any model provided by OpenAI. Just replace ‘gpt-4.1-nano’ with the model you want to use.
4. How can I customize the bot’s responses?
You can customize the bot’s responses by changing the ‘messages’ list and the input it receives.
5. How is the ‘system’ role different from the ‘assistant’ role?
The ‘system’ role sets up the assistant’s behavior, while the ‘assistant’ role refers to the bot itself. The ‘system’ role is generally used to give the assistant instructions or to set the tone of the conversation.