Tự động cải thiện mô tả sản phẩm WooCommerce mới với ChatGPT
Query này lấy sản phẩm WooCommerce theo ID được cung cấp, viết lại nội dung của nó bằng ChatGPT, rồi lưu lại.
(Ở phần tiếp theo, chúng ta sẽ tự động hóa việc thực thi query này mỗi khi một sản phẩm được tạo.)
Custom Post Type product của WooCommerce phải được cho phép truy vấn qua schema GraphQL, như được giải thích trong hướng dẫn Cho phép truy cập vào Custom Post Types.
Để làm điều đó, vào trang Cài đặt, nhấp vào tab "Schema Elements Configuration > Custom Posts", và chọn product từ danh sách các CPT có thể truy vấn (nếu chưa được chọn).
Để 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 điệp hệ thống và prompt để viết lại nội dung bài đăng. Nếu không được cung cấp, các giá trị sau sẽ được sử dụng:
- Thông điệp 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", xem danh sách các mô hình OpenAI) và cung cấp giá trị cho $temperature và $maxCompletionTokens (cả hai đều là null theo mặc định).
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
}
}
}
}Tự động hóa quy trình
Chúng ta có thể sử dụng Internal GraphQL Server để tự động thực thi query mỗi khi một sản phẩm WooCommerce mới được tạo.
Để làm điều này, trước tiên tạo một persisted query mới với tiêu đề "Improve Product Content With ChatGPT" (điều này sẽ gán cho nó slug improve-product-content-with-chatgpt), cùng với query GraphQL ở trên.
Sau đó, ở bất kỳ đâu trong ứng dụng của bạn (ví dụ: trong file functions.php, một plugin, hoặc một đoạn mã code snippet), thêm đoạn mã PHP sau, thực thi query trên 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
);