feat: 支持长回复 (#450)

* chore: rename environment variables files

* docs: update README.md about .env file

* feat: support long reply

* chore: upgrade chatgpt package and set long reply to false default

* chore: set long reply to false default
main
Yige 2 years ago committed by GitHub
parent 133a24e25f
commit 076c56d1d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,3 +2,6 @@
VITE_GLOB_API_URL=/api VITE_GLOB_API_URL=/api
VITE_APP_API_BASE_URL=http://localhost:3002/ VITE_APP_API_BASE_URL=http://localhost:3002/
# Whether long replies are supported, which may result in higher API fees
VITE_GLOB_OPEN_LONG_REPLY=false

@ -87,8 +87,8 @@ async function chatReplyProcess(
lastContext?: { conversationId?: string; parentMessageId?: string }, lastContext?: { conversationId?: string; parentMessageId?: string },
process?: (chat: ChatMessage) => void, process?: (chat: ChatMessage) => void,
) { ) {
if (!message) // if (!message)
return sendResponse({ type: 'Fail', message: 'Message is empty' }) // return sendResponse({ type: 'Fail', message: 'Message is empty' })
try { try {
let options: SendMessageOptions = { timeoutMs } let options: SendMessageOptions = { timeoutMs }

@ -15,6 +15,8 @@ import { t } from '@/locales'
let controller = new AbortController() let controller = new AbortController()
const openLongReply = import.meta.env.VITE_GLOB_OPEN_LONG_REPLY === 'true'
const route = useRoute() const route = useRoute()
const dialog = useDialog() const dialog = useDialog()
const ms = useMessage() const ms = useMessage()
@ -41,7 +43,7 @@ function handleSubmit() {
} }
async function onConversation() { async function onConversation() {
const message = prompt.value let message = prompt.value
if (loading.value) if (loading.value)
return return
@ -88,6 +90,8 @@ async function onConversation() {
scrollToBottom() scrollToBottom()
try { try {
let lastText = ''
const fetchChatAPIOnce = async () => {
await fetchChatAPIProcess<Chat.ConversationResponse>({ await fetchChatAPIProcess<Chat.ConversationResponse>({
prompt: message, prompt: message,
options, options,
@ -107,7 +111,7 @@ async function onConversation() {
dataSources.value.length - 1, dataSources.value.length - 1,
{ {
dateTime: new Date().toLocaleString(), dateTime: new Date().toLocaleString(),
text: data.text ?? '', text: lastText + data.text ?? '',
inversion: false, inversion: false,
error: false, error: false,
loading: false, loading: false,
@ -115,6 +119,14 @@ async function onConversation() {
requestOptions: { prompt: message, options: { ...options } }, requestOptions: { prompt: message, options: { ...options } },
}, },
) )
if (openLongReply && data.detail.choices[0].finish_reason === 'length') {
options.parentMessageId = data.id
lastText = data.text
message = ''
return fetchChatAPIOnce()
}
scrollToBottom() scrollToBottom()
} }
catch (error) { catch (error) {
@ -123,6 +135,9 @@ async function onConversation() {
}, },
}) })
} }
await fetchChatAPIOnce()
}
catch (error: any) { catch (error: any) {
const errorMessage = error?.message ?? t('common.wrong') const errorMessage = error?.message ?? t('common.wrong')
@ -181,7 +196,7 @@ async function onRegenerate(index: number) {
const { requestOptions } = dataSources.value[index] const { requestOptions } = dataSources.value[index]
const message = requestOptions?.prompt ?? '' let message = requestOptions?.prompt ?? ''
let options: Chat.ConversationRequest = {} let options: Chat.ConversationRequest = {}
@ -205,6 +220,8 @@ async function onRegenerate(index: number) {
) )
try { try {
let lastText = ''
const fetchChatAPIOnce = async () => {
await fetchChatAPIProcess<Chat.ConversationResponse>({ await fetchChatAPIProcess<Chat.ConversationResponse>({
prompt: message, prompt: message,
options, options,
@ -224,7 +241,7 @@ async function onRegenerate(index: number) {
index, index,
{ {
dateTime: new Date().toLocaleString(), dateTime: new Date().toLocaleString(),
text: data.text ?? '', text: lastText + data.text ?? '',
inversion: false, inversion: false,
error: false, error: false,
loading: false, loading: false,
@ -232,6 +249,13 @@ async function onRegenerate(index: number) {
requestOptions: { prompt: message, ...options }, requestOptions: { prompt: message, ...options },
}, },
) )
if (openLongReply && data.detail.choices[0].finish_reason === 'length') {
options.parentMessageId = data.id
lastText = data.text
message = ''
return fetchChatAPIOnce()
}
} }
catch (error) { catch (error) {
// //
@ -239,6 +263,8 @@ async function onRegenerate(index: number) {
}, },
}) })
} }
await fetchChatAPIOnce()
}
catch (error: any) { catch (error: any) {
if (error.message === 'canceled') { if (error.message === 'canceled') {
updateChatSome( updateChatSome(

Loading…
Cancel
Save