mirror of
https://github.com/songquanpeng/one-api.git
synced 2026-04-22 09:54:25 +08:00
chore: improve implementation
This commit is contained in:
@@ -1,19 +1,20 @@
|
|||||||
package tencent
|
package tencent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"github.com/songquanpeng/one-api/common/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||||
"github.com/songquanpeng/one-api/relay/meta"
|
"github.com/songquanpeng/one-api/relay/meta"
|
||||||
"github.com/songquanpeng/one-api/relay/model"
|
"github.com/songquanpeng/one-api/relay/model"
|
||||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://cloud.tencent.com/document/api/1729/101837
|
// https://cloud.tencent.com/document/api/1729/101837
|
||||||
@@ -54,29 +55,18 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
var convertedRequest any
|
||||||
switch relayMode {
|
switch relayMode {
|
||||||
case relaymode.Embeddings:
|
case relaymode.Embeddings:
|
||||||
a.Action = "GetEmbedding"
|
a.Action = "GetEmbedding"
|
||||||
tencentEmbeddingRequest := ConvertEmbeddingRequest(*request)
|
convertedRequest = ConvertEmbeddingRequest(*request)
|
||||||
payload, err := json.Marshal(tencentEmbeddingRequest)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// we have to calculate the sign here
|
|
||||||
a.Sign = GetSign(payload, a, secretId, secretKey)
|
|
||||||
return tencentEmbeddingRequest, nil
|
|
||||||
default:
|
default:
|
||||||
a.Action = "ChatCompletions"
|
a.Action = "ChatCompletions"
|
||||||
tencentRequest := ConvertRequest(*request)
|
convertedRequest = ConvertRequest(*request)
|
||||||
payload, err := json.Marshal(tencentRequest)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// we have to calculate the sign here
|
|
||||||
a.Sign = GetSign(payload, a, secretId, secretKey)
|
|
||||||
return tencentRequest, nil
|
|
||||||
}
|
}
|
||||||
|
// we have to calculate the sign here
|
||||||
|
a.Sign = GetSign(convertedRequest, a, secretId, secretKey)
|
||||||
|
return convertedRequest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
|
||||||
"github.com/songquanpeng/one-api/common/render"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -17,11 +15,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
"github.com/songquanpeng/one-api/common/conv"
|
"github.com/songquanpeng/one-api/common/conv"
|
||||||
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||||
"github.com/songquanpeng/one-api/common/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
"github.com/songquanpeng/one-api/common/random"
|
"github.com/songquanpeng/one-api/common/random"
|
||||||
|
"github.com/songquanpeng/one-api/common/render"
|
||||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||||
"github.com/songquanpeng/one-api/relay/constant"
|
"github.com/songquanpeng/one-api/relay/constant"
|
||||||
"github.com/songquanpeng/one-api/relay/model"
|
"github.com/songquanpeng/one-api/relay/model"
|
||||||
@@ -256,7 +257,7 @@ func hmacSha256(s, key string) string {
|
|||||||
return string(hashed.Sum(nil))
|
return string(hashed.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSign(payload []byte, adaptor *Adaptor, secId, secKey string) string {
|
func GetSign(req any, adaptor *Adaptor, secId, secKey string) string {
|
||||||
// build canonical request string
|
// build canonical request string
|
||||||
host := "hunyuan.tencentcloudapi.com"
|
host := "hunyuan.tencentcloudapi.com"
|
||||||
httpRequestMethod := "POST"
|
httpRequestMethod := "POST"
|
||||||
@@ -265,6 +266,7 @@ func GetSign(payload []byte, adaptor *Adaptor, secId, secKey string) string {
|
|||||||
canonicalHeaders := fmt.Sprintf("content-type:%s\nhost:%s\nx-tc-action:%s\n",
|
canonicalHeaders := fmt.Sprintf("content-type:%s\nhost:%s\nx-tc-action:%s\n",
|
||||||
"application/json", host, strings.ToLower(adaptor.Action))
|
"application/json", host, strings.ToLower(adaptor.Action))
|
||||||
signedHeaders := "content-type;host;x-tc-action"
|
signedHeaders := "content-type;host;x-tc-action"
|
||||||
|
payload, _ := json.Marshal(req)
|
||||||
hashedRequestPayload := sha256hex(string(payload))
|
hashedRequestPayload := sha256hex(string(payload))
|
||||||
canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s",
|
canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s",
|
||||||
httpRequestMethod,
|
httpRequestMethod,
|
||||||
|
|||||||
Reference in New Issue
Block a user