LanguageDetector
LanguageDetector 是一个输入处理器,用于识别输入文本的语言,并可选择将其翻译成目标语言以实现统一处理。该处理器通过检测接收消息的语言来帮助保持语言一致性,并提供处理多语言内容的灵活策略,包括自动翻译,以确保所有内容都以目标语言进行处理。
🌐 The LanguageDetector is an input processor that identifies the language of input text and optionally translates it to a target language for consistent processing. This processor helps maintain language consistency by detecting the language of incoming messages and providing flexible strategies for handling multilingual content, including automatic translation to ensure all content is processed in the target language.
使用示例Direct link to 使用示例
🌐 Usage example
import { LanguageDetector } from "@mastra/core/processors";
const processor = new LanguageDetector({
model: "openrouter/openai/gpt-oss-safeguard-20b",
targetLanguages: ["English", "en"],
threshold: 0.8,
strategy: "translate"
});
构造函数参数Direct link to 构造函数参数
🌐 Constructor parameters
options:
Options
Configuration options for language detection and translation
选项Direct link to 选项
🌐 Options
model:
MastraModelConfig
Model configuration for the detection/translation agent
targetLanguages?:
string[]
Target language(s) for the project. If content is detected in a different language, it may be translated. Can be language name ('English') or ISO code ('en')
threshold?:
number
Confidence threshold for language detection (0-1). Only process when detection confidence exceeds this threshold
strategy?:
'detect' | 'translate' | 'block' | 'warn'
Strategy when non-target language is detected: 'detect' only detects language, 'translate' automatically translates to target language, 'block' rejects content not in target language, 'warn' logs warning but allows through
preserveOriginal?:
boolean
Whether to preserve original content in message metadata. Useful for audit trails and debugging
instructions?:
string
Custom detection instructions for the agent. If not provided, uses default instructions
minTextLength?:
number
Minimum text length to perform detection. Short text is often unreliable for language detection
includeDetectionDetails?:
boolean
Whether to include detailed detection info in logs
translationQuality?:
'speed' | 'quality' | 'balanced'
Translation quality preference: 'speed' prioritizes fast translation, 'quality' prioritizes accuracy, 'balanced' balances between speed and quality
providerOptions?:
ProviderOptions
Provider-specific options passed to the internal detection agent. Use this to control model behavior like reasoning effort for thinking models (e.g., `{ openai: { reasoningEffort: 'low' } }`)
返回Direct link to 返回
🌐 Returns
id:
string
Processor identifier set to 'language-detector'
name?:
string
Optional processor display name
processInput:
(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never; tracingContext?: TracingContext }) => Promise<MastraDBMessage[]>
Processes input messages to detect language and optionally translate content before sending to LLM
扩展使用示例Direct link to 扩展使用示例
🌐 Extended usage example
src/mastra/agents/multilingual-agent.ts
import { Agent } from "@mastra/core/agent";
import { LanguageDetector } from "@mastra/core/processors";
export const agent = new Agent({
id: "multilingual-agent",
name: "multilingual-agent",
instructions: "You are a helpful assistant",
model: "openai/gpt-5.1",
inputProcessors: [
new LanguageDetector({
model: "openrouter/openai/gpt-oss-safeguard-20b",
targetLanguages: ["English", "en"],
threshold: 0.8,
strategy: "translate",
preserveOriginal: true,
instructions: "Detect language and translate non-English content to English while preserving original intent",
minTextLength: 10,
includeDetectionDetails: true,
translationQuality: "quality"
})
]
});
相关Direct link to 相关
🌐 Related