Skip to content

Commit

Permalink
fix_regenerate_bug (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho authored Mar 28, 2023
1 parent 5c95ea3 commit aa37ee8
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
e2e:
runs-on: ubuntu-latest
needs: [build_api, build_ui]
timeout-minutes: 5
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- name: Login to GitHub Container Registry
Expand Down
33 changes: 20 additions & 13 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func genAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid string, ch
}

if isTest(msgs) {
answerText, answerID, shouldReturn := chatStreamTest(w, chatSession, msgs)
answerText, answerID, shouldReturn := chatStreamTest(w, chatSession, msgs, chatUuid, false)
if shouldReturn {
return
}
Expand All @@ -219,7 +219,7 @@ func genAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid string, ch
)
}
} else {
answerText, answerID, shouldReturn := chatStream(w, chatSession, msgs)
answerText, answerID, shouldReturn := chatStream(w, chatSession, msgs, chatUuid, false)
if shouldReturn {
return
}
Expand Down Expand Up @@ -252,26 +252,25 @@ func regenerateAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid str
isTestChat := isTest(chatCompletionMessages)

if isTestChat {
answerText, answerID, shouldReturn := chatStreamTest(w, chat_session, chatCompletionMessages)
answerText, _, shouldReturn := chatStreamTest(w, chat_session, chatCompletionMessages, chatUuid, true)
if shouldReturn {
return
}
// Delete previous message and create new one
err := h.chatService.DeleteAndCreateChatMessage(chatSessionUuid, chatUuid, chat_session.UserID, answerID, answerText)
err := h.chatService.UpdateChatMessageContent(ctx, chatUuid, answerText)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "fail to create message: ").Error(), nil)
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "fail to update chat message: ").Error(), nil)
}
} else {
answerText, answerID, shouldReturn := chatStream(w, chat_session, chatCompletionMessages)
answerText, _, shouldReturn := chatStream(w, chat_session, chatCompletionMessages, chatUuid, true)
if shouldReturn {
return
}

// Delete previous message and create new one
err := h.chatService.DeleteAndCreateChatMessage(chatSessionUuid, chatUuid, chat_session.UserID, answerID, answerText)

err := h.chatService.UpdateChatMessageContent(ctx, chatUuid, answerText)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "fail to create message: ").Error(), nil)
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "fail to update message: ").Error(), nil)
}
}
}
Expand All @@ -282,7 +281,7 @@ func isTest(msgs []openai.ChatCompletionMessage) bool {
return promptMsg.Content == "test_demo_bestqa" || lastMsgs.Content == "test_demo_bestqa"
}

