88 "time"
99
1010 "github.com/gin-gonic/gin"
11+ "github.com/google/uuid"
1112 "github.com/vhybZApp/api/config"
1213 "github.com/vhybZApp/api/database"
1314 "github.com/vhybZApp/api/models"
@@ -79,6 +80,12 @@ func ChatCompletion(c *gin.Context) {
7980 // Initialize token quota service
8081 tokenQuotaService := services .NewTokenQuotaService (database .GetDB ())
8182
83+ // Check if user has enough quota for a reasonable estimate (e.g., 1000 tokens)
84+ if err := tokenQuotaService .UpdateUsage (userID .(uuid.UUID ), 1000 ); err != nil {
85+ c .JSON (http .StatusTooManyRequests , models .NewErrorResponse (err .Error ()))
86+ return
87+ }
88+
8289 // Validate Azure OpenAI configuration
8390 if config .AppConfig .AzureOpenAIEndpoint == "" || config .AppConfig .AzureOpenAIKey == "" || config .AppConfig .AzureOpenAIDeployment == "" {
8491 c .JSON (http .StatusInternalServerError , models .NewErrorResponse ("Azure OpenAI configuration is incomplete" ))
@@ -94,7 +101,7 @@ func ChatCompletion(c *gin.Context) {
94101
95102 // Create HTTP client
96103 client := & http.Client {
97- Timeout : 30 * time .Second ,
104+ Timeout : 300 * time .Second ,
98105 }
99106
100107 // Marshal request body
@@ -105,7 +112,9 @@ func ChatCompletion(c *gin.Context) {
105112 }
106113
107114 // Create request
108- url := config .AppConfig .AzureOpenAIEndpoint + "/openai/deployments/" + config .AppConfig .AzureOpenAIDeployment + "/chat/completions?api-version=2023-05-15"
115+ url := config .AppConfig .AzureOpenAIEndpoint + "/openai/deployments/" +
116+ config .AppConfig .AzureOpenAIDeployment + "/chat/completions?api-version=" + config .AppConfig .AzureOpenAIDeploymentVersion
117+
109118 httpReq , err := http .NewRequest ("POST" , url , bytes .NewBuffer (reqBody ))
110119 if err != nil {
111120 c .JSON (http .StatusInternalServerError , models .NewErrorResponse ("Error creating request" ))
@@ -141,14 +150,7 @@ func ChatCompletion(c *gin.Context) {
141150 var chatResp ChatCompletionResponse
142151 if err := json .Unmarshal (body , & chatResp ); err != nil {
143152 c .JSON (http .StatusInternalServerError , models .NewErrorResponse ("Error parsing response" ))
144- return
145- }
146-
147- // Update token usage
148- if err := tokenQuotaService .UpdateUsage (userID .(string ), chatResp .Usage .TotalTokens ); err != nil {
149- c .JSON (http .StatusTooManyRequests , models .NewErrorResponse (err .Error ()))
150- return
151- }
153+ returnasdu
152154
153155 c .JSON (http .StatusOK , chatResp )
154156}
0 commit comments