From 1331dcc2e4ceb3dfa51d89c7f426a625d1ef71d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Mon, 11 May 2026 22:22:44 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B0=D0=B2=D1=82=D0=BE=D0=BF=D0=BE=D0=B4=D0=B1=D0=BE?= =?UTF-8?q?=D1=80=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B5=D0=B9=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=20DevConsole?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/model_router.py | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 backend/model_router.py diff --git a/backend/model_router.py b/backend/model_router.py new file mode 100644 index 0000000..e2b20aa --- /dev/null +++ b/backend/model_router.py @@ -0,0 +1,56 @@ +from enum import Enum + +from backend.config_store import get_setting + + +class TaskType(str, Enum): + GENERAL = 'general' + CODING = 'coding' + SERVER = 'server' + ANDROID = 'android' + TEST = 'test' + CHEAP = 'cheap' + + +DEFAULT_MODELS = { + 'default': 'gpt-5', + 'coding': 'gpt-5', + 'cheap': 'gpt-5-mini', +} + + +def get_model_profile() -> dict: + return { + 'default': get_setting('OPENAI_MODEL_DEFAULT') or get_setting('OPENAI_MODEL') or DEFAULT_MODELS['default'], + 'coding': get_setting('OPENAI_MODEL_CODING') or DEFAULT_MODELS['coding'], + 'cheap': get_setting('OPENAI_MODEL_CHEAP') or DEFAULT_MODELS['cheap'], + } + + +def choose_model(task_type: str | None = None, prompt: str = '') -> str: + profile = get_model_profile() + normalized_type = (task_type or '').strip().lower() + text = prompt.lower() + + if normalized_type in {TaskType.CODING, TaskType.SERVER, TaskType.ANDROID, TaskType.TEST}: + return profile['coding'] + + if normalized_type == TaskType.CHEAP: + return profile['cheap'] + + coding_markers = [ + 'код', 'ошибка', 'traceback', 'exception', 'pytest', 'php', 'python', 'fastapi', + 'android', 'flutter', 'gradle', 'apk', 'server', 'docker', 'nginx', 'systemd', + 'build', 'test', 'fix', 'refactor', 'repository', 'git', 'api', 'sql' + ] + cheap_markers = [ + 'кратко', 'название', 'заголовок', 'переведи', 'summary', 'classify' + ] + + if any(marker in text for marker in coding_markers): + return profile['coding'] + + if len(text) < 500 and any(marker in text for marker in cheap_markers): + return profile['cheap'] + + return profile['default']