Skip to content

Commit

Permalink
Add logging and update headers for Claude3 chat stream (#484)
Browse files Browse the repository at this point in the history
* Add logging and update headers for Claude3 chat stream

* Adjust token limit check to use full context window

* Disable logging of chat stream lines in Claude3 handler.

* Simplify content block handling in chatStreamClaude3
  • Loading branch information
swuecho authored Jul 4, 2024
1 parent 696cc75 commit 49d02b1
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func genAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid string, ch
})

// check if total tokens exceed limit
if totalTokens > chatSession.MaxTokens*2/3 {
// context window, max token
if totalTokens > chatSession.MaxTokens {
RespondWithError(w, http.StatusRequestEntityTooLarge, "error.token_length_exceed_limit",
map[string]interface{}{
"max_tokens": chatSession.MaxTokens,
Expand Down Expand Up @@ -778,10 +779,12 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_

// convert data to json format
jsonValue, _ := json.Marshal(jsonData)
log.Printf("%+v", string(jsonValue))
// create the request
req, err := http.NewRequest("POST", chatModel.Url, bytes.NewBuffer(jsonValue))

if err != nil {
log.Printf("%+v", err)
RespondWithError(w, http.StatusInternalServerError, "error.fail_to_make_request", err)
return "", "", true
}
Expand All @@ -799,13 +802,15 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("anthropic-version", "2023-06-01")

// create the http client and send the request
client := &http.Client{
Timeout: 2 * time.Minute,
}
resp, err := client.Do(req)
if err != nil {
log.Printf("%+v", err)
RespondWithError(w, http.StatusInternalServerError, "error.fail_to_do_request", err)
return "", "", true
}
Expand Down Expand Up @@ -840,9 +845,12 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_
break
}
line, err := ioreader.ReadBytes('\n')

// log.Printf("%+v", string(line))
if err != nil {
if errors.Is(err, io.EOF) {
if bytes.HasPrefix(line, []byte("{\"type\":\"error\"")) {
log.Println(string(line))
}
fmt.Println("End of stream reached")
break // Exit loop if end of stream
}
Expand All @@ -867,14 +875,12 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_
}
if bytes.HasPrefix(line, []byte("{\"type\":\"content_block_start\"")) {
answer = claude.AnswerFromBlockStart(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()
}
if bytes.HasPrefix(line, []byte("{\"type\":\"content_block_delta\"")) {
answer = claude.AnswerFromBlockDelta(line)
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))
Expand Down

0 comments on commit 49d02b1

Please sign in to comment.