Skip to content

Models for LLM translations

GeminiTranslator

AVAILABLE_MODELS = available_models_gemini class-attribute instance-attribute

A base class for LLM-based translators, inheriting from TranslatorBase.

__init__(model_name, target_lang, source_lang=None, prompt_type='default', costum_prompt=None, temperature=0.7, max_tokens=1000)

Initializes the LLMTranslator with a model name, target language, optional source language, and prompt type.

Parameters:

Name Type Description Default
model_name str

The name of the LLM model to use for translation.

required
target_lang str

The target language code for translation (e.g., 'fr' for French).

required
source_lang Optional[str]

The source language code for translation (e.g., 'en' for English). Defaults to None, implying auto-detection will be attempted.

None
prompt_type str

The type of prompt to use for the translation. Defaults to "default".

'default'
costum_prompt Optional[str]

A custom prompt to use if the prompt type is "custom". Defaults to None.

None
temperature float

The temperature for the model's responses. Defaults to 0.7.

0.7
max_tokens int

The maximum number of tokens to generate in the response. Defaults to 1000.

1000

detect_language(text)

Detects the language of the given text using langdetect.

Parameters:

Name Type Description Default
text str

The text whose language is to be detected.

required

Returns:

Type Description
str

The detected language code (e.g., 'en', 'fr').

Raises:

Type Description
ValueError

If the text is empty or invalid for detection.

LangDetectException

If language detection by the langdetect library fails for other reasons (e.g., text too short, no features).

ValueError

If the detected language is not in self.LANGUAGE_CODES.

translate(text)

Translates the given text using the configured LLM.

Orchestrates the translation process by: 1. Validating the input text. 2. Rendering the appropriate prompt (including language detection if needed). 3. Sending the prompt to the LLM via _generate. 4. Post-processing the LLM's response via _post_process.

Parameters:

Name Type Description Default
text str

The text to be translated.

required

Returns:

Type Description
str

The translated text.

Raises:

Type Description
ValueError

If input text validation fails, language detection fails when required, or prompt rendering fails.

translate_batch(texts)

Translate a batch of texts from source language to target language.

Parameters:

Name Type Description Default
texts list

A list of texts to be translated.

required

Returns:

Name Type Description
list list

A list of translated texts.

GPTTranslator

AVAILABLE_MODELS = available_models_openai class-attribute instance-attribute

A base class for LLM-based translators, inheriting from TranslatorBase.

__init__(model_name, target_lang, source_lang=None, prompt_type='default', costum_prompt=None, temperature=0.7, max_tokens=1000)

Initializes the LLMTranslator with a model name, target language, optional source language, and prompt type.

Parameters:

Name Type Description Default
model_name str

The name of the LLM model to use for translation.

required
target_lang str

The target language code for translation (e.g., 'fr' for French).

required
source_lang Optional[str]

The source language code for translation (e.g., 'en' for English). Defaults to None, implying auto-detection will be attempted.

None
prompt_type str

The type of prompt to use for the translation. Defaults to "default".

'default'
costum_prompt Optional[str]

A custom prompt to use if the prompt type is "custom". Defaults to None.

None
temperature float

The temperature for the model's responses. Defaults to 0.7.

0.7
max_tokens int

The maximum number of tokens to generate in the response. Defaults to 1000.

1000

detect_language(text)

Detects the language of the given text using langdetect.

Parameters:

Name Type Description Default
text str

The text whose language is to be detected.

required

Returns:

Type Description
str

The detected language code (e.g., 'en', 'fr').

Raises:

Type Description
ValueError

If the text is empty or invalid for detection.

LangDetectException

If language detection by the langdetect library fails for other reasons (e.g., text too short, no features).

ValueError

If the detected language is not in self.LANGUAGE_CODES.

translate(text)

Translates the given text using the configured LLM.

Orchestrates the translation process by: 1. Validating the input text. 2. Rendering the appropriate prompt (including language detection if needed). 3. Sending the prompt to the LLM via _generate. 4. Post-processing the LLM's response via _post_process.

Parameters:

Name Type Description Default
text str

The text to be translated.

required

Returns:

Type Description
str

The translated text.

Raises:

Type Description
ValueError

If input text validation fails, language detection fails when required, or prompt rendering fails.

translate_batch(texts)

Translate a batch of texts from source language to target language.

Parameters:

Name Type Description Default
texts list

A list of texts to be translated.

required

Returns:

Name Type Description
list list

A list of translated texts.

