@@ -122,14 +122,22 @@ func GenerateAliyunImg(ctx context.Context, prompt string, imageContent []byte)
122122 start := time .Now ()
123123 metrics .APIRequestCount .WithLabelValues (model ).Inc ()
124124
125- resp , err := client .Do (req )
126- metrics .APIRequestDuration .WithLabelValues (model ).Observe (time .Since (start ).Seconds ())
127- if err != nil {
128- logger .ErrorCtx (ctx , "create image fail" , "err" , err )
129- return "" , 0 , err
125+ var resp * http.Response
126+ var err error
127+ for i := 0 ; i < * conf .BaseConfInfo .LLMRetryTimes ; i ++ {
128+ resp , err = client .Do (req )
129+ if err != nil {
130+ logger .ErrorCtx (ctx , "create image fail" , "err" , err )
131+ continue
132+ }
133+ break
134+ }
135+ if err != nil || resp == nil {
136+ return "" , 0 , fmt .Errorf ("request fail %v %v" , err , resp )
130137 }
131- defer resp .Body .Close ()
132138
139+ defer resp .Body .Close ()
140+ metrics .APIRequestDuration .WithLabelValues (model ).Observe (time .Since (start ).Seconds ())
133141 body , err := io .ReadAll (resp .Body )
134142 if err != nil {
135143 logger .ErrorCtx (ctx , "read response fail" , "err" , err )
@@ -220,9 +228,18 @@ func GenerateAliyunVideo(ctx context.Context, prompt string, image []byte) (stri
220228 metrics .APIRequestDuration .WithLabelValues (model ).Observe (time .Since (start ).Seconds ())
221229 }()
222230
223- resp , err := client .Do (req )
224- if err != nil {
225- return "" , 0 , err
231+ var resp * http.Response
232+ for i := 0 ; i < * conf .BaseConfInfo .LLMRetryTimes ; i ++ {
233+ resp , err = client .Do (req )
234+ if err != nil {
235+ logger .ErrorCtx (ctx , "create video fail" , "err" , err )
236+ continue
237+ }
238+ break
239+ }
240+
241+ if err != nil || resp == nil {
242+ return "" , 0 , fmt .Errorf ("request fail %v %v" , err , resp )
226243 }
227244 defer resp .Body .Close ()
228245
@@ -232,34 +249,34 @@ func GenerateAliyunVideo(ctx context.Context, prompt string, image []byte) (stri
232249 }
233250
234251 var vr TaskStatusResponse
235- if err : = json .Unmarshal (body , & vr ); err != nil {
252+ if err = json .Unmarshal (body , & vr ); err != nil {
236253 return "" , 0 , err
237254 }
238255
239256 for i := 0 ; i < 100 ; i ++ {
240257 time .Sleep (5 * time .Second )
241258
242- req , err : = http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("https://dashscope.aliyuncs.com/api/v1/tasks/%s" , vr .Output .TaskID ), nil )
259+ req , err = http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("https://dashscope.aliyuncs.com/api/v1/tasks/%s" , vr .Output .TaskID ), nil )
243260 if err != nil {
244261 return "" , 0 , err
245262 }
246263
247264 req .Header .Set ("Authorization" , "Bearer " + * conf .BaseConfInfo .AliyunToken )
248265 req .Header .Set ("Content-Type" , "application/json" )
249266
250- resp , err : = client .Do (req )
267+ resp , err = client .Do (req )
251268 if err != nil {
252269 return "" , 0 , err
253270 }
254271 defer resp .Body .Close ()
255272
256- body , err : = io .ReadAll (resp .Body )
273+ body , err = io .ReadAll (resp .Body )
257274 if err != nil {
258275 return "" , 0 , err
259276 }
260277
261278 var vr videoResponse
262- if err : = json .Unmarshal (body , & vr ); err != nil {
279+ if err = json .Unmarshal (body , & vr ); err != nil {
263280 return "" , 0 , err
264281 }
265282
@@ -330,12 +347,21 @@ func GenerateAliyunText(ctx context.Context, audioContent []byte) (string, int,
330347 client := utils .GetLLMProxyClient ()
331348 start := time .Now ()
332349 metrics .APIRequestCount .WithLabelValues (recModel ).Inc ()
333- resp , err := client .Do (req )
334350
335- metrics .APIRequestDuration .WithLabelValues (recModel ).Observe (time .Since (start ).Seconds ())
336- if err != nil {
337- return "" , 0 , fmt .Errorf ("request failed: %w" , err )
351+ var resp * http.Response
352+ for i := 0 ; i < * conf .BaseConfInfo .LLMRetryTimes ; i ++ {
353+ resp , err = client .Do (req )
354+ if err != nil {
355+ logger .ErrorCtx (ctx , "create video fail" , "err" , err )
356+ continue
357+ }
358+ break
338359 }
360+
361+ if err != nil || resp == nil {
362+ return "" , 0 , fmt .Errorf ("request fail %v %v" , err , resp )
363+ }
364+ metrics .APIRequestDuration .WithLabelValues (recModel ).Observe (time .Since (start ).Seconds ())
339365 defer resp .Body .Close ()
340366
341367 body , err := io .ReadAll (resp .Body )
@@ -405,11 +431,22 @@ func AliyunTTS(ctx context.Context, text, encoding string) ([]byte, int, int, er
405431 client := utils .GetLLMProxyClient ()
406432 start := time .Now ()
407433 metrics .APIRequestCount .WithLabelValues (model ).Inc ()
408- resp , err := client .Do (req )
409- metrics .APIRequestDuration .WithLabelValues (model ).Observe (time .Since (start ).Seconds ())
410- if err != nil {
411- return nil , 0 , 0 , fmt .Errorf ("request error: %w" , err )
434+
435+ var resp * http.Response
436+ for i := 0 ; i < * conf .BaseConfInfo .LLMRetryTimes ; i ++ {
437+ resp , err = client .Do (req )
438+ if err != nil {
439+ logger .ErrorCtx (ctx , "create video fail" , "err" , err )
440+ continue
441+ }
442+ break
412443 }
444+
445+ if err != nil || resp == nil {
446+ return nil , 0 , 0 , fmt .Errorf ("request fail %v %v" , err , resp )
447+ }
448+
449+ metrics .APIRequestDuration .WithLabelValues (model ).Observe (time .Since (start ).Seconds ())
413450 defer resp .Body .Close ()
414451
415452 respData , err := io .ReadAll (resp .Body )
0 commit comments