Skip to content

Commit

Permalink
Regenerate (#561)
Browse files Browse the repository at this point in the history
* Fix regenerate logic and UI button placement in chat components.

* Refactor `onRegenerate` function to handle regeneration logic more explicitly.

* Fix chat regeneration logic and add delete functionality

- Prevent creating empty user messages in `chat_main_handler.go`.
- Add logging for regenerating answers without new questions.
- Update `content.ts` to ensure proper deletion handling.
- Enhance `Conversation.vue` to handle regeneration correctly and delete chat messages.
- Add `deleteChat` function in `useChat.ts` for chat deletion.
  • Loading branch information
swuecho authored Dec 19, 2024
1 parent 0f4eebc commit 58b030d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 30 deletions.
16 changes: 10 additions & 6 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,16 @@ func genAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid string, ch
}

if existingPrompt {
_, err := h.service.CreateChatMessageSimple(ctx, chatSession.Uuid, chatUuid, "user", newQuestion, chatSession.Model, userID, baseURL, chatSession.SummarizeMode)
if err != nil {
http.Error(w,
eris.Wrap(err, "fail to create message: ").Error(),
http.StatusInternalServerError,
)
if newQuestion != "" {
_, err := h.service.CreateChatMessageSimple(ctx, chatSession.Uuid, chatUuid, "user", newQuestion, chatSession.Model, userID, baseURL, chatSession.SummarizeMode)
if err != nil {
http.Error(w,
eris.Wrap(err, "fail to create message: ").Error(),
http.StatusInternalServerError,
)
}
} else {
log.Println("no new question, regenerate answer")
}
} else {
chatPrompt, err := h.service.CreateChatPromptSimple(chatSessionUuid, newQuestion, userID)
Expand Down
1 change: 1 addition & 0 deletions web/src/api/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const deleteChatData = async (chat: Chat.Message) => {
await deleteChatPrompt(chat.uuid)
else
await deleteChatMessage(chat.uuid)

}

export const updateChatData = async (chat: Chat.Message) => {
Expand Down
87 changes: 70 additions & 17 deletions web/src/views/chat/components/Conversation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useScroll } from '@/views/chat/hooks/useScroll'
import { useChat } from '@/views/chat/hooks/useChat'
import HeaderComponent from '@/views/chat/components/Header/index.vue'
import SessionConfig from '@/views/chat/components/Session/SessionConfig.vue'
import { createChatBot, createChatSnapshot, fetchChatStream } from '@/api'
import { createChatBot, createChatSnapshot, deleteChatMessage, fetchChatStream } from '@/api'
import { HoverButton, SvgIcon } from '@/components/common'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { useChatStore, usePromptStore } from '@/store'
Expand Down Expand Up @@ -41,7 +41,7 @@ const { sessionUuid } = defineProps({
const { isMobile } = useBasicLayout()
const { addChat, updateChat, updateChatPartial } = useChat()
const { addChat, deleteChat, updateChat, updateChatPartial } = useChat()
const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll()
// session uuid
chatStore.syncChatMessages(sessionUuid)
Expand Down Expand Up @@ -234,39 +234,91 @@ async function onRegenerate(index: number) {
const chat = dataSources.value[index]
console.log("regen", chat)
const chatUuid = chat.uuid
const message = chat.text
// from user
const inversion = chat.inversion
loading.value = true
updateChat(
sessionUuid,
index,
{
uuid: chatUuid,
dateTime: nowISO(),
text: '',
inversion: false,
error: false,
loading: true,
},
)
let updateIndex = index;
let isRegenerate = true;
if (inversion) {
// trigger from user message
const chatNext = dataSources.value[index + 1]
if (chatNext) {
updateIndex = index + 1
isRegenerate = false
// if there are answer below. then clear
await deleteChatMessage(chatNext.uuid)
updateChat(
sessionUuid,
updateIndex,
{
uuid: chatNext.uuid,
dateTime: nowISO(),
text: '',
inversion: false,
error: false,
loading: true,
},
)
} else {
// add a blank response
updateIndex = index + 1
isRegenerate = false
addChat(
sessionUuid,
{
uuid: '',
dateTime: nowISO(),
text: '',
loading: true,
inversion: false,
error: false,
},
)
}
// if there are answer below. then clear
// if not, add answer
} else {
// clear the old answer for regenerating
updateChat(
sessionUuid,
index,
{
uuid: chatUuid,
dateTime: nowISO(),
text: '',
inversion: false,
error: false,
loading: true,
},
)
}
try {
const subscribleStrem = async () => {
try {
// Send the request with axios
const response = fetchChatStream(
sessionUuid,
chatUuid,
true,
message,
isRegenerate,
"",
(progress: any) => {
const xhr = progress.event.target
const {
responseText,
status
} = xhr
if (status >= 400) {
const error_json: { code: number; message: string; details: any } = JSON.parse(responseText)
nui_msg.error(t(error_json.message), {
Expand Down Expand Up @@ -300,6 +352,7 @@ async function onRegenerate(index: number) {
},
)
}
}
},
)
Expand Down
14 changes: 7 additions & 7 deletions web/src/views/chat/components/Message/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ function handleDelete() {
<TextComponent ref="textRef" class="message-text" :inversion="inversion" :error="error" :text="text"
:code="code" :loading="loading" :idex="index" />
<div class="flex flex-col">
<!-- testid="chat-message-regenerate" not ok, someting like testclass -->
<button v-if="!inversion"
class="chat-message-regenerate mb-2 transition text-neutral-300 hover:text-neutral-800 dark:hover:text-neutral-300"
@click="handleRegenerate">
<SvgIcon icon="ri:restart-line" />
</button>

<button v-if="!isPrompt && inversion"
class="mb-2 transition text-neutral-300 hover:text-neutral-800 dark:hover:text-neutral-300"
:disabled="pining" @click="emit('togglePin')">
<SvgIcon :icon="isPin ? 'ri:unpin-line' : 'ri:pushpin-line'" />
</button>

<!-- testid="chat-message-regenerate" not ok, something like testclass -->
<button v-if="!isPrompt"
class="chat-message-regenerate mb-2 transition text-neutral-300 hover:text-neutral-800 dark:hover:text-neutral-300"
@click="handleRegenerate">
<SvgIcon icon="ri:restart-line" />
</button>

</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions web/src/views/chat/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function useChat() {
chatStore.addChatByUuid(uuid, chat)
}

const deleteChat = (uuid: string, index: number) => {
chatStore.deleteChatByUuid(uuid, index)
}

const updateChat = (uuid: string, index: number, chat: Chat.Message) => {
chatStore.updateChatByUuid(uuid, index, chat)
}
Expand All @@ -35,6 +39,7 @@ export function useChat() {

return {
addChat,
deleteChat,
updateChat,
updateChatText,
updateChatPartial,
Expand Down

0 comments on commit 58b030d

Please sign in to comment.