chore: improve implementation

This commit is contained in:
JustSong
2025-01-31 16:46:33 +08:00
parent bccdcca7cb
commit 64fc8ea3f7
2 changed files with 17 additions and 25 deletions

View File

@@ -1,19 +1,20 @@
package tencent
import (
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/helper"
"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/songquanpeng/one-api/relay/relaymode"
"io"
"net/http"
"strconv"
"strings"
)
// 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 {
return nil, err
}
var convertedRequest any
switch relayMode {
case relaymode.Embeddings:
a.Action = "GetEmbedding"
tencentEmbeddingRequest := 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
convertedRequest = ConvertEmbeddingRequest(*request)
default:
a.Action = "ChatCompletions"
tencentRequest := 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
convertedRequest = ConvertRequest(*request)
}
// 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) {

View File

@@ -8,8 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/render"
"io"
"net/http"
"strconv"
@@ -17,11 +15,14 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"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/logger"
"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/constant"
"github.com/songquanpeng/one-api/relay/model"
@@ -256,7 +257,7 @@ func hmacSha256(s, key string) string {
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
host := "hunyuan.tencentcloudapi.com"
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",
"application/json", host, strings.ToLower(adaptor.Action))
signedHeaders := "content-type;host;x-tc-action"
payload, _ := json.Marshal(req)
hashedRequestPayload := sha256hex(string(payload))
canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s",
httpRequestMethod,