feat: add proxy support and fix streaming mode (#122)

This commit is contained in:
puppywang
2023-02-25 17:13:19 +08:00
committed by GitHub
parent cc91e95eed
commit 628187f5c3
7 changed files with 250 additions and 12 deletions

View File

@@ -8,7 +8,7 @@ import { useChat } from './hooks/useChat'
import { HoverButton, SvgIcon } from '@/components/common'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { useChatStore } from '@/store'
import { fetchChatAPI } from '@/api'
import { fetchChatAPIProcess } from '@/api'
let controller = new AbortController()
@@ -82,6 +82,42 @@ async function onConversation() {
scrollToBottom()
try {
await fetchChatAPIProcess<Chat.ConversationResponse>({
prompt: message,
options,
signal: controller.signal,
onDownloadProgress: ({ event }) => {
const xhr = event.target
const { responseText } = xhr
// Always process the final line
const lastIndex = responseText.lastIndexOf('\n')
let chunk = responseText
if (lastIndex !== -1)
chunk = responseText.substring(lastIndex)
try {
globalThis.console.log(`trunk = ${chunk}`)
const data = JSON.parse(chunk)
updateChat(
+uuid,
dataSources.value.length - 1,
{
dateTime: new Date().toLocaleString(),
text: data.text ?? '',
inversion: false,
error: false,
loading: false,
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
requestOptions: { prompt: message, options: { ...options } },
},
)
scrollToBottom()
}
catch (error) {
//
}
},
})
/*
const { data } = await fetchChatAPI<Chat.ConversationResponse>(message, options, controller.signal)
updateChat(
+uuid,
@@ -97,6 +133,7 @@ async function onConversation() {
},
)
scrollToBottom()
*/
}
catch (error: any) {
let errorMessage = error?.message ?? 'Something went wrong, please try again later.'
@@ -156,6 +193,41 @@ async function onRegenerate(index: number) {
)
try {
await fetchChatAPIProcess<Chat.ConversationResponse>({
prompt: message,
options,
signal: controller.signal,
onDownloadProgress: ({ event }) => {
const xhr = event.target
const { responseText } = xhr
// Always process the final line
const lastIndex = responseText.lastIndexOf('\n')
let chunk = responseText
if (lastIndex !== -1)
chunk = responseText.substring(lastIndex)
try {
globalThis.console.log(`trunk = ${chunk}`)
const data = JSON.parse(chunk)
updateChat(
+uuid,
index,
{
dateTime: new Date().toLocaleString(),
text: data.text ?? '',
inversion: false,
error: false,
loading: false,
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
requestOptions: { prompt: message, ...options },
},
)
}
catch (error) {
//
}
},
})
/*
const { data } = await fetchChatAPI<Chat.ConversationResponse>(message, options, controller.signal)
updateChat(
+uuid,
@@ -170,9 +242,10 @@ async function onRegenerate(index: number) {
requestOptions: { prompt: message, ...options },
},
)
*/
}
catch (error: any) {
let errorMessage = 'Something went wrong, please try again later.'
let errorMessage = error?.message ?? 'Something went wrong, please try again later.'
if (error.message === 'canceled')
errorMessage = 'Request canceled. Please try again.'