### Start LTEngine Server Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/README.md Starts the LTEngine server with a specified model. Ensure the model is downloaded or accessible. ```bash # Start server ./ltengine -m gemma3-4b ``` -------------------------------- ### PromptBuilder Initialization Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Initializes a new `PromptBuilder` instance. This is the starting point for constructing complex prompts. ```rust use ltengine::prompt_builder::PromptBuilder; let builder = PromptBuilder::new(); ``` -------------------------------- ### LLM Initialization Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/llm.md Example demonstrating how to initialize a new LLM instance with a specified model path, CPU/GPU preference, and verbosity. ```rust use std::path::PathBuf; let model_path = PathBuf::from("/models/gemma-3-4b-it-q4_0.gguf"); let llm = LLM::new(model_path, false, true)?; // llm is ready for inference ``` -------------------------------- ### Get Frontend Settings Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Fetches configuration settings relevant for the frontend application. This may include default languages or other UI-related parameters. ```bash curl http://localhost:8000/frontend/settings ``` -------------------------------- ### Development/Testing Configuration Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Quick local testing setup using the smallest model and verbose logging. ```bash ./ltengine --host 127.0.0.1 -m gemma3-1b -v ``` -------------------------------- ### LLM Context Creation Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/llm.md Example showing how to create a new LLM context with a capacity of 512 tokens after initializing the LLM. ```rust let llm = LLM::new(model_path, false, false)?; let ctx = llm.create_context(512)?; // Context can now process up to 512 tokens ``` -------------------------------- ### Usage Example for PromptBuilder::new() Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Demonstrates how to create an instance of PromptBuilder using the new() method. ```rust let pb = PromptBuilder::new(); ``` -------------------------------- ### LLM Prompt Execution Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Illustrates running a prompt through the LLM to get an inference result. This is the core function for text generation. ```rust use ltengine::llm::LLM; let mut llm = LLM::new(config).expect("Failed to initialize LLM"); let mut context = llm.create_context().expect("Failed to create LLM context"); let prompt = "Translate 'hello' to Spanish."; let result = llm.run_prompt(&mut context, prompt).expect("LLM inference failed"); ``` -------------------------------- ### Example: Setting Output Format to HTML Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Shows how to use set_format to prepare the builder for generating HTML prompts. ```rust let mut pb = PromptBuilder::new(); pb.set_format("html"); // Now builds prompts for HTML content ``` -------------------------------- ### HuggingFace Struct Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/models.md An example demonstrating how to instantiate the HuggingFace struct with repository and model details. ```rust let hf = HuggingFace { repo: "libretranslate/gemma3", model: "gemma-3-4b-it-q4_0.gguf" }; ``` -------------------------------- ### JavaScript Async/Await Example for Translation Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Demonstrates how to perform translation using JavaScript with async/await syntax. This example shows a typical POST request to the /translate endpoint. ```javascript async function translateText() { const url = "http://localhost:8000/translate"; const data = { q: "Hello, world!", source: "en", target: "es", }; try { const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const result = await response.json(); console.log("Translation:", result.translatedText); } catch (error) { console.error("Translation failed:", error); } } translateText(); ``` -------------------------------- ### Start with API Key Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/README.md Initialize the translation engine with a required API key for authentication. This key should be kept secret. ```bash ./ltengine --api-key my-secret-key-123 ``` -------------------------------- ### PromptBuilder Usage Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/types.md Demonstrates how to instantiate and configure a PromptBuilder to create a Prompt object for translation. ```rust let mut pb = PromptBuilder::new(); pb.set_source_language("English") .set_target_language("Spanish") .set_format("text"); let prompt = pb.build(&"Hello world".to_string()); ``` -------------------------------- ### Example: Auto-detect Source Language Prompt Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md An example of a user prompt generated when the source language is auto-detected and the target is Spanish. ```rust Translate the text below to Spanish. Text: Hello world Spanish: ``` -------------------------------- ### Example: Specified Source Language Prompt Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md An example of a user prompt generated when the source language is specified as English and the target is Spanish. ```rust Translate the text below from English to Spanish. English: Hello world Spanish: ``` -------------------------------- ### Get Supported Languages Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Retrieves a list of all languages supported by the LibreTranslate API. This is useful for populating language selection dropdowns. ```bash curl http://localhost:8000/languages ``` -------------------------------- ### LLM Initialization Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Shows how to initialize the LLM struct. This typically involves providing configuration parameters for the language model. ```rust use ltengine::llm::LLM; let mut llm = LLM::new(config).expect("Failed to initialize LLM"); ``` -------------------------------- ### High-Performance Setup Configuration Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Configuration for maximum performance using the largest model with GPU acceleration on a custom port. ```bash ./ltengine --port 8080 -m gemma3-27b ``` -------------------------------- ### Example: Setting Source Language Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Demonstrates setting a specific source language like 'Spanish' using set_source_language. ```rust let mut pb = PromptBuilder::new(); // Assuming language struct with name "Spanish" pb.set_source_language("Spanish"); ``` -------------------------------- ### Server Health Check Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md A simple example to check the health status of the LibreTranslate server. This typically involves a GET request to a health endpoint. ```bash curl http://localhost:8000/health ``` -------------------------------- ### API Key Authentication Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Shows how to authenticate requests using an API key. The key should be passed in the 'Authorization' header. ```bash curl -X POST "http://localhost:8000/translate" -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_API_KEY" -d '{"q": "Secret message", "source": "en", "target": "fr"}' ``` -------------------------------- ### GET /frontend/settings Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/INDEX.md Retrieves the current server configuration settings. ```APIDOC ## GET /frontend/settings ### Description Retrieves the server's frontend configuration settings. ### Method GET ### Endpoint /frontend/settings ### Response #### Success Response (200) - **settings** (object) - An object containing various frontend settings. - **default_language** (string) - The default language code. - **default_source_language** (string) - The default source language code. - **default_target_language** (string) - The default target language code. - **api_key_required** (boolean) - Whether an API key is required for certain operations. - **custom_terms** (boolean) - Whether custom terms are enabled. #### Response Example ```json { "settings": { "default_language": "en", "default_source_language": "en", "default_target_language": "es", "api_key_required": false, "custom_terms": false } } ``` ``` -------------------------------- ### LLM Initialization and Processing Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/llm.md Demonstrates how to initialize the LLM, create a context, process tokenized input, and print the generated output. Ensure the `tokens` vector is populated with actual token IDs. ```rust let llm = LLM::new(model_path, false, false)?; let mut ctx = llm.create_context(512)?; // Tokenized input (normally from model.str_to_token()) let tokens = vec![/* token IDs */]; let output = ctx.process(tokens)?; println!("Generated: {}", output); ``` -------------------------------- ### GET /frontend/settings Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md Retrieves the frontend configuration settings. ```APIDOC ## GET /frontend/settings ### Description Retrieves the frontend configuration settings, including character limits, default languages, and other hardcoded settings. ### Method GET ### Endpoint /frontend/settings ### Response #### Success Response (200 OK) - **apiKeys** (boolean) - Whether API keys are required. - **charLimit** (integer) - The character limit for translations. - **filesTranslation** (boolean) - Whether file translation is supported. - **frontendTimeout** (integer) - The frontend timeout in milliseconds. - **keyRequired** (boolean) - Whether an API key is required. - **language** (object) - Default source and target languages for the frontend. - **source** (object) - **code** (string) - Source language code. - **name** (string) - Source language name. - **target** (object) - **code** (string) - Target language code. - **name** (string) - Target language name. - **suggestions** (boolean) - Whether suggestions are supported. - **supportedFilesFormat** (array of strings) - List of supported file formats for translation. #### Response Example ```json { "apiKeys": false, "charLimit": 5000, "filesTranslation": false, "frontendTimeout": 1000, "keyRequired": false, "language": { "source": { "code": "auto", "name": "Auto Detect" }, "target": { "code": "en", "name": "English" } }, "suggestions": false, "supportedFilesFormat": [] } ``` ``` -------------------------------- ### PromptBuilder Build Method Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Constructs the final prompt string using the `build` method after setting various parameters. ```rust use ltengine::prompt_builder::PromptBuilder; let mut builder = PromptBuilder::new(); builder.set_source_language("en").set_target_language("fr"); let prompt = builder.build("Hello world"); ``` -------------------------------- ### Docker Run Command for LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Example of running the LTEngine container with configuration passed as command-line arguments, utilizing environment variables for dynamic settings. ```bash # Docker example docker run ltengine ltengine --port $PORT --model $MODEL --api-key $API_KEY ``` -------------------------------- ### Resource-Constrained Environment Configuration Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Setup for environments with limited resources, using CPU-only mode with a small model and a reduced character limit. ```bash ./ltengine --cpu -m gemma3-1b --char-limit 500 ``` -------------------------------- ### Bash Language Detection and Translation Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md A Bash script demonstrating language detection followed by translation. This example uses `curl` to interact with the LibreTranslate API. ```bash #!/bin/bash API_URL="http://localhost:8000" TEXT_TO_DETECT="Bonjour le monde" # Detect language DETECT_RESPONSE=$(curl -s -X POST "$API_URL/detect" -H "Content-Type: application/json" -d "{\"q\": \"$TEXT_TO_DETECT\"}") DETECTED_LANG=$(echo "$DETECT_RESPONSE" | jq -r '.[0].language') DETECTED_CONFIDENCE=$(echo "$DETECT_RESPONSE" | jq -r '.[0].confidence') echo "Detected language: $DETECTED_LANG with confidence $DETECTED_CONFIDENCE" # Translate if language detected if [ -n "$DETECTED_LANG" ] && [ "$DETECTED_LANG" != "null" ]; then TRANSLATE_RESPONSE=$(curl -s -X POST "$API_URL/translate" -H "Content-Type: application/json" -d "{\"q\": \"$TEXT_TO_DETECT\", \"source\": \"$DETECTED_LANG\", \"target\": \"en\"}") TRANSLATED_TEXT=$(echo "$TRANSLATE_RESPONSE" | jq -r '.translatedText') echo "Translated text: $TRANSLATED_TEXT" else echo "Language detection failed." fi ``` -------------------------------- ### LLM Context Creation Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Demonstrates creating a context for the LLM. This context is used to manage the state of the conversation or inference process. ```rust use ltengine::llm::LLM; let mut llm = LLM::new(config).expect("Failed to initialize LLM"); let context = llm.create_context().expect("Failed to create LLM context"); ``` -------------------------------- ### Model Loading Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Demonstrates loading a machine learning model using the `Model::load` method. This can be a local or remote model. ```rust use ltengine::models::Model; let model = Model::load("model_name_or_path").expect("Failed to load model"); ``` -------------------------------- ### Args Struct Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the `Args` struct, used for parsing command-line arguments. ```rust pub struct Args { pub host: String, pub port: u16, // ... other fields } ``` -------------------------------- ### Example: Setting Target Language Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Illustrates setting the target language to 'French' using the set_target_language method. ```rust let mut pb = PromptBuilder::new(); pb.set_target_language("French"); ``` -------------------------------- ### LLM Structure Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the `LLM` struct, which is the main interface for interacting with the language model. ```rust pub struct LLM { // internal fields } ``` -------------------------------- ### Frontend Settings Response Structure Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/endpoints.md Example JSON response for the /frontend/settings endpoint, detailing the structure and types of configuration settings. ```json { "apiKeys": false, "charLimit": 5000, "filesTranslation": false, "frontendTimeout": 1000, "keyRequired": false, "language": { "source": { "code": "auto", "name": "Auto Detect" }, "target": { "code": "en", "name": "English" } }, "suggestions": false, "supportedFilesFormat": [] } ``` -------------------------------- ### Default LTEngine Runtime Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Starts LTEngine with default settings: host 0.0.0.0, port 5050, gemma3-4b model, 5000 character limit, no API key, and warning-level logging. GPU is enabled if available. ```bash ./ltengine ``` -------------------------------- ### Model Load Method Examples Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/models.md Demonstrates using the `load` method for both local and remote model sources. The remote model will be downloaded if it's not already cached. ```rust // Local model let local = Model::Local { path: PathBuf::from("./models/my-model.gguf") }; let path = local.load()?; println!("Model at: {}", path.display()); // Remote model let remote = Model::Remote { hf: HuggingFace { repo: "libretranslate/gemma3", model: "gemma-3-4b-it-q4_0.gguf" } }; let path = remote.load()?; // Downloads if not cached ``` -------------------------------- ### Load Model Main Function Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Shows the main function `load_model` used for loading models, handling caching and download logic. ```rust use ltengine::models::load_model; let model = load_model("model_name_or_path").expect("Failed to load model"); ``` -------------------------------- ### Production Setup for LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/README.md Configure LTEngine for production use with specific host, port, model, character limit, and API key. Enable verbose logging for debugging. ```bash ./ltengine \ --host 0.0.0.0 \ --port 5050 \ --model gemma3-12b \ --char-limit 2000 \ --api-key prod-api-key-here \ --verbose ``` -------------------------------- ### LLM Context Processing Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Shows how to process tokens within an LLM context. This is part of the internal token generation and sampling logic. ```rust use ltengine::llm::LLMContext; let mut context = LLMContext::new(); let token_id = 123; context.process(token_id); ``` -------------------------------- ### Language Detection Function Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Demonstrates the usage of the `detect_lang` function for identifying the language of a given text. ```rust use ltengine::languages::detect_lang; let text = "This is a sample text."; let detected = detect_lang(text).expect("Language detection failed"); println!("Detected: {:?}", detected); ``` -------------------------------- ### Client Request with API Key Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Example of a client request including the 'api_key' parameter in the JSON body. ```json { "q": "Hello", "source": "en", "target": "es", "api_key": "mysecretkey123" } ``` -------------------------------- ### Language Structure Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the `Language` struct, which holds information about a supported language, including its code and name. ```rust pub struct Language { pub code: String, pub name: String, } ``` -------------------------------- ### Example Language Structs Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/types.md Illustrates the structure of Language structs, showing code, name, supported targets, and internal codes. These are used for language management and validation. ```rust Language { code: "en", name: "English", targets: ["en", "es", "fr", ..., "vi"], // All 56 language codes lang_detect: Some(&Lang::Eng), internal_code: "en" } Language { code: "pt-BR", name: "Portuguese (Brazil)", targets: ["en", "es", "fr", ..., "vi"], lang_detect: Some(&Lang::Por), internal_code: "pb" } ``` -------------------------------- ### LLMContext Structure Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the `LLMContext` struct, used to manage the state and process tokens during LLM inference. ```rust pub struct LLMContext { // internal fields } ``` -------------------------------- ### PromptBuilder Set Source Language Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Specifies the source language for the translation prompt using `set_source_language`. ```rust use ltengine::prompt_builder::PromptBuilder; let mut builder = PromptBuilder::new(); builder.set_source_language("en"); ``` -------------------------------- ### Load Model Examples Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/models.md Demonstrates how to use the `load_model` function to load a pre-configured model by its ID, load a custom model using a local file path (ignoring the model ID), and how to handle potential errors during the loading process. ```rust // Load pre-configured model let path = load_model(&"gemma3-4b".to_string(), &"".to_string())?; // Load custom model (model_id ignored) let path = load_model( &"gemma3-4b".to_string(), // ignored &"/models/custom.gguf".to_string() )?; // Handle error match load_model(&"gemma3-4b".to_string(), &"".to_string()) { Ok(path) => println!("Loaded from: {}", path.display()), Err(e) => eprintln!("Failed: {}", e), } ``` -------------------------------- ### Docker Compose Configuration for LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Example of configuring LTEngine within a docker-compose.yml file, specifying the command with arguments for port and model. ```yaml services: ltengine: image: ltengine command: ltengine --port 5050 --model gemma3-4b ``` -------------------------------- ### PromptBuilder Set Format Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Sets the desired output format for the prompt using the `set_format` method. Supported formats include text and HTML. ```rust use ltengine::prompt_builder::PromptBuilder; let mut builder = PromptBuilder::new(); builder.set_format("text"); ``` -------------------------------- ### Enable GPU Acceleration (Build) Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md Compile the engine with CUDA support to leverage GPU acceleration for significantly faster processing. Ensure you have a compatible NVIDIA GPU and CUDA toolkit installed. ```bash cargo build --features cuda --release ./ltengine # GPU used if available ``` -------------------------------- ### Test Basic Translation with LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/README.md Starts the LTEngine server and performs a basic translation request using curl. Ensure the server is running before executing. ```bash ./ltengine -m gemma3-1b & sleep 2 # Test basic translation curl -X POST http://localhost:5050/translate \ -H "Content-Type: application/json" \ -d '{"q":"hello","source":"en","target":"es"}' ``` -------------------------------- ### Load Custom Model Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/README.md Start the translation engine with a custom GGUF model file. Specify the path to your model using the --model-file argument. ```bash ./ltengine --model-file /path/to/custom-model.gguf ``` -------------------------------- ### Python Batch Processing Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Illustrates batch translation using Python. This snippet shows how to send multiple translation requests efficiently. ```python import requests url = "http://localhost:8000/translate" texts_to_translate = [ {"q": "Hello", "source": "en", "target": "fr"}, {"q": "World", "source": "en", "target": "de"}, ] try: response = requests.post(url, json=texts_to_translate) response.raise_for_status() # Raise an exception for bad status codes translations = response.json() for i, translation in enumerate(translations): print(f"Original: {texts_to_translate[i]['q']}, Translated: {translation['translatedText']}") except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") ``` -------------------------------- ### Alternative Translations Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Requests multiple alternative translations for a given text. The `alternatives` parameter controls the number of suggestions. ```bash curl -X POST "http://localhost:8000/translate" -H "Content-Type: application/json" -d '{"q": "How are you?", "source": "en", "target": "es", "alternatives": 3}' ``` -------------------------------- ### Language Detection Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Performs language detection on a given text. The response includes the detected language and its confidence score. ```bash curl -X POST "http://localhost:8000/detect" -H "Content-Type: application/json" -d '{"q": "Ceci est un test."}' ``` -------------------------------- ### Test Language Detection with LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/README.md Starts the LTEngine server and performs a language detection request using curl. Ensure the server is running before executing. ```bash # Test language detection curl -X POST http://localhost:5050/detect \ -H "Content-Type: application/json" \ -d '{"q":"bonjour"}' pkill ltengine ``` -------------------------------- ### Get Language From Code Lookup Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Shows how to use the `get_language_from_code` function to retrieve language details using its code. ```rust use ltengine::languages::get_language_from_code; let lang_code = "en"; if let Some(language) = get_language_from_code(lang_code) { println!("Language name: {}", language.name); } ``` -------------------------------- ### Get Frontend Settings Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md This endpoint retrieves the frontend configuration settings. It includes information like character limits, supported file types, and default language settings. ```bash curl http://localhost:5050/frontend/settings ``` ```json { "apiKeys": false, "charLimit": 5000, "filesTranslation": false, "frontendTimeout": 1000, "keyRequired": false, "language": { "source": { "code": "auto", "name": "Auto Detect" }, "target": { "code": "en", "name": "English" } }, "suggestions": false, "supportedFilesFormat": [] } ``` -------------------------------- ### Translate Text (Success Response with Alternatives) Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/endpoints.md Example of a successful response when the 'alternatives' parameter is greater than 0. Note that alternatives are currently not supported and will be an empty array. ```json { "translatedText": "¡Hola, mundo!", "alternatives": [] } ``` -------------------------------- ### Error Response: Missing Required Parameter Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md Example of a request missing the required 'q' parameter, resulting in a 400 Bad Request error. Ensure all mandatory parameters are included. ```bash curl -X POST http://localhost:5050/translate \ -H "Content-Type: application/json" \ -d '{ "source": "en", "target": "es" }' ``` ```json { "error": "Invalid request: missing q parameter" } ``` -------------------------------- ### Display Help for LTEngine Arguments Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Run `ltengine --help` to view all available command-line options and their default values. ```bash ltengine --help ``` -------------------------------- ### Error Response Example (Invalid Input) Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Demonstrates a typical error response when the input is invalid. The response includes an error code and a descriptive message. ```json { "error": { "code": 400, "message": "Invalid input provided." } } ``` -------------------------------- ### Multipart Form Request Example for File Translation Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Illustrates sending a file for translation using a multipart form request. Note that file translation might not be implemented. ```html --boundary Content-Disposition: form-data; name="file"; filename="example.txt" Content-Type: text/plain [content of example.txt] --boundary Content-Disposition: form-data; name="source" en --boundary Content-Disposition: form-data; name="target" es --boundary-- ``` -------------------------------- ### Parse Languages with jq: Get Codes Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md This example uses `jq` to extract only the language codes from the `/languages` endpoint response. This is useful for scripting or when only codes are needed. ```bash curl -s http://localhost:5050/languages | jq -r '.[].code' ``` ```text en sq ar az eu bn bg ca zh zh-Hans zt zh-Hant cs da nl eo et fi fr gl de el he hi hu id ga it ja ko lv lt ms nb fa pl pt pb pt-BR ro ru sr sk sl es sv tl th tr uk ur vi ``` -------------------------------- ### Translate Text with API Key (Correct Key) Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md Include a valid API key in the request payload for authentication. The server must be started with the --api-key option. ```bash curl -X POST http://localhost:5050/translate \ -H "Content-Type: application/json" \ -d '{ "q": "Hello", "source": "en", "target": "es", "api_key": "my-secret-key-123" }' ``` ```json { "translatedText": "Hola" } ``` -------------------------------- ### GET /frontend/settings Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/endpoints.md Retrieves the current frontend configuration settings for the LibreTranslate engine. This includes information about API keys, character limits, language defaults, and feature enablement. ```APIDOC ## GET /frontend/settings ### Description Retrieve frontend configuration settings. This endpoint provides details about various features and configurations of the LibreTranslate frontend, such as API key requirements, character limits, and supported languages. ### Method GET ### Endpoint /frontend/settings ### Response #### Success Response (200 OK) - **apiKeys** (boolean) - Whether API keys feature is enabled. - **charLimit** (number) - Maximum character limit from `--char-limit` argument. - **filesTranslation** (boolean) - Whether file translation is supported. - **frontendTimeout** (number) - Frontend timeout in milliseconds. - **keyRequired** (boolean) - Whether API key is required for requests. - **language.source.code** (string) - Default source language code. - **language.source.name** (string) - Default source language display name. - **language.target.code** (string) - Default target language code. - **language.target.name** (string) - Default target language display name. - **suggestions** (boolean) - Whether suggestions feature is enabled. - **supportedFilesFormat** (array) - Array of supported file formats. ### Response Example ```json { "apiKeys": false, "charLimit": 5000, "filesTranslation": false, "frontendTimeout": 1000, "keyRequired": false, "language": { "source": { "code": "auto", "name": "Auto Detect" }, "target": { "code": "en", "name": "English" } }, "suggestions": false, "supportedFilesFormat": [] } ``` ### Example Request ```javascript fetch("http://localhost:5050/frontend/settings") .then(r => r.json()) .then(settings => { console.log(`Max characters allowed: ${settings.charLimit}`); console.log(`Features: Translation=${!settings.filesTranslation}, Suggestions=${settings.suggestions}`); }); ``` ``` -------------------------------- ### Run LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/README.md Execute the compiled LTEngine binary. Specify a model file or let it download a default Gemma3 model. ```bash ./target/release/ltengine ``` ```bash ./target/release/ltengine -m gemma3-12b [--model-file /path/to/model.gguf] ``` -------------------------------- ### GET /languages HTTP Endpoint Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/language-detection.md An asynchronous function that handles GET requests to the /languages endpoint, returning a JSON array of all supported Language objects from the LANGUAGES vector. ```rust async fn get_languages() -> impl Responder { HttpResponse::Ok().json(&*LANGUAGES) } ``` -------------------------------- ### Build LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/README.md Clone the repository and build the LTEngine binary. Use features flags for specific accelerator support. ```bash git clone https://github.com/LibreTranslate/LTEngine --recursive cd LTEngine cargo build [--features cuda,vulkan,metal] --release ``` -------------------------------- ### GET /languages Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/INDEX.md Retrieves a list of all languages supported by the translation engine. ```APIDOC ## GET /languages ### Description Retrieves a list of all supported languages, including their codes and names. ### Method GET ### Endpoint /languages ### Response #### Success Response (200) - **languages** (array) - An array of language objects. - **code** (string) - The language code (e.g., "en"). - **name** (string) - The full name of the language (e.g., "English"). #### Response Example ```json { "languages": [ { "code": "en", "name": "English" }, { "code": "es", "name": "Spanish" } ] } ``` ``` -------------------------------- ### LLM::new Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/llm.md Initializes a new LLM instance by loading a model from disk. This method sets up the backend, loads the model, and prepares the inference environment. ```APIDOC ## LLM::new ### Description Initializes a new LLM instance by loading a model from disk. This method sets up the backend, loads the model, and prepares the inference environment. ### Method `new(model_path: PathBuf, cpu: bool, verbose: bool) -> Result` ### Parameters #### Path Parameters - **model_path** (PathBuf) - Required - Absolute path to .gguf model file. - **cpu** (bool) - Required - If true, use CPU-only inference. If false, use GPU if available (requires feature flag compilation). - **verbose** (bool) - Required - If true, enable detailed logging via tracing framework. If false, disable logs. ### Returns `Result` — LLM instance on success, or anyhow::Error on failure. ### Error Conditions - **"Unable to load model"**: Model file cannot be loaded (missing, corrupted, unsupported format). - **Backend initialization error**: Llama.cpp backend cannot be initialized. ### Example ```rust use std::path::PathBuf; let model_path = PathBuf::from("/models/gemma-3-4b-it-q4_0.gguf"); let llm = LLM::new(model_path, false, true)?; // llm is ready for inference ``` ``` -------------------------------- ### Translate Text (Success Response) Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/endpoints.md Example of a successful response from the /translate endpoint with translated text. ```json { "translatedText": "¡Hola, mundo!" } ``` -------------------------------- ### Get Supported Languages Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/endpoints.md Fetches a list of supported languages from the API and logs information about English. ```javascript fetch("http://localhost:5050/languages") .then(r => r.json()) .then(langs => { const english = langs.find(l => l.code === "en"); console.log(`${english.name}: can translate to ${english.targets.length} languages`); }); ``` -------------------------------- ### Model Enum Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the `Model` enum, which can represent either a local model or a remote model. ```rust pub enum Model { Local(String), Remote(String), } ``` -------------------------------- ### Run LTEngine Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/README.md Execute the compiled LTEngine binary with optional arguments. Use this command after a successful build. ```bash ./target/release/ltengine [options] ``` -------------------------------- ### Translate Request Structure Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the structure of a `TranslateRequest` object, used for incoming translation requests. ```rust pub struct TranslateRequest { pub q: String, pub source: Option, pub target: String, pub alternatives: Option, pub format: Option, } ``` -------------------------------- ### PromptBuilder Set Target Language Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Sets the target language for the translation prompt using `set_target_language`. ```rust use ltengine::prompt_builder::PromptBuilder; let mut builder = PromptBuilder::new(); builder.set_target_language("es"); ``` -------------------------------- ### HuggingFace Struct Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the `HuggingFace` struct, likely used for interacting with models hosted on Hugging Face. ```rust pub struct HuggingFace { // internal fields } ``` -------------------------------- ### PromptBuilder Usage Pattern Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Demonstrates the typical workflow for using PromptBuilder to configure and build prompts for LLM translation. ```rust use languages::get_language_from_code; // Create builder let mut pb = PromptBuilder::new(); // Configure format pb.set_format(&format); // Configure languages if source == "auto" { pb.set_source_language("auto"); } else { let src_lang = get_language_from_code(&source)?; pb.set_source_language(src_lang.name); } let tgt_lang = get_language_from_code(&target)?; pb.set_target_language(tgt_lang.name); // Build prompts let prompt = pb.build(&q); // Use prompts with LLM let result = llm.run_prompt(prompt.system, prompt.user)?; ``` -------------------------------- ### Create New PromptBuilder Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Initializes a new PromptBuilder with default values for source language, target language, and format. ```rust pub fn new() -> PromptBuilder { PromptBuilder { source_language: "auto", target_language: "English", format: "text".to_string(), } } ``` -------------------------------- ### Error Response Structure Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the structure of an `ErrorResponse` object, used for consistent error reporting across the API. ```rust pub struct ErrorResponse { pub error: ErrorDetails, } pub struct ErrorDetails { pub code: u16, pub message: String, } ``` -------------------------------- ### Custom Model Configuration Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Load and use a custom or fine-tuned model file with verbose logging enabled. ```bash ./ltengine --model-file /path/to/custom-translation-model.gguf --verbose ``` -------------------------------- ### Build with Vulkan GPU Support Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Compile LTEngine with Vulkan support for GPU acceleration. ```bash cargo build --features vulkan --release ``` -------------------------------- ### PromptBuilder::new() Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Creates a new instance of PromptBuilder with default configuration. The default settings are auto-detected source language, target language set to English, and text format. ```APIDOC ## PromptBuilder::new() ### Description Creates a new PromptBuilder instance with default values. ### Returns PromptBuilder with default configuration. ### Default Configuration: - Source language: "auto" - Target language: "English" - Format: "text" ### Example ```rust let pb = PromptBuilder::new(); ``` ``` -------------------------------- ### Server Startup Model Loading Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/models.md Shows how the `load_model` function is used within the main server entry point to load the user-selected model. It includes error handling to print a message and exit if the model fails to load. ```rust // From main.rs:332-335 let model_path = load_model(&args.model, &args.model_file) .unwrap_or_else(|err| { eprintln!("Failed to load model: {}", err); std::process::exit(1); }); ``` -------------------------------- ### PromptBuilder Method Chaining Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/prompt-builder.md Illustrates fluent method chaining with PromptBuilder for concise prompt configuration. Note that chaining requires a mutable binding. ```rust let prompt = PromptBuilder::new() .set_source_language("English") .set_target_language("Spanish") .set_format("text") .build(&text); ``` -------------------------------- ### Form-Encoded Request Example for Translation Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Demonstrates how to send translation data using form encoding. The 'Content-Type' header should be 'application/x-www-form-urlencoded'. ```html q=This+is+a+test. &source=en &target=fr ``` -------------------------------- ### Select LTEngine Model Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Choose the LLM model for translation. Options range from `gemma3-1b` (fastest, lowest quality) to `gemma3-27b` (slowest, best quality). Models are downloaded from HuggingFace. ```bash ./ltengine -m gemma3-1b # Quick testing ``` ```bash ./ltengine --model gemma3-12b # Higher quality ``` ```bash ./ltengine -m gemma3-27b # Best quality ``` -------------------------------- ### JSON Request Example for Translation Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Shows a basic JSON request payload for the /translate endpoint. Ensure 'Content-Type' is set to 'application/json'. ```json { "q": "This is a test.", "source": "en", "target": "fr" } ``` -------------------------------- ### Rust Function Signature for Get Languages Endpoint Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/endpoints.md Defines the asynchronous function signature for the get_languages endpoint in Rust, which returns a Responder. ```rust async fn get_languages() -> impl Responder ``` -------------------------------- ### PromptBuilder Build Method Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/types.md Builds the final Prompt structure with system and user messages based on configuration. Returns a Prompt with appropriate system message and user message. ```rust pub fn build(&self, q: &String) -> Prompt ``` -------------------------------- ### Parse Languages with jq: Count Languages Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md This example uses `jq` to count the total number of supported languages returned by the `/languages` endpoint. ```bash curl -s http://localhost:5050/languages | jq 'length' ``` ```text 56 ``` -------------------------------- ### Build with Metal GPU Support Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Compile LTEngine with Metal support for GPU acceleration on macOS. ```bash cargo build --features metal --release ``` -------------------------------- ### Detect Language with Form Encoding Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/api-reference/http-examples.md This example shows how to detect language using form-urlencoded data instead of JSON. The 'q' parameter is URL-encoded. ```bash curl -X POST http://localhost:5050/detect \ -H "Content-Type: application/x-www-form-urlencoded" \ --data-urlencode "q=Ciao, come stai?" ``` ```json [ { "language": "it", "confidence": 92 } ] ``` -------------------------------- ### LangDetect Structure Example Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/GENERATED.md Defines the `LangDetect` struct, used to return language detection results, including the detected language and confidence score. ```rust pub struct LangDetect { pub language: String, pub confidence: f32, } ``` -------------------------------- ### Production Server Configuration Source: https://github.com/libretranslate/ltengine/blob/main/_autodocs/configuration.md Configuration for a public server with a high-quality model, character limit, and optional API key authentication. ```bash ./ltengine --host 0.0.0.0 --port 5050 --model gemma3-12b --char-limit 2000 --api-key prod-key-123 ```