Rust API reference · Kjarni 0.2.8

kjarni::translator

High-level translation API.

Quick Start

use kjarni::translator;

// One-liner
let german = translator::translate("flan-t5-base", "Hello!", "en", "de").await?;

// Reusable instance
let t = Translator::new("flan-t5-base").await?;
let german = t.translate("Hello!", "english", "german").await?;
let french = t.translate("Hello!", "en", "fr").await?;

// With defaults for batch translation
let t = Translator::builder("flan-t5-base")
    .from("english")
    .to("german")
    .build()
    .await?;

for text in texts {
    let translated = t.translate_default(&text).await?;
}

Streaming

use futures::StreamExt;

let mut stream = translator.stream("Long text...", "en", "de").await?;
while let Some(chunk) = stream.next().await {
    print!("{}", chunk?);
}

Supported Languages

English, German, French, Spanish, Italian, Portuguese, Dutch, Russian, Chinese, Japanese, Korean, Arabic, Hindi, Turkish, Polish, Romanian

Languages can be specified as ISO codes ("en", "de") or names ("English", "German").

Translator

pub struct Translator

High-level translation API

new

pub async fn new(model: &str) -> TranslatorResult<Self>

Create a Translator with default settings.

builder

pub fn builder(model: &str) -> TranslatorBuilder

Create a builder for custom configuration.

translate

pub async fn translate(&self, text: &str, from: &str, to: &str) -> TranslatorResult<String>

Translate text with explicit source and target languages

translate_with_config

pub async fn translate_with_config(&self, text: &str, from: &str, to: &str, overrides: &Seq2SeqOverrides) -> TranslatorResult<String>

Translate with custom generation overrides.

translate_default

pub async fn translate_default(&self, text: &str) -> TranslatorResult<String>

Translate using default languages set via builder

translate_to

pub async fn translate_to(&self, text: &str, to: &str) -> TranslatorResult<String>

Translate to a specific language using default source language.

translate_from

pub async fn translate_from(&self, text: &str, from: &str) -> TranslatorResult<String>

Translate from a specific language using default target language.

stream

pub async fn stream(&self, text: &str, from: &str, to: &str) -> TranslatorResult<std::pin::Pin<Box<dyn Stream<Item = TranslatorResult<String>> + Send>>>

Stream translation with explicit languages.

stream_default

pub async fn stream_default(&self, text: &str) -> TranslatorResult<std::pin::Pin<Box<dyn Stream<Item = TranslatorResult<String>> + Send>>>

Stream translation using default languages.

model_type

pub fn model_type(&self) -> ModelType

Get the model type.

model_name

pub fn model_name(&self) -> &str

Get the model's CLI name.

device

pub fn device(&self) -> Device

Get the device the model is running on.

default_from

pub fn default_from(&self) -> Option<&str>

Get the default source language (if set).

default_to

pub fn default_to(&self) -> Option<&str>

Get the default target language (if set).

generator

pub fn generator(&self) -> &Seq2SeqGenerator

Get a reference to the underlying generator.

TranslatorBuilder

pub struct TranslatorBuilder

Builder for configuring a Translator instance.

Example

use kjarni::translator::Translator;

let translator = Translator::builder("flan-t5-base")
    .from("english")
    .to("german")
    .build()
    .await?;

new

pub fn new(model: impl Into<String>) -> Self

Create a new builder for the specified model.

from_preset

pub fn from_preset(preset: &TranslatorPreset) -> Self

Create a builder from a preset.

from

pub fn from(mut self, lang: impl Into<String>) -> Self

Set default source language.

to

pub fn to(mut self, lang: impl Into<String>) -> Self

Set default target language.

languages

pub fn languages(self, from: impl Into<String>, to: impl Into<String>) -> Self

Set both default languages.

device

pub fn device(mut self, device: KjarniDevice) -> Self

Set the device for inference.

cpu

pub fn cpu(self) -> Self

Use CPU for inference.

gpu

pub fn gpu(self) -> Self

Use GPU for inference.

with_context

pub fn with_context(mut self, context: Arc<WgpuContext>) -> Self

Provide a pre-created GPU context.

num_beams

pub fn num_beams(mut self, n: usize) -> Self

Set the number of beams for beam search.

max_length

pub fn max_length(mut self, len: usize) -> Self

Set the maximum output length.

