Skip to content

Commit

Permalink
optimize log
Browse files Browse the repository at this point in the history
  • Loading branch information
ZingLix committed Feb 20, 2024
1 parent 5160162 commit 51e7ce8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
26 changes: 21 additions & 5 deletions go/qianfan/base_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package qianfan

import (
"encoding/json"
"fmt"
"errors"
"io"
"math"
"strconv"
Expand Down Expand Up @@ -135,9 +135,17 @@ func (s *ModelResponseStream) checkResponseError() error {
if err != nil {
return err
}
logger.Warnf("stream request got error: %s, retrying request... retry count: %d", apiError, retryCount)
} else {
return nil
}
time.Sleep(
time.Duration(
math.Pow(
2,
float64(retryCount))*float64(s.Options.LLMRetryBackoffFactor),
) * time.Second,
)
}

if apiError == nil {
Expand Down Expand Up @@ -174,15 +182,24 @@ func checkResponseError(resp ModelAPIResponse) error {
}

func (m *BaseModel) withRetry(fn func() error) error {
for retryCount := 0; retryCount < m.Options.LLMRetryCount; retryCount++ {
err := fn()
var err error
for retryCount := 0; retryCount < m.Options.LLMRetryCount || m.Options.LLMRetryCount == 0; retryCount++ {
err = fn()
if err == nil {
return nil
}
if _, ok := err.(*tryAgainError); ok {
retryCount -= 1
continue
}
var apiErr *APIError
ok := errors.As(err, &apiErr)
if ok {
if apiErr.Code != QPSLimitReachedErrCode && apiErr.Code != ServerHighLoadErrCode {
return err
}
}
logger.Warnf("request got error: %s, retrying request... retry count: %d", err, retryCount)
time.Sleep(
time.Duration(
math.Pow(
Expand All @@ -191,7 +208,7 @@ func (m *BaseModel) withRetry(fn func() error) error {
) * time.Second,
)
}
return fmt.Errorf("g")
return err
}

func (m *BaseModel) requestResource(request *QfRequest, response any) error {
Expand All @@ -206,7 +223,6 @@ func (m *BaseModel) requestResource(request *QfRequest, response any) error {
var err error
tokenRefreshed := false
requestFunc := func() error {

modelApiResponse.ClearError()
err = m.Requestor.request(request, qfResponse)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go/qianfan/chat_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (c *ChatCompletion) realEndpoint() (string, error) {
} else {
url += "/chat/" + c.Endpoint
}
logger.Debugf("requesting endpoint: %s", url)
return url, nil
}

Expand Down
1 change: 1 addition & 0 deletions go/qianfan/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (c *Completion) realEndpoint() (string, error) {
} else {
url += "/completions/" + c.Endpoint
}
logger.Debugf("requesting endpoint: %s", url)
return url, nil
}

Expand Down
1 change: 1 addition & 0 deletions go/qianfan/embdding.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (c *Embedding) realEndpoint() (string, error) {
} else {
url += "/embeddings/" + c.Endpoint
}
logger.Debugf("requesting endpoint: %s", url)
return url, nil
}

Expand Down
9 changes: 9 additions & 0 deletions go/qianfan/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (e *InternalError) Error() string {
return fmt.Sprintf("internal error: %s. there might be a bug in sdk. please contact us", e.Msg)
}

// 参数非法
type InvalidParamError struct {
Msg string
}

func (e *InvalidParamError) Error() string {
return fmt.Sprint("invalid param ", e.Msg)
}

// 内部使用,表示重试
type tryAgainError struct {
}
Expand Down
2 changes: 1 addition & 1 deletion go/qianfan/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ var logger = &logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.WarnLevel,
Level: logrus.InfoLevel,
}
12 changes: 9 additions & 3 deletions go/qianfan/requestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (r *Requestor) addAuthInfo(request *QfRequest) error {
} else if GetConfig().AccessKey != "" && GetConfig().SecretKey != "" {
return r.sign(request)
}
logger.Error("no enough credential found. Please check whether (ak, sk) or (access_key, secret_key) is set in config")
return &CredentialNotFoundError{}
}

Expand Down Expand Up @@ -214,7 +215,11 @@ func (r *Requestor) sign(request *QfRequest) error {
} else if u.Scheme == "https" {
port = "443"
} else {
return fmt.Errorf("unrecognized scheme: %s", u.Scheme)
logger.Errorf("Got unexpected protocol `%s` in requested API url `%s`.", u.Scheme, request.URL)
return &InvalidParamError{
Msg: fmt.Sprintf("unrecognized protocol `%s` is set in API base url."+
"Only http and https are supported.", u.Scheme),
}
}
}
porti, err := strconv.Atoi(port)
Expand Down Expand Up @@ -262,7 +267,7 @@ func (r *Requestor) prepareRequest(request QfRequest) (*http.Request, error) {
} else if request.Type == authRequest {
request.URL = GetConfig().BaseURL + request.URL
} else {
return nil, fmt.Errorf("unexpected request type: %s, this might be an internal error", request.Type)
return nil, &InternalError{"unexpected request type: " + request.Type}
}
bodyBytes, err := json.Marshal(request.Body)
if err != nil {
Expand All @@ -273,7 +278,7 @@ func (r *Requestor) prepareRequest(request QfRequest) (*http.Request, error) {
return nil, err
}
request.Headers["Content-Type"] = "application/json"
// IAM 签名
// 增加鉴权信息
err = r.addAuthInfo(&request)
if err != nil {
return nil, err
Expand Down Expand Up @@ -344,6 +349,7 @@ func newStreamInternal(requestor *Requestor, requestFunc func() (*http.Response,
IsEnd: false,
firstResponse: false,
}
// 初始化请求
err := si.reset()
if err != nil {
return nil, err
Expand Down

0 comments on commit 51e7ce8

Please sign in to comment.