ChatGPT를 사용하여 WooCommerce 상품 설명을 자동으로 개선하기
이 쿼리는 제공된 ID의 WooCommerce 상품을 가져와서 ChatGPT를 사용하여 콘텐츠를 재작성한 후 다시 저장합니다.
(다음 섹션에서는 상품이 생성될 때마다 이 쿼리를 자동으로 실행하는 방법을 설명합니다.)
WooCommerce의 product Custom Post Type은 가이드 Custom Post Types에 대한 액세스 허용에서 설명한 대로 GraphQL 스키마에서 쿼리 가능하도록 설정되어야 합니다.
이를 위해 설정 페이지로 이동하여 「Schema Elements Configuration > Custom Posts」 탭을 클릭하고, 쿼리 가능한 CPT 목록에서 product를 선택하세요(아직 선택되지 않은 경우).
OpenAI API에 연결하려면 API 키를 가진 변수 $openAIAPIKey를 제공해야 합니다.
선택적으로 게시물 콘텐츠를 재작성하기 위한 시스템 메시지와 프롬프트를 제공할 수 있습니다. 제공하지 않을 경우 다음 값이 사용됩니다:
- 시스템 메시지(
$systemMessage): "You are an English Content rewriter and a grammar checker" - 프롬프트(
$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: "
(콘텐츠 문자열은 프롬프트 끝에 추가됩니다.)
또한 변수 $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를 사용하면 새로운 WooCommerce 상품이 생성될 때마다 쿼리를 자동으로 실행할 수 있습니다.
이를 위해 먼저 새 퍼시스티드 쿼리를 생성하고 제목을 "Improve Product Content With ChatGPT"로 설정합니다(이렇게 하면 슬러그 improve-product-content-with-chatgpt가 할당됩니다). 쿼리에는 위의 GraphQL 쿼리를 사용합니다.
그런 다음 애플리케이션의 임의의 위치(예: functions.php 파일, 플러그인, 또는 코드 스니펫)에 다음 PHP 코드를 추가합니다. 이 코드는 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
);