Skip to content

Commit 02e468b

Browse files
committed
feat: save chat brief after question
1 parent 69d2c42 commit 02e468b

File tree

6 files changed

+108
-26
lines changed

6 files changed

+108
-26
lines changed

backend/apps/chat/curd/chat.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414

1515
def list_chats(session: SessionDep, current_user: CurrentUser) -> List[Chat]:
16-
chart_list = session.exec(select(Chat).where(Chat.create_by == current_user.id).order_by(Chat.create_time.desc())).all()
16+
chart_list = session.exec(
17+
select(Chat).where(Chat.create_by == current_user.id).order_by(Chat.create_time.desc())).all()
1718
return chart_list
1819

1920

@@ -133,7 +134,7 @@ def format_record(record: ChatRecord):
133134
def list_base_records(session: SessionDep, chart_id: int, current_user: CurrentUser) -> List[ChatRecord]:
134135
record_list = session.query(ChatRecord).filter(
135136
and_(Chat.create_by == current_user.id, ChatRecord.chat_id == chart_id,
136-
ChatRecord.analysis_record_id is None, ChatRecord.predict_record_id is None)).order_by(
137+
ChatRecord.analysis_record_id.is_(None), ChatRecord.predict_record_id.is_(None))).order_by(
137138
ChatRecord.create_time).all()
138139
return record_list
139140

@@ -203,7 +204,7 @@ def save_question(session: SessionDep, current_user: CurrentUser, question: Chat
203204
if not question.question or question.question.strip() == '':
204205
raise Exception("Question cannot be Empty")
205206

206-
#chat = session.query(Chat).filter(Chat.id == question.chat_id).first()
207+
# chat = session.query(Chat).filter(Chat.id == question.chat_id).first()
207208
chat: Chat = session.get(Chat, question.chat_id)
208209
if not chat:
209210
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
236237
record.engine_type = base_record.engine_type
237238
record.ai_modal_id = base_record.ai_modal_id
238239
record.create_time = datetime.datetime.now()
239-
record.create_by = base_record.id
240+
record.create_by = base_record.create_by
240241
record.chart = base_record.chart
241242
record.data = base_record.data
242243

backend/apps/chat/task/llm.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
save_error_message, save_sql_exec_data, save_full_chart_message, save_full_chart_message_and_answer, save_chart, \
1919
finish_record, save_full_analysis_message_and_answer, save_full_predict_message_and_answer, save_predict_data, \
2020
save_full_select_datasource_message_and_answer, save_full_recommend_question_message_and_answer, \
21-
get_old_questions, save_analysis_predict_record, list_base_records
22-
from apps.chat.models.chat_model import ChatQuestion, ChatRecord, Chat
21+
get_old_questions, save_analysis_predict_record, list_base_records, rename_chat
22+
from apps.chat.models.chat_model import ChatQuestion, ChatRecord, Chat, RenameChat
2323
from apps.datasource.crud.datasource import get_table_schema
2424
from apps.datasource.models.datasource import CoreDatasource
2525
from apps.db.db import exec_sql
@@ -45,13 +45,15 @@ class LLMService:
4545
session: SessionDep
4646
current_user: CurrentUser
4747
current_assistant: Optional[CurrentAssistant] = None
48+
change_title: bool = False
4849

49-
def __init__(self, session: SessionDep, current_user: CurrentUser, chat_question: ChatQuestion, current_assistant: Optional[CurrentAssistant] = None):
50+
def __init__(self, session: SessionDep, current_user: CurrentUser, chat_question: ChatQuestion,
51+
current_assistant: Optional[CurrentAssistant] = None):
5052

5153
self.session = session
5254
self.current_user = current_user
5355
self.current_assistant = current_assistant
54-
#chat = self.session.query(Chat).filter(Chat.id == chat_question.chat_id).first()
56+
# chat = self.session.query(Chat).filter(Chat.id == chat_question.chat_id).first()
5557
chat_id = chat_question.chat_id
5658
chat: Chat = self.session.get(Chat, chat_id)
5759
if not chat:
@@ -71,6 +73,8 @@ def __init__(self, session: SessionDep, current_user: CurrentUser, chat_question
7173
list_base_records(session=self.session,
7274
current_user=current_user,
7375
chart_id=chat_id))))
76+
self.change_title = len(history_records) == 0
77+
7478
# get schema
7579
if ds:
7680
chat_question.db_schema = get_table_schema(session=self.session, ds=ds)
@@ -335,7 +339,7 @@ def select_datasource(self):
335339
datasource_msg: List[Union[BaseMessage, dict[str, Any]]] = []
336340
datasource_msg.append(SystemMessage(self.chat_question.datasource_sys_question()))
337341
if self.current_assistant:
338-
_ds_list = get_assistant_ds(session = self.session, assistant = self.current_assistant)
342+
_ds_list = get_assistant_ds(session=self.session, assistant=self.current_assistant)
339343
else:
340344
_ds_list = self.session.exec(select(CoreDatasource).options(
341345
load_only(CoreDatasource.id, CoreDatasource.name, CoreDatasource.description))).all()
@@ -620,6 +624,14 @@ def run_task(llm_service: LLMService, in_chat: bool = True):
620624
if in_chat:
621625
yield orjson.dumps({'type': 'id', 'id': llm_service.get_record().id}).decode() + '\n\n'
622626

