Skip to content

Commit 05e217a

Browse files
committed
feat: implement parameter configuration in chat
1 parent 6dca923 commit 05e217a

File tree

7 files changed

+87
-13
lines changed

7 files changed

+87
-13
lines changed

backend/apps/chat/task/llm.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from langchain_core.messages import BaseMessage, SystemMessage, HumanMessage, AIMessage, BaseMessageChunk
1818
from sqlalchemy import and_, select
1919
from sqlalchemy.orm import sessionmaker, scoped_session
20+
from sqlbot_xpack.config.model import SysArgModel
2021
from sqlbot_xpack.custom_prompt.curd.custom_prompt import find_custom_prompts
2122
from sqlbot_xpack.custom_prompt.models.custom_prompt_model import CustomPromptTypeEnum
2223
from sqlbot_xpack.license.license_manage import SQLBotLicenseUtil
@@ -39,6 +40,7 @@
3940
from apps.datasource.models.datasource import CoreDatasource
4041
from apps.db.db import exec_sql, get_version, check_connection
4142
from apps.system.crud.assistant import AssistantOutDs, AssistantOutDsFactory, get_assistant_ds
43+
from apps.system.crud.parameter_manage import get_groups
4244
from apps.system.schemas.system_schema import AssistantOutDsSchema
4345
from apps.terminology.curd.terminology import get_terminology_template
4446
from common.core.config import settings
@@ -86,6 +88,8 @@ class LLMService:
8688
last_execute_sql_error: str = None
8789
articles_number: int = 4
8890

91+
enable_sql_row_limit: bool = settings.GENERATE_SQL_QUERY_LIMIT_ENABLED
92+
8993
def __init__(self, session: Session, current_user: CurrentUser, chat_question: ChatQuestion,
9094
current_assistant: Optional[CurrentAssistant] = None, no_reasoning: bool = False,
9195
embedding: bool = False, config: LLMConfig = None):
@@ -152,6 +156,14 @@ def __init__(self, session: Session, current_user: CurrentUser, chat_question: C
152156
async def create(cls, *args, **kwargs):
153157
config: LLMConfig = await get_default_config()
154158
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
155167
return instance
156168

157169
def is_running(self, timeout=0.5):
@@ -179,7 +191,7 @@ def init_messages(self):
179191
self.sql_message = []
180192
# add sys prompt
181193
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)))
183195
if last_sql_messages is not None and len(last_sql_messages) > 0:
184196
# limit count
185197
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]):
861873
limit = 1000
862874
if data_result:
863875
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:
865877
data_obj['data'] = data_result[:limit]
866878
data_obj['limit'] = limit
867879
else:
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
21
from fastapi import APIRouter, Request
32
from sqlbot_xpack.config.model import SysArgModel
43

5-
64
from apps.system.crud.parameter_manage import get_groups, get_parameter_args, save_parameter_args
75
from apps.system.schemas.permission import SqlbotPermission, require_permissions
86
from common.core.deps import SessionDep
97

108
router = APIRouter(tags=["system/parameter"], prefix="/system/parameter", include_in_schema=False)
119

10+
1211
@router.get("/login")
1312
async def get_login_args(session: SessionDep) -> list[SysArgModel]:
1413
return await get_groups(session, "login")
1514

15+
1616
@router.get("")
17-
@require_permissions(permission=SqlbotPermission(role=['admin']))
17+
@require_permissions(permission=SqlbotPermission(role=['admin']))
1818
async def get_args(session: SessionDep) -> list[SysArgModel]:
1919
return await get_parameter_args(session)
2020

21+
2122
@router.post("", )
22-
@require_permissions(permission=SqlbotPermission(role=['admin']))
23+
@require_permissions(permission=SqlbotPermission(role=['admin']))
2324
async def save_args(session: SessionDep, request: Request):
24-
return await save_parameter_args(session = session, request = request)
25+
return await save_parameter_args(session=session, request=request)
26+
27+
28+
@router.get("/chat")
29+
async def get_chat_args(session: SessionDep) -> list[SysArgModel]:
30+
return await get_groups(session, "chat")

backend/common/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn | str:
9898
EMBEDDING_TERMINOLOGY_TOP_COUNT: int = EMBEDDING_DEFAULT_TOP_COUNT
9999
EMBEDDING_DATA_TRAINING_TOP_COUNT: int = EMBEDDING_DEFAULT_TOP_COUNT
100100

101+
# 是否启用SQL查询行数限制,默认值,可被参数配置覆盖
101102
GENERATE_SQL_QUERY_LIMIT_ENABLED: bool = True
102103

103104
PARSE_REASONING_BLOCK_ENABLED: bool = True

