ไลบรารี Queries
ไลบรารี Queriesแทนที่ Lorem Ipsum ทั้งหมดในเทมเพลตของ Bricks ด้วยเนื้อหาจาก ChatGPT

แทนที่ Lorem Ipsum ทั้งหมดในเทมเพลตของ Bricks ด้วยเนื้อหาจาก ChatGPT

query นี้แทนที่ Lorem Ipsum ทั้งหมดในองค์ประกอบ text ที่แทรกอยู่ในหน้า Bricks จากเทมเพลต ด้วยเนื้อหาจริงที่สร้างขึ้นโดยใช้ ChatGPT

ในการสร้างเนื้อหา พรอมต์ที่ส่งไปยัง ChatGPT จะใช้ชื่อจากองค์ประกอบ heading ในคอนเทนเนอร์เดียวกันนั้น

query นี้ต้องเปิดใช้งาน ส่วนขยาย Bricks

query นี้ต้องใช้ตัวแปรต่อไปนี้:

  • $customPostId: ID ของ Bricks custom post ที่จะอัปเดต
  • $openAIAPIKey: API key สำหรับ 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
      }
    }
  }
}