Building AI-Powered Apps in Python — A Step-by-Step Guide

· 2 min read · Wingman Protocol

Looking for affordable hosting? Hostinger starts at $2.99/mo with a free domain and SSL included.Get 80% Off

This tutorial provides a practical guide to building AI-powered applications using Python and the OpenAI SDK. We'll cover installation, authentication, core functionality like chat completions, and real-world examples, all while emphasizing best practices for error handling and managing rate limits. The OpenAI SDK provides a convenient interface to interact with OpenAI's powerful language models.

Need a server? DigitalOcean gives new users $200 in free credit to get started.Claim $200 Credit
Installation and Setup

Before diving in, you'll need to install the OpenAI Python SDK. This can be easily done using pip: pip install openai. You'll also need a valid OpenAI API key. Sign up for an account at https://platform.openai.com/ to obtain your key. We'll be using base_url='https:///v1' for this tutorial, as it represents a hypothetical API endpoint for the OpenAI service.

Authentication

Authentication is crucial for accessing the OpenAI API. You'll need to include your API key in the request headers. The openai.OpenAI() constructor handles this for us when the api_key parameter is used.

import openai

api_key = "YOUR_OPENAI_API_KEY"
client = openai.OpenAI(
    api_key=api_key,
    base_url='https:///v1'
)
Chat Completions Example

Let's start with a fundamental example: generating chat completions. This involves sending a conversation history to the model and receiving a response.

import openai

api_key = "YOUR_OPENAI_API_KEY"
client = openai.OpenAI(
    api_key=api_key,
    base_url='https:///v1'
)

def get_chat_completion(messages, model="gpt-3.5-turbo"):  # Default model
    """
    Generates a chat completion using the OpenAI API.

    Args:
        messages: A list of messages, where each message is a dictionary
                  with 'role' (e.g., "user", "system", "assistant")
                  and 'content' keys.
        model: The model to use (e.g., "gpt-3.5-turbo").

    Returns:
        The content of the assistant's response, or None if an error occurs.
    """
    try:
        response = client.chat.completions.create(
            model=model,
            messages=messages
        )
        return response.choices[0].message.content
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

# Example usage:
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Write a short poem about a cat."},
]
response_content = get_chat_completion(messages)

if response_content:
    print(response_content)
SEO Audit Example

Let's build a more practical example: an SEO audit tool. This will involve prompting the model to analyze a snippet of text for SEO best practices.

import openai

api_key = "YOUR_OPENAI_API_KEY"
client = openai.OpenAI(
    api_key=api_key,
    base_url='https:///v1'
)

def perform_seo_audit(text_snippet, model="gpt-3.5-turbo"):
    """
    Performs an SEO audit of a given text snippet.

    Args:
        text_snippet: The text to analyze.
        model: The model to use.

    Returns:
        An SEO audit report, or None if an error occurs.
    """
    messages = [
        {"role": "system", "content": "You are an SEO expert. Analyze the following text snippet for SEO best practices. Provide suggestions for improvement, focusing on keywords, readability, and overall optimization."},
        {"role": "user", "content": text_snippet},
    ]
    return get_chat_completion(messages, model) # Re-use the get_chat_completion function

# Example usage:
text_to_audit = "This is a blog post about Python programming. Learn Python today!"
audit_report = perform_seo_audit(text_to_audit)
if audit_report:
    print("SEO Audit Report:\n", audit_report)
Error Handling

Robust error handling is essential. The try...except block in get_chat_completion demonstrates how to catch potential exceptions. Consider handling specific exceptions (e.g., openai.RateLimitError, openai.APIConnectionError) for more granular control. Log errors appropriately for debugging.

Rate Limits and Best Practices

OpenAI APIs have rate limits. Exceeding these limits results in errors. To mitigate this:

* Implement retry logic: Use libraries like tenacity to automatically retry failed requests with exponential backoff. * Monitor your usage: Track your API calls and tokens used to stay within your limits. The OpenAI dashboard gives you this information. * Batch requests: If possible, process multiple inputs in a single API call to reduce the number of requests. (Not demonstrated in this basic tutorial) * Optimize prompts: Craft concise and clear prompts to minimize token usage. * Handle API errors gracefully: Implement retries and potentially inform the user if the service is unavailable. * Check the documentation: Always refer to the official OpenAI documentation for up-to-date information on rate limits and best practices.

Written by the Wingman Protocol team — developers building with AI APIs, cloud infrastructure, and automation tools daily. Our guides are based on hands-on experience running production systems.

Our Top Pick
DigitalOcean — $200 Free Credit

Spin up cloud servers, managed databases, and Kubernetes clusters. New users get $200 in free credit.

Claim $200 Free Credit →

· Fact-checked against official documentation and primary sources.

Related Services


Free Printable Resources

This section contains affiliate links. If you purchase through these links, we earn a small commission at no extra cost to you. We only recommend products we have researched for quality and value in the trades and real estate industries.

Tools We Recommend

We have tested these tools ourselves. Here are our top picks for this topic.

DigitalOcean — $200 Free Credit

Spin up cloud servers, managed databases, and Kubernetes clusters. New users get $200 in free credit.

Claim $200 Credit →
📚
Tech Books & Resources on Amazon

Find the best programming books, guides, and tech resources to level up your skills.

Browse on Amazon →
🌐
Hostinger — 80% Off Web Hosting

Start a website from $2.99/mo with a free domain, SSL, and 24/7 support included.

Get 80% Off →

Some links above are affiliate links. We may earn a small commission at no extra cost to you.

Join 500+ developers. Get weekly API tutorials + a free starter guide.

Practical tips on AI APIs, automation, and building with LLMs — delivered every week.

No spam. Unsubscribe anytime.

DigitalOcean Cloud

$200 Free Credit

Developer-friendly cloud platform. Deploy apps, databases, and Kubernetes clusters in seconds.

Quick Comparison
DigitalOceanBest for Developers

Developer-friendly UI, excellent docs, App Platform for easy deploys

$200 free credit
Get Started
HostingerBest Value

Best value for web hosting, free domain + SSL, LiteSpeed servers

From $2.99/mo
Get Started

Affiliate links. We may earn a commission at no extra cost to you.

Claim $200 credit →

Hostinger Hosting

From $2.99/mo

Fast web hosting with free domain, SSL, and LiteSpeed servers. Best value for websites and blogs.

Get 80% off →

Developer Books

Top Picks

Clean Code, The Pragmatic Programmer, and more. Essential reading for leveling up your skills.

Browse on Amazon →

Wingman Protocol Pro

⚡ Get 5 free AI guides + weekly insights

207 templates, tools & guides — one subscription

Contractor forms, financial worksheets, landlord documents, and more. Instant access. Cancel anytime.

Get Pro Access →

No lock-in. Access everything instantly after checkout.

You Might Also Like

Get free weekly AI insights delivered to your inbox