Harnessing ChatGPT to Populate Your Entire Website with Fresh Content: Part 2: 02_Get_Titles_Based_on_Trend
02_Get_Titles_Based_on_Trend
In the era of real-time updates and rapidly changing digital trends, maintaining fresh, engaging content is key to building a successful website. One way to achieve this is by leveraging the power of AI language models, like OpenAI's ChatGPT. In this post, we will discuss how you can use ChatGPT in tandem with the Reddit API to populate your website with trending topics. Let's dive into the process:
In this section, we'll leverage the power of OpenAI's GPT-3 engine to generate engaging and relevant titles for articles based on the trending topics we fetched from Reddit in the previous section.
Here's a step-by-step breakdown of our Python script:
We begin by importing the necessary modules. This includes the OpenAI Python client, which allows us to interact with the OpenAI API, the Pandas library for data handling, and modules to interact with SQLite databases and JSON data.
import json
import pandas as pd?
import openai?
import sqlite3?
from sqlalchemy import and_
Next, we set our OpenAI API key which allows us to interact with the respective APIs. We've stored these in a CSV file for convenience and security.
领英推荐
openai.api_key = "your_openai_api_key_here
Remember, these keys grant access to your specific OpenAI account, so they should never be shared publicly.
Next, we define a function, generate_article_title, which accepts a keyword and uses the OpenAI API to generate an article title related to that keyword. It uses the GPT-3 engine (specifically, the text-davinci-003 engine) to generate these titles.
def generate_article_title(keyword)
? ? prompt = f"Generate a title for an article about {keyword}."
? ? response = openai.Completion.create(
? ? ? ? engine="text-davinci-003",
? ? ? ? prompt=prompt,
? ? ? ? max_tokens=30,
? ? ? ? n=1,
? ? ? ? stop=None,
? ? ? ? temperature=0.7,
? ? )
? ? only_choice = response.choices[0].text.strip()
? ? return only_choice
We then retrieve all trends from our database that have a trend_name but no corresponding title.
all_trends = session.query(Trend).filter
? ? and_(Trend.trend_name != None,
? ? Trend.title == None)).all()
Lastly, we loop over these trends, generating a title for each one using our generate_article_title function. The generated title is then saved back to the corresponding Trend object in the database.
for trend in all_trends
? ? try:
? ? ? ? print(trend.trend_name)
? ? ? ? title = generate_article_title(trend.trend_name)
? ? ? ? trend.title = title
? ? ? ? session.commit()
? ? except:
? ? ? ? pass
session. Close()
This script enables us to automatically generate creative, relevant titles for articles based on current trending topics. In the following sections, we'll dive deeper into how we can use these titles and trends to generate complete articles using GPT-3.