Translate

You can get machine translation and translation quality scores from the /translate REST API endpoint or any of the official SDKs (Python, Go, Rust, JS, or cURL).

Under the hood, the /translate endpoint simultaneously leverages multiple state-of-the-art LLM and machine translation models to perform translations. The translations from these models are scored and ranked using reference-free quality estimation models. This allows us to select the highest quality machine translation for the given source and target languages.

The /translate endpoint will return a JSON object response with two fields:

  • Best translation - The translated text.
  • Score - A score from -1.0 to 1.0 representing the translation quality. Higher the score better the quality.

Additionally, you can opt to use third-party translation engines such as OpenAI, DeepL, and Google. By default, the use of these commercial engines is set to false.

Our translation API supports a wide range of languages, including but not limited to English, Hindi, French, Spanish, German, and more.

Generate a Translation

To generate a translation, you can use the following code examples. Depending on your preference or requirements, select the appropriate method for your application.

1import os
2import json
3
4from predictionguard import PredictionGuard
5
6# Set your Prediction Guard token as an environmental variable.
7os.environ["PREDICTIONGUARD_API_KEY"] = "<api key>"
8
9client = PredictionGuard()
10
11# Translate the text.
12result = client.translate.create(
13 text="The sky is blue",
14 source_lang="eng",
15 target_lang="fra",
16 use_third_party_engine=True
17 )
18
19print(json.dumps(
20 result,
21 sort_keys=True,
22 indent=4,
23 separators=(',', ': ')
24))

The output will look something like:

1{
2 "translations": [
3 {
4 "score": 0.888733446598053,
5 "translation": "Le ciel est bleu.",
6 "model": "hermes_2_pro_llama_3_8b",
7 "status": "success"
8 },
9 {
10 "score": 0.888733446598053,
11 "translation": "Le ciel est bleu.",
12 "model": "hermes_2_pro_mistral_7b",
13 "status": "success"
14 },
15 {
16 "score": 0.888733446598053,
17 "translation": "Le ciel est bleu.",
18 "model": "openai",
19 "status": "success"
20 },
21 {
22 "score": 0.888733446598053,
23 "translation": "Le ciel est bleu.",
24 "model": "deepl",
25 "status": "success"
26 },
27 {
28 "score": 0.888733446598053,
29 "translation": "Le ciel est bleu.",
30 "model": "google",
31 "status": "success"
32 }
33 ],
34 "best_translation": "Le ciel est bleu.",
35 "best_score": 0.888733446598053,
36 "best_translation_model": "hermes_2_pro_llama_3_8b",
37 "created": 1726862165,
38 "id": "translation-88d58be2858d4adc86bc6b8e3ac34aae",
39 "object": "translation"
40}

This approach presents a straightforward way for readers to choose and apply the code example that best suits their needs for generating text completions using either Python, Go, Rust, JS, or cURL.