Merge remote-tracking branch 'origin/main'

This commit is contained in:
wangjiahao
2025-08-06 09:57:45 +08:00
10 changed files with 252 additions and 169 deletions
+2 -1
View File
@@ -44,7 +44,8 @@ docker compose up -d
如你有更多问题,可以加入我们的技术交流群与我们交流。
TBD: 交流群二维码
<img width="396" height="396" alt="contact_me_qr" src="https://github.com/user-attachments/assets/2594ff29-5426-4457-b051-279855610030" />
## UI 展示
+30 -17
View File
@@ -1,10 +1,9 @@
import datetime
import sqlparse
from typing import List
import orjson
from sqlalchemy import and_
from sqlalchemy.orm import load_only
import sqlparse
from sqlalchemy import and_, select
from apps.chat.models.chat_model import Chat, ChatRecord, CreateChat, ChatInfo, RenameChat, ChatQuestion
from apps.datasource.models.datasource import CoreDatasource
@@ -47,26 +46,29 @@ def delete_chat(session, chart_id) -> str:
def get_chat_chart_data(session: SessionDep, chart_record_id: int):
res = session.query(ChatRecord).options(load_only(ChatRecord.data)).get(chart_record_id)
if res:
stmt = select(ChatRecord.data).where(and_(ChatRecord.id == chart_record_id))
res = session.execute(stmt)
for row in res:
try:
return orjson.loads(res.data)
return orjson.loads(row.data)
except Exception:
pass
return {}
def get_chat_predict_data(session: SessionDep, chart_record_id: int):
res = session.query(ChatRecord).options(load_only(ChatRecord.predict_data)).get(chart_record_id)
if res:
stmt = select(ChatRecord.predict_data).where(and_(ChatRecord.id == chart_record_id))
res = session.execute(stmt)
for row in res:
try:
return orjson.loads(res.predict_data)
return orjson.loads(row.predict_data)
except Exception:
pass
return ''
return {}
def get_chat_with_records(session: SessionDep, chart_id: int, current_user: CurrentUser, current_assistant: CurrentAssistant) -> ChatInfo:
def get_chat_with_records(session: SessionDep, chart_id: int, current_user: CurrentUser,
current_assistant: CurrentAssistant) -> ChatInfo:
chat = session.get(Chat, chart_id)
if not chat:
raise Exception(f"Chat with id {chart_id} not found")
@@ -78,7 +80,7 @@ def get_chat_with_records(session: SessionDep, chart_id: int, current_user: Curr
ds = out_ds_instance.get_ds(chat.datasource)
else:
ds = session.get(CoreDatasource, chat.datasource) if chat.datasource else None
if not ds:
chat_info.datasource_exists = False
chat_info.datasource_name = 'Datasource not exist'
@@ -87,14 +89,25 @@ def get_chat_with_records(session: SessionDep, chart_id: int, current_user: Curr
chat_info.datasource_name = ds.name
chat_info.ds_type = ds.type
record_list = session.query(ChatRecord).options(
load_only(ChatRecord.id, ChatRecord.chat_id, ChatRecord.create_time, ChatRecord.finish_time,
ChatRecord.question, ChatRecord.sql_answer, ChatRecord.sql, ChatRecord.data,
stmt = select(ChatRecord.id, ChatRecord.chat_id, ChatRecord.create_time, ChatRecord.finish_time,
ChatRecord.question, ChatRecord.sql_answer, ChatRecord.sql,
ChatRecord.chart_answer, ChatRecord.chart, ChatRecord.analysis, ChatRecord.predict,
ChatRecord.datasource_select_answer, ChatRecord.analysis_record_id, ChatRecord.predict_record_id,
ChatRecord.recommended_question, ChatRecord.first_chat,
ChatRecord.predict_data, ChatRecord.finish, ChatRecord.error)).filter(
and_(Chat.create_by == current_user.id, ChatRecord.chat_id == chart_id)).order_by(ChatRecord.create_time).all()
ChatRecord.finish, ChatRecord.error).where(
and_(ChatRecord.create_by == current_user.id, ChatRecord.chat_id == chart_id)).order_by(ChatRecord.create_time)
result = session.execute(stmt).all()
record_list: list[ChatRecord] = []
for row in result:
record_list.append(
ChatRecord(id=row.id, chat_id=row.chat_id, create_time=row.create_time, finish_time=row.finish_time,
question=row.question, sql_answer=row.sql_answer, sql=row.sql,
chart_answer=row.chart_answer, chart=row.chart,
analysis=row.analysis, predict=row.predict,
datasource_select_answer=row.datasource_select_answer,
analysis_record_id=row.analysis_record_id, predict_record_id=row.predict_record_id,
recommended_question=row.recommended_question, first_chat=row.first_chat,
finish=row.finish, error=row.error))
result = list(map(format_record, record_list))
+39 -27
View File
@@ -655,14 +655,21 @@ class LLMService:
if json_str is None:
raise SingleMessageError(orjson.dumps({'message': 'Cannot parse sql from answer',
'traceback': "Cannot parse sql from answer:\n" + res}).decode())
data: dict = orjson.loads(json_str)
sql = ''
message = ''
if data['success']:
sql = data['sql']
else:
message = data['message']
raise SingleMessageError(message)
sql: str
data: dict
try:
data = orjson.loads(json_str)
if data['success']:
sql = data['sql']
else:
message = data['message']
raise SingleMessageError(message)
except SingleMessageError as e:
raise e
except Exception:
raise SingleMessageError(orjson.dumps({'message': 'Cannot parse sql from answer',
'traceback': "Cannot parse sql from answer:\n" + res}).decode())
if sql.strip() == '':
raise SingleMessageError("SQL query is empty")
@@ -682,31 +689,36 @@ class LLMService:
if json_str is None:
raise SingleMessageError(orjson.dumps({'message': 'Cannot parse chart config from answer',
'traceback': "Cannot parse chart config from answer:\n" + res}).decode())
data = orjson.loads(json_str)
data: dict
chart: Dict[str, Any] = {}
message = ''
error = False
if data['type'] and data['type'] != 'error':
# todo type check
chart = data
if chart.get('columns'):
for v in chart.get('columns'):
v['value'] = v.get('value').lower()
if chart.get('axis'):
if chart.get('axis').get('x'):
chart.get('axis').get('x')['value'] = chart.get('axis').get('x').get('value').lower()
if chart.get('axis').get('y'):
chart.get('axis').get('y')['value'] = chart.get('axis').get('y').get('value').lower()
if chart.get('axis').get('series'):
chart.get('axis').get('series')['value'] = chart.get('axis').get('series').get('value').lower()
elif data['type'] == 'error':
message = data['reason']
error = True
else:
message = 'Chart is empty'
try:
data = orjson.loads(json_str)
if data['type'] and data['type'] != 'error':
# todo type check
chart = data
if chart.get('columns'):
for v in chart.get('columns'):
v['value'] = v.get('value').lower()
if chart.get('axis'):
if chart.get('axis').get('x'):
chart.get('axis').get('x')['value'] = chart.get('axis').get('x').get('value').lower()
if chart.get('axis').get('y'):
chart.get('axis').get('y')['value'] = chart.get('axis').get('y').get('value').lower()
if chart.get('axis').get('series'):
chart.get('axis').get('series')['value'] = chart.get('axis').get('series').get('value').lower()
elif data['type'] == 'error':
message = data['reason']
error = True
else:
raise Exception('Chart is empty')
except Exception:
error = True
message = orjson.dumps({'message': 'Cannot parse chart config from answer',
'traceback': "Cannot parse chart config from answer:\n" + res}).decode()
if error:
raise SingleMessageError(message)
+2 -2
View File
@@ -45,8 +45,8 @@ export const supplierList: Array<{
{ name: 'qwen-long' },
{ name: 'qwen-long-latest' },
{ name: 'qwen-omni-turbo' },
{ name: 'qwen-omni-turbo-realtime' },
{ name: 'qwen-omni-turbo-realtime-latest' },
/* { name: 'qwen-omni-turbo-realtime' },
{ name: 'qwen-omni-turbo-realtime-latest' }, */
{ name: 'qvq-max' },
{ name: 'qvq-max-latest' },
{ name: 'qvq-plus' },
+104 -95
View File
@@ -51,6 +51,7 @@ function listDs() {
const innerDs = ref()
const loading = ref(false)
const statusLoading = ref(false)
function showDs() {
listDs()
@@ -68,12 +69,18 @@ function selectDsInDialog(ds: any) {
function confirmSelectDs() {
if (innerDs.value) {
statusLoading.value = true
//check first
datasourceApi.check_by_id(innerDs.value).then((res: any) => {
if (res) {
createChat(innerDs.value)
}
})
datasourceApi
.check_by_id(innerDs.value)
.then((res: any) => {
if (res) {
createChat(innerDs.value)
}
})
.finally(() => {
statusLoading.value = false
})
}
}
@@ -117,98 +124,100 @@ defineExpose({
</script>
<template>
<el-drawer
v-model="datasourceConfigvVisible"
:close-on-click-modal="false"
size="calc(100% - 100px)"
modal-class="datasource-drawer-chat"
direction="btt"
:before-close="beforeClose"
:show-close="false"
>
<template #header="{ close }">
<span style="white-space: nowrap">{{ $t('qa.select_datasource') }}</span>
<div class="flex-center" style="width: 100%">
<el-input
v-model="keywords"
clearable
style="width: 320px"
:placeholder="$t('datasource.search')"
>
<template #prefix>
<el-icon>
<icon_searchOutline_outlined />
</el-icon>
</template>
</el-input>
</div>
<el-icon style="cursor: pointer" @click="close">
<icon_close_outlined></icon_close_outlined>
</el-icon>
</template>
<div v-if="datasourceListWithSearch.length" class="card-content">
<el-row :gutter="16" class="w-full">
<el-col
v-for="ele in datasourceListWithSearch"
:key="ele.id"
:xs="24"
:sm="12"
:md="12"
:lg="8"
:xl="6"
class="mb-16"
>
<Card
:id="ele.id"
<div v-loading.body.fullscreen.lock="loading || statusLoading">
<el-drawer
v-model="datasourceConfigvVisible"
:close-on-click-modal="false"
size="calc(100% - 100px)"
modal-class="datasource-drawer-chat"
direction="btt"
:before-close="beforeClose"
:show-close="false"
>
<template #header="{ close }">
<span style="white-space: nowrap">{{ $t('qa.select_datasource') }}</span>
<div class="flex-center" style="width: 100%">
<el-input
v-model="keywords"
clearable
style="width: 320px"
:placeholder="$t('datasource.search')"
>
<template #prefix>
<el-icon>
<icon_searchOutline_outlined />
</el-icon>
</template>
</el-input>
</div>
<el-icon style="cursor: pointer" @click="close">
<icon_close_outlined></icon_close_outlined>
</el-icon>
</template>
<div v-if="datasourceListWithSearch.length" class="card-content">
<el-row :gutter="16" class="w-full">
<el-col
v-for="ele in datasourceListWithSearch"
:key="ele.id"
:name="ele.name"
:type="ele.type"
:type-name="ele.type_name"
:num="ele.num"
:is-selected="ele.id === innerDs"
:description="ele.description"
@select-ds="selectDsInDialog(ele)"
></Card>
</el-col>
</el-row>
</div>
<template v-if="!keywords && !datasourceListWithSearch.length && !searchLoading">
<EmptyBackground
class="datasource-yet_btn"
:description="$t('datasource.data_source_yet')"
img-type="noneWhite"
/>
:xs="24"
:sm="12"
:md="12"
:lg="8"
:xl="6"
class="mb-16"
>
<Card
:id="ele.id"
:key="ele.id"
:name="ele.name"
:type="ele.type"
:type-name="ele.type_name"
:num="ele.num"
:is-selected="ele.id === innerDs"
:description="ele.description"
@select-ds="selectDsInDialog(ele)"
></Card>
</el-col>
</el-row>
</div>
<template v-if="!keywords && !datasourceListWithSearch.length && !searchLoading">
<EmptyBackground
class="datasource-yet_btn"
:description="$t('datasource.data_source_yet')"
img-type="noneWhite"
/>
<div style="text-align: center; margin-top: -10px">
<el-button type="primary" @click="handleAddDatasource">
<template #icon>
<icon_add_outlined></icon_add_outlined>
</template>
{{ $t('datasource.new_data_source') }}
</el-button>
</div>
</template>
<EmptyBackground
v-if="!!keywords && !datasourceListWithSearch.length"
:description="$t('datasource.relevant_content_found')"
class="datasource-yet"
img-type="tree"
/>
<template #footer>
<div class="dialog-footer">
<el-button secondary :disabled="loading" @click="hideDs">{{
$t('common.cancel')
}}</el-button>
<el-button
:type="loading || innerDs === undefined ? 'info' : 'primary'"
:disabled="loading || innerDs === undefined"
@click="confirmSelectDs"
>
{{ $t('datasource.confirm') }}
</el-button>
</div>
</template>
</el-drawer>
<div style="text-align: center; margin-top: -10px">
<el-button type="primary" @click="handleAddDatasource">
<template #icon>
<icon_add_outlined></icon_add_outlined>
</template>
{{ $t('datasource.new_data_source') }}
</el-button>
</div>
</template>
<EmptyBackground
v-if="!!keywords && !datasourceListWithSearch.length"
:description="$t('datasource.relevant_content_found')"
class="datasource-yet"
img-type="tree"
/>
<template #footer>
<div class="dialog-footer">
<el-button secondary :disabled="loading" @click="hideDs">{{
$t('common.cancel')
}}</el-button>
<el-button
:type="loading || statusLoading || innerDs === undefined ? 'info' : 'primary'"
:disabled="loading || statusLoading || innerDs === undefined"
@click="confirmSelectDs"
>
{{ $t('datasource.confirm') }}
</el-button>
</div>
</template>
</el-drawer>
</div>
</template>
<style lang="less">
@@ -1,7 +1,8 @@
<script setup lang="ts">
import BaseAnswer from './BaseAnswer.vue'
import { Chat, chatApi, ChatInfo, type ChatMessage, ChatRecord, questionApi } from '@/api/chat.ts'
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import ChartBlock from '@/views/chat/chat-block/ChartBlock.vue'
const props = withDefaults(
defineProps<{
chatList?: Array<ChatInfo>
@@ -234,11 +235,18 @@ onBeforeUnmount(() => {
stop()
})
onMounted(() => {
if (props.message?.record?.id && props.message?.record?.finish) {
getChatData(props.message.record.id)
}
})
defineExpose({ sendMessage, index: () => index.value, stop })
</script>
<template>
<BaseAnswer v-if="message" :message="message" :reasoning-name="reasoningName" :loading="_loading">
<ChartBlock style="margin-top: 12px" :message="message" />
<slot></slot>
<template #tool>
<slot name="tool"></slot>
@@ -1,8 +1,9 @@
<script setup lang="ts">
import BaseAnswer from './BaseAnswer.vue'
import { chatApi, ChatInfo, type ChatMessage, ChatRecord } from '@/api/chat.ts'
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import MdComponent from '@/views/chat/component/MdComponent.vue'
import ChartBlock from '@/views/chat/chat-block/ChartBlock.vue'
const props = withDefaults(
defineProps<{
chatList?: Array<ChatInfo>
@@ -174,6 +175,7 @@ const sendMessage = async () => {
break
case 'predict-success':
//currentChat.value.records[_index].predict_data = data.content
getChatPredictData(_currentChat.value.records[index.value].id)
emits('finish', currentRecord.id)
break
case 'predict_finish':
@@ -198,6 +200,33 @@ const sendMessage = async () => {
}
}
const chartBlockRef = ref()
function getChatPredictData(recordId?: number) {
chatApi.get_chart_predict_data(recordId).then((response) => {
_currentChat.value.records.forEach((record) => {
if (record.id === recordId) {
record.predict_data = response ?? []
if (record.predict_data.length > 1) {
getChatData(recordId)
}
}
})
})
}
function getChatData(recordId?: number) {
chatApi.get_chart_data(recordId).then((response) => {
_currentChat.value.records.forEach((record) => {
if (record.id === recordId) {
record.data = response
console.log(record.data)
}
})
})
}
function stop() {
stopFlag.value = true
_loading.value = false
@@ -208,12 +237,25 @@ onBeforeUnmount(() => {
stop()
})
onMounted(() => {
if (props.message?.record?.id && props.message?.record?.finish) {
getChatPredictData(props.message.record.id)
}
})
defineExpose({ sendMessage, index: () => index.value, chatList: () => _chatList, stop })
</script>
<template>
<BaseAnswer v-if="message" :message="message" :reasoning-name="['predict']" :loading="_loading">
<MdComponent :message="message.record?.predict_content" style="margin-top: 12px" />
<ChartBlock
v-if="message.record?.predict_data?.length > 0 && message.record?.data"
ref="chartBlockRef"
style="margin-top: 12px"
:message="message"
is-predict
/>
<slot></slot>
<template #tool>
<slot name="tool"></slot>
@@ -81,19 +81,17 @@ const data = computed(() => {
}
}
} else {
if (props.message?.record?.predict_data.length > 0) {
if (props.message?.record?.predict_data?.length > 0) {
_list = props.message?.record?.predict_data
}
}
if (_list.length == 0) {
return _list
}
if (dataObject.value.data && dataObject.value.data.length > 0) {
if (dataObject.value.data && dataObject.value.data?.length > 0) {
return concat(dataObject.value.data, _list)
}
return _list
} else {
return dataObject.value.data
@@ -180,6 +178,10 @@ function onTypeChange(val: any) {
chartRef.value?.onTypeChange()
}
function reloadChart() {
chartRef.value?.onTypeChange()
}
const dialogVisible = ref(false)
function openFullScreen() {
@@ -274,6 +276,10 @@ function exportToImage() {
exportRef.value?.hide()
}
defineExpose({
reloadChart,
})
watch(
() => chartObject.value?.type,
(val) => {
+9 -17
View File
@@ -160,7 +160,6 @@
@error="onChartAnswerError"
@stop="onChatStop"
>
<ChartBlock style="margin-top: 12px" :message="message" />
<ErrorInfo :error="message.record?.error" class="error-container" />
<template #tool>
<ChatToolBar v-if="!message.isTyping" :message="message">
@@ -265,7 +264,6 @@
@error="onPredictAnswerError"
@stop="onChatStop"
>
<ChartBlock style="margin-top: 12px" :message="message" is-predict />
<ErrorInfo :error="message.record?.error" class="error-container" />
<template #tool>
<ChatToolBar v-if="!message.isTyping" :message="message" />
@@ -337,7 +335,6 @@ import ChartAnswer from './answer/ChartAnswer.vue'
import AnalysisAnswer from './answer/AnalysisAnswer.vue'
import PredictAnswer from './answer/PredictAnswer.vue'
import UserChat from './chat-block/UserChat.vue'
import ChartBlock from './chat-block/ChartBlock.vue'
import RecommendQuestion from './RecommendQuestion.vue'
import ChatListContainer from './ChatListContainer.vue'
import ChatCreator from '@/views/chat/ChatCreator.vue'
@@ -345,7 +342,7 @@ import ErrorInfo from './ErrorInfo.vue'
import ChatToolBar from './ChatToolBar.vue'
import { dsTypeWithImg } from '@/views/ds/js/ds-type'
import { useI18n } from 'vue-i18n'
import { find } from 'lodash-es'
import { find, forEach } from 'lodash-es'
import icon_new_chat_outlined from '@/assets/svg/icon_new_chat_outlined.svg'
import icon_sidebar_outlined from '@/assets/svg/icon_sidebar_outlined.svg'
import icon_replace_outlined from '@/assets/svg/icon_replace_outlined.svg'
@@ -490,9 +487,14 @@ function getChatList(callback?: () => void) {
})
}
function onClickHistory(chat: Chat) {
function onClickHistory(chat: ChatInfo) {
scrollToBottom()
console.debug('click history', chat)
forEach(chat?.records, (record: ChatRecord) => {
// getChatData(record.id)
if (record.predict_record_id) {
// getChatPredictData(record.id)
}
})
}
const currentChatEngineType = computed(() => {
@@ -629,16 +631,6 @@ const sendMessage = async ($event: any = {}) => {
})
}
function getChatPredictData(recordId?: number) {
chatApi.get_chart_predict_data(recordId).then((response) => {
currentChat.value.records.forEach((record) => {
if (record.id === recordId) {
record.predict_data = response ?? []
}
})
})
}
const analysisAnswerRef = ref()
async function onAnalysisAnswerFinish(id: number) {
@@ -704,7 +696,7 @@ const predictAnswerRef = ref()
async function onPredictAnswerFinish(id: number) {
loading.value = false
isTyping.value = false
getChatPredictData(id)
console.debug('onPredictAnswerFinish: ', id)
//await getRecommendQuestions(id)
}
function onPredictAnswerError() {
+4 -4
View File
@@ -109,12 +109,12 @@ const handleAddEmbedded = (val: any) => {
const wsChanged = (val: any) => {
dsForm.private_list = []
dsForm.oid = val
getDsList()
getDsList(true)
}
const getDsList = () => {
const getDsList = (change: boolean = false) => {
dsApi(dsForm.oid).then((res: any) => {
dsListOptions.value = res || []
if (!currentEmbedded.id) {
if (change || !currentEmbedded.id) {
dsForm.private_list = dsListOptions.value.map((ele) => ele.id)
}
})
@@ -125,7 +125,7 @@ const handleBaseEmbedded = (row: any) => {
if (row) {
Object.assign(dsForm, JSON.parse(row.configuration))
}
getDsList()
getDsList(false)
ruleConfigvVisible.value = true
dialogTitle.value = row?.id
? t('embedded.edit_basic_applications')