fabricatio_translate.capabilities.translate

Translate capability for translating text using LLM.

This module provides the Translate class, which utilizes the UseLLM base class to provide translation functionality. It renders a translation template and executes it via the LLM infrastructure.

Classes

Translate

A translation class that uses LLM for translating text.

Functions

fill_empty(→ List[str])

Fill empty translations.

Module Contents

fabricatio_translate.capabilities.translate.fill_empty(source: List[str], translated: List[str] | List[str | None] | None) List[str]

Fill empty translations.

class fabricatio_translate.capabilities.translate.Translate(/, **data: Any)

Bases: fabricatio_core.capabilities.usages.UseLLM

A translation class that uses LLM for translating text.

This class leverages the UseLLM base class to perform translations by rendering a translation template and executing it using the LLM infrastructure.

async translate(text: str, target_language: str, specification: str = '', **kwargs: Unpack[fabricatio_core.models.kwargs_types.ValidateKwargs[str]]) None | str
async translate(text: List[str], target_language: str, specification: str = '', **kwargs: Unpack[fabricatio_core.models.kwargs_types.ValidateKwargs[str]]) None | List[str] | List[str | None]

Translate the provided text into the target language.

Parameters:
  • text (str | List[str]) – The input text or list of texts to be translated.

  • target_language (str) – The language into which the text should be translated.

  • specification (str) – The translation specification.

  • **kwargs (Unpack[ValidateKwargs[str]]) – Additional keyword arguments for customization.

Returns:

Translated text(s), with possible None values if translation fails for any item in the list.

Return type:

None | str | List[str] | List[str | None]

async translate_chunked(text: str, target_language: str, chunk_size: int = 6000, specification: str = '', **kwargs: Unpack[fabricatio_core.models.kwargs_types.ValidateKwargs[str]]) None | str
async translate_chunked(text: List[str], target_language: str, chunk_size: int = 6000, specification: str = '', **kwargs: Unpack[fabricatio_core.models.kwargs_types.ValidateKwargs[str]]) None | List[str] | List[str | None]

Translate the provided text into the target language in a chunked manner.

Parameters:
  • text – The input text or list of texts to be translated.

  • target_language – The target language for translation.

  • chunk_size – Maximum size of each text chunk.

  • specification – Additional translation instructions.

  • **kwargs – Additional arguments for the translation.

Returns:

Translated text(s) in the same format as input (str or List[str]). Returns None if translation fails.

Warning

the chunk_size is in unit of words, not characters.