feat: supported chat histories

Signed-off-by: Neko Ayaka <neko@ayaka.moe>
This commit is contained in:
Neko Ayaka
2024-12-02 00:31:39 +08:00
parent 36a191b477
commit 212c6937e7
2 changed files with 16 additions and 10 deletions
+13 -3
View File
@@ -137,19 +137,26 @@ function getVolumeWithMinMaxNormalizeWithFrameUpdates() {
mouthOpenSize.value = calculateVolume(audioWaveformRef.value!.analyser(), 'minmax')
}
function onSendMessage(sendingMessage: string) {
function setupLipSync() {
if (!lipSyncStarted.value) {
getVolumeWithMinMaxNormalizeWithFrameUpdates()
audioContext.resume()
lipSyncStarted.value = true
}
}
function onSendMessage(sendingMessage: string) {
if (!sendingMessage)
return
setupLipSync()
const message: Message = { role: 'assistant', content: '' }
messages.value.push({ role: 'user', content: sendingMessage })
messages.value.push(message)
const index = messages.value.length - 1
llm.stream(model.value, sendingMessage).then(async (res) => {
llm.stream(model.value, messages.value.slice(0, messages.value.length - 1)).then(async (res) => {
for await (const textPart of res.textStream) {
messages.value[index].content += textPart
messageContentQueue.add(textPart)
@@ -161,11 +168,14 @@ function onSendMessage(sendingMessage: string) {
messageInput.value = ''
}
watch(openAiApiKey, (value) => {
watch(openAiApiKey, async (value) => {
llm.setupOpenAI({
apiKey: value,
baseURL: openAiApiBaseURL.value,
})
const fetchedModels = await llm.models()
supportedModels.value = fetchedModels.data
})
onMounted(async () => {
+3 -7
View File
@@ -1,4 +1,5 @@
import { defineStore } from 'pinia'
import type { CoreMessage } from 'ai'
import { streamText } from 'ai'
import { type OpenAIProvider, type OpenAIProviderSettings, createOpenAI } from '@ai-sdk/openai'
import { OpenAI } from 'openai'
@@ -17,18 +18,13 @@ export const useLLM = defineStore('llm', () => {
openAIProvider.value = createOpenAI(options)
}
async function stream(model: string, content: string) {
async function stream(model: string, messages: CoreMessage[]) {
if (!openAIProvider.value)
throw new Error('OpenAI not initialized')
return await streamText({
model: openAIProvider.value(model),
messages: [
{
role: 'user',
content,
},
],
messages,
})
}