feat: save chat brief after question

This commit is contained in:
ulleo
2025-07-14 18:37:59 +08:00
parent 69d2c42f73
commit 02e468b3e7
6 changed files with 108 additions and 26 deletions
+5 -4
View File
@@ -13,7 +13,8 @@ from common.utils.utils import extract_nested_json
def list_chats(session: SessionDep, current_user: CurrentUser) -> List[Chat]:
chart_list = session.exec(select(Chat).where(Chat.create_by == current_user.id).order_by(Chat.create_time.desc())).all()
chart_list = session.exec(
select(Chat).where(Chat.create_by == current_user.id).order_by(Chat.create_time.desc())).all()
return chart_list
@@ -133,7 +134,7 @@ def format_record(record: ChatRecord):
def list_base_records(session: SessionDep, chart_id: int, current_user: CurrentUser) -> List[ChatRecord]:
record_list = session.query(ChatRecord).filter(
and_(Chat.create_by == current_user.id, ChatRecord.chat_id == chart_id,
ChatRecord.analysis_record_id is None, ChatRecord.predict_record_id is None)).order_by(
ChatRecord.analysis_record_id.is_(None), ChatRecord.predict_record_id.is_(None))).order_by(
ChatRecord.create_time).all()
return record_list
@@ -203,7 +204,7 @@ def save_question(session: SessionDep, current_user: CurrentUser, question: Chat
if not question.question or question.question.strip() == '':
raise Exception("Question cannot be Empty")
#chat = session.query(Chat).filter(Chat.id == question.chat_id).first()
# chat = session.query(Chat).filter(Chat.id == question.chat_id).first()
chat: Chat = session.get(Chat, question.chat_id)
if not chat:
raise Exception(f"Chat with id {question.chat_id} not found")
@@ -236,7 +237,7 @@ def save_analysis_predict_record(session: SessionDep, base_record: ChatRecord, a
record.engine_type = base_record.engine_type
record.ai_modal_id = base_record.ai_modal_id
record.create_time = datetime.datetime.now()
record.create_by = base_record.id
record.create_by = base_record.create_by
record.chart = base_record.chart
record.data = base_record.data
+17 -5
View File
@@ -18,8 +18,8 @@ from apps.chat.curd.chat import save_question, save_full_sql_message, save_full_
save_error_message, save_sql_exec_data, save_full_chart_message, save_full_chart_message_and_answer, save_chart, \
finish_record, save_full_analysis_message_and_answer, save_full_predict_message_and_answer, save_predict_data, \
save_full_select_datasource_message_and_answer, save_full_recommend_question_message_and_answer, \
get_old_questions, save_analysis_predict_record, list_base_records
from apps.chat.models.chat_model import ChatQuestion, ChatRecord, Chat
get_old_questions, save_analysis_predict_record, list_base_records, rename_chat
from apps.chat.models.chat_model import ChatQuestion, ChatRecord, Chat, RenameChat
from apps.datasource.crud.datasource import get_table_schema
from apps.datasource.models.datasource import CoreDatasource
from apps.db.db import exec_sql
@@ -45,13 +45,15 @@ class LLMService:
session: SessionDep
current_user: CurrentUser
current_assistant: Optional[CurrentAssistant] = None
change_title: bool = False
def __init__(self, session: SessionDep, current_user: CurrentUser, chat_question: ChatQuestion, current_assistant: Optional[CurrentAssistant] = None):
def __init__(self, session: SessionDep, current_user: CurrentUser, chat_question: ChatQuestion,
current_assistant: Optional[CurrentAssistant] = None):
self.session = session
self.current_user = current_user
self.current_assistant = current_assistant
#chat = self.session.query(Chat).filter(Chat.id == chat_question.chat_id).first()
# chat = self.session.query(Chat).filter(Chat.id == chat_question.chat_id).first()
chat_id = chat_question.chat_id
chat: Chat = self.session.get(Chat, chat_id)
if not chat:
@@ -71,6 +73,8 @@ class LLMService:
list_base_records(session=self.session,
current_user=current_user,
chart_id=chat_id))))
self.change_title = len(history_records) == 0
# get schema
if ds:
chat_question.db_schema = get_table_schema(session=self.session, ds=ds)
@@ -335,7 +339,7 @@ class LLMService:
datasource_msg: List[Union[BaseMessage, dict[str, Any]]] = []
datasource_msg.append(SystemMessage(self.chat_question.datasource_sys_question()))
if self.current_assistant:
_ds_list = get_assistant_ds(session = self.session, assistant = self.current_assistant)
_ds_list = get_assistant_ds(session=self.session, assistant=self.current_assistant)
else:
_ds_list = self.session.exec(select(CoreDatasource).options(
load_only(CoreDatasource.id, CoreDatasource.name, CoreDatasource.description))).all()
@@ -620,6 +624,14 @@ def run_task(llm_service: LLMService, in_chat: bool = True):
if in_chat:
yield orjson.dumps({'type': 'id', 'id': llm_service.get_record().id}).decode() + '\n\n'
# return title
if llm_service.change_title:
if llm_service.chat_question.question or llm_service.chat_question.question.strip() != '':
brief = rename_chat(session=llm_service.session,
rename_object=RenameChat(id=llm_service.get_record().chat_id,
brief=llm_service.chat_question.question.strip()[:20]))
yield orjson.dumps({'type': 'brief', 'brief': brief}).decode() + '\n\n'
# select datasource if datasource is none
if not llm_service.ds:
ds_res = llm_service.select_datasource()
@@ -0,0 +1,6 @@
<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M15.75 14.25V15.75H2.25V14.25H15.75ZM15.75 3.75H2.25V15.75L2.09692 15.7419C1.39076 15.6704 0.82965 15.1092 0.758057 14.4031L0.75 14.25V3.75C0.75 2.92157 1.42157 2.25 2.25 2.25H15.75C16.5784 2.25 17.25 2.92157 17.25 3.75V14.25C17.25 15.0784 16.5784 15.75 15.75 15.75V3.75Z" fill="#1F2329"/>
<path d="M5.625 5.25H3.375C3.27554 5.25 3.18016 5.28951 3.10983 5.35984C3.03951 5.43016 3 5.52554 3 5.625V6.375C3 6.47446 3.03951 6.56984 3.10983 6.64017C3.18016 6.71049 3.27554 6.75 3.375 6.75H5.625C5.67425 6.75 5.72301 6.7403 5.76851 6.72145C5.814 6.70261 5.85534 6.67499 5.89017 6.64017C5.92499 6.60534 5.95261 6.564 5.97146 6.51851C5.9903 6.47301 6 6.42425 6 6.375V5.625C6 5.57575 5.9903 5.52699 5.97146 5.48149C5.95261 5.436 5.92499 5.39466 5.89017 5.35984C5.85534 5.32501 5.814 5.29739 5.76851 5.27855C5.72301 5.2597 5.67425 5.25 5.625 5.25Z" fill="#1F2329"/>
<path d="M5.625 7.5H3.375C3.27554 7.5 3.18016 7.53951 3.10983 7.60984C3.03951 7.68016 3 7.77554 3 7.875V8.625C3 8.72446 3.03951 8.81984 3.10983 8.89017C3.18016 8.96049 3.27554 9 3.375 9H5.625C5.67425 9 5.72301 8.9903 5.76851 8.97145C5.814 8.95261 5.85534 8.92499 5.89017 8.89017C5.92499 8.85534 5.95261 8.814 5.97146 8.76851C5.9903 8.72301 6 8.67425 6 8.625V7.875C6 7.82575 5.9903 7.77699 5.97146 7.73149C5.95261 7.686 5.92499 7.64466 5.89017 7.60984C5.85534 7.57501 5.814 7.54739 5.76851 7.52855C5.72301 7.5097 5.67425 7.5 5.625 7.5Z" fill="#1F2329"/>
<path d="M6.75 3.75H8.25V14.25H6.75V3.75Z" fill="#1F2329"/>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

