This page shows you how to use Vertex AI Studio to rapidly translate text from one language to another. Translation in Vertex AI Studio offers the choice between Gemini and two specialized translation models, which includes the Cloud Translation Neural Machine Translation (NMT) model, and a fine-tuned LLM for translations. When you use the translation LLM, you can tailor responses by providing example translations to more closely match your style, tone, and industry domain. The model uses yours examples as few-shot context before translating your text.
You can't pass examples when you translate text with Gemini or the NMT model. But, for direct translations without examples, you can use Gemini or either specialized translation model.
Supported languages
The languages that each model supports can vary. Before you requesting translations, check that the model you're using supports your source and target languages.
Translation LLM
For the translation LLM, you can translation to and from any of the following language pairs:
- Chinese (Simplified) <-> English
- French <-> English
- German <-> English
- Italian <-> English
- Japanese <-> English
- Korean <-> English
- Portuguese <-> English
- Spanish <-> English
Gemini and NMT
For information about which languages Gemini and the NMT model support, see the following documentation:
Translate text
Use the translation LLM, Gemini, or the NMT model to translate text by using the Google Cloud console or API.
Console
In the Cloud Translation section of the Google Cloud console, go to the Translation page in Vertex AI Studio.
In the Run settings pane, select a translation model in the Model field.
To change the model settings (such as temperature), expand Advanced.
Set the source and target languages.
In the input field, enter the text to translate.
Click Submit.
To get the code or curl command that demonstrate how to request translations, click
Get code.
API
Select the model to use for your translations.
Translation LLM
REST
Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: The numeric or alphanumeric ID of your Google Cloud project
- LOCATION: The region where you want to run this operation. For
example,
us-central1
. - SOURCE_LANGUAGE: The language code of the input text. Set to one of the language codes listed in adaptive translation.
- TARGET_LANGUAGE: The target language to translate the input text to. Set to one of the language codes listed in adaptive translation.
- SOURCE_TEXT: Text in the source language to translate.
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/PROJECT_ID/locations/LOCATION:adaptiveMtTranslate
Request JSON body:
{ "sourceLanguageCode": "SOURCE_LANGUAGE_CODE", "targetLanguageCode": "TARGET_LANGUAGE_CODE", "content": ["INPUT_TEXT"], "model": "projects/PROJECT_ID/locations/LOCATION/model/general/translation-llm", }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "translations": [ { "translatedText": "TRANSLATED_TEXT" } ], "languageCode": "TARGET_LANGUAGE" }
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
const projectId = '<PROJECT_ID>'; const location = 'global'; const text = '<INPUT_TEXT>'; // Imports the Google Cloud Translation library const {TranslationServiceClient} = require('@google-cloud/translate'); // Instantiates a client const translationClient = new TranslationServiceClient(); async function translateText() { // Construct request const request = { parent: `projects/${projectId}/locations/${location}`, contents: [text], mimeType: 'text/plain', // mime types: text/plain, text/html sourceLanguageCode: '<SOURCE_LANGUAGE_CODE>', targetLanguageCode: '<TARGET_LANGUAGE_CODE>', model: `projects/${projectId}/locations/${location}/model/general/translation-llm` }; // Run request const [response] = await translationClient.translateText(request); for (const translation of response.translations) { console.log(`Translation: ${translation.translatedText}`); } } translateText();
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
# Imports the Google Cloud Translation library from google.cloud import translate # Initialize Translation client def translate_text( text: str = "YOUR_TEXT_TO_TRANSLATE", project_id: str = "YOUR_PROJECT_ID" ) -> translate.TranslationServiceClient: """Translating Text.""" client = translate.TranslationServiceClient() location = "global" parent = f"projects/{project_id}/locations/{location}" model = f"{parent}/models/general/translation-llm" # Translate text from English to French # Detail on supported types can be found here: # https://cloud.google.com/translate/docs/supported-formats response = client.translate_text( request={ "parent": parent, "contents": [text], "model": model "mime_type": "text/plain", # mime types: text/plain, text/html "source_language_code": "<SOURCE_LANGUAGE_CODE>", "target_language_code": "<TARGET_LANGUAGE_CODE>", } ) # Display the translation for each input text provided for translation in response.translations: print(f"Translated text: {translation.translated_text}") return response translate_text(<INPUT_TEXT>, <PROJECT_ID>)
Gemini
REST
Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project.
- LOCATION: The region to process the request. Available
options include the following:
Click to expand available regions
us-central1
us-west4
northamerica-northeast1
us-east4
us-west1
asia-northeast3
asia-southeast1
asia-northeast1
- MODEL_ID: The ID of the model, such as
gemini-1.0-pro-002
- SOURCE_LANGUAGE_CODE: The language of the input text.
- TARGET_LANGUAGE_CODE: The target language to translate the input text to.
- SOURCE_TEXT: The text to translate.
- TEMPERATURE:
The temperature is used for sampling during response generation, which occurs when
topP
andtopK
are applied. Temperature controls the degree of randomness in token selection. Lower temperatures are good for prompts that require a less open-ended or creative response, while higher temperatures can lead to more diverse or creative results. A temperature of0
means that the highest probability tokens are always selected. In this case, responses for a given prompt are mostly deterministic, but a small amount of variation is still possible.If the model returns a response that's too generic, too short, or the model gives a fallback response, try increasing the temperature.
- TOP_P:
Top-P changes how the model selects tokens for output. Tokens are selected
from the most (see top-K) to least probable until the sum of their probabilities
equals the top-P value. For example, if tokens A, B, and C have a probability of
0.3, 0.2, and 0.1 and the top-P value is
0.5
, then the model will select either A or B as the next token by using temperature and excludes C as a candidate.Specify a lower value for less random responses and a higher value for more random responses.
- TOP_K:
Top-K changes how the model selects tokens for output. A top-K of
1
means the next selected token is the most probable among all tokens in the model's vocabulary (also called greedy decoding), while a top-K of3
means that the next token is selected from among the three most probable tokens by using temperature.For each token selection step, the top-K tokens with the highest probabilities are sampled. Then tokens are further filtered based on top-P with the final token selected using temperature sampling.
Specify a lower value for less random responses and a higher value for more random responses.
- MAX_OUTPUT_TOKENS:
Maximum number of tokens that can be generated in the response. A token is
approximately four characters. 100 tokens correspond to roughly 60-80 words.
Specify a lower value for shorter responses and a higher value for potentially longer responses.
- SAFETY_CATEGORY:
The safety category to configure a threshold for. Acceptable values include the following:
Click to expand safety categories
HARM_CATEGORY_SEXUALLY_EXPLICIT
HARM_CATEGORY_HATE_SPEECH
HARM_CATEGORY_HARASSMENT
HARM_CATEGORY_DANGEROUS_CONTENT
- THRESHOLD:
The threshold for blocking responses that could belong to the specified safety category based on
probability. Acceptable values include the following:
Click to expand blocking thresholds
BLOCK_NONE
BLOCK_ONLY_HIGH
BLOCK_MEDIUM_AND_ABOVE
(default)BLOCK_LOW_AND_ABOVE
BLOCK_LOW_AND_ABOVE
blocks the most whileBLOCK_ONLY_HIGH
blocks the least.
HTTP method and URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:streamGenerateContent
Request JSON body:
{ "contents": [ { "role": "user", "parts": [ { "text": "SOURCE_LANGUAGE_CODE: SOURCE_TEXT\nTARGET_LANGUAGE_CODE:" } ] } ], "generation_config": { "temperature": TEMPERATURE, "topP": TOP_P, "topK": TOP_K, "candidateCount": 1, "maxOutputTokens": MAX_OUTPUT_TOKENS } "safetySettings": [ { "category": "SAFETY_CATEGORY", "threshold": "THRESHOLD" } ] }
To send your request, expand one of these options:
You should receive a successful status code (2xx) and an empty response.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
const {VertexAI} = require('@google-cloud/vertexai'); // Initialize Vertex with your Cloud project and location const vertex_ai = new VertexAI({project: '<PROJECT_ID>', location: '<LOCATION>'}); const model = 'gemini-1.0-pro'; // Instantiate the models const generativeModel = vertex_ai.preview.getGenerativeModel({ model: model, generationConfig: { 'candidate_count': 1, 'max_output_tokens': <MAX_OUTPUT_TOKENS>, 'temperature': <TEMPERATURE>, 'top_p': <TOP_P>, 'top_k': <TOP_K>, }, safetySettings: [ { 'category': 'HARM_CATEGORY_HATE_SPEECH', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE' }, { 'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE' }, { 'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE' }, { 'category': 'HARM_CATEGORY_HARASSMENT', 'threshold': 'BLOCK_MEDIUM_AND_ABOVE' } ], }); async function generateContent() { const req = { contents: [ {role: 'user', parts: [{text: `<SOURCE_LANGUAGE_CODE>: <TEXT> <TARGET_LANGUAGE_CODE>:`}]} ], }; const streamingResp = await generativeModel.generateContentStream(req); for await (const item of streamingResp.stream) { process.stdout.write('stream chunk: ' + JSON.stringify(item) + '\n'); } process.stdout.write('aggregated response: ' + JSON.stringify(await streamingResp.response)); } generateContent();
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
import base64 import vertexai from vertexai.generative_models import GenerativeModel, Part, FinishReason import vertexai.preview.generative_models as generative_models def generate(): vertexai.init(project="<PROJECT_ID>", location="<LOCATION>") model = GenerativeModel("gemini-1.0-pro") responses = model.generate_content( ["""<SOURCE_LANGUAGE_CODE>: <TEXT> <TARGET_LANGUAGE_CODE>:"""], generation_config=generation_config, safety_settings=safety_settings, ) print(responses) generation_config = { "candidate_count": 1, "max_output_tokens": <MAX_OUTPUT_TOKENS>, "temperature": <TEMPERATURE>, "top_p": <TOP_P>, "top_k": <TOP_K>, } safety_settings = { generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, } generate()
NMT
REST
Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project.
- SOURCE_LANGUAGE: (Optional) The language code of the input text. For supported language codes, see Language support.
- TARGET_LANGUAGE: The target language to translate the input text to. Set to one of the supported language codes.
- SOURCE_TEXT: The text to translate.
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/PROJECT_ID:translateText
Request JSON body:
{ "sourceLanguageCode": "SOURCE_LANGUAGE", "targetLanguageCode": "TARGET_LANGUAGE", "contents": ["SOURCE_TEXT1", "SOURCE_TEXT2"] }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "translations": [ { "translatedText": "TRANSLATED_TEXT1" }, { "translatedText": "TRANSLATED_TEXT2" } ] }
Node.js
Before trying this sample, follow the Node.js setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Node.js API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Python API reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Custom translations
Customize responses from the translation LLM by providing your own example translations. Custom translations only work with the translation LLM.
Data requirements
Example translations must be written as segment pairs in a TMX or TSV file. Each pair includes a source language segment and its translated counterpart. For more information, see Prepare example translations in the Cloud Translation documentation.
To get the most accurate results, include specific examples from a wide variety of scenarios. You must include at least five sentence pairs but no more than 10,000 pairs. Also, a segment pair can be at most a total of 512 characters.
Customize translations
Console
In the Cloud Translation section of the Google Cloud console, go to the Translation page in Vertex AI Studio.
In the Translate text card, click Translate.
In the Parameter pane, configure your translation settings.
- In the Model field, select Translation LLM.
- To change the temperature, expand Advanced.
Click Add examples.
- Select a local file or a file from Cloud Storage. Vertex AI Studio determines the source and target languages from your file.
- Select the number of examples for the model to use before generating a response.
The number of examples you select does count toward input character limit per request of 3,000.
In the input field, enter the text to translate.
Click Submit.
The model attempts to identify patterns from your examples and applies them when generating a response. The output limit per request is 3,000 characters. Any text beyond this limit is dropped.
To get the code or curl command that demonstrate how to request translations, click
Get code.
REST
Create a dataset
Create a dataset where you import your example translations. The source and target languages must match the languages that you intend to use in your translations.
Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
- LOCATION: The region where your source dataset is located, such
as
us-central1
. - DATASET_ID: A unique identifier for your dataset.
- DISPLAY_NAME: A descriptive name for your dataset.
- SOURCE_LANGUAGE: The language code of the input text. For supported language codes, see Supported languages.
- TARGET_LANGUAGE: The target language to translate the input text to. For supported language codes, see Supported languages.
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/PROJECT_ID/locations/LOCATION/adaptiveMtDatasets
Request JSON body:
{ "name": "projects/PROJECT_ID/locations/LOCATION/adaptiveMtDatasets/DATASET_ID, "display_name": "DISPLAY_NAME", "source_language_code": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE" }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "name": "projects/PROJECT_ID/locations/LOCATION/adaptiveMtDatasets/DATASET_ID", "displayName": "DISPLAY_NAME", "sourceLanguageCode": "SOURCE_LANGUAGE", "targetLanguageCode": "TARGET_LANGUAGE" }
Import data
Populate your dataset with example translations from a TSV or TMX file.
Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
- LOCATION: The region where your dataset is located, such as
us-central1
. - DATASET_ID: The unique identifier of your dataset where the data is to be imported.
- GCS_FILE_PATH: The path to the source data file in
Cloud Storage, such as
gs://example/data.tsv
.
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/PROJECT_ID/locations/LOCATION/adaptiveMtDatasets/DATASET_ID:importAdaptiveMtFile
Request JSON body:
{ "gcs_input_source": { "input_uri": "GCS_FILE_PATH" } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "adaptiveMtFile": { "name": "DATASET_NAME", "displayName": "FILE_NAME", "entryCount": TOTAL_ENTRIES } }
Translate text
Provide the text to translate and the dataset to use for the translation.
Before using any of the request data, make the following replacements:
- PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project
- LOCATION: The region where your source dataset is located, such
as
us-central1
. - DATASET_NAME: The name of the dataset that
Cloud Translation uses to customize your translations, formatted as
projects/PROJECT_ID/locations/LOCATION/adaptiveMtDatasets/DATASET_ID
. You can get dataset names by listing all datasets in your project. - SOURCE_TEXT: The text to translate.
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/PROJECT_ID/locations/LOCATION:adaptiveMtTranslate
Request JSON body:
{ "dataset": "DATASET_NAME", "content": ["SOURCE_TEXT"] }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "translations": [ { "translatedText": "TRANSLATED_TEXT" } ], "languageCode": "TARGET_LANGUAGE" }
Before trying this sample, follow the Node.js setup instructions in the
Cloud Translation quickstart using
client libraries.
For more information, see the
Cloud Translation Node.js API
reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
Node.js
const projectId = '<PROJECT_ID>';
const location = 'us-central1';
const text = '<INPUT_TEXT>';
async function createAdaptiveMtDataset() {
// Construct request
const request = {
parent: `projects/${projectId}/locations/${location}`,
adaptiveMtDataset: {
name: `projects/${projectId}/locations/${location}/adaptiveMtDatasets/${
adaptiveMtDatasetName}`,
displayName: 'DATASET_DISPLAY_NAME',
sourceLanguageCode: <SOURCE_LANGUAGE_CODE>,
targetLanguageCode: <TARGET_LANGUAGE_CODE>,
}
};
// Run request
const [response] = await translationClient.createAdaptiveMtDataset(request);
console.log('Created')
console.log(response)
}
async function importAdaptiveMtFile() {
const request = {
parent: `projects/${projectId}/locations/${location}/adaptiveMtDatasets/${
adaptiveMtDatasetName}`,
gcsInputSource: {inputUri: gcs_file_uri}
} const [response] = await translationClient.importAdaptiveMtFile(request)
console.log('Importing file')
console.log(response)
}
async function translate() {
const request = {
parent: `projects/${projectId}/locations/${location}`,
dataset: `projects/${projectId}/locations/${location}/adaptiveMtDatasets/${
adaptiveMtDatasetName}`,
content: [text]
} const [response] = await translationClient.adaptiveMtTranslate(request)
console.log('Translating')
console.log(response)
}
Before trying this sample, follow the Python setup instructions in the
Cloud Translation quickstart using
client libraries.
For more information, see the
Cloud Translation Python API
reference documentation.
To authenticate to Cloud Translation, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
Python
# Imports the Google Cloud Translation library
from google.cloud import translate
def create_adaptive_mt_dataset():
# Create a client
client = translate.TranslationServiceClient()
# Initialize request argument(s)
adaptive_mt_dataset = translate.AdaptiveMtDataset()
adaptive_mt_dataset.name = "projects/<PROJECT_ID>/locations/us-central1/adaptiveMtDatasets/DATASET_ID"
adaptive_mt_dataset.display_name = "DATASET_DISPLAY_NAME"
adaptive_mt_dataset.source_language_code = "<SOURCE_LANGUAGE_CODE>"
adaptive_mt_dataset.target_language_code = "<TARGET_LANGUAGE_CODE>"
request = translate.CreateAdaptiveMtDatasetRequest(
parent="projects/<PROJECT_ID>/locations/us-central1",
adaptive_mt_dataset=adaptive_mt_dataset,
)
# Make the request
response = client.create_adaptive_mt_dataset(request=request)
# Handle the response
print(response)
def import_adaptive_mt_file():
# Create a client
client = translate.TranslationServiceClient()
gcs_input_source = translate.GcsInputSource()
gcs_input_source.input_uri = "gs://SOURCE_LOCATION/FILE.tsv"
# Initialize the request
request = translate.ImportAdaptiveMtFileRequest(
parent="projects/<PROJECT_ID>/locations/us-central1/adaptiveMtDatasets/DATASET_ID",
gcs_input_source=gcs_input_source
)
# Make the request
response = client.import_adaptive_mt_file(request)
# Handle the response
print(response)
def adaptive_mt_translate():
# Create a client
client = translate.TranslationServiceClient()
# Initialize the request
request = translate.AdaptiveMtTranslateRequest(
parent="projects/<PROJECT_ID>/locations/us-central1",
dataset="projects/<PROJECT_ID>/locations/us-central1/adaptiveMtDatasets/DATASET_ID",
content=["<INPUT_TEXT>"],
)
# Make the request
response = client.adaptive_mt_translate(request)
# Handle the response
print(response)