Harnessing ChatGPT to Populate Your Entire Website with Fresh Content: Part 9:

Harnessing ChatGPT to Populate Your Entire Website with Fresh Content: Part 9:

09_Update_Excerpt_WordPress


This script is a continuation of the previous script (08_Get_Excerpt_for_Article). After generating the excerpt for each article using OpenAI, this script updates the corresponding articles in WordPress with the generated excerpts.


First, we import the necessary modules and set up the credentials for OpenAI and WordPress.

import json
import pandas as pd
import openai
import sqlite3
from models import Trend, session
import base64
import requests
from sqlalchemy import and_

# OpenAI credentials
openai.api_key = "your_openai_api_key_here"


# WordPress credentials
username = "your_wordpress_username_here"
api_base_url = "https://www.your_wordpress_site_here.com/wp-json/wp/v2/"
application_password = "your_wordpress_application_password_here"n        

The script retrieves all the trends from the database that have a non-null `article_id`, a non-empty `article`, and non-null `article_tags`. For each trend, the script generates a two-sentence synopsis of the article using OpenAI's completion API:

all_trends = session.query(Trend).filter(and_(Trend.article_id != None, Trend.article != '', Trend.article_tags != None)).all(

for idx, trend in enumerate(all_trends):
??try:
????response = openai.Completion.create(
??????engine="text-davinci-003",
??????prompt=f"Write a two sentence synopsis of [{trend.title}].",
??????max_tokens=50,
??????n=1,
??????stop=None,
??????temperature=0.7)

????only_choice = response.choices[0].text.strip()
????trend.article_excerpt = only_choice
????session.commit()

??except Exception as e:
????print(e)
????break        

Finally, the database session is closed:

session.close()        

Please note that this script is incomplete. As per the given code, it only generates the synopsis for each article and updates the corresponding `Trend` object in the local database. But it doesn't update the article in WordPress with the generated excerpt. You would have to implement that part, typically by making a POST request to the WordPress REST API similar to how it's done in the other scripts.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了