mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-08 01:06:59 +08:00
fix: No need to call the model to recommend problems during each session switch
This commit is contained in:
@@ -12,7 +12,7 @@ from starlette.responses import JSONResponse
|
||||
|
||||
from apps.chat.curd.chat import list_chats, get_chat_with_records, create_chat, rename_chat, \
|
||||
delete_chat, get_chat_chart_data, get_chat_predict_data, get_chat_with_records_with_data, get_chat_record_by_id, \
|
||||
format_json_data, format_json_list_data, get_chart_config, list_recent_questions
|
||||
format_json_data, format_json_list_data, get_chart_config, list_recent_questions,get_chat as get_chat_exec
|
||||
from apps.chat.models.chat_model import CreateChat, ChatRecord, RenameChat, ChatQuestion, AxisObj, QuickCommand, \
|
||||
ChatInfo, Chat, ChatFinishStep
|
||||
from apps.chat.task.llm import LLMService
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import datetime
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
import orjson
|
||||
import sqlparse
|
||||
@@ -650,7 +650,7 @@ def save_select_datasource_answer(session: SessionDep, record_id: int, answer: s
|
||||
|
||||
|
||||
def save_recommend_question_answer(session: SessionDep, record_id: int,
|
||||
answer: dict = None) -> ChatRecord:
|
||||
answer: dict = None, articles_number: Optional[int] = 4) -> ChatRecord:
|
||||
if not record_id:
|
||||
raise Exception("Record id cannot be None")
|
||||
|
||||
@@ -673,12 +673,19 @@ def save_recommend_question_answer(session: SessionDep, record_id: int,
|
||||
)
|
||||
|
||||
session.execute(stmt)
|
||||
|
||||
session.commit()
|
||||
|
||||
record = get_chat_record_by_id(session, record_id)
|
||||
record.recommended_question_answer = recommended_question_answer
|
||||
record.recommended_question = recommended_question
|
||||
if articles_number > 4:
|
||||
stmt_chat = update(Chat).where(and_(Chat.id == record.chat_id)).values(
|
||||
recommended_question_answer=recommended_question_answer,
|
||||
recommended_question=recommended_question,
|
||||
recommended_generate=True
|
||||
)
|
||||
session.execute(stmt_chat)
|
||||
session.commit()
|
||||
|
||||
return record
|
||||
|
||||
|
||||
@@ -86,6 +86,9 @@ class Chat(SQLModel, table=True):
|
||||
origin: Optional[int] = Field(
|
||||
sa_column=Column(Integer, nullable=False, default=0)) # 0: default, 1: mcp, 2: assistant
|
||||
brief_generate: bool = Field(default=False)
|
||||
recommended_question_answer: str = Field(sa_column=Column(Text, nullable=True))
|
||||
recommended_question: str = Field(sa_column=Column(Text, nullable=True))
|
||||
recommended_generate: bool = Field(default=False)
|
||||
|
||||
|
||||
class ChatRecord(SQLModel, table=True):
|
||||
@@ -172,6 +175,8 @@ class ChatInfo(BaseModel):
|
||||
ds_type: str = ''
|
||||
datasource_name: str = ''
|
||||
datasource_exists: bool = True
|
||||
recommended_question: Optional[str] = None
|
||||
recommended_generate: Optional[bool] = False
|
||||
records: List[ChatRecord | dict] = []
|
||||
|
||||
|
||||
|
||||
@@ -397,7 +397,7 @@ class LLMService:
|
||||
reasoning_content=full_thinking_text,
|
||||
token_usage=token_usage)
|
||||
self.record = save_recommend_question_answer(session=_session, record_id=self.record.id,
|
||||
answer={'content': full_guess_text})
|
||||
answer={'content': full_guess_text}, articles_number=self.articles_number)
|
||||
|
||||
yield {'recommended_question': self.record.recommended_question}
|
||||
|
||||
|
||||
@@ -139,6 +139,8 @@ export class Chat {
|
||||
datasource?: number
|
||||
engine_type?: string
|
||||
ds_type?: string
|
||||
recommended_question?: string | undefined
|
||||
recommended_generate?: boolean | undefined
|
||||
|
||||
constructor()
|
||||
constructor(
|
||||
@@ -200,7 +202,9 @@ export class ChatInfo extends Chat {
|
||||
ds_type?: string,
|
||||
datasource_name?: string,
|
||||
datasource_exists: boolean = true,
|
||||
records: Array<ChatRecord> = []
|
||||
records: Array<ChatRecord> = [],
|
||||
recommended_question?: string | undefined,
|
||||
recommended_generate?: boolean | undefined
|
||||
) {
|
||||
super()
|
||||
if (param1 !== undefined) {
|
||||
@@ -213,6 +217,8 @@ export class ChatInfo extends Chat {
|
||||
this.datasource = param1.datasource
|
||||
this.engine_type = param1.engine_type
|
||||
this.ds_type = param1.ds_type
|
||||
this.recommended_question = recommended_question
|
||||
this.recommended_generate = recommended_generate
|
||||
} else {
|
||||
this.id = param1
|
||||
this.create_time = getDate(create_time)
|
||||
@@ -222,6 +228,8 @@ export class ChatInfo extends Chat {
|
||||
this.datasource = datasource
|
||||
this.engine_type = engine_type
|
||||
this.ds_type = ds_type
|
||||
this.recommended_question = recommended_question
|
||||
this.recommended_generate = recommended_generate
|
||||
}
|
||||
}
|
||||
this.datasource_name = datasource_name
|
||||
@@ -287,7 +295,9 @@ export const chatApi = {
|
||||
data.ds_type,
|
||||
data.datasource_name,
|
||||
data.datasource_exists,
|
||||
toChatRecordList(data.records)
|
||||
toChatRecordList(data.records),
|
||||
data.recommended_question,
|
||||
data.recommended_generate
|
||||
)
|
||||
},
|
||||
toChatInfoList: (list: any[] = []): ChatInfo[] => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
|
||||
import { computed, nextTick, onBeforeUnmount, ref, toRefs } from 'vue'
|
||||
import { endsWith, startsWith } from 'lodash-es'
|
||||
import { chatApi } from '@/api/chat.ts'
|
||||
import { chatApi, ChatInfo } from '@/api/chat.ts'
|
||||
import { recommendedApi } from '@/api/recommendedApi.ts'
|
||||
|
||||
const props = withDefaults(
|
||||
@@ -9,14 +9,19 @@ const props = withDefaults(
|
||||
recordId?: number
|
||||
disabled?: boolean
|
||||
datasource?: number
|
||||
currentChat?: ChatInfo
|
||||
}>(),
|
||||
{
|
||||
recordId: undefined,
|
||||
disabled: false,
|
||||
datasource: undefined,
|
||||
chatRecommendedQuestions: undefined,
|
||||
currentChat: () => new ChatInfo(),
|
||||
}
|
||||
)
|
||||
|
||||
const { currentChat } = toRefs(props)
|
||||
|
||||
const emits = defineEmits(['clickQuestion', 'stop', 'loadingOver'])
|
||||
|
||||
const loading = ref(false)
|
||||
@@ -47,6 +52,8 @@ async function getRecommendQuestions(articles_number: number) {
|
||||
recommendedApi.get_datasource_recommended_base(props.datasource).then((res) => {
|
||||
if (res.recommended_config === 2) {
|
||||
questions.value = res.questions
|
||||
} else if (currentChat.value.recommended_generate) {
|
||||
questions.value = currentChat.value.recommended_question
|
||||
} else {
|
||||
getRecommendQuestionsLLM(articles_number)
|
||||
}
|
||||
@@ -116,6 +123,8 @@ async function getRecommendQuestionsLLM(articles_number: number) {
|
||||
endsWith(data.content.trim(), ']')
|
||||
) {
|
||||
questions.value = data.content
|
||||
currentChat.value.recommended_question = data.content
|
||||
currentChat.value.recommended_generate = true
|
||||
await nextTick()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user