Algorithm and Prompt Flow

Sophisticated 3-step AI prompt chain for intelligent content analysis

Algorithm Overview

Our content analysis uses a sophisticated 3-step AI prompt chain that intelligently determines whether to use real-world data or generate thematically appropriate contextual information:

Step 1: Location Analysis

API Call: OpenAI GPT-4
Prompt Role: “Ken Burns for real-world locations or A.O. Scott for fictional places”
Input: Book title
Output: LocationAnalysis object with name, isReal, primaryCity, cityDescription

Analyzes the book to identify the most specific city/location where significant events occur, distinguishing between real and fictional locations with historical insights or cultural analysis.

Step 2: Weather Type Determination

API Call: OpenAI GPT-4
Prompt Role: “A.O. Scott, chief film and literary critic for The New York Times”
Input: Book title + location from Step 1
Output: WeatherType object with type (“real” or “fictional”), useRealWeather

Determines if the location is real-world and whether to use actual weather data or generate thematically appropriate fictional weather.

Step 3A: Real Weather Generation (if real location)

API Call: OpenWeather API
Input: Location name
Output: Current weather data (temperature, humidity, wind speed, conditions, timezone)

Calls OpenWeather API with the identified location. Fallback: If OpenWeather fails (404, 401, 429, timeout) → proceed to Step 3B.

Step 3B: Fictional Weather Generation (if fictional or fallback)

API Call: OpenAI GPT-4
Prompt Role: “Harvard professor of Literature and Climate Science”
Input: Book title + location + context
Output: Thematically appropriate weather data with literary context

Generates weather conditions that reflect the story's themes and atmosphere, including appropriate time systems for fictional universes.

Final Assembly

Process: Combines all data into final response
Output: Complete WeatherResponse object with location data, weather data, metadata including source (“openweather” vs “fictional”), timestamp and session information.

Error Handling & Decision Points

Decision Point 1: Real vs Fictional Location → Try OpenWeather API or Generate fictional weather

Decision Point 2: API Success vs Failure → Use real data or Fallback to fictional generation

Error Scenarios: Missing parameters (400 error), API failures (automatic fallback), Invalid JSON (validation), Timeouts (retry logic)

This creates a robust system that can handle both real-world and fictional content while providing meaningful, thematically appropriate data for any content analysis.

Workflow Diagram

The workflow below shows the decision points and fallback mechanisms that ensure robust, contextually relevant results.

Weather Analysis Workflow

Loading workflow diagram...

Key Decision Points

1.
Location Reality Check: Determines if the story location exists in the real world
2.
Weather Strategy: Chooses between real API data or AI-generated fictional weather
3.
Fallback Mechanism: Automatically switches to fictional generation if real weather API fails