Cloudflare Docs
Workers AI
Edit this page on GitHub
Set theme to dark (⇧+D)

Translation

Translation models convert a sequence of text from one language to another.

  • Task type: translation
  • TypeScript class: AiTranslation

​​ Available Embedding Models

List of available models in for this task type:

Model IDDescription
@cf/meta/m2m100-1.2bMultilingual encoder-decoder (seq-to-seq) model trained for Many-to-Many multilingual translation
languages: english, chinese, french, spanish, arabic, russian, german, japanese, portuguese, hindi
More information
Terms and license

​​ Examples

import { Ai } from '@cloudflare/ai'
export interface Env {
AI: any;
}
export default {
async fetch(request: Request, env: Env) {
const ai = new Ai(env.AI);
const response = await ai.run('@cf/meta/m2m100-1.2b', {
text: "I'll have an order of the moule frites",
source_lang: "english", // defaults to english
target_lang: "french"
}
);
return new Response(JSON.stringify(response));
},
}
async function run(model, input) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/${model}`,
{
headers: { Authorization: "Bearer {API_TOKEN}" },
method: "POST",
body: JSON.stringify(input),
}
);
const result = await response.json();
return result;
}
run('@cf/meta/m2m100-1.2b', {
text: "I'll have an order of the moule frites",
source_lang: "english", // defaults to english
target_lang: "french"
}).then((response) => {
console.log(JSON.stringify(response));
});
import requests
API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/"
headers = {"Authorization": "Bearer {API_TOKEN}"}
def run(model, input):
response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json=input)
return response.json()
output = run('@cf/meta/m2m100-1.2b', {
"text": "I'll have an order of the moule frites",
"source_lang": "english",
"target_lang": "french"
})
print(output)
$ curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/m2m100-1.2b \
-X POST \
-H "Authorization: Bearer {API_TOKEN}" \
-d '{ "text": "Ill have an order of the moule frites", "source_lang": "english", "target_lang": "french" }'

Example Workers AI response

{
"result": {
"translated_text": "Je vais commander des moules frites",
"success": true,
"errors":[],
"messages":[]
}

​​ API schema

The following schema is based on JSON Schema

​​ Input

{
"type": "object",
"properties": {
"text": {
"type": "string"
},
"source_lang": {
"type": "string",
"default": "en"
},
"target_lang": {
"type": "string"
}
},
"required": [
"text",
"target_lang"
]
}

TypeScript class: AiTranslationInput

​​ Output

{
"type": "object",
"contentType": "application/json",
"properties": {
"translated_text": {
"type": "string"
}
}
}

TypeScript class: AiTranslationOutput