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
);