mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-11 02:43:44 +08:00
Merge commit '1c2654320e5b6268b13b6efca40ce37a523d032b'
This commit is contained in:
@@ -6,19 +6,20 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Laisky/one-api/relay/adaptor"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/meta"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Adaptor struct {
|
||||
request *model.GeneralOpenAIRequest
|
||||
meta *meta.Meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
|
||||
a.meta = meta
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
@@ -27,6 +28,14 @@ func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
|
||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
||||
adaptor.SetupCommonRequestHeader(c, req, meta)
|
||||
version := parseAPIVersionByModelName(meta.ActualModelName)
|
||||
if version == "" {
|
||||
version = a.meta.Config.APIVersion
|
||||
}
|
||||
if version == "" {
|
||||
version = "v1.1"
|
||||
}
|
||||
a.meta.Config.APIVersion = version
|
||||
// check DoResponse for auth part
|
||||
return nil
|
||||
}
|
||||
@@ -62,9 +71,9 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Met
|
||||
return nil, openai.ErrorWrapper(errors.New("request is nil"), "request_is_nil", http.StatusBadRequest)
|
||||
}
|
||||
if meta.IsStream {
|
||||
err, usage = StreamHandler(c, *a.request, splits[0], splits[1], splits[2])
|
||||
err, usage = StreamHandler(c, meta, *a.request, splits[0], splits[1], splits[2])
|
||||
} else {
|
||||
err, usage = Handler(c, *a.request, splits[0], splits[1], splits[2])
|
||||
err, usage = Handler(c, meta, *a.request, splits[0], splits[1], splits[2])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Laisky/one-api/common"
|
||||
"github.com/Laisky/one-api/common/ctxkey"
|
||||
"github.com/Laisky/one-api/common/helper"
|
||||
"github.com/Laisky/one-api/common/logger"
|
||||
"github.com/Laisky/one-api/common/random"
|
||||
"github.com/Laisky/one-api/relay/adaptor/openai"
|
||||
"github.com/Laisky/one-api/relay/constant"
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/constant"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
@@ -150,8 +150,8 @@ func buildXunfeiAuthUrl(hostUrl string, apiKey, apiSecret string) string {
|
||||
return callUrl
|
||||
}
|
||||
|
||||
func StreamHandler(c *gin.Context, textRequest model.GeneralOpenAIRequest, appId string, apiSecret string, apiKey string) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
domain, authUrl := getXunfeiAuthUrl(c, apiKey, apiSecret, textRequest.Model)
|
||||
func StreamHandler(c *gin.Context, meta *meta.Meta, textRequest model.GeneralOpenAIRequest, appId string, apiSecret string, apiKey string) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
domain, authUrl := getXunfeiAuthUrl(meta.Config.APIVersion, apiKey, apiSecret)
|
||||
dataChan, stopChan, err := xunfeiMakeRequest(textRequest, domain, authUrl, appId)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "xunfei_request_failed", http.StatusInternalServerError), nil
|
||||
@@ -180,8 +180,8 @@ func StreamHandler(c *gin.Context, textRequest model.GeneralOpenAIRequest, appId
|
||||
return nil, &usage
|
||||
}
|
||||
|
||||
func Handler(c *gin.Context, textRequest model.GeneralOpenAIRequest, appId string, apiSecret string, apiKey string) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
domain, authUrl := getXunfeiAuthUrl(c, apiKey, apiSecret, textRequest.Model)
|
||||
func Handler(c *gin.Context, meta *meta.Meta, textRequest model.GeneralOpenAIRequest, appId string, apiSecret string, apiKey string) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
domain, authUrl := getXunfeiAuthUrl(meta.Config.APIVersion, apiKey, apiSecret)
|
||||
dataChan, stopChan, err := xunfeiMakeRequest(textRequest, domain, authUrl, appId)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "xunfei_request_failed", http.StatusInternalServerError), nil
|
||||
@@ -269,25 +269,12 @@ func xunfeiMakeRequest(textRequest model.GeneralOpenAIRequest, domain, authUrl,
|
||||
return dataChan, stopChan, nil
|
||||
}
|
||||
|
||||
func getAPIVersion(c *gin.Context, modelName string) string {
|
||||
query := c.Request.URL.Query()
|
||||
apiVersion := query.Get("api-version")
|
||||
if apiVersion != "" {
|
||||
return apiVersion
|
||||
}
|
||||
func parseAPIVersionByModelName(modelName string) string {
|
||||
parts := strings.Split(modelName, "-")
|
||||
if len(parts) == 2 {
|
||||
apiVersion = parts[1]
|
||||
return apiVersion
|
||||
|
||||
return parts[1]
|
||||
}
|
||||
apiVersion = c.GetString(ctxkey.ConfigAPIVersion)
|
||||
if apiVersion != "" {
|
||||
return apiVersion
|
||||
}
|
||||
apiVersion = "v1.1"
|
||||
logger.SysLog("api_version not found, using default: " + apiVersion)
|
||||
return apiVersion
|
||||
return ""
|
||||
}
|
||||
|
||||
// https://www.xfyun.cn/doc/spark/Web.html#_1-%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
|
||||
@@ -305,8 +292,7 @@ func apiVersion2domain(apiVersion string) string {
|
||||
return "general" + apiVersion
|
||||
}
|
||||
|
||||
func getXunfeiAuthUrl(c *gin.Context, apiKey string, apiSecret string, modelName string) (string, string) {
|
||||
apiVersion := getAPIVersion(c, modelName)
|
||||
func getXunfeiAuthUrl(apiVersion string, apiKey string, apiSecret string) (string, string) {
|
||||
domain := apiVersion2domain(apiVersion)
|
||||
authUrl := buildXunfeiAuthUrl(fmt.Sprintf("wss://spark-api.xf-yun.com/%s/chat", apiVersion), apiKey, apiSecret)
|
||||
return domain, authUrl
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package xunfei
|
||||
|
||||
import (
|
||||
"github.com/Laisky/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
|
||||
Reference in New Issue
Block a user