Skip to content

Commit 17fcfd4

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents c068ae5 + 257609d commit 17fcfd4

File tree

8 files changed

+74
-59
lines changed

8 files changed

+74
-59
lines changed

backend/apps/chat/models/chat_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def guess_user_question(self, old_questions: str = "[]"):
147147
old_questions=old_questions)
148148

149149
def filter_sys_question(self):
150-
return get_permissions_template()['system'].format(lang=self.lang)
150+
return get_permissions_template()['system'].format(lang=self.lang, engine=self.engine)
151151

152152
def filter_user_question(self):
153153
return get_permissions_template()['user'].format(sql=self.sql, filter=self.filter)

backend/apps/chat/task/llm.py

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -386,51 +386,56 @@ def select_datasource(self):
386386
]
387387
""" _ds_list = self.session.exec(select(CoreDatasource).options(
388388
load_only(CoreDatasource.id, CoreDatasource.name, CoreDatasource.description))).all() """
389-
_ds_list_dict = []
390-
for _ds in _ds_list:
391-
_ds_list_dict.append(_ds)
392-
datasource_msg.append(
393-
HumanMessage(self.chat_question.datasource_user_question(orjson.dumps(_ds_list_dict).decode())))
394-
395-
history_msg = []
396-
if self.record.full_select_datasource_message and self.record.full_select_datasource_message.strip() != '':
397-
history_msg = orjson.loads(self.record.full_select_datasource_message)
398-
399-
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
400-
answer='',
401-
full_message=orjson.dumps(history_msg +
402-
[{'type': msg.type,
403-
'content': msg.content}
404-
for msg
405-
in
406-
datasource_msg]).decode())
407-
full_thinking_text = ''
408-
full_text = ''
409-
token_usage = {}
410-
res = self.llm.stream(datasource_msg)
411-
for chunk in res:
412-
SQLBotLogUtil.info(chunk)
413-
reasoning_content_chunk = ''
414-
if 'reasoning_content' in chunk.additional_kwargs:
415-
reasoning_content_chunk = chunk.additional_kwargs.get('reasoning_content', '')
416-
# else:
417-
# reasoning_content_chunk = chunk.get('reasoning_content')
418-
if reasoning_content_chunk is None:
389+
390+
ignore_auto_select = _ds_list and len(_ds_list) == 1
391+
# ignore auto select ds
392+
393+
if not ignore_auto_select:
394+
_ds_list_dict = []
395+
for _ds in _ds_list:
396+
_ds_list_dict.append(_ds)
397+
datasource_msg.append(
398+
HumanMessage(self.chat_question.datasource_user_question(orjson.dumps(_ds_list_dict).decode())))
399+
400+
history_msg = []
401+
if self.record.full_select_datasource_message and self.record.full_select_datasource_message.strip() != '':
402+
history_msg = orjson.loads(self.record.full_select_datasource_message)
403+
404+
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
405+
answer='',
406+
full_message=orjson.dumps(history_msg +
407+
[{'type': msg.type,
408+
'content': msg.content}
409+
for msg
410+
in
411+
datasource_msg]).decode())
412+
full_thinking_text = ''
413+
full_text = ''
414+
token_usage = {}
415+
res = self.llm.stream(datasource_msg)
416+
for chunk in res:
417+
SQLBotLogUtil.info(chunk)
419418
reasoning_content_chunk = ''
420-
full_thinking_text += reasoning_content_chunk
419+
if 'reasoning_content' in chunk.additional_kwargs:
420+
reasoning_content_chunk = chunk.additional_kwargs.get('reasoning_content', '')
421+
# else:
422+
# reasoning_content_chunk = chunk.get('reasoning_content')
423+
if reasoning_content_chunk is None:
424+
reasoning_content_chunk = ''
425+
full_thinking_text += reasoning_content_chunk
421426

422-
full_text += chunk.content
423-
yield {'content': chunk.content, 'reasoning_content': reasoning_content_chunk}
424-
get_token_usage(chunk, token_usage)
425-
datasource_msg.append(AIMessage(full_text))
427+
full_text += chunk.content
428+
yield {'content': chunk.content, 'reasoning_content': reasoning_content_chunk}
429+
get_token_usage(chunk, token_usage)
430+
datasource_msg.append(AIMessage(full_text))
426431

427-
json_str = extract_nested_json(full_text)
432+
json_str = extract_nested_json(full_text)
428433

429434
_error: Exception | None = None
430435
_datasource: int | None = None
431436
_engine_type: str | None = None
432437
try:
433-
data: dict = orjson.loads(json_str)
438+
data: dict = _ds_list[0] if ignore_auto_select else orjson.loads(json_str)
434439

435440
if data.get('id') and data.get('id') != 0:
436441
_datasource = data['id']
@@ -465,17 +470,18 @@ def select_datasource(self):
465470
except Exception as e:
466471
_error = e
467472

