Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging and update headers for Claude3 chat stream #484

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading