Skip to content

Commit 2fa1a94

Browse files
committed
fix: No need to call the model to recommend problems during each session switch
1 parent 8d75f93 commit 2fa1a94

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

backend/apps/chat/api/chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from apps.chat.curd.chat import list_chats, get_chat_with_records, create_chat, rename_chat, \
1414
delete_chat, get_chat_chart_data, get_chat_predict_data, get_chat_with_records_with_data, get_chat_record_by_id, \
15-
format_json_data, format_json_list_data, get_chart_config, list_recent_questions
15+
format_json_data, format_json_list_data, get_chart_config, list_recent_questions,get_chat as get_chat_exec
1616
from apps.chat.models.chat_model import CreateChat, ChatRecord, RenameChat, ChatQuestion, AxisObj, QuickCommand, \
1717
ChatInfo, Chat, ChatFinishStep
1818
from apps.chat.task.llm import LLMService

backend/apps/chat/curd/chat.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
from typing import List
2+
from typing import List, Optional
33

44
import orjson
55
import sqlparse
@@ -650,7 +650,7 @@ def save_select_datasource_answer(session: SessionDep, record_id: int, answer: s
650650

651651

652652
def save_recommend_question_answer(session: SessionDep, record_id: int,
653-
answer: dict = None) -> ChatRecord:
653+
answer: dict = None, articles_number: Optional[int] = 4) -> ChatRecord:
654654
if not record_id:
655655
raise Exception("Record id cannot be None")
656656

@@ -673,12 +673,19 @@ def save_recommend_question_answer(session: SessionDep, record_id: int,
673673
)
674674

675675
session.execute(stmt)
676-
677676
session.commit()
678677

679678
record = get_chat_record_by_id(session, record_id)
680679
record.recommended_question_answer = recommended_question_answer
681680
record.recommended_question = recommended_question
681+
if articles_number > 4:
682+
stmt_chat = update(Chat).where(and_(Chat.id == record.chat_id)).values(
683+
recommended_question_answer=recommended_question_answer,
684+
recommended_question=recommended_question,
685+
recommended_generate=True
686+
)
687+
session.execute(stmt_chat)
688+
session.commit()
682689

683690
return record
684691

backend/apps/chat/models/chat_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Chat(SQLModel, table=True):
8686
origin: Optional[int] = Field(
8787
sa_column=Column(Integer, nullable=False, default=0)) # 0: default, 1: mcp, 2: assistant
8888
brief_generate: bool = Field(default=False)
89+
recommended_question_answer: str = Field(sa_column=Column(Text, nullable=True))
90+
recommended_question: str = Field(sa_column=Column(Text, nullable=True))
91+
recommended_generate: bool = Field(default=False)
8992

9093

9194
class ChatRecord(SQLModel, table=True):
@@ -172,6 +175,8 @@ class ChatInfo(BaseModel):
172175
ds_type: str = ''
173176
datasource_name: str = ''
174177
datasource_exists: bool = True
178+
recommended_question: Optional[str] = None
179+
recommended_generate: Optional[bool] = False
175180
records: List[ChatRecord | dict] = []
176181

177182

backend/apps/chat/task/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def generate_recommend_questions_task(self, _session: Session):
397397
reasoning_content=full_thinking_text,
398398
token_usage=token_usage)
399399
self.record = save_recommend_question_answer(session=_session, record_id=self.record.id,
400-
answer={'content': full_guess_text})
400+
answer={'content': full_guess_text}, articles_number=self.articles_number)
401401

402402
yield {'recommended_question': self.record.recommended_question}
403403

frontend/src/api/chat.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export class Chat {
139139
datasource?: number
140140
engine_type?: string
141141
ds_type?: string
142+
recommended_question?: string | undefined
143+
recommended_generate?: boolean | undefined
142144

143145
constructor()
144146
constructor(
@@ -200,7 +202,9 @@ export class ChatInfo extends Chat {
200202
ds_type?: string,
201203
datasource_name?: string,
202204
datasource_exists: boolean = true,
203-
records: Array<ChatRecord> = []
205+
records: Array<ChatRecord> = [],
206+
recommended_question?: string | undefined,
207+
recommended_generate?: boolean | undefined
204208
) {
205209
super()
206210
if (param1 !== undefined) {
@@ -213,6 +217,8 @@ export class ChatInfo extends Chat {
213217
this.datasource = param1.datasource
214218
this.engine_type = param1.engine_type
215219
this.ds_type = param1.ds_type
220+
this.recommended_question = recommended_question
221+
this.recommended_generate = recommended_generate
216222
} else {
217223
this.id = param1
218224
this.create_time = getDate(create_time)
@@ -222,6 +228,8 @@ export class ChatInfo extends Chat {
222228
this.datasource = datasource
223229
this.engine_type = engine_type
224230
this.ds_type = ds_type
231+
this.recommended_question = recommended_question
232+
this.recommended_generate = recommended_generate
225233
}
226234
}
227235
this.datasource_name = datasource_name
@@ -287,7 +295,9 @@ export const chatApi = {
287295
data.ds_type,
288296
data.datasource_name,
289297
data.datasource_exists,
290-
toChatRecordList(data.records)
298+
toChatRecordList(data.records),
299+
data.recommended_question,
300+
data.recommended_generate
291301
)
292302
},
293303
toChatInfoList: (list: any[] = []): ChatInfo[] => {

frontend/src/views/chat/RecommendQuestionQuick.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
<script setup lang="ts">
2-
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
2+
import { computed, nextTick, onBeforeUnmount, ref, toRefs } from 'vue'
33
import { endsWith, startsWith } from 'lodash-es'
4-
import { chatApi } from '@/api/chat.ts'
4+
import { chatApi, ChatInfo } from '@/api/chat.ts'
55
import { recommendedApi } from '@/api/recommendedApi.ts'
66
77
const props = withDefaults(
88
defineProps<{
99
recordId?: number
1010
disabled?: boolean
1111
datasource?: number
12+
currentChat?: ChatInfo
1213
}>(),
1314
{
1415
recordId: undefined,
1516
disabled: false,
1617
datasource: undefined,
18+
chatRecommendedQuestions: undefined,
19+
currentChat: () => new ChatInfo(),
1720
}
1821
)
1922
23+
const { currentChat } = toRefs(props)
24+
2025
const emits = defineEmits(['clickQuestion', 'stop', 'loadingOver'])
2126
2227
const loading = ref(false)
@@ -47,6 +52,8 @@ async function getRecommendQuestions(articles_number: number) {
4752
recommendedApi.get_datasource_recommended_base(props.datasource).then((res) => {
4853
if (res.recommended_config === 2) {
4954
questions.value = res.questions
55+
} else if (currentChat.value.recommended_generate) {
56+
questions.value = currentChat.value.recommended_question
5057
} else {
5158
getRecommendQuestionsLLM(articles_number)
5259
}
@@ -116,6 +123,8 @@ async function getRecommendQuestionsLLM(articles_number: number) {
116123
endsWith(data.content.trim(), ']')
117124
) {
118125
questions.value = data.content
126+
currentChat.value.recommended_question = data.content
127+
currentChat.value.recommended_generate = true
119128
await nextTick()
120129
}
121130
}

0 commit comments

Comments
 (0)