Query 库从 WordPress RSS 订阅源导入文章并用 ChatGPT 改写内容
从 WordPress RSS 订阅源导入文章并用 ChatGPT 改写内容
此 query 从 WordPress RSS 订阅源(包括标题、内容和摘要)获取文章数据,使用 ChatGPT 改写内容,并将其保存到本地 WordPress 站点。
如果该用户名的作者在本地存在,则使用该作者;否则替换为变量 $defaultAuthorUsername 所定义的作者。
变量 $url 接收 WordPress 单篇文章的 RSS 订阅源 URL。通常为博客文章 URL + "/feed/rss/?withoutcomments=1"。例如:
https://wordpress.com/blog/2024/07/16/wordpress-6-6/feed/rss/?withoutcomments=1要连接 OpenAI API,必须通过变量 $openAIAPIKey 提供 API 密钥。
可以选择性地提供系统消息和提示词来改写文章内容。若未提供,则使用以下默认值:
- 系统消息(
$systemMessage):"You are an English Content rewriter and a grammar checker" - 提示词(
$prompt):"Please rewrite the following English text, by changing the simple A0-level words and sentences with more beautiful and elegant upper-level English words and sentences, while maintaining the original meaning: "
(内容字符串将追加到提示词末尾。)
此外,可以覆盖变量 $model("gpt-4o-mini")的默认值,并为 $temperature 和 $maxCompletionTokens(默认均为 null)提供值。
query GetPostFromRSSFeed(
$url: URL!
) {
_sendHTTPRequest(input: {
url: $url,
method: GET
}) {
body
rssJSON: _strDecodeXMLAsJSON(
xml: $__body
)
# Fields to be imported
authorUsername: _objectProperty(
object: $__rssJSON,
by: {
path: "rss.channel.item.dc:creator"
}
)
@export(as: "authorUsername")
content: _objectProperty(
object: $__rssJSON,
by: {
path: "rss.channel.item.content:encoded"
}
)
@export(as: "content")
excerpt: _objectProperty(
object: $__rssJSON,
by: {
path: "rss.channel.item.description"
}
)
@export(as: "excerpt")
title: _objectProperty(
object: $__rssJSON,
by: {
path: "rss.channel.item.title"
}
)
@export(as: "title")
}
}
query RewriteContentWithChatGPT(
$openAIAPIKey: String!
$systemMessage: String! = "You are an English Content rewriter and a grammar checker"
$prompt: String! = "Please rewrite the following English text, by changing the simple A0-level words and sentences with more beautiful and elegant upper-level English words and sentences, while maintaining the original meaning: "
$model: String! = "gpt-4o-mini"
$temperature: Float
$maxCompletionTokens: Int
)
@depends(on: "GetPostFromRSSFeed")
{
promptWithContent: _strAppend(
after: $prompt
append: $content
)
openAIResponse: _sendJSONObjectItemHTTPRequest(input: {
url: "https://api.openai.com/v1/chat/completions",
method: POST,
options: {
auth: {
password: $openAIAPIKey
},
json: {
model: $model,
temperature: $temperature,
max_completion_tokens: $maxCompletionTokens,
messages: [
{
role: "system",
content: $systemMessage
},
{
role: "user",
content: $__promptWithContent
}
]
}
}
})
@underJSONObjectProperty(by: { key: "choices" })
@underArrayItem(index: 0)
@underJSONObjectProperty(by: { path: "message.content" })
@export(as: "rewrittenContent")
}
# If the author's username exists in this site, keep it
# Otherwise, use the default one
query CheckAuthorExistsOrChange(
$defaultAuthorUsername: String! = "admin"
)
@depends(on: "RewriteContentWithChatGPT")
{
existingUserByUsername: user(by: { username: $authorUsername })
{
id
username
}
userExists: _notNull(value: $__existingUserByUsername)
username: _if(
condition: $__userExists,
then: $authorUsername,
else: $defaultAuthorUsername
)
@export(as: "existingAuthorUsername")
}
mutation ImportPostFromWordPressRSSFeedAndRewriteContent
@depends(on: "CheckAuthorExistsOrChange")
{
createPost(input: {
status: draft,
authorBy: {
username: $existingAuthorUsername
},
contentAs: {
html: $rewrittenContent
},
excerpt: $excerpt
title: $title
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
slug
date
status
author {
id
username
}
content
excerpt
title
}
}
}