| from typing import Dict, Any |
| from .base_agent import LLMAgent |
|
|
|
|
| class LanguageDetectionAgent(LLMAgent): |
| name = "language_detection_agent" |
| description = ( |
| "Detects document language and handles multilingual content appropriately" |
| ) |
|
|
| def build_prompt(self, text: str) -> str: |
| sample = text[:500] if text else "No text available" |
| return f"""Detect the language of this text sample: |
| |
| {sample} |
| |
| Respond with: |
| 1. Primary language (name and ISO code) |
| 2. Confidence level (high/medium/low) |
| 3. Are there multiple languages present? |
| 4. Any special characters or scripts detected? |
| |
| Be specific about the detection basis.""" |
|
|
| def execute(self, text: str, reasoning: str) -> Dict[str, Any]: |
| return { |
| "language": self._extract_lang(reasoning), |
| "confidence": self._extract_confidence(reasoning), |
| "detected": True, |
| "multilingual": "multiple" in reasoning.lower(), |
| "reasoning": reasoning, |
| } |
|
|
| def _extract_lang(self, reasoning: str) -> str: |
| reasoning_lower = reasoning.lower() |
| langs = { |
| "estonian": "et", |
| "english": "en", |
| "german": "de", |
| "russian": "ru", |
| "finnish": "fi", |
| "swedish": "sv", |
| "latvian": "lv", |
| "lithuanian": "lt", |
| } |
| for name, code in langs.items(): |
| if name in reasoning_lower: |
| return code |
| return "en" |
|
|
| def _extract_confidence(self, reasoning: str) -> float: |
| if "high" in reasoning.lower(): |
| return 0.9 |
| if "medium" in reasoning.lower(): |
| return 0.7 |
| return 0.5 |
|
|