Introduction to OpenAI SDK

To install the OpenAI SDK run the following command:

Bash

pip install openai
    

Install the python-dotenv module to use the environment file in the code.

Bash

pip install python-dotenv
    

What is Zero-Shot Prompting?

Zero-shot prompting means:

Prompt Types

  1. Zero-shot – Only instructions are provided.
  2. One-shot – One example is provided along with instructions.
  3. Few-shot – Multiple examples are provided along with instructions.

Create Environment File

Create an .env file and update the API key in the following format:

.env

GEMINI_API_KEY = "Your-Google-API-Key"
    

Create Python Script

Create a main.py file and copy the following code.

Python

# Zero Shot Prompting
from openai import OpenAI
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Get Gemini API key from environment variables
API_Key = os.getenv('GEMINI_API_KEY')

# Initialize OpenAI client configured for Gemini models
client = OpenAI(
    api_key=API_Key,
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)

# Zero Shot Prompting: Direct instruction to the model
SYSTEM_PROMPT = """
You are a programming assistant.

Rules:
1. Only answer questions related to programming or coding.
2. If the question is not related to coding, respond only with the word:
   "Sorry"
3. Do not provide explanations for refusal.
"""

response = client.chat.completions.create(
    model='gemini-3-flash-preview',
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": "Write Python code to reverse a list."}
    ]
)

# Print the AI response
print(response.choices[0].message.content)
    

Project Folder Structure

Folder Structure

πŸ“ MyProject
β”œβ”€β”€ πŸ“„ .env
└── πŸ“„ main.py

Run the Python Script

Go inside the MyProject directory and run the following command:

Bash

python main.py
    

The result returned by the AI varies every time, but for reference it returned the following Python code:

Python Output

# Method 1: Using slicing
my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]
print(reversed_list)

# Method 2: Using the reverse() method (modifies the list in-place)
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)

# Method 3: Using the reversed() function (returns an iterator)
my_list = [1, 2, 3, 4, 5]
reversed_iterator = reversed(my_list)
print(list(reversed_iterator))
    

Content Generation

We can use the OpenAI SDK for many different tasks. One common use case is content generation.

Python

from openai import OpenAI
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Retrieve API key
API_Key = os.getenv('GEMINI_API_KEY')

# Initialize client
client = OpenAI(
    api_key=API_Key,
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)

# Content generation example
SYSTEM_PROMPT = "You are an experienced technical blogger. Write engaging and informative blog posts."

user_prompt = """
Write a short blog post (200-250 words) about:
"Why Python is the Best Language for Beginners"

Make it engaging, include practical reasons, and end with a call to action.
"""

response = client.chat.completions.create(
    model='gemini-3-flash-preview',
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": user_prompt}
    ]
)

print(response.choices[0].message.content)
    

The result returned by the AI varies every time, but the following response was generated for reference:

Generated Output

### Why Python is Your Ultimate First Step into Coding

So, you’ve decided to learn to code. Congratulations! But now you're facing the ultimate dilemma: which language should you pick first? While C++ has its power and JavaScript dominates the web, there is one clear winner for newcomers: Python.

The primary reason Python reigns supreme is its readability. Python’s syntax is designed to mimic the English language, effectively removing the "bracket-and-semicolon" anxiety common in other languages. Writing a script in Python feels less like deciphering an ancient code and more like writing a logical list of instructions.

Secondly, Python is a Swiss Army Knife. It powers Data Science, Artificial Intelligence, Web Development, and Automation. Libraries like Pandas, Django, and TensorFlow make it extremely powerful.

Finally, the community support is unmatched. Almost every problem already has a solution online.

Ready to start your journey? Write your first print("Hello, World!") and begin exploring Python.