mirror of
https://github.com/denizsafak/abogen.git
synced 2026-07-18 13:40:27 +02:00
feat: Update LLM context handling and prompt template for improved clarity and legacy support
This commit is contained in:
@@ -727,12 +727,13 @@ def _normalize_with_llm(
|
|||||||
normalized_lines.append(line_body + newline)
|
normalized_lines.append(line_body + newline)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
paragraph_context = core
|
||||||
rewritten_sentences: List[str] = []
|
rewritten_sentences: List[str] = []
|
||||||
for sentence in sentences:
|
for sentence in sentences:
|
||||||
prompt_context = {
|
prompt_context = {
|
||||||
"text": sentence,
|
"text": sentence,
|
||||||
"sentence": sentence,
|
"sentence": sentence,
|
||||||
"paragraph": sentence,
|
"paragraph": paragraph_context,
|
||||||
}
|
}
|
||||||
prompt = _render_mustache(prompt_template, prompt_context)
|
prompt = _render_mustache(prompt_template, prompt_context)
|
||||||
completion = generate_completion(
|
completion = generate_completion(
|
||||||
|
|||||||
@@ -16,6 +16,13 @@ DEFAULT_LLM_PROMPT = (
|
|||||||
"Sentence: {{ sentence }}"
|
"Sentence: {{ sentence }}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_LEGACY_REWRITE_ONLY_PROMPT = (
|
||||||
|
"You are assisting with audiobook preparation. Rewrite the provided sentence so apostrophes and "
|
||||||
|
"contractions are unambiguous for text-to-speech. Respond with only the rewritten sentence.\n"
|
||||||
|
"Sentence: {{ sentence }}\n"
|
||||||
|
"Context: {{ paragraph }}"
|
||||||
|
)
|
||||||
|
|
||||||
_SETTINGS_DEFAULTS: Dict[str, Any] = {
|
_SETTINGS_DEFAULTS: Dict[str, Any] = {
|
||||||
"llm_base_url": "",
|
"llm_base_url": "",
|
||||||
"llm_api_key": "",
|
"llm_api_key": "",
|
||||||
@@ -67,7 +74,10 @@ def _environment_defaults() -> Dict[str, Any]:
|
|||||||
|
|
||||||
|
|
||||||
def environment_llm_defaults() -> Dict[str, Any]:
|
def environment_llm_defaults() -> Dict[str, Any]:
|
||||||
return dict(_environment_defaults())
|
defaults = dict(_environment_defaults())
|
||||||
|
if defaults:
|
||||||
|
_apply_llm_migrations(defaults)
|
||||||
|
return defaults
|
||||||
|
|
||||||
|
|
||||||
def _coerce_bool(value: Any, default: bool) -> bool:
|
def _coerce_bool(value: Any, default: bool) -> bool:
|
||||||
@@ -89,6 +99,16 @@ def _coerce_float(value: Any, default: float) -> float:
|
|||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_llm_migrations(settings: Dict[str, Any]) -> None:
|
||||||
|
prompt_value = str(settings.get("llm_prompt") or "")
|
||||||
|
if prompt_value.strip() == _LEGACY_REWRITE_ONLY_PROMPT.strip():
|
||||||
|
settings["llm_prompt"] = DEFAULT_LLM_PROMPT
|
||||||
|
|
||||||
|
context_mode = str(settings.get("llm_context_mode") or "").strip().lower()
|
||||||
|
if context_mode != "sentence":
|
||||||
|
settings["llm_context_mode"] = "sentence"
|
||||||
|
|
||||||
|
|
||||||
def _extract_settings(source: Mapping[str, Any]) -> Dict[str, Any]:
|
def _extract_settings(source: Mapping[str, Any]) -> Dict[str, Any]:
|
||||||
env_defaults = _environment_defaults()
|
env_defaults = _environment_defaults()
|
||||||
extracted: Dict[str, Any] = {}
|
extracted: Dict[str, Any] = {}
|
||||||
@@ -105,6 +125,7 @@ def _extract_settings(source: Mapping[str, Any]) -> Dict[str, Any]:
|
|||||||
extracted[key] = _coerce_float(raw_value, default)
|
extracted[key] = _coerce_float(raw_value, default)
|
||||||
else:
|
else:
|
||||||
extracted[key] = str(raw_value or "") if isinstance(default, str) else raw_value
|
extracted[key] = str(raw_value or "") if isinstance(default, str) else raw_value
|
||||||
|
_apply_llm_migrations(extracted)
|
||||||
return extracted
|
return extracted
|
||||||
|
|
||||||
|
|
||||||
@@ -148,4 +169,5 @@ def apply_overrides(base: Mapping[str, Any], overrides: Mapping[str, Any]) -> Di
|
|||||||
if key not in _SETTINGS_DEFAULTS:
|
if key not in _SETTINGS_DEFAULTS:
|
||||||
continue
|
continue
|
||||||
merged[key] = value
|
merged[key] = value
|
||||||
|
_apply_llm_migrations(merged)
|
||||||
return merged
|
return merged
|
||||||
|
|||||||
@@ -2025,7 +2025,7 @@ def _normalize_setting_value(key: str, value: Any, defaults: Dict[str, Any]) ->
|
|||||||
if key == "llm_context_mode":
|
if key == "llm_context_mode":
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
normalized_scope = value.strip().lower()
|
normalized_scope = value.strip().lower()
|
||||||
if normalized_scope in {"sentence", "paragraph"}:
|
if normalized_scope == "sentence":
|
||||||
return normalized_scope
|
return normalized_scope
|
||||||
return defaults[key]
|
return defaults[key]
|
||||||
if key == "llm_prompt":
|
if key == "llm_prompt":
|
||||||
|
|||||||
@@ -209,7 +209,7 @@
|
|||||||
<div class="field">
|
<div class="field">
|
||||||
<label for="llm_prompt">Prompt Template</label>
|
<label for="llm_prompt">Prompt Template</label>
|
||||||
<textarea id="llm_prompt" name="llm_prompt" rows="6">{{ settings.llm_prompt }}</textarea>
|
<textarea id="llm_prompt" name="llm_prompt" rows="6">{{ settings.llm_prompt }}</textarea>
|
||||||
<p class="hint">Use placeholders like <code>{{ '{{sentence}}' }}</code> and <code>{{ '{{paragraph}}' }}</code> to inject content.</p>
|
<p class="hint">Use <code>{{ '{{sentence}}' }}</code> for the active sentence. <code>{{ '{{paragraph}}' }}</code> remains available for legacy prompts.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="field field--choices">
|
<div class="field field--choices">
|
||||||
<span class="field__label">Context Mode</span>
|
<span class="field__label">Context Mode</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user