-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stream output and memory mode
- Loading branch information
1 parent
fdcb490
commit 06969ec
Showing
9 changed files
with
130 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,68 @@ | ||
import { getOpenAIResultApi } from '@/api' | ||
import { getOpenAIResultStream } from '@/api' | ||
import { executeSQL } from '@/sqls' | ||
import { useSessionStore, useRoleStore } from '@/stores' | ||
import { RecordData } from '@/types' | ||
import { useSessionStore, useRoleStore, useSettingsStore } from '@/stores' | ||
import { RecordData, SessionData } from '@/types' | ||
|
||
export const getAiMessage = async (value?: string) => { | ||
const { currentRole } = useRoleStore() | ||
if (!currentRole) return | ||
|
||
let messages: RecordData[] | ||
const messages: RecordData[] = [] | ||
|
||
const { sessionDataList, currentSession } = useSessionStore() | ||
const { isMemory } = useSettingsStore() | ||
|
||
const lastQuestion = sessionDataList.filter((item) => item.is_ask).at(-1) | ||
|
||
// 记忆模式,或者是第一次对话,都要生成角色描述 | ||
if (sessionDataList.length < 3 || isMemory) | ||
messages.push({ | ||
role: 'system', | ||
content: currentRole.description | ||
}) | ||
|
||
// 获取记忆(限制5条),往前推直到出现is_momery为false的 | ||
// TODO 应该进行限流,防止出现过多的记忆,导致token超出 | ||
const addMemory = async () => { | ||
if (isMemory) { | ||
const sql = `SELECT * FROM session_data WHERE session_id = '${currentSession?.id}' ORDER BY id DESC LIMIT 5;` | ||
const memoryList = (await executeSQL(sql)) as SessionData[] | ||
|
||
let count = 0 | ||
const arr = [] | ||
while (count < memoryList.length) { | ||
if (!memoryList[count].is_memory) break | ||
arr.push(JSON.parse(memoryList[count++].message as any)) | ||
} | ||
messages.push(...arr.reverse()) | ||
} | ||
} | ||
|
||
// 再次生成上一次问题 | ||
if (!value) { | ||
const { sessionDataList } = useSessionStore() | ||
const lastQuestion = sessionDataList.filter((item) => item.is_ask).at(-1) | ||
|
||
if (!lastQuestion) return | ||
|
||
// 为了保证统一,这之后的内容全部删掉 | ||
const deleteSql = `DELETE FROM session_data WHERE session_id = '${lastQuestion?.session_id}' AND id >= ${lastQuestion?.id};` | ||
|
||
await executeSQL(deleteSql) | ||
|
||
messages = JSON.parse(lastQuestion?.messages as any) || [] | ||
await addMemory() | ||
messages.push(JSON.parse(lastQuestion?.message as any)) | ||
} else { | ||
// TODO 这里可以优化,如何携带上一次的对话内容 | ||
messages = [ | ||
{ | ||
role: 'system', | ||
content: currentRole.description | ||
}, | ||
{ | ||
role: 'user', | ||
content: value | ||
} | ||
] | ||
await addMemory() | ||
messages.push({ | ||
role: 'user', | ||
content: value | ||
}) | ||
} | ||
|
||
const { isThinking } = storeToRefs(useSessionStore()) | ||
const { addSessionData } = useSessionStore() | ||
|
||
isThinking.value = true | ||
|
||
addSessionData(true, '', messages) | ||
const result = await getOpenAIResultApi(messages) | ||
addSessionData(true, '', messages.at(-1)!) | ||
await getOpenAIResultStream(messages) | ||
|
||
isThinking.value = false | ||
|
||
console.log('result', result) | ||
|
||
if (!result) return | ||
|
||
// TODO 处理流式输出的结果 | ||
addSessionData(false, '', result.message) | ||
} |