Skip to content

Commit 3954340

Browse files
author
farhoud.m7
committed
Enhance ChatCompletion function to handle token usage updates and improve error handling for JSON parsing.
1 parent e386b9c commit 3954340

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

azure/openai.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ func ChatCompletion(c *gin.Context) {
150150
var chatResp ChatCompletionResponse
151151
if err := json.Unmarshal(body, &chatResp); err != nil {
152152
c.JSON(http.StatusInternalServerError, models.NewErrorResponse("Error parsing response"))
153-
returnasdu
153+
return
154+
}
155+
156+
// Update token usage with actual tokens used
157+
if err := tokenQuotaService.UpdateUsage(userID.(uuid.UUID), chatResp.Usage.TotalTokens); err != nil {
158+
c.JSON(http.StatusTooManyRequests, models.NewErrorResponse(err.Error()))
159+
return
160+
}
154161

155162
c.JSON(http.StatusOK, chatResp)
156163
}

0 commit comments

Comments
 (0)