montana/Русский/Логистика/patch_anti_hallucination5.py

98 lines
5.3 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
"""v3.40.10: Final anti-hallucination — ban 'Рекомендация:' header pattern,
strengthen cargo/caspian rules. Fix QA detector false positives."""
fixes = 0
PATH = '/opt/app/seafare_agent.py'
def read():
with open(PATH, 'r') as f:
return f.read()
def write(c):
with open(PATH, 'w') as f:
f.write(c)
c = read()
# ============================================================
# FIX 1: Add explicit ban of "Рекомендация:" as a header/bold pattern
# The AI bypasses "Рекомендации" ban by using singular "Рекомендация:"
# ============================================================
old = """ × "Рекомендации" / "Recommendations" / "Analysis"
× "Альтернативные варианты" / "Альтернативный вариант" / "Alternatives"
× "Проверенные компании" / "Проверенные альтернативы" / "Проверенные агенты"
× "Рабочие решения" / "Working solutions" / "Verified contacts"
× "Ключевые нюансы" / "Key considerations" / "Important notes"
× "Особенности региона" / "Что делать дальше" / "Важно учитывать"
× "Что можно сделать" / "Как связаться" (after not-found)
× "Оптимальный" / "Optimal" / "Подходит для" / "Suitable for" / "Идеально для"
× Draft capacity, route suitability, business advice
× Caspian Sea characteristics (draft limits, canal constraints)
× Lists of companies with emails/phones NOT from our tools
× ANY content after saying "не найдено" / "not found" (except 1-line follow-up)"""
new = """ × "Рекомендации" / "Рекомендация:" / "Recommendations" / "Analysis"
× "Альтернативные варианты" / "Альтернативный вариант" / "Alternatives"
× "Проверенные компании" / "Проверенные альтернативы" / "Проверенные агенты"
× "Рабочие решения" / "Working solutions" / "Verified contacts"
× "Ключевые нюансы" / "Key considerations" / "Important notes"
× "Особенности региона" / "Что делать дальше" / "Важно учитывать"
× "Что можно сделать" / "Как связаться" (after not-found)
× "Оптимальный" / "оптимальн" / "Optimal" / "Подходит для" / "Suitable for" / "Идеально для"
× "Рекомендация:" as bold/header even singular form is BANNED
× Draft capacity, route suitability, business advice
× Caspian Sea characteristics (draft limits, canal constraints)
× Lists of companies with emails/phones NOT from our tools
× ANY content after saying "не найдено" / "not found" (except 1-line follow-up)
× NEVER use the word "рекомендация/рекомендую/рекомендуем" in ANY form you are a data tool, not an advisor"""
if old in c:
c = c.replace(old, new, 1)
fixes += 1
print("FIX 1: Banned singular 'Рекомендация:' + all forms of рекомендую/рекомендуем")
else:
print("FIX 1: marker not found")
# ============================================================
# FIX 2: Strengthen "NOT FOUND" section for cargo queries
# The AI finds no vessels but then adds "Рекомендация: проверить..."
# ============================================================
old = """- **NOT FOUND response template (MANDATORY):**
"Не найдено в нашей базе." / "Not found in our database."
Then ONE of: "Попробовать другой запрос?" / "Search by IMO?" / "Уточнить название?"
That's it. FULL STOP. Do not write ANYTHING else after this."""
new = """- **NOT FOUND / EMPTY RESULTS response (MANDATORY):**
"Не найдено в нашей базе." / "Not found in our database." / "В текущем радиусе не обнаружено."
Then ONE of: "Попробовать другой запрос?" / "Search by IMO?" / "Уточнить название?" / "Расширить радиус?"
That's it. FULL STOP. Do not write ANYTHING else after this.
ESPECIALLY do NOT add: "Рекомендация:", "Проверьте", "Свяжитесь с", "Обратите внимание", analysis of port depths/drafts.
When cargo search returns 0 vessels say "не обнаружено" + offer to change search params. NOTHING MORE."""
if old in c:
c = c.replace(old, new, 1)
fixes += 1
print("FIX 2: NOT FOUND template strengthened for cargo/caspian")
else:
print("FIX 2: marker not found")
write(c)
# ============================================================
# FIX 3: Version bump
# ============================================================
PATH_CFG = '/opt/app/config.py'
c2 = open(PATH_CFG).read()
old = "APP_VERSION = '3.40.9'"
new = "APP_VERSION = '3.40.10'"
if old in c2:
with open(PATH_CFG, 'w') as f:
f.write(c2.replace(old, new, 1))
fixes += 1
print("FIX 3: Version → 3.40.10")
else:
print("FIX 3: version marker not found")
print(f"\nTotal fixes: {fixes}")