627+
# return title
628+
if llm_service.change_title:
629+
if llm_service.chat_question.question or llm_service.chat_question.question.strip() != '':
630+
brief = rename_chat(session=llm_service.session,
631+
rename_object=RenameChat(id=llm_service.get_record().chat_id,
632+
brief=llm_service.chat_question.question.strip()[:20]))
633+
yield orjson.dumps({'type': 'brief', 'brief': brief}).decode() + '\n\n'
634+
623635
# select datasource if datasource is none
624636
if not llm_service.ds:
625637
ds_res = llm_service.select_datasource()
Lines changed: 6 additions & 0 deletions
Loading

frontend/src/views/chat/ChatList.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ function handleCommand(command: string | number | object, chat: Chat) {
137137
--hover-color: var(--ed-color-primary-light-9);
138138
--active-color: var(--hover-color);
139139
140-
padding-left: 14px;
141-
padding-right: 14px;
140+
padding-left: 16px;
141+
padding-right: 16px;
142142
width: 100%;
143143
144144
display: flex;
@@ -152,6 +152,7 @@ function handleCommand(command: string | number | object, chat: Chat) {
152152
cursor: pointer;
153153
border-radius: 6px;
154154
line-height: 1em;
155+
font-size: 14px;
155156
156157
display: flex;
157158
align-items: center;
@@ -177,15 +178,21 @@ function handleCommand(command: string | number | object, chat: Chat) {
177178
}
178179
179180
&:hover {
180-
background-color: var(--hover-color);
181+
background-color: rgba(31, 35, 41, 0.1);
181182
182183
.icon-more {
183184
display: inline-flex;
185+
color: rgba(100, 106, 115, 1);
186+
187+
&:hover {
188+
background-color: rgba(31, 35, 41, 0.1);
189+
}
184190
}
185191
}
186192
187193
&.active {
188-
background-color: var(--active-color);
194+
background-color: rgba(255, 255, 255, 1);
195+
font-weight: 500;
189196
}
190197
}
191198
}

