GettextTranslator.Processor.LLM (gettext_translator v0.8.0)

Copy Markdown View Source

LLM processor for the translator behaviour.

Summary

Functions

Builds a TranslateGemma-formatted prompt for translation.

Translates a single message using the configured LLM provider, with optional additional instructions to guide the translation.

Checks if the given model name is a TranslateGemma model.

Functions

build_translategemma_prompt(message, source_code, target_code)

@spec build_translategemma_prompt(String.t(), String.t(), String.t()) :: String.t()

Builds a TranslateGemma-formatted prompt for translation.

TranslateGemma expects a single user message with a specific structure: professional translator persona, followed by two blank lines before the text.

Parameters

  • message - The text to translate
  • source_code - POSIX code of the source language (e.g., "en")
  • target_code - POSIX code of the target language (e.g., "es")

Examples

iex> GettextTranslator.Processor.LLM.build_translategemma_prompt("Hello", "en", "es")
"You are a professional English (en) to Spanish (es) translator. " <>
"Your goal is to accurately convey the meaning and nuances of the original English text " <>
"while adhering to Spanish grammar, vocabulary, and cultural sensitivities.\n" <>
"Produce only the Spanish translation, without any additional explanations or commentary. " <>
"Please translate the following English text into Spanish:\n\n\nHello"

translate(provider, map)

Callback implementation for GettextTranslator.Processor.Translator.translate/2.

translate_single(provider, opts, additional_instructions \\ nil)

@spec translate_single(
  GettextTranslator.Processor.Translator.provider(),
  map(),
  String.t() | nil
) ::
  {:ok, map()} | {:error, any()}

Translates a single message using the configured LLM provider, with optional additional instructions to guide the translation.

Parameters

  • provider - The provider config map from Parser.parse_provider/0
  • opts - Map with :language_code, :message, :type, and optionally :plural_message
  • additional_instructions - Optional string with extra instructions for the LLM

Returns

  • {:ok, %{translation: String.t(), plural_translation: String.t() | nil}}

  • {:error, reason}

translategemma?(model_name)

@spec translategemma?(String.t()) :: boolean()

Checks if the given model name is a TranslateGemma model.

Examples

iex> GettextTranslator.Processor.LLM.translategemma?("translategemma-27b")
true

iex> GettextTranslator.Processor.LLM.translategemma?("gpt-4")
false