更新历史记录消息(获得更加准确的历史消息,后面考虑将 conv.history 移除)

This commit is contained in:
Wenjie Zhang 2025-03-30 15:16:08 +08:00
parent 7e6a974757
commit b653042e90

View File

@ -271,7 +271,7 @@ const meta = reactive(JSON.parse(localStorage.getItem('meta')) || {
selectedKB: null, selectedKB: null,
stream: true, stream: true,
summary_title: false, summary_title: false,
history_round: 5, history_round: 20,
db_id: null, db_id: null,
fontSize: 'default', fontSize: 'default',
wideScreen: false, wideScreen: false,
@ -303,6 +303,24 @@ const renderMarkdown = (msg) => {
} }
} }
// message history {role, content}
const getHistory = () => {
const history = conv.value.messages.map((msg) => {
if (msg.text) {
return {
role: msg.role === 'sent' ? 'user' : 'assistant',
content: msg.text
}
}
}).reduce((acc, cur) => {
if (cur) {
acc.push(cur)
}
return acc
}, [])
return history.slice(-meta.history_round)
}
const useDatabase = (index) => { const useDatabase = (index) => {
const selected = opts.databases[index] const selected = opts.databases[index]
console.log(selected) console.log(selected)
@ -494,14 +512,17 @@ const fetchChatResponse = (user_input, cur_res_id) => {
const controller = new AbortController(); const controller = new AbortController();
const signal = controller.signal; const signal = controller.signal;
const params = {
query: user_input,
history: getHistory(),
meta: meta,
cur_res_id: cur_res_id,
}
console.log(params)
fetch('/api/chat/', { fetch('/api/chat/', {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify(params),
query: user_input,
history: conv.value.history,
meta: meta,
cur_res_id: cur_res_id,
}),
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },