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)