feat: Implement translation prompt history injection for Chat/Mic/Speaker
- Added a history management system in model.py to store and retrieve recent messages from Chat, Mic, and Speaker. - Updated controller.py to automatically add messages to the translation history after processing. - Enhanced translation clients (OpenAI, Gemini, Groq, etc.) to accept and utilize context history for improved translation quality. - Introduced YAML configuration options for enabling history injection and customizing its behavior across different translation models. - Ensured that only original messages are stored in history to optimize token usage during translation.
This commit is contained in:
@@ -76,6 +76,16 @@ class OpenRouterClient:
|
||||
prompt_config = loadTranslatePromptConfig(root_path, "translation_openrouter.yml")
|
||||
self.supported_languages = list(translation_lang["OpenRouter_API"]["source"].keys())
|
||||
self.prompt_template = prompt_config["system_prompt"]
|
||||
# history config (optional)
|
||||
self.history_cfg = prompt_config.get("history", {
|
||||
"use_history": False,
|
||||
"sources": [],
|
||||
"max_messages": 0,
|
||||
"max_chars": 0,
|
||||
"header_template": "",
|
||||
"item_template": "[{source}] {role}: {text}",
|
||||
})
|
||||
self._context_history: list[dict] = []
|
||||
|
||||
self.openrouter_llm = None
|
||||
|
||||
@@ -109,12 +119,58 @@ class OpenRouterClient:
|
||||
streaming=False,
|
||||
)
|
||||
|
||||
def setContextHistory(self, history_items: list[dict]) -> None:
|
||||
"""Set recent conversation history for prompt injection.
|
||||
|
||||
Each item should be a dict containing:
|
||||
- source: "chat" | "mic" | "speaker"
|
||||
- text: message string
|
||||
- timestamp: ISO format datetime string
|
||||
"""
|
||||
self._context_history = history_items or []
|
||||
|
||||
def translate(self, text: str, input_lang: str, output_lang: str) -> str:
|
||||
system_prompt = self.prompt_template.format(
|
||||
supported_languages=self.supported_languages,
|
||||
input_lang=input_lang,
|
||||
output_lang=output_lang,
|
||||
)
|
||||
|
||||
# Inject recent conversation history if enabled by YAML config
|
||||
if self.history_cfg.get("use_history"):
|
||||
allowed_sources = set(self.history_cfg.get("sources", []))
|
||||
max_messages = int(self.history_cfg.get("max_messages", 0))
|
||||
max_chars = int(self.history_cfg.get("max_chars", 0))
|
||||
item_tmpl = self.history_cfg.get("item_template", "[{source}] {role}: {text}")
|
||||
header_tmpl = self.history_cfg.get("header_template", "{history}")
|
||||
|
||||
filtered = [h for h in self._context_history if h.get("source") in allowed_sources]
|
||||
recent = filtered[-max_messages:] if max_messages > 0 else filtered
|
||||
formatted_items = []
|
||||
for h in recent:
|
||||
# Format timestamp as HH:MM to save tokens
|
||||
timestamp_str = ''
|
||||
if 'timestamp' in h:
|
||||
from datetime import datetime
|
||||
try:
|
||||
ts = datetime.fromisoformat(h['timestamp'])
|
||||
timestamp_str = ts.strftime('%H:%M')
|
||||
except:
|
||||
timestamp_str = ''
|
||||
formatted_items.append(
|
||||
item_tmpl.format(
|
||||
timestamp=timestamp_str,
|
||||
source=h.get("source", ""),
|
||||
text=h.get("text", ""),
|
||||
)
|
||||
)
|
||||
history_blob = "\n".join(formatted_items).strip()
|
||||
if max_chars and len(history_blob) > max_chars:
|
||||
history_blob = history_blob[-max_chars:]
|
||||
history_header = header_tmpl.format(max_messages=max_messages, history=history_blob)
|
||||
if history_header:
|
||||
system_prompt = f"{system_prompt}\n\n{history_header}"
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": text},
|
||||
|
||||
Reference in New Issue
Block a user