all database switched to repository model. some other fixes and features

This commit is contained in:
2026-01-22 20:58:46 +03:00
parent 804d683f9e
commit 331f6854b6
14 changed files with 283 additions and 187 deletions

View File

@@ -99,7 +99,9 @@ type CreateCompletionReq struct {
MaxCompletionTokens int `json:"max_completition_tokens,omitempty"`
}
func (o *OpenAIAPI) DoRequest(url string, params any) ([]byte, error) {
var MaxRetriesErr = fmt.Errorf("max retries exceeded")
func (o *OpenAIAPI) DoRequest(url string, params any, retries int) ([]byte, error) {
responseBody := make([]byte, 0)
data, err := json.Marshal(params)
if err != nil {
@@ -122,15 +124,11 @@ func (o *OpenAIAPI) DoRequest(url string, params any) ([]byte, error) {
defer res.Body.Close()
if res.StatusCode == 504 || res.StatusCode == 400 || res.StatusCode == 502 {
o.Logger.Warn(fmt.Sprintf("[%d] %s", res.StatusCode, res.Status))
time.Sleep(5 * time.Second)
res, err = o.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode == 504 || res.StatusCode == 400 || res.StatusCode == 502 {
return nil, fmt.Errorf("[%d] %s", res.StatusCode, res.Status)
if retries >= 3 {
return responseBody, MaxRetriesErr
}
time.Sleep(1 * time.Second)
return o.DoRequest(url, params, retries+1)
}
responseBody, err = io.ReadAll(res.Body)
if err != nil {
@@ -148,7 +146,7 @@ func (o *OpenAIAPI) CreateCompletion(request CreateCompletionReq) (*OpenAIRespon
}
o.Logger.Debug("REQ", u, string(data))
body, err := o.DoRequest(u, request)
body, err := o.DoRequest(u, request, 0)
if err != nil {
return nil, err
}
@@ -176,7 +174,7 @@ func (o *OpenAIAPI) CompressChat(history []Message) (*OpenAIResponse, error) {
}
o.Logger.Debug("COMPRESS REQ", u, string(data))
body, err := o.DoRequest(u, request)
body, err := o.DoRequest(u, request, 0)
if err != nil {
return nil, err
}

View File

@@ -102,3 +102,7 @@ func DecimalComma(d *decimal.Decimal) string {
exp := strings.Split(d.String(), ".")[1]
return BigComma(d.BigInt()) + "." + exp
}
func IntComma(i int) string {
return BigComma(big.NewInt(int64(i)))
}