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
-
Clone or download this repository
-
Install dependencies using poetry:
poetry install -
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/.zshrcfor 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 limerickapi_key(str, optional): OpenAI API key. If not provided, usesOPENAI_API_KEYenvironment variablemodel(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 performancegpt-5- Latest and most advanced modelgpt-5-mini- Efficient with good qualitygpt-5-nano- Ultra-fast and lightweight
Filth Levels:
0- Family Friendly: Cheeky and family friendly1- Risque: Funny and innuendo-filled, without being overly explicit2- Dirty: Sexually charged and innuendo-filled3- 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 stringyaml- 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 specifiedException: 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_KEYenvironment 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ozgrav_limerick_generator_test-1.0.1.tar.gz.
File metadata
- Download URL: ozgrav_limerick_generator_test-1.0.1.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-60-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e5310ed3da3a5a63b9e6f5cfa5ee5af0dcccc529c69e2237651c9c6c2cd2c62
|
|
| MD5 |
1ced743389874877053cef8660ec9052
|
|
| BLAKE2b-256 |
37b06b7b643801c6da1855f140d49d792ee460b47aa260060066341fe9a202c2
|
File details
Details for the file ozgrav_limerick_generator_test-1.0.1-py3-none-any.whl.
File metadata
- Download URL: ozgrav_limerick_generator_test-1.0.1-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-60-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b21b15ba41d7cae9e3d85789d6cb667f62a145fd4db545f08519eb4164eb3992
|
|
| MD5 |
94f0297f83ae0c0cc4e333199e8abf56
|
|
| BLAKE2b-256 |
5e3ff4c449870dd8010f63190693a830190ee16f0749bd1f19ed2f001f9a68e7
|