|
17 | 17 | from langchain_core.messages import BaseMessage, SystemMessage, HumanMessage, AIMessage, BaseMessageChunk |
18 | 18 | from sqlalchemy import and_, select |
19 | 19 | from sqlalchemy.orm import sessionmaker, scoped_session |
| 20 | +from sqlbot_xpack.config.model import SysArgModel |
20 | 21 | from sqlbot_xpack.custom_prompt.curd.custom_prompt import find_custom_prompts |
21 | 22 | from sqlbot_xpack.custom_prompt.models.custom_prompt_model import CustomPromptTypeEnum |
22 | 23 | from sqlbot_xpack.license.license_manage import SQLBotLicenseUtil |
|
39 | 40 | from apps.datasource.models.datasource import CoreDatasource |
40 | 41 | from apps.db.db import exec_sql, get_version, check_connection |
41 | 42 | from apps.system.crud.assistant import AssistantOutDs, AssistantOutDsFactory, get_assistant_ds |
| 43 | +from apps.system.crud.parameter_manage import get_groups |
42 | 44 | from apps.system.schemas.system_schema import AssistantOutDsSchema |
43 | 45 | from apps.terminology.curd.terminology import get_terminology_template |
44 | 46 | from common.core.config import settings |
@@ -86,6 +88,8 @@ class LLMService: |
86 | 88 | last_execute_sql_error: str = None |
87 | 89 | articles_number: int = 4 |
88 | 90 |
|
| 91 | + enable_sql_row_limit: bool = settings.GENERATE_SQL_QUERY_LIMIT_ENABLED |
| 92 | + |
89 | 93 | def __init__(self, session: Session, current_user: CurrentUser, chat_question: ChatQuestion, |
90 | 94 | current_assistant: Optional[CurrentAssistant] = None, no_reasoning: bool = False, |
91 | 95 | embedding: bool = False, config: LLMConfig = None): |
@@ -152,6 +156,14 @@ def __init__(self, session: Session, current_user: CurrentUser, chat_question: C |
152 | 156 | async def create(cls, *args, **kwargs): |
153 | 157 | config: LLMConfig = await get_default_config() |
154 | 158 | instance = cls(*args, **kwargs, config=config) |
| 159 | + |
| 160 | + chat_params: list[SysArgModel] = await get_groups(args[0], "chat") |
| 161 | + for config in chat_params: |
| 162 | + if config.pkey == 'chat.limit_rows': |
| 163 | + if config.pval.lower().strip() == 'true': |
| 164 | + instance.enable_sql_row_limit = True |
| 165 | + else: |
| 166 | + instance.enable_sql_row_limit = False |
155 | 167 | return instance |
156 | 168 |
|
157 | 169 | def is_running(self, timeout=0.5): |
@@ -179,7 +191,7 @@ def init_messages(self): |
179 | 191 | self.sql_message = [] |
180 | 192 | # add sys prompt |
181 | 193 | self.sql_message.append(SystemMessage( |
182 | | - content=self.chat_question.sql_sys_question(self.ds.type, settings.GENERATE_SQL_QUERY_LIMIT_ENABLED))) |
| 194 | + content=self.chat_question.sql_sys_question(self.ds.type, self.enable_sql_row_limit))) |
183 | 195 | if last_sql_messages is not None and len(last_sql_messages) > 0: |
184 | 196 | # limit count |
185 | 197 | for last_sql_message in last_sql_messages[count_limit:]: |
@@ -861,7 +873,7 @@ def save_sql_data(self, session: Session, data_obj: Dict[str, Any]): |
861 | 873 | limit = 1000 |
862 | 874 | if data_result: |
863 | 875 | data_result = prepare_for_orjson(data_result) |
864 | | - if data_result and len(data_result) > limit and settings.GENERATE_SQL_QUERY_LIMIT_ENABLED: |
| 876 | + if data_result and len(data_result) > limit and self.enable_sql_row_limit: |
865 | 877 | data_obj['data'] = data_result[:limit] |
866 | 878 | data_obj['limit'] = limit |
867 | 879 | else: |
|
0 commit comments