Skip to content

Commit 7126c7b

Browse files
perf: Assistant chat
1 parent 77e5513 commit 7126c7b

File tree

8 files changed

+72
-10
lines changed

8 files changed

+72
-10
lines changed

backend/apps/chat/api/chat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ async def start_chat(session: SessionDep, current_user: CurrentUser, create_chat
8181
status_code=500,
8282
detail=str(e)
8383
)
84+
85+
@router.post("/assistant/start")
86+
async def start_chat(session: SessionDep, current_user: CurrentUser):
87+
try:
88+
return create_chat(session, current_user, CreateChat(), False)
89+
except Exception as e:
90+
raise HTTPException(
91+
status_code=500,
92+
detail=str(e)
93+
)
8494

8595

8696
@router.post("/recommend_questions/{chat_record_id}")

backend/apps/chat/curd/chat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def save_question(session: SessionDep, current_user: CurrentUser, question: Chat
203203
if not question.question or question.question.strip() == '':
204204
raise Exception("Question cannot be Empty")
205205

206-
chat = session.query(Chat).filter(Chat.id == question.chat_id).first()
206+
#chat = session.query(Chat).filter(Chat.id == question.chat_id).first()
207+
chat: Chat = session.get(Chat, question.chat_id)
207208
if not chat:
208209
raise Exception(f"Chat with id {question.chat_id} not found")
209210

backend/apps/chat/task/llm.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ def __init__(self, session: SessionDep, current_user: CurrentUser, chat_question
4949
self.session = session
5050
self.current_user = current_user
5151

52-
chat = self.session.query(Chat).filter(Chat.id == chat_question.chat_id).first()
52+
#chat = self.session.query(Chat).filter(Chat.id == chat_question.chat_id).first()
53+
chat_id = chat_question.chat_id
54+
chat: Chat = self.session.get(Chat, chat_id)
5355
if not chat:
54-
raise Exception(f"Chat with id {chat_question.chat_id} not found")
56+
raise Exception(f"Chat with id {chat_id} not found")
5557
ds: CoreDatasource | None = None
5658
if chat.datasource:
5759
# Get available datasource
58-
ds = self.session.query(CoreDatasource).filter(CoreDatasource.id == chat.datasource).first()
60+
# ds = self.session.query(CoreDatasource).filter(CoreDatasource.id == chat.datasource).first()
61+
ds = self.session.get(CoreDatasource, chat.datasource)
5962
if not ds:
6063
raise Exception("No available datasource configuration found")
6164

@@ -65,7 +68,7 @@ def __init__(self, session: SessionDep, current_user: CurrentUser, chat_question
6568
map(lambda x: ChatRecord(**x.model_dump()), filter(lambda r: True if r.first_chat != True else False,
6669
list_base_records(session=self.session,
6770
current_user=current_user,
68-
chart_id=chat_question.chat_id))))
71+
chart_id=chat_id))))
6972
# get schema
7073
if ds:
7174
chat_question.db_schema = get_table_schema(session=self.session, ds=ds)

