Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions backend/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

# from apps.system.models.user import SQLModel # noqa
# from apps.settings.models.setting_models import SQLModel
from apps.chat.models.chat_model import SQLModel
from apps.terminology.models.terminology_model import SQLModel
#from apps.chat.models.chat_model import SQLModel
#from apps.terminology.models.terminology_model import SQLModel
#from apps.custom_prompt.models.custom_prompt_model import SQLModel
from apps.data_training.models.data_training_model import SQLModel
#from apps.data_training.models.data_training_model import SQLModel
# from apps.dashboard.models.dashboard_model import SQLModel
from common.core.config import settings # noqa
#from apps.datasource.models.datasource import SQLModel
from apps.system.models.system_model import SQLModel

target_metadata = SQLModel.metadata

Expand Down
30 changes: 30 additions & 0 deletions backend/alembic/versions/066_update_assistant_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""066_update_assistant_model

Revision ID: 8adc3a4919be
Revises: 8ff90df7871d
Create Date: 2026-04-28 15:55:42.757276

"""
from alembic import op
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '8adc3a4919be'
down_revision = '8ff90df7871d'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('sys_assistant', sa.Column('enable_custom_model', sa.Boolean(), nullable=True))
op.add_column('sys_assistant', sa.Column('custom_model', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True))
# ### end Alembic commands ###


def downgrade():
op.drop_column('sys_assistant', 'custom_model')
op.drop_column('sys_assistant', 'enable_custom_model')
# ### end Alembic commands ###
29 changes: 20 additions & 9 deletions backend/apps/ai_model/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from common.utils.utils import prepare_model_arg
from langchain_community.llms import VLLMOpenAI
from langchain_openai import AzureChatOpenAI


# from langchain_community.llms import Tongyi, VLLM

class LLMConfig(BaseModel):
Expand All @@ -24,16 +26,17 @@ class LLMConfig(BaseModel):
api_key: Optional[str] = None
api_base_url: Optional[str] = None
additional_params: Dict[str, Any] = {}

class Config:
frozen = True

def __hash__(self):
if hasattr(self, 'additional_params') and isinstance(self.additional_params, dict):
hashable_params = frozenset((k, tuple(v) if isinstance(v, (list, dict)) else v)
for k, v in self.additional_params.items())
hashable_params = frozenset((k, tuple(v) if isinstance(v, (list, dict)) else v)
for k, v in self.additional_params.items())
else:
hashable_params = None

return hash((
self.model_id,
self.model_type,
Expand Down Expand Up @@ -61,6 +64,7 @@ def llm(self) -> BaseChatModel:
"""Return the langchain LLM instance"""
return self._llm


class OpenAIvLLM(BaseLLM):
def _init_llm(self) -> VLLMOpenAI:
return VLLMOpenAI(
Expand All @@ -71,6 +75,7 @@ def _init_llm(self) -> VLLMOpenAI:
**self.config.additional_params,
)


class OpenAIAzureLLM(BaseLLM):
def _init_llm(self) -> AzureChatOpenAI:
api_version = self.config.additional_params.get("api_version")
Expand All @@ -88,6 +93,8 @@ def _init_llm(self) -> AzureChatOpenAI:
streaming=True,
**self.config.additional_params,
)


class OpenAILLM(BaseLLM):
def _init_llm(self) -> BaseChatModel:
return BaseChatOpenAI(
Expand Down Expand Up @@ -138,26 +145,30 @@ def register_llm(cls, model_type: str, llm_class: Type[BaseLLM]):
return config """


async def get_default_config() -> LLMConfig:
async def get_default_config(custom_model_id: Optional[int] = None) -> LLMConfig:
with Session(engine) as session:
db_model = session.exec(
select(AiModelDetail).where(AiModelDetail.default_model == True)
).first()
db_model: AiModelDetail | None = None
if custom_model_id:
db_model = session.get(AiModelDetail, custom_model_id)
if not db_model:
db_model = session.exec(
select(AiModelDetail).where(AiModelDetail.default_model == True)
).first()
if not db_model:
raise Exception("The system default model has not been set")

additional_params = {}
if db_model.config:
try:
config_raw = json.loads(db_model.config)
additional_params = {item["key"]: prepare_model_arg(item.get('val')) for item in config_raw if "key" in item and "val" in item}
additional_params = {item["key"]: prepare_model_arg(item.get('val')) for item in config_raw if
"key" in item and "val" in item}
except Exception:
pass
if not db_model.api_domain.startswith("http"):
db_model.api_domain = await sqlbot_decrypt(db_model.api_domain)
if db_model.api_key:
db_model.api_key = await sqlbot_decrypt(db_model.api_key)


