Pustaka queriesGanti semua Lorem Ipsum dari template Bricks dengan konten ChatGPT
Ganti semua Lorem Ipsum dari template Bricks dengan konten ChatGPT
Query ini mengganti semua Lorem Ipsum pada elemen text yang disisipkan di halaman Bricks dari sebuah template dengan konten nyata yang dibuat menggunakan ChatGPT.
Untuk membuat konten, prompt ke ChatGPT menyertakan judul elemen heading dari container yang sama.
Query ini memerlukan ekstensi Bricks untuk diaktifkan.
Query ini memerlukan variabel berikut:
$customPostId: ID custom post Bricks yang akan diperbarui$openAIAPIKey: Kunci API untuk OpenAI API
query ExportGlobalVariables
@configureWarningsOnExportingDuplicateVariable(enabled: false)
{
emptyArray: _echo(value: [])
@export(as: "containerIDTitlesList")
emptyObject: _echo(value: {})
@export(as: "containerIDTitles")
}
query GetBricksHeadingTitles($customPostId: ID!)
@depends(on: "ExportGlobalVariables")
{
customPostForContainerHeadings: customPost(by:{ id: $customPostId }, status: any) {
id
title
bricksDataWithContainerTitles: bricksData(filterBy: { include: ["heading"] })
@underEachArrayItem(
passValueOnwardsAs: "elementJSON"
affectDirectivesUnderPos: [1, 2, 3]
)
@applyField(
name: "_objectProperty",
arguments: {
object: $elementJSON
by: { key: "parent" }
failIfNonExistingKeyOrPath: false
},
passOnwardsAs: "elementContainerID"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $elementJSON
by: { path: "settings.text" }
failIfNonExistingKeyOrPath: false
},
passOnwardsAs: "elementSettingsText"
)
@applyField(
name: "_objectAddEntry",
arguments: {
object: $containerIDTitles,
key: $elementContainerID,
value: $elementSettingsText
}
setResultInResponse: true
)
@export(as: "containerIDTitlesList")
}
}
query ExtractBricksHeadingTitles
@depends(on: "GetBricksHeadingTitles")
{
containerIDTitles: _echo(value: $containerIDTitlesList)
@applyField(
name: "_objectMerge",
arguments: {
objects: $containerIDTitlesList
}
setResultInResponse: true
)
@export(as: "containerIDTitles")
}
query GetBricksTextItems($customPostId: ID!)
@depends(on: "ExtractBricksHeadingTitles")
{
customPostForText: customPost(by:{ id: $customPostId }, status: any) {
id
title
bricksDataWithReplacedText: bricksData(filterBy: { include: ["text"] })
@underEachArrayItem(
passValueOnwardsAs: "elementJSON"
affectDirectivesUnderPos: [1, 2, 3, 4]
)
@applyField(
name: "_objectProperty",
arguments: {
object: $elementJSON
by: { key: "parent" }
failIfNonExistingKeyOrPath: false
},
passOnwardsAs: "elementContainerID"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $containerIDTitles,
by: { key: $elementContainerID }
failIfNonExistingKeyOrPath: false
},
passOnwardsAs: "elementContainerTitle"
)
@applyField(
name: "_notNull",
arguments: {
value: $elementContainerTitle
}
passOnwardsAs: "hasElementContainerTitle"
)
@if(
condition: $hasElementContainerTitle
affectDirectivesUnderPos: [1, 3]
)
@underJSONObjectProperty(
by: { key: "id" }
)
@export(as: "elementToUpdateIDs")
@underJSONObjectProperty(
by: { path: "settings.text" }
failIfNonExistingKeyOrPath: false
affectDirectivesUnderPos: [1, 2]
)
@applyField(
name: "_echo",
arguments: {
value: $elementContainerTitle
}
setResultInResponse: true
)
@export(as: "elementToUpdateTexts")
}
}
query CreateDescriptionsWithChatGPT(
$openAIAPIKey: String!
$systemMessage: String! = "You are a content writer"
$promptTemplate: String! = """
I'm working on writing content for a Bricks page.
I've created an array with titles. Please create descriptions for each title.
Keep the array indexes identical, create the descriptions only.
This is the JSON:
{$encodedContentItems}
"""
$model: String! = "gpt-4o-mini"
)
@depends(on: "GetBricksTextItems")
{
encodedContentItems: _arrayEncodeAsJSONString(array: $elementToUpdateTexts)
prompt: _strReplaceMultiple(
search: ["{$encodedContentItems}"],
replaceWith: [$__encodedContentItems],
in: $promptTemplate
)
openAIResponse: _sendJSONObjectItemHTTPRequest(input: {
url: "https://api.openai.com/v1/chat/completions",
method: POST,
options: {
auth: {
password: $openAIAPIKey
},
json: {
model: $model,
messages: [
{
role: "system",
content: $systemMessage
},
{
role: "user",
content: $__prompt
},
],
response_format: {
type: "json_schema",
json_schema: {
name: "content_response",
strict: true,
schema: {
type: "object",
properties: {
descriptions: {
type: "array",
items: {
type: "string"
}
}
},
required: ["descriptions"],
additionalProperties: false
}
}
}
}
}
})
@underJSONObjectProperty(by: { key: "choices" })
@underArrayItem(index: 0)
@underJSONObjectProperty(by: { path: "message.content" })
@export(as: "jsonEncodedCreatedDescriptions")
}
query ExtractCreatedDescriptions
@depends(on: "CreateDescriptionsWithChatGPT")
{
jsonEncodedCreatedDescriptions: _echo(value: $jsonEncodedCreatedDescriptions)
@remove
decodedCreatedDescriptions: _strDecodeJSONObject(string: $jsonEncodedCreatedDescriptions)
@remove
createdDescriptions: _objectProperty(
object: $__decodedCreatedDescriptions,
by: { key: "descriptions" }
)
@export(as: "createdDescriptions")
}
query GetElementToUpdateData
@depends(on: "ExtractCreatedDescriptions")
{
elementToUpdateIDs: _echo(value: $elementToUpdateIDs)
elementToUpdateTexts: _echo(value: $createdDescriptions)
elementToUpdateMergeInputElements: _echo(value: $createdDescriptions)
@underEachArrayItem(
passIndexOnwardsAs: "index",
passValueOnwardsAs: "elementToUpdateText"
affectDirectivesUnderPos: [1, 2]
)
@applyField(
name: "_arrayItem",
arguments: {
array: $elementToUpdateIDs,
position: $index
},
passOnwardsAs: "elementToUpdateID"
)
@applyField(
name: "_echo",
arguments: {
value: {
id: $elementToUpdateID,
settings: {
text: $elementToUpdateText
}
}
}
setResultInResponse: true
)
@export(as: "elementToUpdateMergeInputElements")
}
mutation StoreUpdatedElementText($customPostId: ID!)
@depends(on: "GetElementToUpdateData")
{
bricksMergeCustomPostElementDataItem(input: {
customPostID: $customPostId
elements: $elementToUpdateMergeInputElements
}) {
status
errors {
__typename
...on ErrorPayload {
message
@passOnwards(as: "message")
@fail(
message: $message
condition: ALWAYS
)
}
}
customPost {
__typename
...on CustomPost {
id
bricksData
}
}
}
}