468-
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
469-
answer=orjson.dumps({'content': full_text,
470-
'reasoning_content': full_thinking_text}).decode(),
471-
datasource=_datasource,
472-
engine_type=_engine_type,
473-
full_message=orjson.dumps(history_msg +
474-
[{'type': msg.type,
475-
'content': msg.content}
476-
for msg
477-
in
478-
datasource_msg]).decode())
473+
if not ignore_auto_select:
474+
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
475+
answer=orjson.dumps({'content': full_text,
476+
'reasoning_content': full_thinking_text}).decode(),
477+
datasource=_datasource,
478+
engine_type=_engine_type,
479+
full_message=orjson.dumps(history_msg +
480+
[{'type': msg.type,
481+
'content': msg.content}
482+
for msg
483+
in
484+
datasource_msg]).decode())
479485
self.init_messages()
480486

481487
if _error:
@@ -815,7 +821,7 @@ def run_task(self, in_chat: bool = True):
815821
SQLBotLogUtil.info(full_sql_text)
816822

817823
# todo row permission
818-
if is_normal_user(self.current_user) or (self.current_assistant and self.current_assistant.type == 1):
824+
if (not self.current_assistant and is_normal_user(self.current_user)) or (self.current_assistant and self.current_assistant.type == 1):
819825
sql_json_str = extract_nested_json(full_sql_text)
820826
data = orjson.loads(sql_json_str)
821827

backend/apps/datasource/crud/datasource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_ds(session: SessionDep, id: int):
4141

4242

4343
def check_status(session: SessionDep, ds: CoreDatasource, is_raise: bool = False):
44-
conn = get_engine(ds)
44+
conn = get_engine(ds, 5)
4545
try:
4646
with conn.connect() as connection:
4747
SQLBotLogUtil.info("success")

backend/apps/db/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def get_uri_from_config(type: str, conf: DatasourceConf) -> str:
5252
return db_url
5353

5454

55-
def get_engine(ds: CoreDatasource) -> Engine:
55+
def get_engine(ds: CoreDatasource, timeout: int = 30) -> Engine:
5656
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if ds.type != "excel" else get_engine_config()
5757
if conf.timeout is None:
58-
conf.timeout = 30
58+
conf.timeout = timeout
5959
if ds.type == "pg" and (conf.dbSchema is not None and conf.dbSchema != ""):
6060
engine = create_engine(get_uri(ds),
6161
connect_args={"options": f"-c search_path={urllib.parse.quote(conf.dbSchema)}",

backend/apps/system/crud/assistant.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@ def get_ds_from_api(self):
101101
certificateList: list[any] = json.loads(self.certificate)
102102
header = {}
103103
cookies = {}
104+
param = {}
104105
for item in certificateList:
105106
if item['target'] == 'header':
106107
header[item['key']] = item['value']
107108
if item['target'] == 'cookie':
108109
cookies[item['key']] = item['value']
109-
110-
res = requests.get(url=endpoint, headers=header, cookies=cookies, timeout=10)
110+
if item['target'] == 'param':
111+
param[item['key']] = item['value']
112+
res = requests.get(url=endpoint, params=param, headers=header, cookies=cookies, timeout=10)
111113
if res.status_code == 200:
112114
result_json: dict[any] = json.loads(res.text)
113115
if result_json.get('code') == 0:

frontend/src/i18n/en.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@
472472
"duplicate_email": "Duplicate email",
473473
"interface_credentials": "Interface credentials",
474474
"no_credentials_yet": "No credentials yet",
475-
"intelligent_customer_service": "SQLBot Intelligent Customer Service"
475+
"intelligent_customer_service": "SQLBot Intelligent Customer Service",
476+
"origin_format_error": "Format invalid, starts with http or https and cannot end with /"
476477
},
477478
"chat": {
478479
"type": "Chart Type",
@@ -518,4 +519,4 @@
518519
"back_community": "Restore to Community Edition",
519520
"confirm_tips": "Are you sure to restore to Community Edition?"
520521
}
521-
}
522+
}

frontend/src/i18n/zh-CN.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@
487487
"stop_replying": "停止回答",
488488
"i_am_sqlbot": "你好,我是 SQLBot",
489489
"predict_data_etc": "我可以查询数据、生成图表、检测数据异常、预测数据等",
490-
"intelligent_data_query": "赶快开启智能问数吧~"
490+
"intelligent_data_query": "赶快开启智能问数吧~",
491+
"origin_format_error": "格式错误,http或https开头,不能以 / 结尾"
491492
},
492493
"chat": {
493494
"type": "图表类型",
@@ -533,4 +534,4 @@
533534
"back_community": "还原至社区版",
534535
"confirm_tips": "确定还原至社区版?"
535536
}
536-
}
537+
}

frontend/src/views/system/embedded/index.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ const rules = {
245245
t('datasource.please_enter') + t('common.empty') + t('embedded.cross_domain_settings'),
246246
trigger: 'blur',
247247
},
248+
{
249+
pattern: /^(https?:\/\/)?([\w-]+\.)*[\w-]+(:\d+)?$/,
250+
message: t('embedded.origin_format_error'),
251+
trigger: 'blur',
252+
},
248253
],
249254
}
250255

0 commit comments

Comments
 (0)