ไลบรารี Queries
ไลบรารี Queriesสร้างเมตาดาทา Yoast SEO ด้วย ChatGPT

สร้างเมตาดาทา Yoast SEO ด้วย ChatGPT

query นี้ถูกสาธิตไว้ในเดโม การสร้างเมตาดาทา Yoast SEO สำหรับคอร์สและบทเรียนของ MasterStudy LMS โดยใช้ ChatGPT

query นี้สร้างและอัปเดตเมตาดาทา SEO ใน Yoast สำหรับโพสต์โดยใช้ ChatGPT

ตัวแปรที่จำเป็นมีดังนี้:

  • postId: ID ของโพสต์ (หรือ custom post สำหรับ custom post type ใด ๆ ที่เข้าถึงได้) ที่จะอัปเดต
  • openAIAPIKey: API key สำหรับ OpenAI API
query GetPostData($postId: ID!) {
  customPost(by: { id: $postId }) {
    id
    ...on CustomPost {
      title
        @export(as: "title")
      content
        @export(as: "content")
    }
    ...WithMetaData
  }
}
 
query GenerateSeoMetadataWithChatGPT(
  $openAIAPIKey: String!
  $systemMessage: String! = "You are an SEO specialist"
  $promptTemplate: String! = """
I'm working on creating the SEO metadata for courses and lessons in my Learning Management System.
 
Please evaluate the course or lesson data, and generate the following SEO metadata:
 
- Title
- Excerpt
- Focus Keyword
- Open Graph Title
- Open Graph Description
- Twitter Title
- Twitter Description
 
The data is:
 
- Title: {$title}
- Content: {$content}
"""
  $model: String! = "gpt-4o-mini"
)
  @depends(on: "GetPostData")
{
  prompt: _strReplaceMultiple(
    search: ["{$title}", "{$content}"],
    replaceWith: [$title, $content],
    in: $promptTemplate
  )
  openAIResponse: _sendJSONObjectItemHTTPRequest(input: {
    url: "https://api.openai.com/v1/chat/completions",
    method: POST,
    options: {
      auth: {
        password: $openAIAPIKey
      },
      json: {
        model: $model,
        messages: [
          {
            role: "system",
            content: $systemMessage
          },
          {
            role: "user",
            content: $__prompt
          },
        ],
        response_format: {
          type: "json_schema",
          json_schema: {
            name: "seo_metadata_response",
            strict: true,
            schema: {
              type: "object",
              properties: {
                seoMetadata: {
                  type: "object",
                  properties: {
                    title: {
                      type: "string"
                    },
                    excerpt: {
                      type: "string"
                    },
                    focusKeyword: {
                      type: "string"
                    },
                    openGraphTitle: {
                      type: "string"
                    },
                    openGraphDescription: {
                      type: "string"
                    },
                    twitterTitle: {
                      type: "string"
                    },
                    twitterDescription: {
                      type: "string"
                    }
                  },
                  required: ["title", "excerpt", "focusKeyword", "openGraphTitle", "openGraphDescription", "twitterTitle", "twitterDescription"],
                  additionalProperties: false
                }
              },
              required: ["seoMetadata"],
              additionalProperties: false
            }
          }
        }
      }
    }
  })
    @underJSONObjectProperty(by: { key: "choices" })
      @underArrayItem(index: 0)
        @underJSONObjectProperty(by: { path: "message.content" })
          @export(as: "jsonEncodedSeoMetadataResponse")
}
 
query ExtractSeoMetadata
  @depends(on: "GenerateSeoMetadataWithChatGPT")
{
  decodedSeoMetadataResponse: _strDecodeJSONObject(string: $jsonEncodedSeoMetadataResponse)
    @underJSONObjectProperty(by: { path: "seoMetadata" })
      @export(as: "seoMetadata")
}
 
mutation GenerateSeoMetadataAndUpdateYoast(
  $postId: ID!
)
  @depends(on: "ExtractSeoMetadata")
{
  seoMetadataTitle: _objectProperty(
    object: $seoMetadata,
    by: { key: "title" }
  )
  seoMetadataExcerpt: _objectProperty(
    object: $seoMetadata,
    by: { key: "excerpt" }
  )
  seoMetadataFocusKeyword: _objectProperty(
    object: $seoMetadata,
    by: { key: "focusKeyword" }
  )
  seoMetadataOpenGraphTitle: _objectProperty(
    object: $seoMetadata,
    by: { key: "openGraphTitle" }
  )
  seoMetadataOpenGraphDescription: _objectProperty(
    object: $seoMetadata,
    by: { key: "openGraphDescription" }
  )
  seoMetadataTwitterTitle: _objectProperty(
    object: $seoMetadata,
    by: { key: "twitterTitle" }
  )
  seoMetadataTwitterDescription: _objectProperty(
    object: $seoMetadata,
    by: { key: "twitterDescription" }
  )
 
  updateCustomPostMetas(inputs: [
    { id: $postId, key: "_yoast_wpseo_title", value: $__seoMetadataTitle },
    { id: $postId, key: "_yoast_wpseo_metadesc", value: $__seoMetadataExcerpt },
    { id: $postId, key: "_yoast_wpseo_focuskw", value: $__seoMetadataFocusKeyword },
    { id: $postId, key: "_yoast_wpseo_opengraph-title", value: $__seoMetadataOpenGraphTitle },
    { id: $postId, key: "_yoast_wpseo_opengraph-description", value: $__seoMetadataOpenGraphDescription },
    { id: $postId, key: "_yoast_wpseo_twitter-title", value: $__seoMetadataTwitterTitle },
    { id: $postId, key: "_yoast_wpseo_twitter-description", value: $__seoMetadataTwitterDescription }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
query GenerateAndUpdatePostSeoMetadataAndCheckResults($postId: ID!)
  @depends(on: "GenerateSeoMetadataAndUpdateYoast")
{
  postResults: customPost(by: { id: $postId }) {
    id
    ...WithMetaData
  }
}
 
fragment WithMetaData on WithMeta {
  metaTitle: metaValue(key: "_yoast_wpseo_title")
  metaDesc: metaValue(key: "_yoast_wpseo_metadesc")
  focusKeyword: metaValue(key: "_yoast_wpseo_focuskw")
  socialFBTitle: metaValue(key: "_yoast_wpseo_opengraph-title")
  socialFBDesc: metaValue(key: "_yoast_wpseo_opengraph-description")
  socialTwitterTitle: metaValue(key: "_yoast_wpseo_twitter-title")
  socialTwitterDesc: metaValue(key: "_yoast_wpseo_twitter-description")
}

ตัวแปรจะมีลักษณะดังนี้:

{
  "postId": "123",
  "openAIAPIKey": "sk-..."
}