feat: make chat model routing configurable

This commit is contained in:
2026-07-31 17:57:26 +08:00
parent 85a6da5949
commit ab2c945f0b
22 changed files with 567 additions and 88 deletions

View File

@@ -25,7 +25,9 @@ def upgrade() -> None:
sa.Column("period_start", sa.DateTime(), nullable=False),
sa.Column("period_end", sa.DateTime(), nullable=False),
sa.Column("title", sa.String(length=160), nullable=False),
sa.Column("content", sa.Text(), nullable=False, server_default=""),
# MySQL 8.4 rejects literal defaults on TEXT columns. The ORM already
# supplies an empty string while a report job is pending.
sa.Column("content", sa.Text(), nullable=False),
sa.Column("source_summary_ids", sa.Text(), nullable=True),
sa.Column("source_topic_ids", sa.Text(), nullable=True),
sa.Column("model_name", sa.String(length=100), nullable=True),

View File

@@ -1,83 +1,79 @@
"""persist cleaned question insights
Revision ID: 0020_question_insight_persistence
Revision ID: 0020_question_insight_store
Revises: 0019_periodic_reports
Create Date: 2026-07-31 00:00:00.000000
"""
from __future__ import annotations
from alembic import op
from alembic import context, op
import sqlalchemy as sa
revision = "0020_question_insight_persistence"
revision = "0020_question_insight_store"
down_revision = "0019_periodic_reports"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"sys_question_insight_cleaned_question",
sa.Column(
"id",
sa.BigInteger().with_variant(sa.Integer(), "sqlite"),
primary_key=True,
autoincrement=True,
),
sa.Column("message_id", sa.BigInteger(), nullable=False),
sa.Column("session_id", sa.BigInteger(), nullable=False),
sa.Column("user_id", sa.BigInteger(), nullable=False),
sa.Column("part_index", sa.Integer(), nullable=False),
sa.Column("cleaner_version", sa.String(length=20), nullable=False),
sa.Column("source_hash", sa.String(length=64), nullable=False),
sa.Column("cleaned_text", sa.Text(), nullable=False),
sa.Column("normalized_text", sa.Text(), nullable=False),
sa.Column("category", sa.String(length=50), nullable=False, server_default="other"),
sa.Column("tokens_json", sa.Text(), nullable=False),
sa.Column("accepted", sa.Integer(), nullable=False, server_default="1"),
sa.Column("filtered_reason", sa.String(length=100), nullable=True),
sa.Column("source_created_at", sa.DateTime(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(
["message_id"],
["sys_chat_message.id"],
ondelete="CASCADE",
),
sa.UniqueConstraint(
"message_id",
"cleaner_version",
"part_index",
name="uq_question_insight_message_version_part",
),
)
op.create_index(
"ix_sys_question_insight_cleaned_question_session_id",
"sys_question_insight_cleaned_question",
["session_id"],
)
op.create_index(
"ix_sys_question_insight_cleaned_question_user_id",
"sys_question_insight_cleaned_question",
["user_id"],
)
op.create_index(
"ix_question_insight_version_accepted_created",
"sys_question_insight_cleaned_question",
["cleaner_version", "accepted", "source_created_at"],
)
op.create_index(
"ix_question_insight_category_created",
"sys_question_insight_cleaned_question",
["category", "source_created_at"],
)
op.create_index(
"ix_question_insight_session_created",
"sys_question_insight_cleaned_question",
["session_id", "source_created_at"],
)
table_name = "sys_question_insight_cleaned_question"
table_exists = False
existing_indexes: set[str] = set()
if not context.is_offline_mode():
inspector = sa.inspect(op.get_bind())
table_exists = inspector.has_table(table_name)
if table_exists:
existing_indexes = {item["name"] for item in inspector.get_indexes(table_name)}
if not table_exists:
op.create_table(
table_name,
sa.Column(
"id",
sa.BigInteger().with_variant(sa.Integer(), "sqlite"),
primary_key=True,
autoincrement=True,
),
sa.Column("message_id", sa.BigInteger(), nullable=False),
sa.Column("session_id", sa.BigInteger(), nullable=False),
sa.Column("user_id", sa.BigInteger(), nullable=False),
sa.Column("part_index", sa.Integer(), nullable=False),
sa.Column("cleaner_version", sa.String(length=20), nullable=False),
sa.Column("source_hash", sa.String(length=64), nullable=False),
sa.Column("cleaned_text", sa.Text(), nullable=False),
sa.Column("normalized_text", sa.Text(), nullable=False),
sa.Column("category", sa.String(length=50), nullable=False, server_default="other"),
sa.Column("tokens_json", sa.Text(), nullable=False),
sa.Column("accepted", sa.Integer(), nullable=False, server_default="1"),
sa.Column("filtered_reason", sa.String(length=100), nullable=True),
sa.Column("source_created_at", sa.DateTime(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.ForeignKeyConstraint(
["message_id"],
["sys_chat_message.id"],
ondelete="CASCADE",
),
sa.UniqueConstraint(
"message_id",
"cleaner_version",
"part_index",
name="uq_question_insight_message_version_part",
),
)
indexes = {
"ix_sys_question_insight_cleaned_question_session_id": ["session_id"],
"ix_sys_question_insight_cleaned_question_user_id": ["user_id"],
"ix_question_insight_version_accepted_created": ["cleaner_version", "accepted", "source_created_at"],
"ix_question_insight_category_created": ["category", "source_created_at"],
"ix_question_insight_session_created": ["session_id", "source_created_at"],
}
for index_name, columns in indexes.items():
if index_name not in existing_indexes:
op.create_index(index_name, table_name, columns)
def downgrade() -> None:

View File

@@ -1,7 +1,7 @@
"""add durable periodic report job fields
Revision ID: 0021_periodic_report_async_jobs
Revises: 0020_question_insight_persistence
Revises: 0020_question_insight_store
Create Date: 2026-07-31 00:00:00.000000
"""
@@ -12,7 +12,7 @@ import sqlalchemy as sa
revision = "0021_periodic_report_async_jobs"
down_revision = "0020_question_insight_persistence"
down_revision = "0020_question_insight_store"
branch_labels = None
depends_on = None

View File

@@ -0,0 +1,71 @@
"""add simple knowledge model capability
Revision ID: 0023_simple_knowledge_route
Revises: 0022_model_routing
"""
from alembic import op
import sqlalchemy as sa
revision = "0023_simple_knowledge_route"
down_revision = "0022_model_routing"
branch_labels = None
depends_on = None
def upgrade() -> None:
# Existing models retain the capability so deployment behavior stays on the
# current default model until an administrator explicitly reassigns it.
op.add_column(
"sys_model",
sa.Column("allow_simple_knowledge", sa.Integer(), nullable=False, server_default="1"),
)
# Materialize the new three-state setting so the admin UI and runtime show
# the same value even when the legacy emergency switch had been disabled.
op.execute(
"""
INSERT IGNORE INTO sys_system_config (config_key, config_value, description)
SELECT
'chat_model_routing_mode',
CASE
WHEN EXISTS (
SELECT 1
FROM sys_system_config
WHERE config_key = 'fixed_info_model_routing_enabled'
AND LOWER(TRIM(config_value)) NOT IN ('1', 'true', 'yes', 'on', '启用')
) THEN 'off'
ELSE 'fixed_only'
END,
'正式问答模型分流规则off / fixed_only / conservative'
"""
)
def downgrade() -> None:
# Preserve the closest legacy behavior before removing the new setting.
op.execute(
"""
UPDATE sys_system_config AS legacy
JOIN sys_system_config AS current_mode
ON current_mode.config_key = 'chat_model_routing_mode'
SET legacy.config_value = CASE
WHEN current_mode.config_value = 'off' THEN 'false'
ELSE 'true'
END
WHERE legacy.config_key = 'fixed_info_model_routing_enabled'
"""
)
op.execute(
"""
INSERT IGNORE INTO sys_system_config (config_key, config_value, description)
SELECT
'fixed_info_model_routing_enabled',
CASE WHEN config_value = 'off' THEN 'false' ELSE 'true' END,
'固定信息模型分流'
FROM sys_system_config
WHERE config_key = 'chat_model_routing_mode'
"""
)
op.execute("DELETE FROM sys_system_config WHERE config_key = 'chat_model_routing_mode'")
op.drop_column("sys_model", "allow_simple_knowledge")