What You Will Learn
In this lesson, you’ll learn:
- How to connect Python with an AI model
- How to send your first prompt
- How to receive an AI response
- How to understand the code
Before You Start
Make sure you have completed these lessons before starting to generate AI response using Python
- Python Installed
- VS Code Installed
- AI Project Created
- OpenAI Python SDK Installed
- OpenAI API Key Ready
Now let’s generate our first AI response.
Step 1: Open Your Project
Open the AI-Assistant project in VS Code.
Open the file:
app.py
Step 2: Import the OpenAI Library
Add the following code.
from openai import OpenAI
This imports the official OpenAI Python SDK.
Step 3: Create the Client
Replace "YOUR_API_KEY" with your actual API key.
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY"
)
Tip: In real projects, never hardcode your API key. Later in this course, you’ll learn how to use environment variables.
Step 4: Send Your First Prompt
Now add the following code.
response = client.responses.create(
model="gpt-5",
input="Say Hello to the Codermantra students."
)
This sends your prompt to the AI model.
Step 5: Print the Response
Finally, print the AI response.
print(response.output_text)
Complete Program
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY"
)
response = client.responses.create(
model="gpt-5",
input="Say Hello to the Codermantra students."
)
print(response.output_text)
Expected Output
The exact response may be different every time.
Example:
Hello, Codermantra students!
Welcome to the world of AI development. Let's build amazing AI applications together!
AI models generate responses dynamically, so your output may not exactly match this example.
Understanding the Code
Import
from openai import OpenAI
Imports the OpenAI SDK.
Client
client = OpenAI(api_key="YOUR_API_KEY")
Connects your application to the OpenAI API.
Prompt
input="Say Hello to the Codermantra students."
This is the instruction sent to the AI model.
Output
print(response.output_text)
Displays the AI-generated response.
Common Errors
Invalid API Key
Check whether your API key is correct.
Quota Exceeded
Your account may not have available API credits.
Internet Connection Error
Make sure your computer is connected to the internet.
Incorrect Model Name
Use a valid model name supported by your account.
Key Points
- Your Python program communicates with the AI model using the OpenAI SDK.
- The prompt is sent through the API.
- The AI generates the response.
- Every response can be different.
Mini Quiz
1. Which function sends the prompt to the AI?
A.
client.responses.create()
B.
print()
C.
input()
D.
response.show()
Answer: A
2. Which line prints the AI response?
A.
print(response.output_text)
B.
response.print()
C.
client.print()
D.
show(response)
Answer: A
3. What is a prompt?
A. A database
B. An instruction given to the AI
C. A Python package
D. A variable
Answer: B
4. Can the AI generate different answers for the same prompt?
A. Yes
B. No
Answer: A
5. Should you hardcode your API key in a production application?
A. Yes
B. No
Answer: B
Practice Exercise
Change the prompt and run the program again.
Try these prompts.
Explain Python variables in one sentence.
Write a motivational quote for programmers.
Suggest three AI project ideas for beginners.
Observe how the response changes based on the prompt.
Lesson Summary
Congratulations!
You have successfully written your first AI program using Python.
Your application sent a prompt to an AI model and received a real AI-generated response.
This is the foundation of every AI chatbot, coding assistant, and AI-powered application.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.