# 构造 LLMConfig
return LLMConfig(
Expand Down
9 changes: 8 additions & 1 deletion backend/apps/chat/task/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from concurrent.futures import ThreadPoolExecutor, Future
from datetime import datetime
from dis import specialized
from typing import Any, List, Optional, Union, Dict, Iterator

import orjson
Expand Down Expand Up @@ -174,7 +175,13 @@ def __init__(self, session: Session, current_user: CurrentUser, chat_question: C

@classmethod
async def create(cls, *args, **kwargs):
config: LLMConfig = await get_default_config()
specialized_model_id = None
if args[3]:
if args[3].enable_custom_model:
if args[3].custom_model:
specialized_model_id = args[3].custom_model
print("use custom model: id[" + args[3].custom_model + "]")
config: LLMConfig = await get_default_config(specialized_model_id)
instance = cls(*args, **kwargs, config=config)

chat_params: list[SysArgModel] = await get_groups(args[0], "chat")
Expand Down
2 changes: 2 additions & 0 deletions backend/apps/swagger/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
"assistant_type": "Assistant Type (0: Basic, 1: Advanced, 4: Page)",
"assistant_configuration": "Configuration",
"assistant_description": "Description",
"assistant_enableCustomModel": "Use specified model",
"assistant_customModel": "Large Language Model",

"system_embedded_api": "Page Embedded API",
"embedded_resetsecret_api": "Reset Secret",
Expand Down
2 changes: 2 additions & 0 deletions backend/apps/swagger/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
"assistant_type": "助手类型(0: 基础, 1: 高级, 4: 页面)",
"assistant_configuration": "配置",
"assistant_description": "描述",
"assistant_enableCustomModel": "使用指定大模型",
"assistant_customModel": "大语言模型",

"system_embedded_api": "页面嵌入式api",
"embedded_resetsecret_api": "重置 Secret",
Expand Down
2 changes: 2 additions & 0 deletions backend/apps/system/models/system_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class AssistantBaseModel(SQLModel):
app_id: Optional[str] = Field(default=None, max_length=255, nullable=True)
app_secret: Optional[str] = Field(default=None, max_length=255, nullable=True)
oid: Optional[int] = Field(nullable=True, sa_type=BigInteger(), default=1)
enable_custom_model: Optional[bool] = Field(default=False, nullable=True)
custom_model: Optional[str] = Field(default=None, max_length=255, nullable=True)

class AssistantModel(SnowflakeBase, AssistantBaseModel, table=True):
__tablename__ = "sys_assistant"
Expand Down
2 changes: 2 additions & 0 deletions backend/apps/system/schemas/system_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class AssistantBase(BaseModel):
configuration: Optional[str] = Field(default=None, description=f"{PLACEHOLDER_PREFIX}assistant_configuration")
description: Optional[str] = Field(default=None, description=f"{PLACEHOLDER_PREFIX}assistant_description")
oid: Optional[int] = Field(default=1, description=f"{PLACEHOLDER_PREFIX}oid")
enable_custom_model: Optional[bool] = Field(default=False, description=f"{PLACEHOLDER_PREFIX}oid")
custom_model: Optional[str] = Field(description=f"{PLACEHOLDER_PREFIX}oid")


class AssistantDTO(AssistantBase, BaseCreatorDTO):
Expand Down
2 changes: 1 addition & 1 deletion backend/templates/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ template:
generate_rules: |
以下是你必须遵守的规则和可以参考的基础示例:
<Rules>
<rule>
<rule priority="critical">
你只能生成查询用的SQL语句,不得生成增删改相关或操作数据库以及操作数据库数据的SQL
</rule>
<rule>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@
"application_name": "Application name",
"application_description": "Application description",
"cross_domain_settings": "Cross-domain settings",
"enableCustomModel": "Use specified model",
"third_party_address": "Please enter the embedded third party address,multiple items separated by semicolons",
"set_to_private": "Set as private",
"set_to_public": "Set as public",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/ko-KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@
"application_name": "애플리케이션 이름",
"application_description": "애플리케이션 설명",
"cross_domain_settings": "교차 도메인 설정",
"enableCustomModel": "지정된 모델 사용",
"third_party_address": "임베디드할 제3자 주소를 입력하십시오, 여러 항목을 세미콜론으로 구분",
"set_to_private": "비공개로 설정",
"set_to_public": "공개로 설정",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@
"application_name": "应用名称",
"application_description": "应用描述",
"cross_domain_settings": "跨域设置",
"enableCustomModel": "使用指定大模型",
"third_party_address": "请输入嵌入的第三方地址,多个以分号分割",
"set_to_private": "设为私有",
"set_to_public": "设为公共",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@
"application_name": "應用名稱",
"application_description": "應用描述",
"cross_domain_settings": "跨網域設定",
"enableCustomModel": "使用指定模型",
"third_party_address": "請輸入嵌入的第三方位址,多個以分號分割",
"set_to_private": "設為私有",
"set_to_public": "設為公共",
Expand Down
Loading
Loading