쿼리 라이브러리WordPress RSS 피드에서 포스트를 가져와 ChatGPT로 콘텐츠 재작성하기
WordPress RSS 피드에서 포스트를 가져와 ChatGPT로 콘텐츠 재작성하기
이 쿼리는 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=1OpenAI 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
}
}
}