Queries-Bibliothek
Queries-BibliothekMehrere Sätze mit DeepSeek übersetzen

Mehrere Sätze mit DeepSeek übersetzen

Diese query ruft den Inhalt mehrerer Beiträge ab und übersetzt diese Strings mit DeepSeek in eine beliebige Sprache.

Um eine Verbindung zur DeepSeek-API herzustellen, musst du die Variable $apiKey mit dem API-Schlüssel angeben.

Du kannst die Variable $model ("deepseek-chat" standardmäßig) mit dem DeepSeek-Modell überschreiben, das du verwenden möchtest.

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 TranslateContentWithDeepSeek(
  $fromLang: String!
  $toLang: String!
  $apiKey: String!
  $systemMessage: String! = "You are a language translator"
  $promptTemplate: String! = """
I'm working on internationalizing my application.
 
I've created a JSON with sentences in {$fromLang}. Please translate the sentences to {$toLang}. If a sentence contains HTML, do not translate inside the HTML tags.
 
Return a JSON with entry "translations", and the translations as an array, in the same order as in the input.
 
This is the JSON:
 
{$encodedContentItems}
"""
  $model: String! = "deepseek-chat"
)
  @depends(on: "GetPostContent")
{
  contentItems: _echo(value: $contentItems)
  encodedContentItems: _arrayEncodeAsJSONString(array: $contentItems)
  prompt: _strReplaceMultiple(
    search: ["{$fromLang}", "{$toLang}", "{$encodedContentItems}"],
    replaceWith: [$fromLang, $toLang, $__encodedContentItems],
    in: $promptTemplate
  )
  authorizationHeader: _sprintf(
    string: "Bearer %s"
    values: [$apiKey]
  )
    @remove
  mistralAIResponse: _sendJSONObjectItemHTTPRequest(input: {
    url: "https://api.deepseek.com/chat/completions",
    method: POST,
    options: {
      headers: [
        {
          name: "Authorization",
          value: $__authorizationHeader
        },
      ],
      json: {
        model: $model,
        messages: [
          {
            role: "system",
            content: $systemMessage
          },
          {
            role: "user",
            content: $__prompt
          },
        ],
        stream: false,
        response_format: {
          type: "json_object"
        }
      }
    }
  })
    @underJSONObjectProperty(by: { key: "choices" })
      @underArrayItem(index: 0)
        @underJSONObjectProperty(by: { path: "message.content" })
          @export(as: "jsonEncodedTranslatedContent")
}
 
query ExtractTranslatedContent
  @depends(on: "TranslateContentWithDeepSeek")
{
  jsonEncodedTranslatedContent: _echo(value: $jsonEncodedTranslatedContent)
    @remove
  decodedTranslatedContent: _strDecodeJSONObject(string: $jsonEncodedTranslatedContent)
    @remove
  translatedContent: _objectProperty(
    object: $__decodedTranslatedContent,
    by: { key: "translations" }
  )
}