Bibliothèque de queries
Bibliothèque de queriesRemplacer tout le Lorem Ipsum d'un modèle Bricks avec du contenu ChatGPT

Remplacer tout le Lorem Ipsum d'un modèle Bricks avec du contenu ChatGPT

Cette requête remplace tout le Lorem Ipsum dans les éléments text insérés dans une page Bricks depuis un modèle avec du contenu réel créé à l'aide de ChatGPT.

Pour créer le contenu, le prompt envoyé à ChatGPT fournit le titre de l'élément heading de ce même conteneur.

Cette requête nécessite que l'extension Bricks soit activée.

La requête nécessite les variables suivantes :

  • $customPostId : L'ID du post personnalisé Bricks à mettre à jour
  • $openAIAPIKey : La clé API pour l'API OpenAI
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
      }
    }
  }
}