GitHunt
NG

ngeorger/ghostai

Harnessing AI technology with Ghost Blogging Platform

For discussion about this project. Please refer to this forum post:
https://forum.ghost.org/t/ghostai-ghost-openai-integration/37578

Ghost Blog Relationship

GhostAI Related Posts is a project that utilizes OpenAI Embedding API to generate related blog post tags for a given Ghost Blogging site. The project provides the functionality to display "Related Posts" at the bottom of each blog post. Currently, this feature is not available in Ghost.

The project comprises of two scripts that can be executed either on a local machine or directly on the server. Unless the site has tens of thousands of blog posts, running the scripts on a local machine should suffice.

The first script iterates through all public blog posts on the server and extracts the text content, which is then sent to the OpenAI (Ada model) to generate a set of vectors. These vectors are then stored in a text file (find them in ./output/). The script also supports incremental generation, which means that it only generates vectors for blog posts that have not yet been processed.

The second script iterates through all the vectors in the directory and ranks their similarity by giving a score. For each blog post, a few other blog posts with high similarity ranking will be grouped together. Ghost internal tagging feature is used for the grouping mechanism, and each blog post ID becomes the internal tag name.

To display the related blog posts at the bottom of another blog post, the post.hbs template needs to be edited by inserting a simple tag filter. This design works with both self-hosted Ghost sites and Ghost Pro sites (hosted by the Ghost team) and does not require any additional configuration, database changes, or other adjustments.

Users can contribute to the GhostAI Related Posts project by submitting pull requests on Github. We welcome any improvements to the project, including bug fixes, new features, and enhancements.

Requirements

  • Python 3.7.1+

To ensure compatibility with our customers' Python versions, we recommend using Python 3.7.1 or higher.

Installation

Before starting the script, ensure that the following packages are installed:

pip install --upgrade pip
pip install openai pandas matplotlib scipy scikit-learn plotly pyjwt

Usage

The library needs to be configured with your OpenAI account's API key. OpenAI API Key is available on their webiste at https://platform.openai.com/account/api-keys. Ghost Admin API Key can be found by following instructions at https://ghost.org/docs/admin-api/#token-authentication

Either set it as the OPENAI_API_KEY & GHOST_ADMIN_API_KEY environment variable before using the library:

export OPENAI_API_KEY='sk-...'
export GHOST_API_KEY='...'

Or set OPENAI_API_KEY & GHOST_ADMIN_API_KEY to ./.env:

[BASIC]
GHOST_ADMIN_API_KEY={YOUR_GHOST_ADMIN_API_KEY}
GHOST_SITE_URL={YOUR_BLOG_SITE_URL}  ** Make sure it does not end with / **
OPENAI_API_KEY={YOUR_OPENAI_API_KEY}
EMBEDDING_OUTPUT_PATH=./output
MAX_RELATED_BLOG_COUNT=20

After all set, start the script as follows:

python ghost_embeddings.py

This will generate the vector files in the file system.

Once everything looks good, you are now ready to run another script to tag the related posts back to your Ghost site:

python ghost_relation_tags.py

We also provided a script to generate tags for your blogs.

python ghost_tag_blogs.py

Please note that the script will not overwrite your existing tags; instead, it will merely add new ones.

To overwrite all your existing tags, please follow these steps:

python ghost_tag_blogs.py reset

By default, the script will generate 5 tags for each blog. You can change the number in .env config file if you would like to see more.

BLOG_TAG_COUNT=5

We've also provided a script to clean up all tags:

#Clean up public tags. NOTE! This will not only delete tags generated by the script, but also all existing public tags, include your original tags (if you have any)
python ghost_tag_cleanup.py public
#Clean up internal tags
python ghost_tag_cleanup.py internal
#Clean up both
python ghost_tag_cleanup.py both

To check the tagging, go to the Ghost console, click on one of the post, on the right hand side where you can assign tagging for the post, you should see a few internal tags generated by the script already.

To show the related posts, edit your post.hbs template. Here is an example for the Casper template:

{{#get "posts" include="tags,authors" limit="12" filter="id:-{{id}}+tag:{{id}}" as |more_posts|}}
    {{#if more_posts}}
        <aside class="read-more-wrap">
            <div class="read-more inner">
                {{#foreach more_posts}}
                    {{> "post-card"}}
                {{/foreach}}
            </div>
        </aside>
    {{/if}}
{{/get}}

Languages

Python100.0%

Contributors

Created June 25, 2023
Updated August 5, 2024
ngeorger/ghostai | GitHunt