mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-19 01:56:37 +08:00
Merge remote-tracking branch 'origin/upstream/main'
This commit is contained in:
commit
0164cdbcf2
3
.env.example
Normal file
3
.env.example
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
PORT=3000
|
||||||
|
DEBUG=false
|
||||||
|
HTTPS_PROXY=http://localhost:7890
|
71
.github/workflows/ci.yml
vendored
71
.github/workflows/ci.yml
vendored
@ -3,16 +3,56 @@ name: ci
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- 'master'
|
- "master"
|
||||||
- 'main'
|
- "main"
|
||||||
- 'test/ci'
|
- "test/ci"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
unit_tests:
|
||||||
|
name: "Unit tests"
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v4
|
||||||
|
with:
|
||||||
|
go-version: ^1.22
|
||||||
|
|
||||||
|
# When you execute your unit tests, make sure to use the "-coverprofile" flag to write a
|
||||||
|
# coverage profile to a file. You will need the name of the file (e.g. "coverage.txt")
|
||||||
|
# in the next step as well as the next job.
|
||||||
|
- name: Test
|
||||||
|
run: go test -cover -coverprofile=coverage.txt ./...
|
||||||
|
|
||||||
|
- name: Archive code coverage results
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: code-coverage
|
||||||
|
path: coverage.txt # Make sure to use the same file name you chose for the "-coverprofile" in the "Test" step
|
||||||
|
|
||||||
|
code_coverage:
|
||||||
|
name: "Code coverage report"
|
||||||
|
if: github.event_name == 'pull_request' # Do not run when workflow is triggered by push to main branch
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: unit_tests # Depends on the artifact uploaded by the "unit_tests" job
|
||||||
|
steps:
|
||||||
|
- uses: fgrosse/go-coverage-report@v1.0.2 # Consider using a Git revision for maximum security
|
||||||
|
with:
|
||||||
|
coverage-artifact-name: "code-coverage" # can be omitted if you used this default value
|
||||||
|
coverage-file-name: "coverage.txt" # can be omitted if you used this default value
|
||||||
|
|
||||||
|
commit_lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: wagoid/commitlint-github-action@v6
|
||||||
|
|
||||||
build_latest:
|
build_latest:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
-
|
- name: Checkout
|
||||||
name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
@ -21,15 +61,13 @@ jobs:
|
|||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
-
|
- name: Login to Docker Hub
|
||||||
name: Login to Docker Hub
|
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
|
|
||||||
-
|
- name: Build and push latest
|
||||||
name: Build and push latest
|
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
@ -41,8 +79,7 @@ jobs:
|
|||||||
build_hash:
|
build_hash:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
-
|
- name: Checkout
|
||||||
name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
@ -51,8 +88,7 @@ jobs:
|
|||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
-
|
- name: Login to Docker Hub
|
||||||
name: Login to Docker Hub
|
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
@ -61,8 +97,7 @@ jobs:
|
|||||||
- name: Add SHORT_SHA env property with commit short sha
|
- name: Add SHORT_SHA env property with commit short sha
|
||||||
run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-7`" >> $GITHUB_ENV
|
run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-7`" >> $GITHUB_ENV
|
||||||
|
|
||||||
-
|
- name: Build and push hash label
|
||||||
name: Build and push hash label
|
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
@ -72,9 +107,9 @@ jobs:
|
|||||||
cache-to: type=gha,mode=max
|
cache-to: type=gha,mode=max
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: build_latest
|
needs: build_latest
|
||||||
steps:
|
steps:
|
||||||
- name: executing remote ssh commands using password
|
- name: executing remote ssh commands using password
|
||||||
uses: appleboy/ssh-action@v1.0.3
|
uses: appleboy/ssh-action@v1.0.3
|
||||||
with:
|
with:
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -8,4 +8,6 @@ build
|
|||||||
logs
|
logs
|
||||||
data
|
data
|
||||||
node_modules
|
node_modules
|
||||||
|
/web/node_modules
|
||||||
cmd.md
|
cmd.md
|
||||||
|
.env
|
||||||
|
@ -2,6 +2,7 @@ package image_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"github.com/songquanpeng/one-api/common/client"
|
||||||
"image"
|
"image"
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
@ -44,6 +45,11 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
client.Init()
|
||||||
|
m.Run()
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecode(t *testing.T) {
|
||||||
// Bytes read: varies sometimes
|
// Bytes read: varies sometimes
|
||||||
// jpeg: 1063892
|
// jpeg: 1063892
|
||||||
|
@ -3,11 +3,12 @@ package common
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -24,8 +25,8 @@ func printHelp() {
|
|||||||
fmt.Println("Usage: one-api [--port <port>] [--log-dir <log directory>] [--version] [--help]")
|
fmt.Println("Usage: one-api [--port <port>] [--log-dir <log directory>] [--version] [--help]")
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func Init() {
|
||||||
// flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// if *PrintVersion {
|
// if *PrintVersion {
|
||||||
// fmt.Println(Version)
|
// fmt.Println(Version)
|
||||||
|
1
go.mod
1
go.mod
@ -22,6 +22,7 @@ require (
|
|||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/gorilla/websocket v1.5.1
|
github.com/gorilla/websocket v1.5.1
|
||||||
github.com/jinzhu/copier v0.4.0
|
github.com/jinzhu/copier v0.4.0
|
||||||
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/pkoukk/tiktoken-go v0.1.7
|
github.com/pkoukk/tiktoken-go v0.1.7
|
||||||
github.com/smartystreets/goconvey v1.8.1
|
github.com/smartystreets/goconvey v1.8.1
|
||||||
|
2
go.sum
2
go.sum
@ -125,6 +125,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
|
|||||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
|
3
main.go
3
main.go
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-contrib/sessions/cookie"
|
"github.com/gin-contrib/sessions/cookie"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
_ "github.com/joho/godotenv/autoload"
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
"github.com/songquanpeng/one-api/common/client"
|
"github.com/songquanpeng/one-api/common/client"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
@ -25,6 +26,7 @@ import (
|
|||||||
var buildFS embed.FS
|
var buildFS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
common.Init()
|
||||||
logger.SetupLogger()
|
logger.SetupLogger()
|
||||||
logger.SysLogf("One API %s started", common.Version)
|
logger.SysLogf("One API %s started", common.Version)
|
||||||
if os.Getenv("GIN_MODE") != "debug" {
|
if os.Getenv("GIN_MODE") != "debug" {
|
||||||
@ -120,6 +122,7 @@ func main() {
|
|||||||
if port == "" {
|
if port == "" {
|
||||||
port = strconv.Itoa(*common.Port)
|
port = strconv.Itoa(*common.Port)
|
||||||
}
|
}
|
||||||
|
logger.SysLogf("server started on http://localhost:%s", port)
|
||||||
err = server.Run(":" + port)
|
err = server.Run(":" + port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.FatalLog("failed to start HTTP server: " + err.Error())
|
logger.FatalLog("failed to start HTTP server: " + err.Error())
|
||||||
|
@ -2,12 +2,13 @@ package tencent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"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"
|
||||||
"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"
|
||||||
@ -17,21 +18,28 @@ import (
|
|||||||
// https://cloud.tencent.com/document/api/1729/101837
|
// https://cloud.tencent.com/document/api/1729/101837
|
||||||
|
|
||||||
type Adaptor struct {
|
type Adaptor struct {
|
||||||
Sign string
|
Sign string
|
||||||
|
Action string
|
||||||
|
Version string
|
||||||
|
Timestamp int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||||
|
a.Action = "ChatCompletions"
|
||||||
|
a.Version = "2023-09-01"
|
||||||
|
a.Timestamp = helper.GetTimestamp()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||||
return fmt.Sprintf("%s/hyllm/v1/chat/completions", meta.BaseURL), nil
|
return meta.BaseURL + "/", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
||||||
adaptor.SetupCommonRequestHeader(c, req, meta)
|
adaptor.SetupCommonRequestHeader(c, req, meta)
|
||||||
req.Header.Set("Authorization", a.Sign)
|
req.Header.Set("Authorization", a.Sign)
|
||||||
req.Header.Set("X-TC-Action", meta.ActualModelName)
|
req.Header.Set("X-TC-Action", a.Action)
|
||||||
|
req.Header.Set("X-TC-Version", a.Version)
|
||||||
|
req.Header.Set("X-TC-Timestamp", strconv.FormatInt(a.Timestamp, 10))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,15 +49,13 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
|
|||||||
}
|
}
|
||||||
apiKey := c.Request.Header.Get("Authorization")
|
apiKey := c.Request.Header.Get("Authorization")
|
||||||
apiKey = strings.TrimPrefix(apiKey, "Bearer ")
|
apiKey = strings.TrimPrefix(apiKey, "Bearer ")
|
||||||
appId, secretId, secretKey, err := ParseConfig(apiKey)
|
_, secretId, secretKey, err := ParseConfig(apiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tencentRequest := ConvertRequest(*request)
|
tencentRequest := ConvertRequest(*request)
|
||||||
tencentRequest.AppId = appId
|
|
||||||
tencentRequest.SecretId = secretId
|
|
||||||
// we have to calculate the sign here
|
// we have to calculate the sign here
|
||||||
a.Sign = GetSign(*tencentRequest, secretKey)
|
a.Sign = GetSign(*tencentRequest, a, secretId, secretKey)
|
||||||
return tencentRequest, nil
|
return tencentRequest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package tencent
|
package tencent
|
||||||
|
|
||||||
var ModelList = []string{
|
var ModelList = []string{
|
||||||
"ChatPro",
|
"hunyuan-lite",
|
||||||
"ChatStd",
|
"hunyuan-standard",
|
||||||
"hunyuan",
|
"hunyuan-standard-256K",
|
||||||
|
"hunyuan-pro",
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,16 @@ package tencent
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha1"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
@ -25,29 +25,21 @@ import (
|
|||||||
"github.com/songquanpeng/one-api/relay/model"
|
"github.com/songquanpeng/one-api/relay/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://cloud.tencent.com/document/product/1729/97732
|
|
||||||
|
|
||||||
func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
|
func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
|
||||||
messages := make([]Message, 0, len(request.Messages))
|
messages := make([]*Message, 0, len(request.Messages))
|
||||||
for i := 0; i < len(request.Messages); i++ {
|
for i := 0; i < len(request.Messages); i++ {
|
||||||
message := request.Messages[i]
|
message := request.Messages[i]
|
||||||
messages = append(messages, Message{
|
messages = append(messages, &Message{
|
||||||
Content: message.StringContent(),
|
Content: message.StringContent(),
|
||||||
Role: message.Role,
|
Role: message.Role,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
stream := 0
|
|
||||||
if request.Stream {
|
|
||||||
stream = 1
|
|
||||||
}
|
|
||||||
return &ChatRequest{
|
return &ChatRequest{
|
||||||
Timestamp: helper.GetTimestamp(),
|
Model: &request.Model,
|
||||||
Expired: helper.GetTimestamp() + 24*60*60,
|
Stream: &request.Stream,
|
||||||
QueryID: random.GetUUID(),
|
|
||||||
Temperature: request.Temperature,
|
|
||||||
TopP: request.TopP,
|
|
||||||
Stream: stream,
|
|
||||||
Messages: messages,
|
Messages: messages,
|
||||||
|
TopP: &request.TopP,
|
||||||
|
Temperature: &request.Temperature,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +47,11 @@ func responseTencent2OpenAI(response *ChatResponse) *openai.TextResponse {
|
|||||||
fullTextResponse := openai.TextResponse{
|
fullTextResponse := openai.TextResponse{
|
||||||
Object: "chat.completion",
|
Object: "chat.completion",
|
||||||
Created: helper.GetTimestamp(),
|
Created: helper.GetTimestamp(),
|
||||||
Usage: response.Usage,
|
Usage: model.Usage{
|
||||||
|
PromptTokens: response.Usage.PromptTokens,
|
||||||
|
CompletionTokens: response.Usage.CompletionTokens,
|
||||||
|
TotalTokens: response.Usage.TotalTokens,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
if len(response.Choices) > 0 {
|
if len(response.Choices) > 0 {
|
||||||
choice := openai.TextResponseChoice{
|
choice := openai.TextResponseChoice{
|
||||||
@ -155,6 +151,7 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
|
|||||||
|
|
||||||
func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||||
var TencentResponse ChatResponse
|
var TencentResponse ChatResponse
|
||||||
|
var responseP ChatResponseP
|
||||||
responseBody, err := io.ReadAll(resp.Body)
|
responseBody, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
||||||
@ -163,10 +160,11 @@ func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(responseBody, &TencentResponse)
|
err = json.Unmarshal(responseBody, &responseP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
||||||
}
|
}
|
||||||
|
TencentResponse = responseP.Response
|
||||||
if TencentResponse.Error.Code != 0 {
|
if TencentResponse.Error.Code != 0 {
|
||||||
return &model.ErrorWithStatusCode{
|
return &model.ErrorWithStatusCode{
|
||||||
Error: model.Error{
|
Error: model.Error{
|
||||||
@ -203,29 +201,62 @@ func ParseConfig(config string) (appId int64, secretId string, secretKey string,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSign(req ChatRequest, secretKey string) string {
|
func sha256hex(s string) string {
|
||||||
params := make([]string, 0)
|
b := sha256.Sum256([]byte(s))
|
||||||
params = append(params, "app_id="+strconv.FormatInt(req.AppId, 10))
|
return hex.EncodeToString(b[:])
|
||||||
params = append(params, "secret_id="+req.SecretId)
|
}
|
||||||
params = append(params, "timestamp="+strconv.FormatInt(req.Timestamp, 10))
|
|
||||||
params = append(params, "query_id="+req.QueryID)
|
func hmacSha256(s, key string) string {
|
||||||
params = append(params, "temperature="+strconv.FormatFloat(req.Temperature, 'f', -1, 64))
|
hashed := hmac.New(sha256.New, []byte(key))
|
||||||
params = append(params, "top_p="+strconv.FormatFloat(req.TopP, 'f', -1, 64))
|
hashed.Write([]byte(s))
|
||||||
params = append(params, "stream="+strconv.Itoa(req.Stream))
|
return string(hashed.Sum(nil))
|
||||||
params = append(params, "expired="+strconv.FormatInt(req.Expired, 10))
|
}
|
||||||
|
|
||||||
var messageStr string
|
func GetSign(req ChatRequest, adaptor *Adaptor, secId, secKey string) string {
|
||||||
for _, msg := range req.Messages {
|
// build canonical request string
|
||||||
messageStr += fmt.Sprintf(`{"role":"%s","content":"%s"},`, msg.Role, msg.Content)
|
host := "hunyuan.tencentcloudapi.com"
|
||||||
}
|
httpRequestMethod := "POST"
|
||||||
messageStr = strings.TrimSuffix(messageStr, ",")
|
canonicalURI := "/"
|
||||||
params = append(params, "messages=["+messageStr+"]")
|
canonicalQueryString := ""
|
||||||
|
canonicalHeaders := fmt.Sprintf("content-type:%s\nhost:%s\nx-tc-action:%s\n",
|
||||||
sort.Strings(params)
|
"application/json", host, strings.ToLower(adaptor.Action))
|
||||||
url := "hunyuan.cloud.tencent.com/hyllm/v1/chat/completions?" + strings.Join(params, "&")
|
signedHeaders := "content-type;host;x-tc-action"
|
||||||
mac := hmac.New(sha1.New, []byte(secretKey))
|
payload, _ := json.Marshal(req)
|
||||||
signURL := url
|
hashedRequestPayload := sha256hex(string(payload))
|
||||||
mac.Write([]byte(signURL))
|
canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s",
|
||||||
sign := mac.Sum([]byte(nil))
|
httpRequestMethod,
|
||||||
return base64.StdEncoding.EncodeToString(sign)
|
canonicalURI,
|
||||||
|
canonicalQueryString,
|
||||||
|
canonicalHeaders,
|
||||||
|
signedHeaders,
|
||||||
|
hashedRequestPayload)
|
||||||
|
// build string to sign
|
||||||
|
algorithm := "TC3-HMAC-SHA256"
|
||||||
|
requestTimestamp := strconv.FormatInt(adaptor.Timestamp, 10)
|
||||||
|
timestamp, _ := strconv.ParseInt(requestTimestamp, 10, 64)
|
||||||
|
t := time.Unix(timestamp, 0).UTC()
|
||||||
|
// must be the format 2006-01-02, ref to package time for more info
|
||||||
|
date := t.Format("2006-01-02")
|
||||||
|
credentialScope := fmt.Sprintf("%s/%s/tc3_request", date, "hunyuan")
|
||||||
|
hashedCanonicalRequest := sha256hex(canonicalRequest)
|
||||||
|
string2sign := fmt.Sprintf("%s\n%s\n%s\n%s",
|
||||||
|
algorithm,
|
||||||
|
requestTimestamp,
|
||||||
|
credentialScope,
|
||||||
|
hashedCanonicalRequest)
|
||||||
|
|
||||||
|
// sign string
|
||||||
|
secretDate := hmacSha256(date, "TC3"+secKey)
|
||||||
|
secretService := hmacSha256("hunyuan", secretDate)
|
||||||
|
secretKey := hmacSha256("tc3_request", secretService)
|
||||||
|
signature := hex.EncodeToString([]byte(hmacSha256(string2sign, secretKey)))
|
||||||
|
|
||||||
|
// build authorization
|
||||||
|
authorization := fmt.Sprintf("%s Credential=%s/%s, SignedHeaders=%s, Signature=%s",
|
||||||
|
algorithm,
|
||||||
|
secId,
|
||||||
|
credentialScope,
|
||||||
|
signedHeaders,
|
||||||
|
signature)
|
||||||
|
return authorization
|
||||||
}
|
}
|
||||||
|
@ -1,63 +1,75 @@
|
|||||||
package tencent
|
package tencent
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/songquanpeng/one-api/relay/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Role string `json:"role"`
|
Role string `json:"Role"`
|
||||||
Content string `json:"content"`
|
Content string `json:"Content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRequest struct {
|
type ChatRequest struct {
|
||||||
AppId int64 `json:"app_id"` // 腾讯云账号的 APPID
|
// 模型名称,可选值包括 hunyuan-lite、hunyuan-standard、hunyuan-standard-256K、hunyuan-pro。
|
||||||
SecretId string `json:"secret_id"` // 官网 SecretId
|
// 各模型介绍请阅读 [产品概述](https://cloud.tencent.com/document/product/1729/104753) 中的说明。
|
||||||
// Timestamp当前 UNIX 时间戳,单位为秒,可记录发起 API 请求的时间。
|
//
|
||||||
// 例如1529223702,如果与当前时间相差过大,会引起签名过期错误
|
// 注意:
|
||||||
Timestamp int64 `json:"timestamp"`
|
// 不同的模型计费不同,请根据 [购买指南](https://cloud.tencent.com/document/product/1729/97731) 按需调用。
|
||||||
// Expired 签名的有效期,是一个符合 UNIX Epoch 时间戳规范的数值,
|
Model *string `json:"Model"`
|
||||||
// 单位为秒;Expired 必须大于 Timestamp 且 Expired-Timestamp 小于90天
|
// 聊天上下文信息。
|
||||||
Expired int64 `json:"expired"`
|
// 说明:
|
||||||
QueryID string `json:"query_id"` //请求 Id,用于问题排查
|
// 1. 长度最多为 40,按对话时间从旧到新在数组中排列。
|
||||||
// Temperature 较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定
|
// 2. Message.Role 可选值:system、user、assistant。
|
||||||
// 默认 1.0,取值区间为[0.0,2.0],非必要不建议使用,不合理的取值会影响效果
|
// 其中,system 角色可选,如存在则必须位于列表的最开始。user 和 assistant 需交替出现(一问一答),以 user 提问开始和结束,且 Content 不能为空。Role 的顺序示例:[system(可选) user assistant user assistant user ...]。
|
||||||
// 建议该参数和 top_p 只设置1个,不要同时更改 top_p
|
// 3. Messages 中 Content 总长度不能超过模型输入长度上限(可参考 [产品概述](https://cloud.tencent.com/document/product/1729/104753) 文档),超过则会截断最前面的内容,只保留尾部内容。
|
||||||
Temperature float64 `json:"temperature"`
|
Messages []*Message `json:"Messages"`
|
||||||
// TopP 影响输出文本的多样性,取值越大,生成文本的多样性越强
|
// 流式调用开关。
|
||||||
// 默认1.0,取值区间为[0.0, 1.0],非必要不建议使用, 不合理的取值会影响效果
|
// 说明:
|
||||||
// 建议该参数和 temperature 只设置1个,不要同时更改
|
// 1. 未传值时默认为非流式调用(false)。
|
||||||
TopP float64 `json:"top_p"`
|
// 2. 流式调用时以 SSE 协议增量返回结果(返回值取 Choices[n].Delta 中的值,需要拼接增量数据才能获得完整结果)。
|
||||||
// Stream 0:同步,1:流式 (默认,协议:SSE)
|
// 3. 非流式调用时:
|
||||||
// 同步请求超时:60s,如果内容较长建议使用流式
|
// 调用方式与普通 HTTP 请求无异。
|
||||||
Stream int `json:"stream"`
|
// 接口响应耗时较长,**如需更低时延建议设置为 true**。
|
||||||
// Messages 会话内容, 长度最多为40, 按对话时间从旧到新在数组中排列
|
// 只返回一次最终结果(返回值取 Choices[n].Message 中的值)。
|
||||||
// 输入 content 总数最大支持 3000 token。
|
//
|
||||||
Messages []Message `json:"messages"`
|
// 注意:
|
||||||
|
// 通过 SDK 调用时,流式和非流式调用需用**不同的方式**获取返回值,具体参考 SDK 中的注释或示例(在各语言 SDK 代码仓库的 examples/hunyuan/v20230901/ 目录中)。
|
||||||
|
Stream *bool `json:"Stream"`
|
||||||
|
// 说明:
|
||||||
|
// 1. 影响输出文本的多样性,取值越大,生成文本的多样性越强。
|
||||||
|
// 2. 取值区间为 [0.0, 1.0],未传值时使用各模型推荐值。
|
||||||
|
// 3. 非必要不建议使用,不合理的取值会影响效果。
|
||||||
|
TopP *float64 `json:"TopP"`
|
||||||
|
// 说明:
|
||||||
|
// 1. 较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定。
|
||||||
|
// 2. 取值区间为 [0.0, 2.0],未传值时使用各模型推荐值。
|
||||||
|
// 3. 非必要不建议使用,不合理的取值会影响效果。
|
||||||
|
Temperature *float64 `json:"Temperature"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Error struct {
|
type Error struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"Code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"Message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Usage struct {
|
type Usage struct {
|
||||||
InputTokens int `json:"input_tokens"`
|
PromptTokens int `json:"PromptTokens"`
|
||||||
OutputTokens int `json:"output_tokens"`
|
CompletionTokens int `json:"CompletionTokens"`
|
||||||
TotalTokens int `json:"total_tokens"`
|
TotalTokens int `json:"TotalTokens"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseChoices struct {
|
type ResponseChoices struct {
|
||||||
FinishReason string `json:"finish_reason,omitempty"` // 流式结束标志位,为 stop 则表示尾包
|
FinishReason string `json:"FinishReason,omitempty"` // 流式结束标志位,为 stop 则表示尾包
|
||||||
Messages Message `json:"messages,omitempty"` // 内容,同步模式返回内容,流模式为 null 输出 content 内容总数最多支持 1024token。
|
Messages Message `json:"Message,omitempty"` // 内容,同步模式返回内容,流模式为 null 输出 content 内容总数最多支持 1024token。
|
||||||
Delta Message `json:"delta,omitempty"` // 内容,流模式返回内容,同步模式为 null 输出 content 内容总数最多支持 1024token。
|
Delta Message `json:"Delta,omitempty"` // 内容,流模式返回内容,同步模式为 null 输出 content 内容总数最多支持 1024token。
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatResponse struct {
|
type ChatResponse struct {
|
||||||
Choices []ResponseChoices `json:"choices,omitempty"` // 结果
|
Choices []ResponseChoices `json:"Choices,omitempty"` // 结果
|
||||||
Created string `json:"created,omitempty"` // unix 时间戳的字符串
|
Created int64 `json:"Created,omitempty"` // unix 时间戳的字符串
|
||||||
Id string `json:"id,omitempty"` // 会话 id
|
Id string `json:"Id,omitempty"` // 会话 id
|
||||||
Usage model.Usage `json:"usage,omitempty"` // token 数量
|
Usage Usage `json:"Usage,omitempty"` // token 数量
|
||||||
Error Error `json:"error,omitempty"` // 错误信息 注意:此字段可能返回 null,表示取不到有效值
|
Error Error `json:"Error,omitempty"` // 错误信息 注意:此字段可能返回 null,表示取不到有效值
|
||||||
Note string `json:"note,omitempty"` // 注释
|
Note string `json:"Note,omitempty"` // 注释
|
||||||
ReqID string `json:"req_id,omitempty"` // 唯一请求 Id,每次请求都会返回。用于反馈接口入参
|
ReqID string `json:"Req_id,omitempty"` // 唯一请求 Id,每次请求都会返回。用于反馈接口入参
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChatResponseP struct {
|
||||||
|
Response ChatResponse `json:"Response,omitempty"`
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,12 @@ func requestOpenAI2Xunfei(request model.GeneralOpenAIRequest, xunfeiAppId string
|
|||||||
xunfeiRequest.Payload.Message.Text = messages
|
xunfeiRequest.Payload.Message.Text = messages
|
||||||
|
|
||||||
if strings.HasPrefix(domain, "generalv3") {
|
if strings.HasPrefix(domain, "generalv3") {
|
||||||
|
functions := make([]model.Function, len(request.Tools))
|
||||||
|
for i, tool := range request.Tools {
|
||||||
|
functions[i] = tool.Function
|
||||||
|
}
|
||||||
xunfeiRequest.Payload.Functions = &Functions{
|
xunfeiRequest.Payload.Functions = &Functions{
|
||||||
Text: request.Tools,
|
Text: functions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ type Message struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Functions struct {
|
type Functions struct {
|
||||||
Text []model.Tool `json:"text,omitempty"`
|
Text []model.Function `json:"text,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRequest struct {
|
type ChatRequest struct {
|
||||||
|
16
relay/adaptor_test.go
Normal file
16
relay/adaptor_test.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package relay
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
"github.com/songquanpeng/one-api/relay/apitype"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetAdaptor(t *testing.T) {
|
||||||
|
Convey("get adaptor", t, func() {
|
||||||
|
for i := 0; i < apitype.Dummy; i++ {
|
||||||
|
a := GetAdaptor(i)
|
||||||
|
So(a, ShouldNotBeNil)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -24,7 +24,7 @@ var ChannelBaseURLs = []string{
|
|||||||
"https://openrouter.ai/api", // 20
|
"https://openrouter.ai/api", // 20
|
||||||
"https://api.aiproxy.io", // 21
|
"https://api.aiproxy.io", // 21
|
||||||
"https://fastgpt.run/api/openapi", // 22
|
"https://fastgpt.run/api/openapi", // 22
|
||||||
"https://hunyuan.cloud.tencent.com", // 23
|
"https://hunyuan.tencentcloudapi.com", // 23
|
||||||
"https://generativelanguage.googleapis.com", // 24
|
"https://generativelanguage.googleapis.com", // 24
|
||||||
"https://api.moonshot.cn", // 25
|
"https://api.moonshot.cn", // 25
|
||||||
"https://api.baichuan-ai.com", // 26
|
"https://api.baichuan-ai.com", // 26
|
||||||
|
12
relay/channeltype/url_test.go
Normal file
12
relay/channeltype/url_test.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package channeltype
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChannelBaseURLs(t *testing.T) {
|
||||||
|
Convey("channel base urls", t, func() {
|
||||||
|
So(len(ChannelBaseURLs), ShouldEqual, Dummy)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user