Skip to content

Commit

Permalink
update (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho authored Mar 27, 2023
1 parent 96bb3e2 commit 245fe93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ build: vet

serve:
@echo "Starting server..."
echo 'sudo lsof -i -P -n | grep 8077'
echo $(OPENAI_API_KEY)
echo $(PG_HOST)
air
Expand Down
21 changes: 13 additions & 8 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,26 +234,31 @@ func regenerateAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid str
}

if isTest(chatCompletionMessages) {
answerText, _, shouldReturn := test_replay(w, chat_session, chatCompletionMessages)
answerText, answerID, shouldReturn := test_replay(w, chat_session, chatCompletionMessages)
if shouldReturn {
return true
}

err = h.chatService.UpdateChatMessageContent(ctx, chatUuid, answerText)
// remove the previous message
h.chatService.q.DeleteChatMessageByUUID(ctx, chatUuid)
// re-insert new message
_, err := h.chatService.CreateChatMessageSimple(ctx, chatSessionUuid, answerID, "assistant", answerText, chat_session.UserID)

if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Update chat message error", err)
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "fail to create message: ").Error(), nil)
}
} else {
answerText, _, shouldReturn := chat_stream(w, chat_session, chatCompletionMessages)
answerText, answerID, shouldReturn := chat_stream(w, chat_session, chatCompletionMessages)
if shouldReturn {
return true
}

err = h.chatService.UpdateChatMessageContent(ctx, chatUuid, answerText)
// remove the previous message
h.chatService.q.DeleteChatMessageByUUID(ctx, chatUuid)
// re-insert new message
_, err := h.chatService.CreateChatMessageSimple(ctx, chatSessionUuid, answerID, "assistant", answerText, chat_session.UserID)

if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Update chat message error", err)
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "fail to create message: ").Error(), nil)
}
}
return false
Expand Down Expand Up @@ -335,6 +340,7 @@ func chat_stream(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, ch
func test_replay(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, chat_compeletion_messages []openai.ChatCompletionMessage) (string, string, bool) {
//message := Message{Role: "assitant", Content:}
uuid, _ := uuid.NewV4()
answer_id := uuid.String()
setSSEHeader(w)

flusher, ok := w.(http.Flusher)
Expand All @@ -344,7 +350,6 @@ func test_replay(w http.ResponseWriter, chatSession sqlc_queries.ChatSession, ch
return "", "", true
}
answer := "Hi, I am a chatbot. I can help you to find the best answer for your question. Please ask me a question."
answer_id := uuid.String()
resp := constructChatCompletionStreamReponse(answer_id, answer)
data, _ := json.Marshal(resp)
fmt.Fprintf(w, "data: %v\n\n", string(data))
Expand Down
10 changes: 4 additions & 6 deletions web/src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,18 @@ async function onRegenerate(index: number) {
controller = new AbortController()
const { requestOptions } = dataSources.value[index]
const chat = dataSources.value[index]
const requestOptions = chat.requestOptions
const message = requestOptions?.prompt ?? ''
const chatUuid = chat.uuid
let options: Chat.ConversationRequest = {}
if (requestOptions.options)
options = { ...requestOptions.options }
loading.value = true
const chatUuid = dataSources.value[index].uuid
updateChat(
sessionUuid,
index,
Expand Down Expand Up @@ -229,11 +230,8 @@ async function onRegenerate(index: number) {
const {
responseText,
} = xhr
const lastIndex = responseText.lastIndexOf('data: ')
// Extract the JSON data chunk from the responseText
const chunk = responseText.slice(lastIndex + 6)
const chunk = getDataFromResponseText(responseText)
// Check if the chunk is not empty
if (chunk) {
Expand Down

0 comments on commit 245fe93

Please sign in to comment.