ClaudeTranslator

AVAILABLE_MODELS = available_models_claude class-attribute instance-attribute

A class for LLM-based translations using Anthropic's Claude models, inheriting from LLMTranslator.

__init__(model_name, target_lang, source_lang=None, prompt_type='default', custom_prompt=None, temperature=0.7, max_tokens=1000)

Initializes the ClaudeTranslator with a model name, target language, optional source language, and prompt type.

Parameters:

Name Type Description Default
model_name str

The name of the Claude model to use for translation.

required
target_lang str

The target language code for translation (e.g., 'fr' for French).

required
source_lang Optional[str]

The source language code for translation (e.g., 'en' for English).

None
prompt_type str

The type of prompt to use for the translation. Defaults to "default".

'default'
custom_prompt Optional[str]

A custom prompt to use if the prompt type is "custom". Defaults to None.

None
temperature float

The temperature for the model's responses. Defaults to 0.7.

0.7
max_tokens int

The maximum number of tokens to generate in the response. Defaults to 1000.

1000

detect_language(text)

Detects the language of the given text using langdetect.

Parameters:

Name Type Description Default
text str

The text whose language is to be detected.

required

Returns:

Type Description
str

The detected language code (e.g., 'en', 'fr').

Raises:

Type Description
ValueError

If the text is empty or invalid for detection.

LangDetectException

If language detection by the langdetect library fails for other reasons (e.g., text too short, no features).

ValueError

If the detected language is not in self.LANGUAGE_CODES.

translate(text)

Translates the given text using the configured LLM.

Orchestrates the translation process by: 1. Validating the input text. 2. Rendering the appropriate prompt (including language detection if needed). 3. Sending the prompt to the LLM via _generate. 4. Post-processing the LLM's response via _post_process.

Parameters:

Name Type Description Default
text str

The text to be translated.

required

Returns:

Type Description
str

The translated text.

Raises:

Type Description
ValueError

If input text validation fails, language detection fails when required, or prompt rendering fails.

translate_batch(texts)

Translate a batch of texts from source language to target language.

Parameters:

Name Type Description Default
texts list

A list of texts to be translated.

required

Returns:

Name Type Description
list list

A list of translated texts.

OllamaTranslator

AVAILABLE_MODELS = [] if os.environ.get('TEST_OLLAMA_LIST') else [model_obj.model for model_obj in ollama.list()['models']] class-attribute instance-attribute

A class for LLM-based translations using local Ollama models, inheriting from LLMTranslator.

__init__(model_name, target_lang, source_lang=None, prompt_type='default', costum_prompt=None, temperature=0.7, max_tokens=1000)

Initializes the OllamaTranslator.

Parameters:

Name Type Description Default
model_name str

The name of the Ollama model to use (e.g., 'llama3', 'mistral').

required
target_lang str

The target language code (e.g., 'fr' for French).

required
source_lang Optional[str]

The source language code (e.g., 'en' for English).

None
prompt_type str

The type of prompt to use. Defaults to "default".

'default'
costum_prompt Optional[str]

A custom prompt to use if the prompt type is "custom". Defaults to None.

None
temperature float

The temperature for model responses. Defaults to 0.7.

0.7
max_tokens int

Max tokens for the response (maps to 'num_predict'). Defaults to 1000.

1000

detect_language(text)

Detects the language of the given text using langdetect.

Parameters:

Name Type Description Default
text str

The text whose language is to be detected.

required

Returns:

Type Description
str

The detected language code (e.g., 'en', 'fr').

Raises:

Type Description
ValueError

If the text is empty or invalid for detection.

LangDetectException

If language detection by the langdetect library fails for other reasons (e.g., text too short, no features).

ValueError

If the detected language is not in self.LANGUAGE_CODES.

translate(text)

Translates the given text using the configured LLM.

Orchestrates the translation process by: 1. Validating the input text. 2. Rendering the appropriate prompt (including language detection if needed). 3. Sending the prompt to the LLM via _generate. 4. Post-processing the LLM's response via _post_process.

Parameters:

Name Type Description Default
text str

The text to be translated.

required

Returns:

Type Description
str

The translated text.

Raises:

Type Description
ValueError

If input text validation fails, language detection fails when required, or prompt rendering fails.

translate_batch(texts)

Translate a batch of texts from source language to target language.

Parameters:

Name Type Description Default
texts list

A list of texts to be translated.

required

Returns:

Name Type Description
list list

A list of translated texts.