Overview
Historia Diaria uses the OpenRouter API to generate daily science fiction and fantasy stories. The process involves prompt engineering, API communication, response parsing, and image generation.OpenRouter API Integration
API Configuration
The script uses OpenRouter’s chat completions endpoint with the freestepfun/step-3.5-flash:free model:
generar_historia.py
Why OpenRouter?OpenRouter provides access to multiple AI models through a single API, including free models like
stepfun/step-3.5-flash:free that are perfect for automated daily generation.API Key Security
The API key is never hardcoded in the script. Instead, it’s:- Stored as a GitHub Secret named
OPENROUTER_API_KEY - Passed as an environment variable during workflow execution
- Retrieved using
os.environ.get()at runtime
actualizar.yml
Prompt Engineering
The prompt is designed to return structured, parseable output:generar_historia.py
Prompt Structure
Genre Specification
Genre Specification
“ciencia ficción o fantasía” - Constrains the AI to generate content within specific genres, ensuring thematic consistency across all daily stories.
Format Requirements
Format Requirements
“EXACTAMENTE en este formato” - Strong directive that instructs the AI to follow the XML-like tag structure strictly, making regex parsing reliable.
Length Control
Length Control
“unos dos o tres párrafos” - Limits story length to maintain quick readability and consistent page layouts.
Image Keyword
Image Keyword
“una_sola_palabra_clave_en_ingles” - Originally intended for image search, now unused since the system generates random images via Picsum.
Response Parsing
Regex Extraction
The script uses regular expressions to extract structured data from the AI response:generar_historia.py
Regex Pattern Breakdown
| Component | Meaning |
|---|---|
<TITULO> | Literal opening tag |
(.*?) | Capture group: any character, non-greedy |
</TITULO> | Literal closing tag |
re.DOTALL | Makes . match newlines (multi-paragraph support) |
Fallback Handling
The parsing includes defensive fallbacks:generar_historia.py
Image Generation
UUID-Based Random Images
Instead of using the AI’s image keyword, the script generates random images from Picsum Photos:generar_historia.py
Why UUID Seeds?Using a UUID as the seed ensures:
- Each story gets a unique, random image
- The same image loads consistently on each page view
- No API keys or image search services required
- 600x350px dimensions provide optimal aspect ratio
Image URL Example
Error Handling
API Failure Recovery
The entire API call is wrapped in a try-except block:generar_historia.py
Error Scenarios Handled
Network Failures
Network Failures
If GitHub Actions can’t reach OpenRouter’s servers, the fallback response is used.
API Rate Limits
API Rate Limits
Free tier rate limits trigger the exception handler, allowing the workflow to complete.
Invalid API Keys
Invalid API Keys
Authentication failures are caught, preventing workflow crashes.
Malformed Responses
Malformed Responses
If the JSON structure is unexpected, the exception is caught.
Complete Generation Flow
Testing Story Generation
You can test the story generation manually:Manual GitHub Trigger
Use the “Run workflow” button in the GitHub Actions tab to trigger generation on-demand.
Next Steps
How It Works
Understand the overall system architecture
HTML Templates
Learn how generated content fills the template