func chatStream(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, chat_compeletion_messages []openai.ChatCompletionMessage) (string, string, bool) {
func chatStream(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, chat_compeletion_messages []openai.ChatCompletionMessage, chatUuid string, regenerate bool) (string, string, bool) {
client := openai.NewClient(appConfig.OPENAI.API_KEY)

openai_req := NewChatCompletionRequest(chatSession, chat_compeletion_messages)
Expand All @@ -305,6 +304,11 @@ func chatStream(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, cha

var answer string
var answer_id string

if regenerate {
answer_id = chatUuid
}

for {
response, err := stream.Recv()
if errors.Is(err, io.EOF) {
Expand Down Expand Up @@ -349,10 +353,13 @@ func chatStream(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, cha
return answer, answer_id, false
}

func chatStreamTest(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, chat_compeletion_messages []openai.ChatCompletionMessage) (string, string, bool) {
func chatStreamTest(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, chat_compeletion_messages []openai.ChatCompletionMessage, chatUuid string, regenerate bool) (string, string, bool) {
//message := Message{Role: "assitant", Content:}
uuid, _ := uuid.NewV4()
answer_id := uuid.String()
answer_id := chatUuid
if !regenerate {
uuid, _ := uuid.NewV4()
answer_id = uuid.String()
}
setSSEHeader(w)

flusher, ok := w.(http.Flusher)
Expand Down
2 changes: 1 addition & 1 deletion e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.32.0",
"@playwright/test": "^1.32.1",
"@types/node": "^18.15.3"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions e2e/tests/09_session_answer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('test', async ({ page }) => {
await input_area?.press('Enter');
await page.waitForTimeout(1000);

const first_answer = await page.$eval('#image-wrapper .chat-message:nth-child(2) .message-text', el => el.innerText);
const first_answer = await page.$eval('#image-wrapper .chat-message:nth-child(2) .message-text', (el: HTMLElement) => el.innerText);
// check the answer return by the server
expect(first_answer).toContain('test_demo_bestqa');

Expand All @@ -38,7 +38,7 @@ test('test', async ({ page }) => {
await page.waitForTimeout(1000);
// check the answer return by the server
await page.waitForSelector('#image-wrapper .chat-message:nth-child(4) .message-text');
const sec_answer = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', el => el.innerText);
const sec_answer = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', (el: HTMLElement) => el.innerText);
// check the sec_answer has the debug message
expect(sec_answer).toContain('test_debug_1');

Expand All @@ -49,7 +49,7 @@ test('test', async ({ page }) => {
await page.waitForTimeout(1000);
// check the answer return by the server
await page.waitForSelector('#image-wrapper .chat-message:nth-child(6) .message-text');
const third_answer = await page.$eval('#image-wrapper .chat-message:nth-child(6) .message-text', el => el.innerText);
const third_answer = await page.$eval('#image-wrapper .chat-message:nth-child(6) .message-text', (el: HTMLElement) => el.innerText);
// check the third_answer has the debug message
expect(third_answer).toContain('test_debug_2');

Expand Down
16 changes: 7 additions & 9 deletions e2e/tests/10_session_answer_regenerate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ test('test', async ({ page }) => {
await input_area?.press('Enter');
await page.waitForTimeout(1000);

const first_answer = await page.$eval('#image-wrapper .chat-message:nth-child(2) .message-text', el => el.innerText);
console.log(first_answer)
const first_answer = await page.$eval('#image-wrapper .chat-message:nth-child(2) .message-text', (el: HTMLElement) => el.innerText);
// check the answer return by the server
expect(first_answer).toContain('test_demo_bestqa');

Expand All @@ -44,17 +43,15 @@ test('test', async ({ page }) => {
await page.waitForTimeout(1000);
// check the answer return by the server
await page.waitForSelector('#image-wrapper .chat-message:nth-child(4) .message-text');
const sec_answer = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', el => el.innerText);
const sec_answer = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', (el: HTMLElement) => el.innerText);
// check the sec_answer has the debug message
console.log(sec_answer)
expect(sec_answer).toContain('test_debug_1');

await page.locator('#image-wrapper .chat-message:nth-child(4) .chat-message-regenerate').click();
await page.waitForTimeout(1000);

const sec_answer_regen = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', el => el.innerText);
const sec_answer_regen = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', (el: HTMLElement) => el.innerText);
// check the sec_answer has the debug message
console.log(sec_answer_regen)
expect(sec_answer_regen).toContain('test_debug_1');

// add new message "test_debug_2"
Expand All @@ -64,14 +61,14 @@ test('test', async ({ page }) => {
await page.waitForTimeout(1000);
// check the answer return by the server
await page.waitForSelector('#image-wrapper .chat-message:nth-child(6) .message-text');
const third_answer = await page.$eval('#image-wrapper .chat-message:nth-child(6) .message-text', el => el.innerText);
const third_answer = await page.$eval('#image-wrapper .chat-message:nth-child(6) .message-text', (el: HTMLElement) => el.innerText);
// check the third_answer has the debug message
expect(third_answer).toContain('test_debug_2');

await page.locator('#image-wrapper .chat-message:nth-child(6) .chat-message-regenerate').click();
await page.waitForTimeout(1000);

const third_answer_regen = await page.$eval('#image-wrapper .chat-message:nth-child(6) .message-text', el => el.innerText);
const third_answer_regen = await page.$eval('#image-wrapper .chat-message:nth-child(6) .message-text', (el: HTMLElement) => el.innerText);
// check the third_answer has the debug message
expect(third_answer_regen).toContain('test_debug_2');

Expand All @@ -80,8 +77,9 @@ test('test', async ({ page }) => {
await page.waitForTimeout(1000);

// check the second answer has been regenerated
const sec_answer_regen_2 = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', el => el.innerText);
const sec_answer_regen_2 = await page.$eval('#image-wrapper .chat-message:nth-child(4) .message-text', (el: HTMLElement) => el.innerText);
// check the sec_answer has the debug message
expect(sec_answer_regen_2).toContain('test_debug_1');
expect(sec_answer_regen_2).not.toContain('test_debug_2')

});
18 changes: 12 additions & 6 deletions web/src/views/chat/components/Message/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,27 @@ function handleRegenerate() {

<template>
<div class="flex w-full mb-6 overflow-hidden" :class="[{ 'flex-row-reverse': inversion }]">
<div class="flex items-center justify-center flex-shrink-0 h-8 overflow-hidden rounded-full basis-8"
:class="[inversion ? 'ml-2' : 'mr-2']">
<div
class="flex items-center justify-center flex-shrink-0 h-8 overflow-hidden rounded-full basis-8"
:class="[inversion ? 'ml-2' : 'mr-2']"
>
<AvatarComponent :image="inversion" />
</div>
<div class="overflow-hidden text-sm " :class="[inversion ? 'items-end' : 'items-start']">
<p class="text-xs text-[#b4bbc4]" :class="[inversion ? 'text-right' : 'text-left']">
{{ dateTime }}
</p>
<div class="flex items-end gap-1 mt-2" :class="[inversion ? 'flex-row-reverse' : 'flex-row']">
<TextComponent ref="textRef" class="message-text" :inversion="inversion" :error="error" :text="text"
:loading="loading" :contenteditable="editable" :idex="index" @blur="event => onContentChange(event, index)" />
<TextComponent
ref="textRef" class="message-text" :inversion="inversion" :error="error" :text="text"
:loading="loading" :contenteditable="editable" :idex="index" @blur="event => onContentChange(event, index)"
/>
<div class="flex flex-col">
<button v-if="!inversion"
<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">
@click="handleRegenerate"
>
<SvgIcon icon="ri:restart-line" />
</button>
<NDropdown :placement="!inversion ? 'right' : 'left'" :options="options" @select="handleSelect">
Expand Down
2 changes: 2 additions & 0 deletions web/src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ async function onConversationStream() {
)
}
catch (error) {
// eslint-disable-next-line no-console
console.log(error)
}
}
Expand All @@ -167,6 +168,7 @@ async function onConversationStream() {
return response
}
catch (error: any) {
// eslint-disable-next-line no-console
console.log(error)
const response = error.response
if (response.status === 500)
Expand Down

0 comments on commit aa37ee8

Please sign in to comment.