使用 ChatGPT 自动改善 WooCommerce 商品描述
此 query 获取指定 ID 的 WooCommerce 商品,使用 ChatGPT 重写其内容,然后重新保存。
(在下一节中,我们将介绍如何在每次创建商品时自动执行此 query。)
WooCommerce 的 product Custom Post Type 必须在 GraphQL schema 中设为可查询,具体方法请参阅指南 允许访问 Custom Post Types。
为此,请前往设置页面,点击「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 商品时自动执行该 query。
为此,首先创建一个新的持久化 query,标题设为 "Improve Product Content With ChatGPT"(系统将自动分配 slug improve-product-content-with-chatgpt),并使用上面的 GraphQL query。
然后,在应用程序的任意位置(例如:functions.php 文件、插件或代码片段中),添加以下 PHP 代码,该代码将在 publish_product 钩子上执行该 query:
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
);