ไลบรารี Queries
ไลบรารี Queriesปรับปรุงคำอธิบายของสินค้า WooCommerce ใหม่โดยอัตโนมัติด้วย ChatGPT

ปรับปรุงคำอธิบายของสินค้า WooCommerce ใหม่โดยอัตโนมัติด้วย ChatGPT

query นี้จะดึงสินค้า WooCommerce ที่มี ID ตามที่กำหนด เขียนเนื้อหาของสินค้านั้นใหม่ด้วย ChatGPT แล้วจัดเก็บอีกครั้ง

(ในส่วนถัดไป เราจะทำให้การรัน query นี้เป็นแบบอัตโนมัติทุกครั้งที่มีการสร้างสินค้า)

product Custom Post Type ของ WooCommerce จะต้องสามารถ query ผ่าน GraphQL schema ได้ ตามที่อธิบายไว้ในคู่มือ การอนุญาตให้เข้าถึง Custom Post Types

ในการทำเช่นนั้น ให้ไปที่หน้า Settings คลิกที่แท็บ "Schema Elements Configuration > Custom Posts" และเลือก product จากรายการ CPT ที่สามารถ query ได้ (หากยังไม่ได้เลือกไว้)

ในการเชื่อมต่อกับ OpenAI API คุณจะต้องระบุตัวแปร $openAIAPIKey พร้อมกับ API key

คุณสามารถเลือกที่จะระบุ system message และ prompt เพื่อเขียนเนื้อหาของโพสต์ใหม่ได้ หากไม่ได้ระบุ จะใช้ค่าต่อไปนี้:

  • System message ($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: "

(สตริงเนื้อหาจะถูกต่อท้าย prompt)

นอกจากนี้ คุณยังสามารถแทนที่ค่าเริ่มต้นของตัวแปร $model ("gpt-4o-mini" ดูรายการโมเดลของ OpenAI) และระบุค่าให้กับ $temperature และ $maxCompletionTokens (ทั้งคู่มีค่าเริ่มต้นเป็น null) ได้

query GetProductContent(
  $productId: ID!
) {
  customPost(by: { id: $productId }, customPostTypes: "product", status: any) {
    content
      @export(as: "content")
  }
}
 
query RewriteProductContentWithChatGPT(
  $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: "GetProductContent")
{
  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")
}
 
mutation UpdateProduct(
  $productId: ID!
)
  @depends(on: "RewriteProductContentWithChatGPT")
{
  updateCustomPost(input: {
    id: $productId,
    customPostType: "product"
    contentAs: {
      html: $rewrittenContent
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        content
      }
    }
  }
}

การทำให้กระบวนการเป็นแบบอัตโนมัติ

เราสามารถใช้ Internal GraphQL Server เพื่อรัน query โดยอัตโนมัติทุกครั้งที่มีการสร้างสินค้า WooCommerce ใหม่

ในการทำเช่นนี้ ขั้นแรกให้ สร้าง persisted query ใหม่ โดยตั้งชื่อว่า "Improve Product Content With ChatGPT" (ซึ่งจะกำหนด slug improve-product-content-with-chatgpt ให้) และใช้ GraphQL query ด้านบน

จากนั้น ที่ใดก็ได้ในแอปพลิเคชันของคุณ (เช่น ในไฟล์ functions.php ปลั๊กอิน หรือ code snippet) ให้เพิ่มโค้ด PHP ต่อไปนี้ ซึ่งจะรัน query บน hook publish_product:

use GatoGraphQL\InternalGraphQLServer\GraphQLServer;
 
add_action(
  'publish_product',
  function (int $productId, WP_Post $post, string $oldStatus): void {
    // Only execute when it's a newly-published product
    if ($oldStatus === 'publish') {
      return;
    }
 
    GraphQLServer::executePersistedQuery('improve-product-content-with-chatgpt', [
      'productId' => $productId,
 
      // Provide your Open AI's API Key
      'openAIAPIKey' => '{ OPENAI_API_KEY }',
 
      // Customize any of the other variables, for instance:
      'maxCompletionTokens' => 5000,
    ]);
  }, 10, 3
);