+11 -4
View File
@@ -137,8 +137,8 @@ function handleCommand(command: string | number | object, chat: Chat) {
--hover-color: var(--ed-color-primary-light-9);
--active-color: var(--hover-color);
padding-left: 14px;
padding-right: 14px;
padding-left: 16px;
padding-right: 16px;
width: 100%;
display: flex;
@@ -152,6 +152,7 @@ function handleCommand(command: string | number | object, chat: Chat) {
cursor: pointer;
border-radius: 6px;
line-height: 1em;
font-size: 14px;
display: flex;
align-items: center;
@@ -177,15 +178,21 @@ function handleCommand(command: string | number | object, chat: Chat) {
}
&:hover {
background-color: var(--hover-color);
background-color: rgba(31, 35, 41, 0.1);
.icon-more {
display: inline-flex;
color: rgba(100, 106, 115, 1);
&:hover {
background-color: rgba(31, 35, 41, 0.1);
}
}
}
&.active {
background-color: var(--active-color);
background-color: rgba(255, 255, 255, 1);
font-weight: 500;
}
}
}
+64 -8
View File
@@ -1,14 +1,23 @@
<template>
<el-container class="chat-container">
<el-container class="chat-container no-padding">
<el-aside v-if="!isAssistant" class="chat-container-left">
<el-container class="chat-container-right-container">
<el-header class="chat-list-header">
<el-button type="primary" @click="createNewChat">
<div class="title">
<div>智能问数</div>
<el-button link type="primary" class="icon-btn">
<el-icon>
<icon_sidebar_outlined />
</el-icon>
</el-button>
</div>
<el-button class="btn" type="primary" @click="createNewChat">
<el-icon>
<Plus />
</el-icon>
{{ t('qa.New Conversation') }}
</el-button>
<el-input v-model="search" class="search" placeholder="placeholder" />
</el-header>
<el-main class="chat-list">
<ChatList
@@ -166,6 +175,7 @@ import MdComponent from './component/MdComponent.vue'
import RecommendQuestion from './RecommendQuestion.vue'
import ChatCreator from './ChatCreator.vue'
import { useI18n } from 'vue-i18n'
import icon_sidebar_outlined from '@/assets/svg/icon_sidebar_outlined.svg'
import { endsWith, find, startsWith } from 'lodash-es'
import { useAssistantStore } from '@/stores/assistant'
@@ -231,6 +241,8 @@ const goEmpty = () => {
inputMessage.value = ''
}
const search = ref<string>()
const createNewChat = async () => {
goEmpty()
if (isAssistant.value) {
@@ -494,6 +506,14 @@ const sendMessage = async () => {
case 'info':
console.log(data.msg)
break
case 'brief':
currentChat.value.brief = data.brief
chatList.value.forEach((c: Chat) => {
if (c.id === currentChat.value.id) {
c.brief = currentChat.value.brief
}
})
break
case 'error':
currentRecord.error = data.content
isTyping.value = false
@@ -808,21 +828,57 @@ defineExpose({
.chat-container {
height: 100%;
.icon-btn {
min-width: unset;
width: 26px;
height: 26px;
font-size: 18px;
&:hover {
background: rgba(31, 35, 41, 0.1);
}
}
.chat-container-left {
--ed-aside-width: 260px;
--ed-aside-width: 280px;
border-radius: 12px 0 0 12px;
//box-shadow: 0 0 3px #d7d7d7;
//z-index: 1;
box-shadow: 0 0 3px #d7d7d7;
z-index: 1;
background: var(--ed-fill-color-blank);
background: rgba(245, 246, 247, 1);
.chat-container-right-container {
height: 100%;
.chat-list-header {
--ed-header-padding: 16px;
--ed-header-height: calc(16px + 24px + 16px + 40px + 16px + 32px + 16px);
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
gap: 16px;
.title {
height: 24px;
width: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
font-weight: 500;
}
.btn {
width: 100%;
height: 40px;
}
.search {
height: 32px;
width: 100%;
}
}
.chat-list {
@@ -833,7 +889,7 @@ defineExpose({
.chat-record-list {
padding: 0 0 20px 0;
background: rgba(224, 224, 226, 0.29);
background: rgba(255, 255, 255, 1);
}
.chat-footer {
@@ -216,7 +216,7 @@ defineExpose({
}
</style>
<style lang="less">
<style lang="less" scoped>
.custom-drawer {
.ed-drawer__footer {
height: 64px !important;
@@ -235,9 +235,9 @@ defineExpose({
.chat-container-left {
padding-top: 20px;
--ed-aside-width: 260px;
box-shadow: 0 0 3px #d7d7d7;
z-index: 1;
border-radius: 12px 0 0 12px;
//box-shadow: 0 0 3px #d7d7d7;
//z-index: 1;
background: var(--ed-fill-color-blank);
@@ -258,7 +258,7 @@ defineExpose({
.chat-record-list {
padding: 0 0 20px 0;
background: rgba(224, 224, 226, 0.29);
background: rgba(255, 255, 255, 1);
}
.chat-footer {