Thư viện Queries
Thư viện QueriesViết lại nhiều câu sử dụng Claude (Anthropic)

Viết lại nhiều câu sử dụng Claude (Anthropic)

Query này lấy nội dung từ nhiều bài viết, và viết lại những chuỗi đó để cải thiện chúng bằng cách sử dụng Claude của Anthropic.

Để kết nối với API của Anthropic, bạn phải cung cấp biến $apiKey với khóa API.

Bạn có thể ghi đè các đầu vào cho Claude thông qua các biến:

  • $model với bất kỳ mô hình Claude nào được hỗ trợ ("claude-3-5-haiku-latest" theo mặc định)
  • $prompt
  • $maxTokens
query GetPostContent($limit: Int! = 5, $offset: Int! = 0) {
  posts(pagination: {limit: $limit, offset: $offset}, sort: {by: ID, order: ASC}) {
    content
      @export(
        as: "contentItems",
        type: LIST
      )
  }
}
 
query RewriteContentWithClaude(
  $apiKey: String!
  $maxTokens: Int! = 1024
  $prompt: String! = "You are an English Content rewriter and a grammar checker. I have a JSON with text in English. Please rewrite them, 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. If the text contains HTML, do not modify the HTML tags."
  $model: String! = "claude-3-5-haiku-latest"
)
  @depends(on: "GetPostContent")
{
  contentItems: _echo(value: $contentItems)
  encodedContentItems: _arrayEncodeAsJSONString(array: $contentItems)
  prompt: _strAppend(
    after: $prompt,
    append: """
 
Output in JSON format with key: "rewritten".
 
Do not explain why you do what you do. Simply return the JSON.
"""
  )
  jsonPrompt: _strReplaceMultiple(
    search: ["{$encodedContentItems}"],
    replaceWith: [$__encodedContentItems],
    in: """
This is the JSON:
 
{$encodedContentItems}
"""
  )
  claudeResponse: _sendJSONObjectItemHTTPRequest(input: {
    url: "https://api.anthropic.com/v1/messages",
    method: POST,
    options: {
      headers: [
        {
          name: "x-api-key",
          value: $apiKey
        },
        {
          name: "anthropic-version",
          value: "2023-06-01"
        }
      ],
      json: {
        model: $model,
        max_tokens: $maxTokens,
        messages: [
          {
            role: "user",
            content: $__prompt
          },
          {
            role: "assistant",
            content: """{
  "rewritten": ["Salutations, magnificent global landscape!", "Greetings to the expansive realm of existence!"],
}"""
          },
          {
            role: "user",
            content: $__jsonPrompt
          },
          {
            role: "assistant",
            content:  "Here is the JSON requested:"
          },
        ],
      }
    }
  })
    @underJSONObjectProperty(by: { key: "content" })
      @underArrayItem(index: 0)
        @underJSONObjectProperty(by: { path: "text" })
          @export(as: "jsonEncodedRewrittenContent")
}
 
query ExtractRewrittenContent
  @depends(on: "RewriteContentWithClaude")
{
  jsonEncodedRewrittenContent: _echo(value: $jsonEncodedRewrittenContent)
    @remove
  decodedRewrittenContent: _strDecodeJSONObject(string: $jsonEncodedRewrittenContent)
    @remove
  rewrittenContent: _objectProperty(
    object: $__decodedRewrittenContent,
    by: { key: "rewritten" }
  )
}