Yeni bir WooCommerce ürününün açıklamasını ChatGPT ile otomatik olarak iyileştirme
Bu query, sağlanan ID ile WooCommerce ürününü getirir, içeriğini ChatGPT kullanarak yeniden yazar ve tekrar kaydeder.
(Bir sonraki bölümde, bu query'nin yürütülmesini, ürün oluşturulduğunda otomatik olarak gerçekleşecek şekilde ayarlayacağız.)
WooCommerce'in product Custom Post Type'ının, Custom Post Types'a erişime izin verme kılavuzunda açıklandığı gibi GraphQL şeması üzerinden sorgulanabilir hale getirilmesi gerekir.
Bunun için Ayarlar sayfasına gidin, "Schema Elements Configuration > Custom Posts" sekmesine tıklayın ve sorgulanabilir CPT listesinden product öğesini seçin (zaten seçili değilse).
OpenAI API'sine bağlanmak için $openAIAPIKey değişkenini API anahtarıyla sağlamanız gerekir.
İsteğe bağlı olarak, gönderi içeriğini yeniden yazmak için sistem mesajı ve prompt sağlayabilirsiniz. Sağlanmazsa aşağıdaki değerler kullanılır:
- Sistem mesajı (
$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: "
(İçerik dizesi, prompt'un sonuna eklenir.)
Ayrıca $model değişkeninin varsayılan değerini ("gpt-4o-mini", OpenAI modelleri listesine bakın) geçersiz kılabilir ve $temperature ile $maxCompletionTokens için değerler sağlayabilirsiniz (her ikisi de varsayılan olarak null'dır).
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
}
}
}
}Süreci otomatikleştirme
Yeni bir WooCommerce ürünü oluşturulduğunda query'yi otomatik olarak yürütmek için Internal GraphQL Server kullanabiliriz.
Bunu yapmak için önce "Improve Product Content With ChatGPT" başlığıyla yeni bir persisted query oluşturun (bu, ona improve-product-content-with-chatgpt slug'ını atayacaktır) ve yukarıdaki GraphQL query'sini ekleyin.
Ardından uygulamanızın herhangi bir yerinde (örneğin functions.php dosyanızda, bir eklentide veya bir kod parçacığında) aşağıdaki PHP kodunu ekleyin; bu kod, publish_product hook'unda query'yi çalıştırır:
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
);