frontend/src/views/chat/index.vue

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
<template>
2-
<el-container class="chat-container">
2+
<el-container class="chat-container no-padding">
33
<el-aside v-if="!isAssistant" class="chat-container-left">
44
<el-container class="chat-container-right-container">
55
<el-header class="chat-list-header">
6-
<el-button type="primary" @click="createNewChat">
6+
<div class="title">
7+
<div>智能问数</div>
8+
<el-button link type="primary" class="icon-btn">
9+
<el-icon>
10+
<icon_sidebar_outlined />
11+
</el-icon>
12+
</el-button>
13+
</div>
14+
<el-button class="btn" type="primary" @click="createNewChat">
715
<el-icon>
816
<Plus />
917
</el-icon>
1018
{{ t('qa.New Conversation') }}
1119
</el-button>
20+
<el-input v-model="search" class="search" placeholder="placeholder" />
1221
</el-header>
1322
<el-main class="chat-list">
1423
<ChatList
@@ -166,6 +175,7 @@ import MdComponent from './component/MdComponent.vue'
166175
import RecommendQuestion from './RecommendQuestion.vue'
167176
import ChatCreator from './ChatCreator.vue'
168177
import { useI18n } from 'vue-i18n'
178+
import icon_sidebar_outlined from '@/assets/svg/icon_sidebar_outlined.svg'
169179
import { endsWith, find, startsWith } from 'lodash-es'
170180
171181
import { useAssistantStore } from '@/stores/assistant'
@@ -231,6 +241,8 @@ const goEmpty = () => {
231241
inputMessage.value = ''
232242
}
233243
244+
const search = ref<string>()
245+
234246
const createNewChat = async () => {
235247
goEmpty()
236248
if (isAssistant.value) {
@@ -494,6 +506,14 @@ const sendMessage = async () => {
494506
case 'info':
495507
console.log(data.msg)
496508
break
509+
case 'brief':
510+
currentChat.value.brief = data.brief
511+
chatList.value.forEach((c: Chat) => {
512+
if (c.id === currentChat.value.id) {
513+
c.brief = currentChat.value.brief
514+
}
515+
})
516+
break
497517
case 'error':
498518
currentRecord.error = data.content
499519
isTyping.value = false
@@ -808,21 +828,57 @@ defineExpose({
808828
.chat-container {
809829
height: 100%;
810830
811-
.chat-container-left {
812-
--ed-aside-width: 260px;
831+
.icon-btn {
832+
min-width: unset;
833+
width: 26px;
834+
height: 26px;
835+
font-size: 18px;
836+
837+
&:hover {
838+
background: rgba(31, 35, 41, 0.1);
839+
}
840+
}
813841
814-
box-shadow: 0 0 3px #d7d7d7;
815-
z-index: 1;
842+
.chat-container-left {
843+
--ed-aside-width: 280px;
844+
border-radius: 12px 0 0 12px;
845+
//box-shadow: 0 0 3px #d7d7d7;
846+
//z-index: 1;
816847
817-
background: var(--ed-fill-color-blank);
848+
background: rgba(245, 246, 247, 1);
818849
819850
.chat-container-right-container {
820851
height: 100%;
821852
822853
.chat-list-header {
854+
--ed-header-padding: 16px;
855+
--ed-header-height: calc(16px + 24px + 16px + 40px + 16px + 32px + 16px);
856+
823857
display: flex;
824858
align-items: center;
825859
justify-content: center;
860+
flex-direction: column;
861+
gap: 16px;
862+
863+
.title {
864+
height: 24px;
865+
width: 100%;
866+
display: flex;
867+
flex-direction: row;
868+
align-items: center;
869+
justify-content: space-between;
870+
font-weight: 500;
871+
}
872+
873+
.btn {
874+
width: 100%;
875+
height: 40px;
876+
}
877+
878+
.search {
879+
height: 32px;
880+
width: 100%;
881+
}
826882
}
827883
828884
.chat-list {
@@ -833,7 +889,7 @@ defineExpose({
833889
834890
.chat-record-list {
835891
padding: 0 0 20px 0;
836-
background: rgba(224, 224, 226, 0.29);
892+
background: rgba(255, 255, 255, 1);
837893
}
838894
839895
.chat-footer {

frontend/src/views/dashboard/editor/ChatChartSelection.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ defineExpose({
216216
}
217217
</style>
218218

219-
<style lang="less">
219+
<style lang="less" scoped>
220220
.custom-drawer {
221221
.ed-drawer__footer {
222222
height: 64px !important;
@@ -235,9 +235,9 @@ defineExpose({
235235
.chat-container-left {
236236
padding-top: 20px;
237237
--ed-aside-width: 260px;
238-
239-
box-shadow: 0 0 3px #d7d7d7;
240-
z-index: 1;
238+
border-radius: 12px 0 0 12px;
239+
//box-shadow: 0 0 3px #d7d7d7;
240+
//z-index: 1;
241241
242242
background: var(--ed-fill-color-blank);
243243
@@ -258,7 +258,7 @@ defineExpose({
258258
259259
.chat-record-list {
260260
padding: 0 0 20px 0;
261-
background: rgba(224, 224, 226, 0.29);
261+
background: rgba(255, 255, 255, 1);
262262
}
263263
264264
.chat-footer {

0 commit comments

Comments
 (0)