Skip to content

Commit

Permalink
Fix JSON unmarshaling and error handling in chatStreamGemini function
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho committed Sep 15, 2024
1 parent b778147 commit 22635cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 10 additions & 2 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,11 +1483,19 @@ func (h *ChatHandler) chatStreamGemini(w http.ResponseWriter, chatSession sqlc_q
log.Printf("%s", line)
}
if err != nil {
// Create an instance of ErrorResponse
var errorResponse gemini.ErrorResponse

// Unmarshal the JSON string into the ErrorResponse struct
err := json.Unmarshal(line, &errorResponse)
if err != nil {
fmt.Println("Error unmarshaling JSON:", err)
}
if errors.Is(err, io.EOF) {
log.Printf("End of stream reached: %+v", err)
log.Printf("End of stream reached: %+v, %+v", err, errorResponse)
return answer, answer_id, false
} else {
log.Printf("Error while reading response: %+v", err)
log.Printf("Error while reading response: %+v, %+v", err, errorResponse)
return "", "", true
}
}
Expand Down
8 changes: 8 additions & 0 deletions api/llm/gemini/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,11 @@ func GenGemminPayload(chat_compeletion_messages []models.Message, chatFiles []sq
}
return payloadBytes, nil
}

type ErrorResponse struct {
Error struct {
Code int `json:"code"`
Message string `json:"message"`
Status string `json:"status"`
} `json:"error"`
}

0 comments on commit 22635cc

Please sign in to comment.