DeepL API Documentation

website·Indexed 33 days ago

https://pyvideotrans.com

Documentation for the DeepL API, including supported languages, language codes, variants, and feature availability for glossaries, tag handling, and text improvement.

Tokens
13.5K
Snippets
38
Records
50
Agent score
68%

What's inside DeepL

  1. Guide custom prompts for Qwen-MT translations

    Qwen-MT supports guiding translations through custom prompts (e.g., specifying role, style, or legal terminology).

    Priority Note: If both translation_options and a custom prompt are provided, the settings in translation_options (such as target_lang and source_lang) take precedence and will override the prompt's specifications. It is recommended to use translation_options for the best results.

    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Example for North China 2 (Beijing) region
        base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
    )
    
    # Custom prompt for legal translation
    prompt_template = """
    # Role
    You are a professional legal translation expert...
    # Task
    Translate the following Chinese legal text into professional, precise, and formal English.
    # Requirements
    1. Faithful to original... 2. Precise terminology... 3. Formal tone... 4. Clear phrasing... 5. Preserve format...
    # Text to translate
    {text_to_translate}
    """
    
    chinese_legal_text = "本合同自双方签字盖章之日起生效,有效期为一年。"
    final_prompt = prompt_template.format(text_to_translate=chinese_legal_text)
    
    messages = [{"role": "user", "content": final_prompt}]
    completion = client.chat.completions.create(model="qwen-mt-plus", messages=messages)
    print(completion.choices[0].message.content)
  2. Translate text using Qwen-MT via OpenAI-compatible SDK

    To perform machine translation using the qwen-mt-plus model via the OpenAI Python SDK, you must pass translation settings within the extra_body parameter because translation_options is not a standard OpenAI API parameter. The messages array must contain exactly one message with the role user, where the content is the text to be translated.
    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Replace {WorkspaceId} with your actual business space ID
        base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
    )
    
    messages = [
        {
            "role": "user",
            "content": "Text to be translated"
        }
    ]
    
    translation_options = {
        "source_lang": "auto", # or specific language like 'Chinese'
        "target_lang": "English"
    }
    
    completion = client.chat.completions.create(
        model="qwen-mt-plus",
        messages=messages,
        extra_body={
            "translation_options": translation_options
        }
    )
    print(completion.choices[0].message.content)
  3. Translate text using the DashScope Python SDK with Qwen-MT

    When using the DashScope SDK, translation configurations are passed directly via the translation_options parameter.

    Constraints:

    • The messages array must contain exactly one message with the role user, where the content is the text to be translated.
    • To automatically detect the source language, set source_lang to auto.
    import os
    import dashscope
    
    # Example for Singapore region; replace {WorkspaceId} with your actual business space ID
    dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
    
    response = dashscope.Generation.call(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        model="qwen-mt-flash",
        messages=[
            {
                "role": "user",
                "content": "我看到这个视频后没有笑"
            }
        ],
        translation_options={
            "source_lang": "auto",
            "target_lang": "English",
        },
        result_format="message"
    )
    
    print(response.output.choices[0].message.content)
  4. Implement domain-specific translation with Qwen-MT using OpenAI-compatible SDK

    To perform high-quality, domain-specific translations (e.g., legal documents) using the qwen-mt-plus model, use a structured prompt that defines the expert role, specific translation requirements (such as terminology and tone), and the target text. The request must contain exactly one message with the role user containing the prompt.
    import OpenAI from 'openai';
    
    const client = new OpenAI({
        apiKey: process.env.DASHSCOPE_API_KEY,
        // Replace {WorkspaceId} with your actual business space ID. 
        // Example for North China 2 (Beijing) region:
        baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
    });
    
    const promptTemplate = `
    # Role
    You are a professional legal translation expert...
    # Task
    Translate the following Chinese legal text into professional English.
    # Requirements
    1. Faithful to original... 2. Precise terminology... 3. Formal tone...
    # Text to translate
    {text_to_translate}
    `;
    
    const chineseLegalText = "本合同自双方签字盖章之日起生效,有效期为一年。";
    const finalPrompt = promptTemplate.replace('{text_to_translate}', chineseLegalText);
    
    async function main() {
        const completion = await client.chat.completions.create({
            model: "qwen-mt-plus",
            messages: [{ "role": "user", "content": finalPrompt }]
        });
        console.log(completion.choices[0].message.content);
    }
    
    main();
  5. Improve translation accuracy with Terminology Intervention

    To ensure consistency for brand names, product names, or professional terminology, use the terms field within translation_options. Provide a JSON array of objects where each object contains a source term and its corresponding target translation.
    "translation_options": {
        "source_lang": "Chinese",
        "target_lang": "English",
        "terms": [
            {
                "source": "术语",
                "target": "提前翻译好的术语"
            }
        ]
    }
  6. Translate text using Qwen-MT via DashScope SDK

    When using the dashscope Python SDK, the translation_options can be passed directly as a parameter to the Generation.call method.
    import os
    import dashscope
    
    # Replace {WorkspaceId} with your actual business space ID
    dashscope.base_http_api_url = "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1"
    
    messages = [{"role": "user", "content": "Text to be translated"}]
    translation_options = {
        "source_lang": "Chinese",
        "target_lang": "English",
        "terms": [{"source": "term", "target": "translation"}]
    }
    
    response = dashscope.Generation.call(
        api_key=os.getenv('DASHSCOPE_API_KEY'),
        model="qwen-mt-plus",
        messages=messages,
        result_format='message',
        translation_options=translation_options
    )
    print(response.output.choices[0].message.content)
  7. Implement streaming output for Qwen-MT translations

    Streaming output allows real-time translation results. Support varies by model: qwen-mt-flash and qwen-mt-lite support incremental streaming (via the incremental_output parameter), while qwen-mt-plus and qwen-mt-turbo only support non-incremental streaming (returning the full sequence each time).
    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
    )
    
    messages = [{"role": "user", "content": "我看到这个视频后没有笑"}]
    translation_options = {"source_lang": "Chinese", "target_lang": "English"}
    
    completion = client.chat.completions.create(
        model="qwen-mt-flash",
        messages=messages,
        stream=True,
        stream_options={"include_usage": True},
        extra_body={"translation_options": translation_options},
    )
    
    for chunk in completion:
        if chunk.choices:
            content = chunk.choices[0].delta.content or ""
            print(content, end="", flush=True)
        else:
            print("\n" + "="*20 + "usage 消耗" + "="*20)
            print(chunk.usage)
  8. Integrate Qwen-MT with Immersive Translate plugin

    To use Qwen-MT within the Immersive Translate plugin:

    1. Add a Custom Translation Service in the latest version of the plugin.
    2. Select Qwen MT as the service.
    3. Enter your API Key.
    4. Select either qwen-mt-plus or qwen-mt-flash from the model dropdown menu.
    5. Set Qwen MT as the default in the left sidebar.
  9. Translate text using the OpenAI Python SDK with Qwen-MT

    To use Qwen-MT via the OpenAI-compatible interface, you must pass translation configurations (like source and target languages) inside the extra_body parameter, as translation_options is not a standard OpenAI parameter.

    Constraints:

    • Only single-turn translation is supported; multi-turn dialogue is not supported.
    • System messages are not supported. All configurations must be defined in translation_options.
    • The messages array must contain exactly one message with the role user, where the content is the text to be translated.
    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Example for Singapore region; replace {WorkspaceId} with your actual business space ID
        base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
    )
    
    completion = client.chat.completions.create(
        model="qwen-mt-flash",
        messages=[
            {
                "role": "user",
                "content": "我看到这个视频后没有笑"
            }
        ],
        extra_body={
            "translation_options": {
                "source_lang": "Chinese",
                "target_lang": "English"
            }
        },
    )
  10. Configure Qwen-MT via OpenAI-compatible interface

    When using the OpenAI Python SDK with Qwen-MT, translation_options is not a standard OpenAI parameter. It must be passed inside the extra_body argument of the chat.completions.create method. Ensure the base_url is set to the region-specific endpoint (e.g., cn-beijing) and the WorkspaceId is replaced with your actual business space ID.
    completion = client.chat.completions.create(
        model="qwen-mt-plus",
        messages=[{"role": "user", "content": "Text to translate"}],
        extra_body={
            "translation_options": {
                "source_lang": "Chinese",
                "target_lang": "English"
            }
        }
    )
  11. Use Translation Memory (tm_list) for style and consistency

    To ensure the model follows a specific translation style, sentence structure, or corporate writing standard, use the tm_list field within translation_options. This field accepts an array of source-target sentence pairs that the model will imitate during the current translation task.
    import os
    from openai import OpenAI
    
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Replace {WorkspaceId} with your actual business space ID.
        base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
    )
    
    messages = [
        {
            "role": "user",
            "content": "通过如下命令可以看出安装thrift的版本信息;"
        }
    ]
    
    translation_options = {
        "source_lang": "Chinese",
        "target_lang": "English",
        "tm_list": [
            {
                "source": "您可以通过如下方式查看集群的内核版本信息:",
                "target": "You can use one of the following methods to query the engine version of a cluster:"
            },
            {
                "source": "我们云HBase的thrift环境是0.9.0,所以建议客户端的版本也为 0.9.0...",
                "target": "The version of Thrift used by ApsaraDB for HBase is 0.9.0. Therefore, we recommend..."
            }
        ]
    }
    
    completion = client.chat.completions.create(
        model="qwen-mt-plus",
        messages=messages,
        extra_body={
            "translation_options": translation_options
        }
    )
    print(completion.choices[0].message.content)