Queries LibraryReplace all the Lorem Ipsum from a Bricks' template with ChatGPT content
Replace all the Lorem Ipsum from a Bricks' template with ChatGPT content
This query replaces all Lorem Ipsum in the text
elements inserted in a Bricks page from a template with actual content created using ChatGPT.
To create the content, the prompt to ChatGPT provides the title of the heading
element from that same container.
This query requires the Bricks extension to be enabled.
The query requires the following variables:
$customPostId
: The ID of the Bricks custom post to update$openAIAPIKey
: The API key for the 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
}
}
}
}