fix: cloudflare test & expose detailed info about test failures (#715)

* fix: cloudflare test & expose detailed info about test failures

* fix: cloudflare test & expose detailed info about test failures

---------

Co-authored-by: JustSong <songquanpeng@foxmail.com>
This commit is contained in:
Mikey 2023-11-17 05:45:55 -08:00 committed by GitHub
parent ddcaf95f5f
commit 34d517cfa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,11 +6,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io"
"net/http" "net/http"
"one-api/common" "one-api/common"
"one-api/model" "one-api/model"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
) )
@ -45,13 +45,11 @@ func testChannel(channel *model.Channel, request ChatRequest) (err error, openai
if channel.Type == common.ChannelTypeAzure { if channel.Type == common.ChannelTypeAzure {
requestURL = fmt.Sprintf("%s/openai/deployments/%s/chat/completions?api-version=2023-03-15-preview", channel.GetBaseURL(), request.Model) requestURL = fmt.Sprintf("%s/openai/deployments/%s/chat/completions?api-version=2023-03-15-preview", channel.GetBaseURL(), request.Model)
} else { } else {
if channel.GetBaseURL() != "" { if baseURL := channel.GetBaseURL(); len(baseURL) > 0 {
requestURL = channel.GetBaseURL() requestURL = baseURL
} }
requestURL += "/v1/chat/completions" requestURL = getFullRequestURL(requestURL, "/v1/chat/completions", channel.Type)
} }
// for Cloudflare AI gateway: https://github.com/songquanpeng/one-api/pull/639
requestURL = strings.Replace(requestURL, "/v1/v1", "/v1", 1)
jsonData, err := json.Marshal(request) jsonData, err := json.Marshal(request)
if err != nil { if err != nil {
@ -73,10 +71,14 @@ func testChannel(channel *model.Channel, request ChatRequest) (err error, openai
} }
defer resp.Body.Close() defer resp.Body.Close()
var response TextResponse var response TextResponse
err = json.NewDecoder(resp.Body).Decode(&response) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err, nil return err, nil
} }
err = json.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("Error: %s\nResp body: %s", err, body), nil
}
if response.Usage.CompletionTokens == 0 { if response.Usage.CompletionTokens == 0 {
return errors.New(fmt.Sprintf("type %s, code %v, message %s", response.Error.Type, response.Error.Code, response.Error.Message)), &response.Error return errors.New(fmt.Sprintf("type %s, code %v, message %s", response.Error.Type, response.Error.Code, response.Error.Message)), &response.Error
} }