frontend/src/stores/chatConfig.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { defineStore } from 'pinia'
2+
import { store } from '@/stores/index.ts'
3+
import { request } from '@/utils/request.ts'
4+
import { formatArg } from '@/utils/utils.ts'
5+
6+
interface ChatConfig {
7+
expand_thinking_block: boolean
8+
limit_rows: boolean
9+
}
10+
11+
export const chatConfigStore = defineStore('chatConfigStore', {
12+
state: (): ChatConfig => {
13+
return {
14+
expand_thinking_block: false,
15+
limit_rows: true,
16+
}
17+
},
18+
getters: {
19+
getExpandThinkingBlock(): boolean {
20+
return this.expand_thinking_block
21+
},
22+
getLimitRows(): boolean {
23+
return this.limit_rows
24+
},
25+
},
26+
actions: {
27+
fetchGlobalConfig() {
28+
request.get('/system/parameter/chat').then((res: any) => {
29+
if (res) {
30+
res.forEach((item: any) => {
31+
if (item.pkey === 'chat.expand_thinking_block') {
32+
this.expand_thinking_block = formatArg(item.pval)
33+
}
34+
if (item.pkey === 'chat.limit_rows') {
35+
this.limit_rows = formatArg(item.pval)
36+
}
37+
})
38+
}
39+
})
40+
},
41+
},
42+
})
43+
44+
export const useChatConfigStore = () => {
45+
return chatConfigStore(store)
46+
}

frontend/src/views/chat/answer/BaseAnswer.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import MdComponent from '@/views/chat/component/MdComponent.vue'
55
import icon_up_outlined from '@/assets/svg/icon_up_outlined.svg'
66
import icon_down_outlined from '@/assets/svg/icon_down_outlined.svg'
77
import { useI18n } from 'vue-i18n'
8+
import { useChatConfigStore } from '@/stores/chatConfig.ts'
89
910
const props = withDefaults(
1011
defineProps<{
@@ -24,6 +25,8 @@ const props = withDefaults(
2425
2526
const { t } = useI18n()
2627
28+
const chatConfig = useChatConfigStore()
29+
2730
const show = ref<boolean>(false)
2831
2932
const reasoningContent = computed<Array<string>>(() => {
@@ -63,7 +66,8 @@ function clickShow() {
6366
6467
onMounted(() => {
6568
if (props.message.isTyping) {
66-
show.value = true
69+
// 根据配置项是否默认展开
70+
show.value = chatConfig.getExpandThinkingBlock
6771
}
6872
})
6973
</script>

frontend/src/views/chat/index.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@
410410
type="primary"
411411
class="input-icon"
412412
:disabled="isTyping"
413-
@click.stop="sendMessage"
413+
@click.stop="($event: any) => sendMessage(undefined, $event)"
414414
>
415415
<el-icon size="16">
416416
<icon_send_filled />
@@ -458,6 +458,7 @@ import { debounce } from 'lodash-es'
458458
import { isMobile } from '@/utils/utils'
459459
import router from '@/router'
460460
import QuickQuestion from '@/views/chat/QuickQuestion.vue'
461+
import { useChatConfigStore } from '@/stores/chatConfig.ts'
461462
const userStore = useUserStore()
462463
const props = defineProps<{
463464
startChatDsId?: number
@@ -486,6 +487,9 @@ const customName = computed(() => {
486487
return ''
487488
})
488489
const { t } = useI18n()
490+
491+
const chatConfig = useChatConfigStore()
492+
489493
const isPhone = computed(() => {
490494
return isMobile()
491495
})
@@ -1062,6 +1066,7 @@ function jumpCreatChat() {
10621066
}
10631067
10641068
onMounted(() => {
1069+
chatConfig.fetchGlobalConfig()
10651070
if (isPhone.value) {
10661071
chatListSideBarShow.value = false
10671072
if (props.pageEmbedded) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const { t } = useI18n()
99
1010
const state = reactive({
1111
parameterForm: reactive<any>({
12-
'chat.enable_model_thinking': false,
13-
'chat.rows_of_data': false,
12+
'chat.expand_thinking_block': false,
13+
'chat.limit_rows': false,
1414
}),
1515
})
1616
provide('parameterForm', state.parameterForm)
@@ -99,7 +99,7 @@ onMounted(() => {
9999
</el-tooltip>
100100
</div>
101101
<div class="value">
102-
<el-switch v-model="state.parameterForm['chat.enable_model_thinking']" />
102+
<el-switch v-model="state.parameterForm['chat.expand_thinking_block']" />
103103
</div>
104104
</div>
105105

@@ -118,7 +118,7 @@ onMounted(() => {
118118
</div>
119119
<div class="value">
120120
<el-switch
121-
v-model="state.parameterForm['chat.rows_of_data']"
121+
v-model="state.parameterForm['chat.limit_rows']"
122122
:before-change="beforeChange"
123123
/>
124124
</div>

0 commit comments

Comments
 (0)