Thư viện Queries
Thư viện QueriesNhập bài viết từ RSS feed WordPress và viết lại nội dung với ChatGPT

Nhập bài viết từ RSS feed WordPress và viết lại nội dung với ChatGPT

Query này lấy dữ liệu bài viết từ RSS feed WordPress (bao gồm tiêu đề, nội dung và trích đoạn), viết lại nội dung bằng ChatGPT, và lưu vào trang WordPress cục bộ.

Nếu tác giả với tên người dùng đó tồn tại cục bộ, hệ thống sẽ sử dụng nó; nếu không, nó sẽ được thay thế bằng tác giả được xác định qua biến $defaultAuthorUsername.

Biến $url nhận URL của RSS feed bài viết WordPress đơn lẻ. Thông thường đó là URL bài viết blog + "/feed/rss/?withoutcomments=1". Ví dụ:

https://wordpress.com/blog/2024/07/16/wordpress-6-6/feed/rss/?withoutcomments=1

Để kết nối với API OpenAI, bạn phải cung cấp biến $openAIAPIKey với khóa API.

Bạn có thể tùy chọn cung cấp thông báo hệ thống và prompt để viết lại nội dung bài viết. Nếu không được cung cấp, các giá trị mặc định sau sẽ được sử dụng:

  • Thông báo hệ thống ($systemMessage): "You are an English Content rewriter and a grammar checker"
  • Prompt ($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: "

(Chuỗi nội dung được thêm vào cuối prompt.)

Ngoài ra, bạn có thể ghi đè giá trị mặc định cho biến $model ("gpt-4o-mini") và cung cấp giá trị cho $temperature$maxCompletionTokens (cả hai mặc định là 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
    }
  }
}