frontend/src/api/chat.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ export const chatApi = {
302302
startChat: (data: any): Promise<ChatInfo> => {
303303
return request.post('/chat/start', data)
304304
},
305+
startAssistantChat: (): Promise<ChatInfo> => {
306+
return request.post('/chat/assistant/start')
307+
},
305308
renameChat: (chat_id: number | undefined, brief: string): Promise<string> => {
306309
return request.post('/chat/rename', { id: chat_id, brief: brief })
307310
},

frontend/src/stores/assistant.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
import { defineStore } from 'pinia'
22
import { store } from './index'
3+
import { chatApi, ChatInfo } from '@/api/chat'
34

45
interface AssistantState {
56
token: string
7+
assistant: boolean
68
}
79

810
export const AssistantStore = defineStore('assistant', {
911
state: (): AssistantState => {
1012
return {
1113
token: '',
14+
assistant: false,
1215
}
1316
},
1417
getters: {
1518
getToken(): string {
1619
return this.token
1720
},
21+
getAssistant(): boolean {
22+
return this.assistant
23+
},
1824
},
1925
actions: {
2026
setToken(token: string) {
2127
this.token = token
2228
},
29+
setAssistant(assistant: boolean) {
30+
this.assistant = assistant
31+
},
32+
async setChat() {
33+
if (!this.assistant) {
34+
return null
35+
}
36+
const res = await chatApi.startAssistantChat()
37+
const chat: ChatInfo | undefined = chatApi.toChatInfo(res)
38+
return chat
39+
},
2340
clear() {
2441
this.$reset()
2542
},

frontend/src/utils/request.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ class HttpService {
180180
}
181181
if (error?.response?.data) {
182182
const msgData: any = error.response.data
183-
msgData.msg && (errorMessage = msgData.msg)
183+
if (msgData?.msg) {
184+
errorMessage = msgData.msg
185+
}
184186
}
185187
} else if (error.request) {
186188
errorMessage = 'No response from server'
@@ -234,6 +236,11 @@ class HttpService {
234236
if (token) {
235237
heads['X-SQLBOT-TOKEN'] = `Bearer ${token}`
236238
}
239+
if (assistantStore.getToken) {
240+
heads['X-SQLBOT-ASSISTANT-TOKEN'] = `Assistant ${assistantStore.getToken}`
241+
if (heads['X-SQLBOT-TOKEN']) heads.delete('X-SQLBOT-TOKEN')
242+
}
243+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
237244
// @ts-ignore
238245
const request_key = LicenseGenerator.generate()
239246
heads['X-SQLBOT-KEY'] = request_key

frontend/src/views/chat/index.vue

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<el-container class="chat-container">
3-
<el-aside class="chat-container-left">
3+
<el-aside v-if="!isAssistant" class="chat-container-left">
44
<el-container class="chat-container-right-container">
55
<el-header class="chat-list-header">
66
<el-button type="primary" @click="createNewChat">
@@ -114,7 +114,10 @@
114114
</template>
115115
</el-scrollbar>
116116
</el-main>
117-
<el-footer v-if="computedMessages.length > 0" class="chat-footer">
117+
<el-footer
118+
v-if="computedMessages.length > 0 || (isAssistant && currentChatId)"
119+
class="chat-footer"
120+
>
118121
<div style="height: 24px">
119122
<template v-if="currentChat.datasource && currentChat.datasource_name">
120123
{{ t('ds.title') }}:{{ currentChat.datasource_name }}
@@ -147,7 +150,7 @@
147150
</el-footer>
148151
</el-container>
149152

150-
<ChatCreator ref="chatCreatorRef" @on-chat-created="onChatCreated" />
153+
<ChatCreator v-if="!isAssistant" ref="chatCreatorRef" @on-chat-created="onChatCreated" />
151154
</el-container>
152155
</template>
153156

@@ -165,6 +168,11 @@ import ChatCreator from './ChatCreator.vue'
165168
import { useI18n } from 'vue-i18n'
166169
import { endsWith, find, startsWith } from 'lodash-es'
167170
171+
import { useAssistantStore } from '@/stores/assistant'
172+
const assistantStore = useAssistantStore()
173+
174+
const isAssistant = computed(() => assistantStore.getAssistant)
175+
168176
const { t } = useI18n()
169177
170178
const inputMessage = ref('')
@@ -223,8 +231,15 @@ const goEmpty = () => {
223231
inputMessage.value = ''
224232
}
225233
226-
const createNewChat = () => {
234+
const createNewChat = async () => {
227235
goEmpty()
236+
if (isAssistant.value) {
237+
const assistantChat = await assistantStore.setChat()
238+
if (assistantChat) {
239+
onChatCreated(assistantChat as any)
240+
}
241+
return
242+
}
228243
chatCreatorRef.value?.showDs()
229244
}
230245
@@ -391,6 +406,11 @@ const sendMessage = async () => {
391406
loading.value = true
392407
isTyping.value = true
393408
409+
/* const assistantChat = await assistantStore.setChat()
410+
if (assistantChat) {
411+
onChatCreated(assistantChat as any)
412+
} */
413+
394414
const currentRecord = new ChatRecord()
395415
currentRecord.create_time = new Date()
396416
currentRecord.chat_id = currentChatId.value

frontend/src/views/embedded/assistant.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ onBeforeMount(async () => {
9898
const assistantId = route.params.id
9999
validator.value = await assistantApi.validate(assistantId)
100100
assistantStore.setToken(validator.value.token)
101+
assistantStore.setAssistant(true)
101102
loading.value = false
102103
})
103104
</script>

0 commit comments

Comments
 (0)