WP-HistographAI: reviving history with AI on WordPress - createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    WP-HistographAI: reviving history with AI on WordPress

    November 21, 2023
    Last update: April 4, 2024
    5 min read
    9
    0
    0
    WP-HistographAI: reviving history with AI on WordPress

    Challenge: delivering concise, AI-generated historical summaries for any given year

    Solution: WP-HistographAI – a WP plugin that integrates with OpenAI, fetching and presenting history

    In the digital age, where everything is a click away, the vast tapestry of our world’s history often gets lost in the shuffle. Enter WP-HistographAI – a WordPress plugin designed to bring the most significant worldwide events to your fingertips. Leveraging the power of OpenAI’s advanced language models, this plugin provides a captivating journey through time, all within the comfortable confines of your WordPress site. In this post, we’ll delve into the magic behind WP-HistographAI and how it’s changing the way we interact with history.

    A glimpse into the past with OpenAI

    The crux of WP-HistographAI lies in its integration with the OpenAI’s GPT-3.5 model. This state-of-the-art language model dives deep into historical data, presenting events in a concise, table format for any given year. Users simply select a year, and the plugin fetches a comprehensive summary, all in real-time.

    But what truly sets WP-HistographAI apart is its efficiency. To minimize operational costs and improve performance, the plugin stores summaries in the WordPress database after the first fetch. This means subsequent requests for the same year will bypass the API call, pulling data directly from the database. It’s a seamless blend of AI innovation and smart data management.

    colorful website with text and a form

    Interacting with OpenAI API

    The histographai_get_summary($year) function is pivotal to WP-HistographAI’s core functionality. Given a year as input, this function communicates with the OpenAI API, fetching a concise summary of significant events for that particular year.

    The function reaches out to the OpenAI API with a specified model (like ‘gpt-3.5-turbo’), the constructed prompt, and other parameters such as max_tokens and temperature to fetch the historical summary:

    Upon a successful API response, the function extracts the content, which is the historical summary, and returns it.

    function histographai_get_summary($year) {
        $yourApiKey = get_option('openai_api_key');
    
        if(empty($yourApiKey)){
            wp_die( "Error: OpenAI API key is not set in the WP-HistographAI settings." );
        }
    
        // Assuming you've included the necessary library for OpenAI.
        $client = OpenAI::client($yourApiKey);
    
        $final_prompt = GPT_PROMPT . ' ' . $year;
    
        try {
            $response = $client->chat()->create([
                'model' => 'gpt-3.5-turbo', // gpt-4
                'messages' => [
                    ['role' => 'user', 'content' => $final_prompt],
                ],
                'max_tokens' => 3000,
                'temperature' => 1
            ]);
    
            $summary = '';
    
            foreach ($response->choices as $result) {
                if(isset($result->message->content)){
                    $summary = $result->message->content;
                }
            }
    
            return $summary;
    
        } catch (Exception $e) {
            ct_debug_log( "Error fetching summary from OpenAI: " . $e->getMessage());
        }
    
        return false;
    }

    Action scheduler & background jobs

    To maintain a smooth and responsive user experience, WP-HistographAI employs the Action Scheduler for background data fetching. Instead of burdening the server with on-the-spot data retrieval, the plugin schedules tasks to fetch historical summaries in the background. This ensures that users aren’t kept waiting and the frontend remains snappy.

    While the frontend provides quick summaries from the database, the backend diligently works to update and expand the database with new summaries. This asynchronous approach guarantees up-to-date content without compromising user experience.

    Content in markdown format

    When summaries from the OpenAI API are stored, they’re saved in markdown format. This ensures a lightweight storage solution that is both human-readable and easy to manage. But what happens when this markdown content needs to be displayed on a webpage? This is where the Parsedown library is used, converting markdown into beautifully formatted HTML content.

    The added advantage? For those familiar with markdown, tweaking or augmenting stored summaries becomes a breeze. This markdown approach, combined with Parsedown’s capabilities, ensures that WP-HistographAI offers both simplicity in content management and elegance in content display.

    Wordpress dashboard with text and panels

    Storing data

    For efficiency, the plugin avoids redundant API calls. Once a historical summary for a particular year is fetched, it’s securely stored in the WordPress database. This means that repeated requests for the same year will not trigger additional API calls, ensuring faster response times and reduced operational costs. Moreover, by utilizing WordPress’s in-built capabilities, like custom post types, WP-HistographAI ensures that the stored data is both secure and easily retrievable.

    WordPress panel with years and tick boxes

    Engaging users with typewriting effect

    WP-HistographAI goes a step further in user engagement with its typewriting table effect. As summaries are displayed, each table cell’s content appears character by character, simulating a typewriter’s charm. This interactive feature not only captures attention but also enhances content immersion.

    Colorful website with gradually appearing text

    Social integrations

    One of WP-HistographAI’s standout features is its built-in social sharing capability. Once users have delved into a year’s history, they can instantly share their discoveries across major platforms like Facebook, Twitter, and LinkedIn. Each year summary comes equipped with dedicated share buttons, allowing readers to spread knowledge with just a click.

    Moreover, the plugin generates a direct link, enabling users to share the historical summary on platforms beyond the provided options or even in personal messages. The ease of sharing ensures that the enriching content reaches a wider audience, promoting historical awareness.

    API key configuration

    To ensure the plugin can communicate with the OpenAI API, you must provide your OpenAI API key. This can be done through the WP-HistographAI settings page in your WordPress admin dashboard:

    1. Navigate to the WP-HistographAI settings page.
    2. Enter your OpenAI API key in the provided field.
    3. Save your settings.
    WordPress dashboard with settings window

    Accessing WP-HistographAI on GitHub

    For those eager to delve deeper into the WP-HistographAI codebase or perhaps contribute to its development, the GitHub repository is the place to go: https://github.com/createit-dev/314-WP-HistographAI

    The README file provides a quick start guide, helping newcomers set up the plugin in their local environment.

    To ensure smooth operation, WP-HistographAI relies on various external PHP packages. After cloning the WP-HistographAI repository from GitHub, it’s essential to run:

    composer install

    This command reads the composer.json file, which lists all the required packages, and generates the vendor directory. This directory contains all the necessary libraries and their respective versions, ensuring that WP-HistographAI has everything it needs to operate seamlessly.

    Exploring AI demos

    This is part of the series: Exploring AI Demos: Discovering OpenAI’s Potential with WordPress, a captivating journey that melds the robustness of WordPress with the brilliance of OpenAI. This series unravels the transformative capabilities of artificial intelligence, offering live demonstrations and insights. From creative storytelling to analytical prowess, experience firsthand how AI is reshaping the digital narrative within the WordPress ecosystem. Join us as we unlock the future of content, one demo at a time.

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us