Nhiệm vụ: Sửa trình dịch
Sửa trình dịch
Vì lý do nào đó, rất thường xuyên văn bản không được dịch chút nào, hoặc chỉ có tiêu đề được dịch. Đồng thời, phản hồi trả về success: true
Điều này rất không đáng tin cậy
Ворклоги
Đã sửa lỗi này. Vấn đề là agent đang làm sai định dạng trong tài liệu yaml. Phản hồi dạng văn bản trả về như sau:
en:
name: |
Growth in Specialist Productivity Requires Corresponding Development of the Business Technological Environment
description: |
Business gets the maximum out of new technologies and strong specialists only when its own technological environment allows realizing their productivity.
content: |
New technologies increase specialist productivity, but this productivity is not realized in a vacuum.
Ở đây, description và content không được lồng vào bên trong en giống như name, mà lại trở thành các phần mới. Kết quả là chúng ta nhận được cấu trúc phản hồi cuối cùng như sau:
{
en: {
name: 'Growth in Specialist Productivity Requires Corresponding Development of the Business Technological Environment\n'
},
description: 'Business gets the maximum out of new technologies and strong specialists only when its own technological environment allows realizing their productivity.\n',
content: 'New technologies increase specialist productivity, but this productivity is not realized in a vacuum.
Nói cách khác, chỉ có tiêu đề được dịch ở đây, còn mọi thứ khác đều không được xử lý.
Cuối cùng, tôi đã làm hai việc:
-
Tách chúng thành các tin nhắn riêng biệt: prompt hệ thống với tất cả các quy tắc được đặt riêng, trong khi tài liệu cần dịch được đưa vào tin nhắn người dùng. Với cách tiếp cận này, cấu trúc dữ liệu logic đối với llm sẽ dễ phân biệt hơn.
-
Củng cố các quy tắc định dạng yaml.
Kết quả thu được như sau:
const fieldsYaml = fieldsToTranslate
.map(
({ field, value }) =>
`${field}: |\n${value
.split('\n')
.map((line) => ' ' + line)
.join('\n')}`,
)
.join('\n')
const fieldNames = fieldsToTranslate.map((f) => f.field)
const systemPrompt = `You are a professional translator specializing in technical documentation and web content.
# YAML OUTPUT RULES (CRITICAL)
You MUST output valid YAML with this EXACT structure:
\`\`\`
<lang_code>:
<field_name>: |
<translated text line 1>
<translated text line 2>
\`\`\`
**STRICT REQUIREMENTS:**
1. Each language code (en, de, etc.) MUST be at the ROOT level (no indentation)
2. Each field (name, description, content) MUST be indented with exactly 2 spaces under its language
3. Field values MUST use the literal block scalar (|) syntax
4. Text content MUST be indented with exactly 4 spaces (2 for field + 2 for content)
5. NEVER put fields at the root level - they MUST always be nested under a language code
# TRANSLATION RULES
1. **Translate, do not transliterate.** Convert meaning, not just letters.
2. Only include fields that were provided in the source.
3. Preserve all markdown and HTML formatting exactly.
4. Exception: Proper nouns, brand names, company names, and product names should be transliterated to Latin script.
# OUTPUT FORMAT
Respond ONLY with valid YAML. No markdown code blocks, no explanations, no comments - just raw YAML.`
const userPrompt = `Translate the following fields from Russian into: ${targetLangs.join(', ')}
## Source fields:
${fieldsYaml}
## Expected output structure:
${targetLangs
.map(
(lang) =>
`${lang}:\n${fieldNames.map((f) => ` ${f}: |\n <translated ${f}>`).join('\n')}`,
)
.join('\n')}`
const chatResponse = await llmChatCompletionResolver(
null,
{
input: {
provider: LlmProvider.OpenRouter,
messages: [
{
role: LLMChatMessageRole.system,
content: systemPrompt,
},
{
role: LLMChatMessageRole.user,
content: userPrompt,
},
],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
model: LlmModel.GEMINI_3_5_FLASH_LITE as any,
},
},
ctx,
)
const responseContent = chatResponse.choices?.[0]?.message?.content
Công bằng mà nói, ở đây các hướng dẫn kỹ thuật vẫn bị lẫn vào tin nhắn của người dùng một phần, nhưng dù sao thì nó cũng đã hoạt động ổn định hơn rất nhiều.