Artificial Intelligence 9 min read

How to Write Effective Prompts and Use the OpenAI Python API

This article explains practical principles and techniques for crafting clear, specific prompts for ChatGPT, demonstrates how to structure prompts with separators and output formats, and provides a step‑by‑step guide—including code examples—for installing the OpenAI Python library, obtaining an API key, and writing a reusable get_completion function to interact with the model.

Continuous Delivery 2.0
Continuous Delivery 2.0
Continuous Delivery 2.0
How to Write Effective Prompts and Use the OpenAI Python API

Everyone knows that ChatGPT is a type of AIGC and its answers depend heavily on the quality of the prompts you give; this article presents a "secret formula" for writing efficient prompts to improve work efficiency.

Principle 1: Write clear and specific instructions. Use separators such as triple quotes ("""), triple backticks (```), three hyphens (---), angle brackets (<>), or XML tags to delimit parts of the prompt.

Example prompt for an image‑cropping task is shown in the following code block:

suppose you are a senior python developer engineer. There is a requirement as following.
```
Given there is an image,
Please crop  a new image out of the original image.
The input is image filename and 4 integers(x1,y1,x2,y2 ).
x1 and y1 represent the point of top-left.
x2 and y2 represent the point of bottom-right.
The program should check whether the image exists or not. If not, show a warning message and exit.
The program should check whether the two points top-left and bottom-right are integers and located in the images. If not, show a warning message which image size and  exit.
```
could you give me the python program which could take params in commandline.

Technique 1: Use separators. The article lists several delimiter options (triple quotes, triple backticks, three hyphens, angle brackets, XML tags) and shows how to embed them in prompts.

Technique 2: Specify the desired output format. Ask the model to return results in HTML, XML, JSON, or Markdown, and give a concrete example of the expected format.

Technique 3: Verify assumptions. Because ChatGPT is generative and its output depends on the temperature setting, you must check the answer yourself.

Technique 4: Provide a small amount of training data. Show a simple example of the expected answer before asking the real question.

Principle 2: Give ChatGPT thinking time. Break the task into explicit steps (Step 1, Step 2, …) so the model can reason before producing the final answer.

The second part of the article shifts to a practical guide for using the OpenAI Python API.

2.1 Prerequisites – you need an OpenAI account and a paid plan.

2.2 Install the API

pip install openai

2.3 Open the API key page – navigate to the OpenAI website, log in, and open the API Keys section.

2.4 Create an API key – give the key a memorable name and copy the generated secret.

2.5 Set the secret key as an environment variable

export OPENAI_KEY="sk‑xdtD … aZlD9n"

2.6 Write the Python function get_completion

import openai
import os

openai.api_key = os.environ.get("OPENAI_KEY")

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [
        {
            "role": "user",
            "content": prompt
        }
    ]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message["content"]

2.7 Call get_completion and interact with OpenAI

text = f"""
  Given there is a image,
  Please crop  a new image out of the original image.
  The input is image filename and 4 integers(x1,y1,x2,y2 ).
  x1 and y1 represent the point of top-left.
  x2 and y2 represent the point of bottom-right.
  The program should check whether the image exists or not. If not, show a warning message and exit.
  The program should check whether the two points top-left and bottom-right are integers and located in the images. If not, show a warning message which image size and  exit.
"""
prompt = f"""
  suppose you are a senior python developer engineer. There is a  requirement as following.

  ```{text}```
  could you give me the python program which could take params in commandline.
"""
response = get_completion(prompt)
print(response)

At the end, the article includes a promotional notice for a video course titled "Continuous Deployment Training Camp (Python)" with pricing information, but the main focus remains on prompt engineering and practical OpenAI API usage.

PythonaiPrompt EngineeringChatGPTcode examplesOpenAI API
Continuous Delivery 2.0
Written by

Continuous Delivery 2.0

Tech and case studies on organizational management, team management, and engineering efficiency

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.