min_length

pub fn min_length(mut self, len: usize) -> Self

Set the minimum output length.

length_penalty

pub fn length_penalty(mut self, penalty: f32) -> Self

Set length penalty for beam search.

no_repeat_ngram_size

pub fn no_repeat_ngram_size(mut self, size: usize) -> Self

Set no-repeat n-gram size.

repetition_penalty

pub fn repetition_penalty(mut self, penalty: f32) -> Self

Set repetition penalty.

greedy

pub fn greedy(mut self) -> Self

high_quality

pub fn high_quality(mut self) -> Self

cache_dir

pub fn cache_dir(mut self, path: impl Into<PathBuf>) -> Self

download_policy

pub fn download_policy(mut self, policy: DownloadPolicy) -> Self

offline

pub fn offline(mut self) -> Self

Never download models automatically.

quiet

pub fn quiet(mut self) -> Self

build

pub async fn build(self) -> TranslatorResult<Translator>

Build the Translator instance.

TranslatorPreset

pub struct TranslatorPreset

A translation model preset.

Fields

  • pub name: &'static str,: Preset name.
  • pub model: &'static str,: CLI model name.
  • pub architecture: &'static str,: Model architecture.
  • pub supported_languages: &'static [&'static str],: Languages known to work well.
  • pub recommended_device: KjarniDevice,: Recommended device.
  • pub memory_mb: usize,: Approximate memory in MB.
  • pub description: &'static str,: Description.

TranslatorError

pub enum TranslatorError

Errors that can occur during translation.

Variants

  • UnknownModel(String),: Unknown model name.
  • IncompatibleModel: Model incompatible with translation.
  • UnknownLanguage(String),: Unknown or unsupported language.
  • MissingLanguage,: Missing required language specification.
  • TranslationFailed(#[source] anyhow::Error),: Translation failed.
  • Seq2Seq(#[from] Seq2SeqError),: Underlying seq2seq error.

TranslatorResult

pub type TranslatorResult<T> = Result<T, TranslatorError>

Result type for translation operations.

TranslatorTier

pub enum TranslatorTier

Tier-based preset selection.

Variants

  • Fast,: Fast, smaller model.
  • Quality,: Higher quality, larger model.

resolve

pub fn resolve(&self) -> &'static TranslatorPreset

Resolve tier to default preset.

available_models

pub fn available_models() -> Vec<&'static str>

List available translation models.

is_translation_model

pub fn is_translation_model(model: &str) -> TranslatorResult<()>

Check if a model can be used for translation.

normalize_language

pub fn normalize_language(input: &str) -> Option<&'static str>

Normalize language input to the canonical form used in prompts.

Accepts:

  • ISO codes: "en", "de", "fr"
  • Full names: "English", "German", "French"
  • Lowercase names: "english", "german", "french"

Returns the canonical name (e.g., "English") or None if unknown.

supported_languages

pub fn supported_languages() -> Vec<&'static str>

List all supported languages.

translate

pub async fn translate(model: &str, text: &str, from: &str, to: &str) -> TranslatorResult<String>

Translate text with explicit languages.

Example

let german = kjarni::translator::translate("flan-t5-base", "Hello", "en", "de").await?;

translate_preset

pub async fn translate_preset(preset: &TranslatorPreset, text: &str, from: &str, to: &str) -> TranslatorResult<String>

Translate using a preset.

translate_tier

pub async fn translate_tier(tier: TranslatorTier, text: &str, from: &str, to: &str) -> TranslatorResult<String>

Translate using a tier.

translator::presets

Versioned presets for translation models.

ALL_V1_PRESETS

pub const ALL_V1_PRESETS: &[&TranslatorPreset] = &[&TRANSLATION_FAST_V1, &TRANSLATION_QUALITY_V1]

All V1 presets.

TRANSLATION_FAST_V1

pub const TRANSLATION_FAST_V1: TranslatorPreset = TranslatorPreset

Fast translation with FLAN-T5 Base.

TRANSLATION_QUALITY_V1

pub const TRANSLATION_QUALITY_V1: TranslatorPreset = TranslatorPreset

High-quality translation with FLAN-T5 Large.

find_preset

pub fn find_preset(name: &str) -> Option<&'static TranslatorPreset>

Find a preset by name.

Kjarni runs the same engine on every platform: