Skip to main content

A Python module that connects to OpenAI's ChatGPT API to generate humorous limericks on any topic

Project description

Limerick Generator

A Python module that connects to OpenAI's ChatGPT API to generate limericks on any topic you specify.

Features

  • 🎭 Generate limericks on any topic
  • 🤖 Multiple OpenAI model support (gpt-3.5-turbo, gpt-4, gpt-5, gpt-5-mini, gpt-5-nano)
  • 🔞 Filth level control (0-3: family-friendly to filthy/offensive limericks)
  • 📄 Multiple output formats (text, JSON, YAML) for accessing full OpenAI response data

Installation

  1. Clone or download this repository

  2. Install dependencies using poetry:

    poetry install
    
  3. Set up your OpenAI API key:

    You need an OpenAI API key to use this module. Get one from OpenAI's website.

    Set the API key as an environment variable:

    On Linux/macOS:

    export OPENAI_API_KEY="your-api-key-here"
    

    On Windows:

    set OPENAI_API_KEY=your-api-key-here
    

    Or add to your .bashrc/.zshrc for persistence:

    echo 'export OPENAI_API_KEY="your-api-key-here"' >> ~/.bashrc
    source ~/.bashrc
    

Usage

Basic Usage

from limerick_generator import generate_limerick

# Generate a limerick about cats (uses default gpt-3.5-turbo model)
limerick = generate_limerick("cats")
print(limerick)

# Generate a limerick using a specific model
limerick = generate_limerick("cats", model="gpt-4")
print(limerick)

Example Output

There once was a cat from Peru,
Who dreamed of a mouse or two,
It pounced with great might,
Through day and through night,
But caught only dreams, it's true!

Advanced Usage

from limerick_generator import generate_limerick

# You can also pass the API key directly (not recommended for production)
limerick = generate_limerick("programming", api_key="your-api-key")
print(limerick)

Running the Examples

Run the example script to see the module in action:

python example.py

This will generate limericks for several topics and allow you to enter a custom topic.

API Reference

generate_limerick(topic, api_key=None, model="gpt-3.5-turbo", filth_level=0, output="text")

Generate a limerick about a specified topic using OpenAI's ChatGPT API.

Parameters:

  • topic (str): The topic or theme for the limerick
  • api_key (str, optional): OpenAI API key. If not provided, uses OPENAI_API_KEY environment variable
  • model (str, optional): OpenAI model to use. Defaults to "gpt-3.5-turbo"
  • filth_level (int, optional): Content filth level from 0-3. Defaults to 0 (family-friendly)
  • output (str, optional): Output format. Defaults to "text"

Supported Models:

  • gpt-3.5-turbo - Fast and cost-effective (default)
  • gpt-4 - High quality, balanced performance
  • gpt-5 - Latest and most advanced model
  • gpt-5-mini - Efficient with good quality
  • gpt-5-nano - Ultra-fast and lightweight

Filth Levels:

  • 0 - Family Friendly: Cheeky and family friendly
  • 1 - Risque: Funny and innuendo-filled, without being overly explicit
  • 2 - Dirty: Sexually charged and innuendo-filled
  • 3 - Filthy: Offensive, full of innuendo and sexual double meanings

Output Formats:

  • text - Returns just the limerick text (default)
  • json - Returns the full OpenAI response as JSON string
  • yaml - Returns the full OpenAI response as YAML string

Returns:

  • str: A generated limerick about the specified topic (text), or the full OpenAI response in JSON/YAML format

Raises:

  • ValueError: If no API key is provided, topic is invalid, or unsupported model/filth_level/output is specified
  • Exception: If there's an error communicating with the OpenAI API

Model Selection Guide

Choose the right model for your needs:

  • gpt-3.5-turbo: Best for general use, fastest response time, most cost-effective
  • gpt-4: Higher quality output, better understanding of complex topics
  • gpt-5: Latest model with advanced capabilities and improved creativity
  • gpt-5-mini: Balanced option with good quality and efficiency
  • gpt-5-nano: Optimized for speed with minimal resource usage

Model Usage Examples

