Skip to content

Commit

Permalink
Enable logging and adjust stream completion logic in chat handler
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho committed Jul 5, 2024
1 parent 49d02b1 commit 4b3fab8
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_
break
}
line, err := ioreader.ReadBytes('\n')
// log.Printf("%+v", string(line))
log.Printf("%+v", string(line))
if err != nil {
if errors.Is(err, io.EOF) {
if bytes.HasPrefix(line, []byte("{\"type\":\"error\"")) {
Expand All @@ -858,7 +858,7 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_
}
line = bytes.TrimPrefix(line, headerData)

if bytes.HasPrefix(line, []byte("[DONE]")) {
if bytes.HasPrefix(line, []byte("event: message_stop")) {
// stream.isFinished = true
data, _ := json.Marshal(constructChatCompletionStreamReponse(answer_id, answer))
fmt.Fprintf(w, "data: %v\n\n", string(data))
Expand All @@ -881,11 +881,9 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_
}
if bytes.HasPrefix(line, []byte("{\"type\":\"content_block_delta\"")) {
answer += claude.AnswerFromBlockDelta(line)
if len(answer) < 200 || len(answer)%2 == 0 {
data, _ := json.Marshal(constructChatCompletionStreamReponse(answer_id, answer))
fmt.Fprintf(w, "data: %v\n\n", string(data))
flusher.Flush()
}
data, _ := json.Marshal(constructChatCompletionStreamReponse(answer_id, answer))
fmt.Fprintf(w, "data: %v\n\n", string(data))
flusher.Flush()
}
}

Expand Down

0 comments on commit 4b3fab8

Please sign in to comment.