# Using different models
limerick1 = generate_limerick("cats", model="gpt-3.5-turbo")  # Fast and economical
limerick2 = generate_limerick("quantum physics", model="gpt-4")  # Better for complex topics
limerick3 = generate_limerick("poetry", model="gpt-5")  # Most advanced creativity
limerick4 = generate_limerick("cooking", model="gpt-5-mini")  # Efficient quality
limerick5 = generate_limerick("weather", model="gpt-5-nano")  # Ultra-fast generation

Content Style Examples

# Generate family-friendly limericks (default)
family_limerick = generate_limerick("dating", filth_level=0)
print(family_limerick)

# Generate risque limericks with innuendo
risque_limerick = generate_limerick("dating", filth_level=1)
print(risque_limerick)

# Generate dirty, sexually charged limericks
dirty_limerick = generate_limerick("dating", filth_level=2)
print(dirty_limerick)

# Generate filthy, offensive limericks
filthy_limerick = generate_limerick("dating", filth_level=3)
print(filthy_limerick)

# Combine model selection with filth level
advanced_dirty = generate_limerick("romance", model="gpt-5", filth_level=2)
print(advanced_dirty)

Output Format Examples

# Default text output (just the limerick)
text_limerick = generate_limerick("cats", output="text")
print(text_limerick)

# JSON output (full OpenAI response)
json_response = generate_limerick("cats", output="json")
print(json_response)

# YAML output (full OpenAI response)
yaml_response = generate_limerick("cats", output="yaml")
print(yaml_response)

# Combine all parameters
full_example = generate_limerick(
    topic="programming",
    model="gpt-4",
    filth_level=1,
    output="json"
)
print(full_example)

Requirements

  • Python 3.12+
  • OpenAI Python library (>=1.0.0)
  • Valid OpenAI API key

Error Handling

The module includes comprehensive error handling for:

  • Missing or invalid API keys
  • Network connectivity issues
  • Invalid input parameters
  • OpenAI API errors

Cost Considerations

This module supports multiple OpenAI models with different pricing tiers:

  • gpt-3.5-turbo: Most cost-effective option
  • gpt-4: Higher cost but better quality
  • gpt-5: Premium pricing for latest capabilities
  • gpt-5-mini: Balanced cost and performance
  • gpt-5-nano: Optimized for cost efficiency

Each limerick generation costs a small amount based on OpenAI's pricing for the selected model. Monitor your usage through the OpenAI dashboard. Consider using gpt-3.5-turbo for high-volume applications or gpt-5 models when quality is paramount.

Troubleshooting

"OpenAI API key not found" Error

  • Ensure you've set the OPENAI_API_KEY environment variable
  • Verify the API key is valid and active
  • Check that you have sufficient credits in your OpenAI account

"Unsupported model" Error

  • Check that you're using one of the supported models: gpt-3.5-turbo, gpt-4, gpt-5, gpt-5-mini, gpt-5-nano
  • Verify the model name is spelled correctly (case-sensitive)
  • Ensure your OpenAI account has access to the requested model

"Error generating limerick" Error

  • Check your internet connection
  • Verify your OpenAI API key has the necessary permissions
  • Ensure you haven't exceeded rate limits
  • Try using a different model if one specific model is failing

License

This project is open source. Feel free to modify and distribute as needed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ozgrav_limerick_generator_test-1.1.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file ozgrav_limerick_generator_test-1.1.1.tar.gz.

File metadata

File hashes

Hashes for ozgrav_limerick_generator_test-1.1.1.tar.gz
Algorithm Hash digest
SHA256 593817f268956591406b8e6ae5e133bae745aeb988ee727f76b1b0f3a53d95c7
MD5 9f027d643f33938e31c86cfc50fa38de
BLAKE2b-256 bf95fe7367c2e27d5186e391e35d726dcf7410316de3818ec724e059236fa071

See more details on using hashes here.

File details

Details for the file ozgrav_limerick_generator_test-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ozgrav_limerick_generator_test-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 69def5d54262d293cbce1980dcaf555f490f45e358485ba13c47eb7fb001793b
MD5 35e12a9df428e5b6493df99bc5fc400e
BLAKE2b-256 65fbf8921cb8195f74ec39d099f99bea542e55bb4a48da057e158facdff4f174

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page