diff --git a/README.md b/README.md index 40f6e4e0..a1a24e0a 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ _✨ 通过标准的 OpenAI API 格式访问所有的大模型,开箱即用 + [x] [Anthropic Claude 系列模型](https://anthropic.com) (支持 AWS Claude) + [x] [Google PaLM2/Gemini 系列模型](https://developers.generativeai.google) + [x] [Mistral 系列模型](https://mistral.ai/) + + [x] [字节跳动豆包大模型](https://console.volcengine.com/ark/region:ark+cn-beijing/model) + [x] [百度文心一言系列模型](https://cloud.baidu.com/doc/WENXINWORKSHOP/index.html) + [x] [阿里通义千问系列模型](https://help.aliyun.com/document_detail/2400395.html) + [x] [讯飞星火认知大模型](https://www.xfyun.cn/doc/spark/Web.html) @@ -76,7 +77,6 @@ _✨ 通过标准的 OpenAI API 格式访问所有的大模型,开箱即用 + [x] [腾讯混元大模型](https://cloud.tencent.com/document/product/1729) + [x] [Moonshot AI](https://platform.moonshot.cn/) + [x] [百川大模型](https://platform.baichuan-ai.com) - + [ ] [字节云雀大模型](https://www.volcengine.com/product/ark) (WIP) + [x] [MINIMAX](https://api.minimax.chat/) + [x] [Groq](https://wow.groq.com/) + [x] [Ollama](https://github.com/ollama/ollama) @@ -384,14 +384,17 @@ graph LR + `TIKTOKEN_CACHE_DIR`:默认程序启动时会联网下载一些通用的词元的编码,如:`gpt-3.5-turbo`,在一些网络环境不稳定,或者离线情况,可能会导致启动有问题,可以配置此目录缓存数据,可迁移到离线环境。 + `DATA_GYM_CACHE_DIR`:目前该配置作用与 `TIKTOKEN_CACHE_DIR` 一致,但是优先级没有它高。 17. `RELAY_TIMEOUT`:中继超时设置,单位为秒,默认不设置超时时间。 -18. `SQLITE_BUSY_TIMEOUT`:SQLite 锁等待超时设置,单位为毫秒,默认 `3000`。 -19. `GEMINI_SAFETY_SETTING`:Gemini 的安全设置,默认 `BLOCK_NONE`。 -20. `GEMINI_VERSION`:One API 所使用的 Gemini 版本,默认为 `v1`。 -21. `THEME`:系统的主题设置,默认为 `default`,具体可选值参考[此处](./web/README.md)。 -22. `ENABLE_METRIC`:是否根据请求成功率禁用渠道,默认不开启,可选值为 `true` 和 `false`。 -23. `METRIC_QUEUE_SIZE`:请求成功率统计队列大小,默认为 `10`。 -24. `METRIC_SUCCESS_RATE_THRESHOLD`:请求成功率阈值,默认为 `0.8`。 -25. `INITIAL_ROOT_TOKEN`:如果设置了该值,则在系统首次启动时会自动创建一个值为该环境变量值的 root 用户令牌。 +18. `RELAY_PROXY`:设置后使用该代理来请求 API。 +19. `USER_CONTENT_REQUEST_TIMEOUT`:用户上传内容下载超时时间,单位为秒。 +20. `USER_CONTENT_REQUEST_PROXY`:设置后使用该代理来请求用户上传的内容,例如图片。 +21. `SQLITE_BUSY_TIMEOUT`:SQLite 锁等待超时设置,单位为毫秒,默认 `3000`。 +22. `GEMINI_SAFETY_SETTING`:Gemini 的安全设置,默认 `BLOCK_NONE`。 +23. `GEMINI_VERSION`:One API 所使用的 Gemini 版本,默认为 `v1`。 +24. `THEME`:系统的主题设置,默认为 `default`,具体可选值参考[此处](./web/README.md)。 +25. `ENABLE_METRIC`:是否根据请求成功率禁用渠道,默认不开启,可选值为 `true` 和 `false`。 +26. `METRIC_QUEUE_SIZE`:请求成功率统计队列大小,默认为 `10`。 +27. `METRIC_SUCCESS_RATE_THRESHOLD`:请求成功率阈值,默认为 `0.8`。 +28. `INITIAL_ROOT_TOKEN`:如果设置了该值,则在系统首次启动时会自动创建一个值为该环境变量值的 root 用户令牌。 ### 命令行参数 1. `--port `: 指定服务器监听的端口号,默认为 `3000`。 diff --git a/common/client/init.go b/common/client/init.go new file mode 100644 index 00000000..f803cbf8 --- /dev/null +++ b/common/client/init.go @@ -0,0 +1,60 @@ +package client + +import ( + "fmt" + "github.com/songquanpeng/one-api/common/config" + "github.com/songquanpeng/one-api/common/logger" + "net/http" + "net/url" + "time" +) + +var HTTPClient *http.Client +var ImpatientHTTPClient *http.Client +var UserContentRequestHTTPClient *http.Client + +func Init() { + if config.UserContentRequestProxy != "" { + logger.SysLog(fmt.Sprintf("using %s as proxy to fetch user content", config.UserContentRequestProxy)) + proxyURL, err := url.Parse(config.UserContentRequestProxy) + if err != nil { + logger.FatalLog(fmt.Sprintf("USER_CONTENT_REQUEST_PROXY set but invalid: %s", config.UserContentRequestProxy)) + } + transport := &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + } + UserContentRequestHTTPClient = &http.Client{ + Transport: transport, + Timeout: time.Second * time.Duration(config.UserContentRequestTimeout), + } + } else { + UserContentRequestHTTPClient = &http.Client{} + } + var transport http.RoundTripper + if config.RelayProxy != "" { + logger.SysLog(fmt.Sprintf("using %s as api relay proxy", config.RelayProxy)) + proxyURL, err := url.Parse(config.RelayProxy) + if err != nil { + logger.FatalLog(fmt.Sprintf("USER_CONTENT_REQUEST_PROXY set but invalid: %s", config.UserContentRequestProxy)) + } + transport = &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + } + } + + if config.RelayTimeout == 0 { + HTTPClient = &http.Client{ + Transport: transport, + } + } else { + HTTPClient = &http.Client{ + Timeout: time.Duration(config.RelayTimeout) * time.Second, + Transport: transport, + } + } + + ImpatientHTTPClient = &http.Client{ + Timeout: 5 * time.Second, + Transport: transport, + } +} diff --git a/common/config/config.go b/common/config/config.go index 0864d844..4f1c25b6 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -117,10 +117,10 @@ var ValidThemes = map[string]bool{ // All duration's unit is seconds // Shouldn't larger then RateLimitKeyExpirationDuration var ( - GlobalApiRateLimitNum = env.Int("GLOBAL_API_RATE_LIMIT", 180) + GlobalApiRateLimitNum = env.Int("GLOBAL_API_RATE_LIMIT", 240) GlobalApiRateLimitDuration int64 = 3 * 60 - GlobalWebRateLimitNum = env.Int("GLOBAL_WEB_RATE_LIMIT", 60) + GlobalWebRateLimitNum = env.Int("GLOBAL_WEB_RATE_LIMIT", 120) GlobalWebRateLimitDuration int64 = 3 * 60 UploadRateLimitNum = 10 @@ -144,3 +144,7 @@ var MetricFailChanSize = env.Int("METRIC_FAIL_CHAN_SIZE", 128) var InitialRootToken = os.Getenv("INITIAL_ROOT_TOKEN") var GeminiVersion = env.String("GEMINI_VERSION", "v1") + +var RelayProxy = env.String("RELAY_PROXY", "") +var UserContentRequestProxy = env.String("USER_CONTENT_REQUEST_PROXY", "") +var UserContentRequestTimeout = env.Int("USER_CONTENT_REQUEST_TIMEOUT", 30) diff --git a/common/image/image.go b/common/image/image.go index 12f0adff..beebd0c6 100644 --- a/common/image/image.go +++ b/common/image/image.go @@ -3,6 +3,7 @@ package image import ( "bytes" "encoding/base64" + "github.com/songquanpeng/one-api/common/client" "image" _ "image/gif" _ "image/jpeg" @@ -19,7 +20,7 @@ import ( var dataURLPattern = regexp.MustCompile(`data:image/([^;]+);base64,(.*)`) func IsImageUrl(url string) (bool, error) { - resp, err := http.Head(url) + resp, err := client.UserContentRequestHTTPClient.Head(url) if err != nil { return false, err } @@ -34,7 +35,7 @@ func GetImageSizeFromUrl(url string) (width int, height int, err error) { if !isImage { return } - resp, err := http.Get(url) + resp, err := client.UserContentRequestHTTPClient.Get(url) if err != nil { return } diff --git a/common/logger/logger.go b/common/logger/logger.go index c3dcd89d..f725c619 100644 --- a/common/logger/logger.go +++ b/common/logger/logger.go @@ -43,11 +43,19 @@ func SysLog(s string) { _, _ = fmt.Fprintf(gin.DefaultWriter, "[SYS] %v | %s \n", t.Format("2006/01/02 - 15:04:05"), s) } +func SysLogf(format string, a ...any) { + SysLog(fmt.Sprintf(format, a...)) +} + func SysError(s string) { t := time.Now() _, _ = fmt.Fprintf(gin.DefaultErrorWriter, "[SYS] %v | %s \n", t.Format("2006/01/02 - 15:04:05"), s) } +func SysErrorf(format string, a ...any) { + SysError(fmt.Sprintf(format, a...)) +} + func Debug(ctx context.Context, msg string) { if config.DebugEnabled { logHelper(ctx, loggerDEBUG, msg) diff --git a/controller/channel-billing.go b/controller/channel-billing.go index b7ac61fd..53592744 100644 --- a/controller/channel-billing.go +++ b/controller/channel-billing.go @@ -4,12 +4,12 @@ import ( "encoding/json" "errors" "fmt" + "github.com/songquanpeng/one-api/common/client" "github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/model" "github.com/songquanpeng/one-api/monitor" "github.com/songquanpeng/one-api/relay/channeltype" - "github.com/songquanpeng/one-api/relay/client" "io" "net/http" "strconv" diff --git a/controller/user.go b/controller/user.go index af90acf6..e79881c2 100644 --- a/controller/user.go +++ b/controller/user.go @@ -173,6 +173,7 @@ func Register(c *gin.Context) { }) return } + c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", diff --git a/main.go b/main.go index bdcdcd61..95f8f62b 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "github.com/gin-contrib/sessions/cookie" "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/common" + "github.com/songquanpeng/one-api/common/client" "github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/controller" @@ -23,7 +24,7 @@ var buildFS embed.FS func main() { logger.SetupLogger() - logger.SysLog(fmt.Sprintf("One API %s started", common.Version)) + logger.SysLogf("One API %s started", common.Version) if os.Getenv("GIN_MODE") != "debug" { gin.SetMode(gin.ReleaseMode) } @@ -94,6 +95,7 @@ func main() { logger.SysLog("metric enabled, will disable channel if too much request failed") } openai.InitTokenEncoders() + client.Init() // Initialize HTTP server server := gin.New() diff --git a/middleware/distributor.go b/middleware/distributor.go index d0fd7ba5..0c4b04c3 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -67,26 +67,28 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode c.Set(ctxkey.BaseURL, channel.GetBaseURL()) cfg, _ := channel.LoadConfig() // this is for backward compatibility - switch channel.Type { - case channeltype.Azure: - if cfg.APIVersion == "" { - cfg.APIVersion = channel.Other - } - case channeltype.Xunfei: - if cfg.APIVersion == "" { - cfg.APIVersion = channel.Other - } - case channeltype.Gemini: - if cfg.APIVersion == "" { - cfg.APIVersion = channel.Other - } - case channeltype.AIProxyLibrary: - if cfg.LibraryID == "" { - cfg.LibraryID = channel.Other - } - case channeltype.Ali: - if cfg.Plugin == "" { - cfg.Plugin = channel.Other + if channel.Other != nil { + switch channel.Type { + case channeltype.Azure: + if cfg.APIVersion == "" { + cfg.APIVersion = *channel.Other + } + case channeltype.Xunfei: + if cfg.APIVersion == "" { + cfg.APIVersion = *channel.Other + } + case channeltype.Gemini: + if cfg.APIVersion == "" { + cfg.APIVersion = *channel.Other + } + case channeltype.AIProxyLibrary: + if cfg.LibraryID == "" { + cfg.LibraryID = *channel.Other + } + case channeltype.Ali: + if cfg.Plugin == "" { + cfg.Plugin = *channel.Other + } } } c.Set(ctxkey.Config, cfg) diff --git a/model/channel.go b/model/channel.go index ec52683e..a92f402c 100644 --- a/model/channel.go +++ b/model/channel.go @@ -27,7 +27,7 @@ type Channel struct { TestTime int64 `json:"test_time" gorm:"bigint"` ResponseTime int `json:"response_time"` // in milliseconds BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"` - Other string `json:"other"` // DEPRECATED: please save config to field Config + Other *string `json:"other"` // DEPRECATED: please save config to field Config Balance float64 `json:"balance"` // in USD BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"` Models string `json:"models"` diff --git a/model/user.go b/model/user.go index 1dc633b1..924d72f9 100644 --- a/model/user.go +++ b/model/user.go @@ -6,6 +6,7 @@ import ( "github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common/blacklist" "github.com/songquanpeng/one-api/common/config" + "github.com/songquanpeng/one-api/common/helper" "github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/common/random" "gorm.io/gorm" @@ -140,6 +141,22 @@ func (user *User) Insert(inviterId int) error { RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %s", common.LogQuota(config.QuotaForInviter))) } } + // create default token + cleanToken := Token{ + UserId: user.Id, + Name: "default", + Key: random.GenerateKey(), + CreatedTime: helper.GetTimestamp(), + AccessedTime: helper.GetTimestamp(), + ExpiredTime: -1, + RemainQuota: -1, + UnlimitedQuota: true, + } + result.Error = cleanToken.Insert() + if result.Error != nil { + // do not block + logger.SysError(fmt.Sprintf("create default token for user %d failed: %s", user.Id, result.Error.Error())) + } return nil } diff --git a/relay/adaptor/baidu/main.go b/relay/adaptor/baidu/main.go index 6df5ce84..b816e0f4 100644 --- a/relay/adaptor/baidu/main.go +++ b/relay/adaptor/baidu/main.go @@ -7,9 +7,9 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/common" + "github.com/songquanpeng/one-api/common/client" "github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/relay/adaptor/openai" - "github.com/songquanpeng/one-api/relay/client" "github.com/songquanpeng/one-api/relay/constant" "github.com/songquanpeng/one-api/relay/model" "io" diff --git a/relay/adaptor/cloudflare/main.go b/relay/adaptor/cloudflare/main.go index e85bbc25..f6d496f7 100644 --- a/relay/adaptor/cloudflare/main.go +++ b/relay/adaptor/cloudflare/main.go @@ -17,15 +17,21 @@ import ( ) func ConvertRequest(textRequest model.GeneralOpenAIRequest) *Request { - lastMessage := textRequest.Messages[len(textRequest.Messages)-1] - return &Request{ - MaxTokens: textRequest.MaxTokens, - Prompt: lastMessage.StringContent(), - Stream: textRequest.Stream, - Temperature: textRequest.Temperature, - } + var promptBuilder strings.Builder + for _, message := range textRequest.Messages { + promptBuilder.WriteString(message.StringContent()) + promptBuilder.WriteString("\n") // 添加换行符来分隔每个消息 + } + + return &Request{ + MaxTokens: textRequest.MaxTokens, + Prompt: promptBuilder.String(), + Stream: textRequest.Stream, + Temperature: textRequest.Temperature, + } } + func ResponseCloudflare2OpenAI(cloudflareResponse *Response) *openai.TextResponse { choice := openai.TextResponseChoice{ Index: 0, diff --git a/relay/adaptor/common.go b/relay/adaptor/common.go index 82a5160e..8953d7a3 100644 --- a/relay/adaptor/common.go +++ b/relay/adaptor/common.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "github.com/gin-gonic/gin" - "github.com/songquanpeng/one-api/relay/client" + "github.com/songquanpeng/one-api/common/client" "github.com/songquanpeng/one-api/relay/meta" "io" "net/http" diff --git a/relay/adaptor/doubao/constants.go b/relay/adaptor/doubao/constants.go new file mode 100644 index 00000000..dbe819dd --- /dev/null +++ b/relay/adaptor/doubao/constants.go @@ -0,0 +1,13 @@ +package doubao + +// https://console.volcengine.com/ark/region:ark+cn-beijing/model + +var ModelList = []string{ + "Doubao-pro-128k", + "Doubao-pro-32k", + "Doubao-pro-4k", + "Doubao-lite-128k", + "Doubao-lite-32k", + "Doubao-lite-4k", + "Doubao-embedding", +} diff --git a/relay/adaptor/doubao/main.go b/relay/adaptor/doubao/main.go new file mode 100644 index 00000000..ea26e6ba --- /dev/null +++ b/relay/adaptor/doubao/main.go @@ -0,0 +1,14 @@ +package doubao + +import ( + "fmt" + "github.com/songquanpeng/one-api/relay/meta" + "github.com/songquanpeng/one-api/relay/relaymode" +) + +func GetRequestURL(meta *meta.Meta) (string, error) { + if meta.Mode == relaymode.ChatCompletions { + return fmt.Sprintf("%s/api/v3/chat/completions", meta.BaseURL), nil + } + return "", fmt.Errorf("unsupported relay mode %d for doubao", meta.Mode) +} diff --git a/relay/adaptor/gemini/adaptor.go b/relay/adaptor/gemini/adaptor.go index a4dcae93..12f48c71 100644 --- a/relay/adaptor/gemini/adaptor.go +++ b/relay/adaptor/gemini/adaptor.go @@ -13,6 +13,7 @@ import ( "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" ) type Adaptor struct { @@ -24,7 +25,14 @@ func (a *Adaptor) Init(meta *meta.Meta) { func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { version := helper.AssignOrDefault(meta.Config.APIVersion, config.GeminiVersion) - action := "generateContent" + action := "" + switch meta.Mode { + case relaymode.Embeddings: + action = "batchEmbedContents" + default: + action = "generateContent" + } + if meta.IsStream { action = "streamGenerateContent?alt=sse" } @@ -41,7 +49,14 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G if request == nil { return nil, errors.New("request is nil") } - return ConvertRequest(*request), nil + switch relayMode { + case relaymode.Embeddings: + geminiEmbeddingRequest := ConvertEmbeddingRequest(*request) + return geminiEmbeddingRequest, nil + default: + geminiRequest := ConvertRequest(*request) + return geminiRequest, nil + } } func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) { @@ -61,7 +76,12 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Met err, responseText = StreamHandler(c, resp) usage = openai.ResponseText2Usage(responseText, meta.ActualModelName, meta.PromptTokens) } else { - err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName) + switch meta.Mode { + case relaymode.Embeddings: + err, usage = EmbeddingHandler(c, resp) + default: + err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName) + } } return } diff --git a/relay/adaptor/gemini/constants.go b/relay/adaptor/gemini/constants.go index 32e7c240..f65e6bfc 100644 --- a/relay/adaptor/gemini/constants.go +++ b/relay/adaptor/gemini/constants.go @@ -4,5 +4,5 @@ package gemini var ModelList = []string{ "gemini-pro", "gemini-1.0-pro-001", "gemini-1.5-pro", - "gemini-pro-vision", "gemini-1.0-pro-vision-001", + "gemini-pro-vision", "gemini-1.0-pro-vision-001", "embedding-001", "text-embedding-004", } diff --git a/relay/adaptor/gemini/main.go b/relay/adaptor/gemini/main.go index faccc4cb..74a7d5d5 100644 --- a/relay/adaptor/gemini/main.go +++ b/relay/adaptor/gemini/main.go @@ -134,6 +134,29 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest { return &geminiRequest } +func ConvertEmbeddingRequest(request model.GeneralOpenAIRequest) *BatchEmbeddingRequest { + inputs := request.ParseInput() + requests := make([]EmbeddingRequest, len(inputs)) + model := fmt.Sprintf("models/%s", request.Model) + + for i, input := range inputs { + requests[i] = EmbeddingRequest{ + Model: model, + Content: ChatContent{ + Parts: []Part{ + { + Text: input, + }, + }, + }, + } + } + + return &BatchEmbeddingRequest{ + Requests: requests, + } +} + type ChatResponse struct { Candidates []ChatCandidate `json:"candidates"` PromptFeedback ChatPromptFeedback `json:"promptFeedback"` @@ -222,14 +245,33 @@ func responseGeminiChat2OpenAI(response *ChatResponse) *openai.TextResponse { func streamResponseGeminiChat2OpenAI(geminiResponse *ChatResponse) *openai.ChatCompletionsStreamResponse { var choice openai.ChatCompletionsStreamResponseChoice choice.Delta.Content = geminiResponse.GetResponseText() - choice.FinishReason = &constant.StopFinishReason + //choice.FinishReason = &constant.StopFinishReason var response openai.ChatCompletionsStreamResponse + response.Id = fmt.Sprintf("chatcmpl-%s", random.GetUUID()) + response.Created = helper.GetTimestamp() response.Object = "chat.completion.chunk" response.Model = "gemini" response.Choices = []openai.ChatCompletionsStreamResponseChoice{choice} return &response } +func embeddingResponseGemini2OpenAI(response *EmbeddingResponse) *openai.EmbeddingResponse { + openAIEmbeddingResponse := openai.EmbeddingResponse{ + Object: "list", + Data: make([]openai.EmbeddingResponseItem, 0, len(response.Embeddings)), + Model: "gemini-embedding", + Usage: model.Usage{TotalTokens: 0}, + } + for _, item := range response.Embeddings { + openAIEmbeddingResponse.Data = append(openAIEmbeddingResponse.Data, openai.EmbeddingResponseItem{ + Object: `embedding`, + Index: 0, + Embedding: item.Values, + }) + } + return &openAIEmbeddingResponse +} + func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, string) { responseText := "" scanner := bufio.NewScanner(resp.Body) @@ -337,3 +379,39 @@ func Handler(c *gin.Context, resp *http.Response, promptTokens int, modelName st _, err = c.Writer.Write(jsonResponse) return nil, &usage } + +func EmbeddingHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) { + var geminiEmbeddingResponse EmbeddingResponse + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil + } + err = resp.Body.Close() + if err != nil { + return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil + } + err = json.Unmarshal(responseBody, &geminiEmbeddingResponse) + if err != nil { + return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil + } + if geminiEmbeddingResponse.Error != nil { + return &model.ErrorWithStatusCode{ + Error: model.Error{ + Message: geminiEmbeddingResponse.Error.Message, + Type: "gemini_error", + Param: "", + Code: geminiEmbeddingResponse.Error.Code, + }, + StatusCode: resp.StatusCode, + }, nil + } + fullTextResponse := embeddingResponseGemini2OpenAI(&geminiEmbeddingResponse) + jsonResponse, err := json.Marshal(fullTextResponse) + if err != nil { + return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil + } + c.Writer.Header().Set("Content-Type", "application/json") + c.Writer.WriteHeader(resp.StatusCode) + _, err = c.Writer.Write(jsonResponse) + return nil, &fullTextResponse.Usage +} diff --git a/relay/adaptor/gemini/model.go b/relay/adaptor/gemini/model.go index 47b74fbc..f7179ea4 100644 --- a/relay/adaptor/gemini/model.go +++ b/relay/adaptor/gemini/model.go @@ -7,6 +7,33 @@ type ChatRequest struct { Tools []ChatTools `json:"tools,omitempty"` } +type EmbeddingRequest struct { + Model string `json:"model"` + Content ChatContent `json:"content"` + TaskType string `json:"taskType,omitempty"` + Title string `json:"title,omitempty"` + OutputDimensionality int `json:"outputDimensionality,omitempty"` +} + +type BatchEmbeddingRequest struct { + Requests []EmbeddingRequest `json:"requests"` +} + +type EmbeddingData struct { + Values []float64 `json:"values"` +} + +type EmbeddingResponse struct { + Embeddings []EmbeddingData `json:"embeddings"` + Error *Error `json:"error,omitempty"` +} + +type Error struct { + Code int `json:"code,omitempty"` + Message string `json:"message,omitempty"` + Status string `json:"status,omitempty"` +} + type InlineData struct { MimeType string `json:"mimeType"` Data string `json:"data"` diff --git a/relay/adaptor/openai/adaptor.go b/relay/adaptor/openai/adaptor.go index 2e2e4100..db569e4f 100644 --- a/relay/adaptor/openai/adaptor.go +++ b/relay/adaptor/openai/adaptor.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/relay/adaptor" + "github.com/songquanpeng/one-api/relay/adaptor/doubao" "github.com/songquanpeng/one-api/relay/adaptor/minimax" "github.com/songquanpeng/one-api/relay/channeltype" "github.com/songquanpeng/one-api/relay/meta" @@ -45,6 +46,8 @@ func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { return GetFullRequestURL(meta.BaseURL, requestURL, meta.ChannelType), nil case channeltype.Minimax: return minimax.GetRequestURL(meta) + case channeltype.Doubao: + return doubao.GetRequestURL(meta) default: return GetFullRequestURL(meta.BaseURL, meta.RequestURLPath, meta.ChannelType), nil } diff --git a/relay/adaptor/openai/compatible.go b/relay/adaptor/openai/compatible.go index 0116a2eb..5d5b4008 100644 --- a/relay/adaptor/openai/compatible.go +++ b/relay/adaptor/openai/compatible.go @@ -4,6 +4,7 @@ import ( "github.com/songquanpeng/one-api/relay/adaptor/ai360" "github.com/songquanpeng/one-api/relay/adaptor/baichuan" "github.com/songquanpeng/one-api/relay/adaptor/deepseek" + "github.com/songquanpeng/one-api/relay/adaptor/doubao" "github.com/songquanpeng/one-api/relay/adaptor/groq" "github.com/songquanpeng/one-api/relay/adaptor/lingyiwanwu" "github.com/songquanpeng/one-api/relay/adaptor/minimax" @@ -20,6 +21,7 @@ var CompatibleChannels = []int{ channeltype.Moonshot, channeltype.Baichuan, channeltype.Minimax, + channeltype.Doubao, channeltype.Mistral, channeltype.Groq, channeltype.LingYiWanWu, @@ -52,6 +54,8 @@ func GetCompatibleChannelMeta(channelType int) (string, []string) { return "deepseek", deepseek.ModelList case channeltype.TogetherAI: return "together.ai", togetherai.ModelList + case channeltype.Doubao: + return "doubao", doubao.ModelList default: return "openai", ModelList } diff --git a/relay/adaptor/openai/token.go b/relay/adaptor/openai/token.go index bb9c38a9..ddbfad86 100644 --- a/relay/adaptor/openai/token.go +++ b/relay/adaptor/openai/token.go @@ -24,6 +24,10 @@ func InitTokenEncoders() { logger.FatalLog(fmt.Sprintf("failed to get gpt-3.5-turbo token encoder: %s", err.Error())) } defaultTokenEncoder = gpt35TokenEncoder + gpt4oTokenEncoder, err := tiktoken.EncodingForModel("gpt-4o") + if err != nil { + logger.FatalLog(fmt.Sprintf("failed to get gpt-4o token encoder: %s", err.Error())) + } gpt4TokenEncoder, err := tiktoken.EncodingForModel("gpt-4") if err != nil { logger.FatalLog(fmt.Sprintf("failed to get gpt-4 token encoder: %s", err.Error())) @@ -31,6 +35,8 @@ func InitTokenEncoders() { for model := range billingratio.ModelRatio { if strings.HasPrefix(model, "gpt-3.5") { tokenEncoderMap[model] = gpt35TokenEncoder + } else if strings.HasPrefix(model, "gpt-4o") { + tokenEncoderMap[model] = gpt4oTokenEncoder } else if strings.HasPrefix(model, "gpt-4") { tokenEncoderMap[model] = gpt4TokenEncoder } else { diff --git a/relay/adaptor/tencent/adaptor.go b/relay/adaptor/tencent/adaptor.go index a97476d6..0de92d4a 100644 --- a/relay/adaptor/tencent/adaptor.go +++ b/relay/adaptor/tencent/adaptor.go @@ -2,35 +2,43 @@ package tencent import ( "errors" - "fmt" "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" "io" "net/http" + "strconv" "strings" ) // https://cloud.tencent.com/document/api/1729/101837 type Adaptor struct { - Sign string + Sign string + Action string + Version string + Timestamp int64 } 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) { - 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 { adaptor.SetupCommonRequestHeader(c, req, meta) 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 } @@ -40,15 +48,13 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G } apiKey := c.Request.Header.Get("Authorization") apiKey = strings.TrimPrefix(apiKey, "Bearer ") - appId, secretId, secretKey, err := ParseConfig(apiKey) + _, secretId, secretKey, err := ParseConfig(apiKey) if err != nil { return nil, err } tencentRequest := ConvertRequest(*request) - tencentRequest.AppId = appId - tencentRequest.SecretId = secretId // we have to calculate the sign here - a.Sign = GetSign(*tencentRequest, secretKey) + a.Sign = GetSign(*tencentRequest, a, secretId, secretKey) return tencentRequest, nil } diff --git a/relay/adaptor/tencent/constants.go b/relay/adaptor/tencent/constants.go index fe176c2c..be415a94 100644 --- a/relay/adaptor/tencent/constants.go +++ b/relay/adaptor/tencent/constants.go @@ -1,7 +1,8 @@ package tencent var ModelList = []string{ - "ChatPro", - "ChatStd", - "hunyuan", + "hunyuan-lite", + "hunyuan-standard", + "hunyuan-standard-256K", + "hunyuan-pro", } diff --git a/relay/adaptor/tencent/main.go b/relay/adaptor/tencent/main.go index 2ca5724e..0a57dcf7 100644 --- a/relay/adaptor/tencent/main.go +++ b/relay/adaptor/tencent/main.go @@ -3,8 +3,8 @@ package tencent import ( "bufio" "crypto/hmac" - "crypto/sha1" - "encoding/base64" + "crypto/sha256" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -19,34 +19,26 @@ import ( "github.com/songquanpeng/one-api/relay/model" "io" "net/http" - "sort" "strconv" "strings" + "time" ) -// https://cloud.tencent.com/document/product/1729/97732 - 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++ { message := request.Messages[i] - messages = append(messages, Message{ + messages = append(messages, &Message{ Content: message.StringContent(), Role: message.Role, }) } - stream := 0 - if request.Stream { - stream = 1 - } return &ChatRequest{ - Timestamp: helper.GetTimestamp(), - Expired: helper.GetTimestamp() + 24*60*60, - QueryID: random.GetUUID(), - Temperature: request.Temperature, - TopP: request.TopP, - Stream: stream, + Model: &request.Model, + Stream: &request.Stream, Messages: messages, + TopP: &request.TopP, + Temperature: &request.Temperature, } } @@ -54,7 +46,11 @@ func responseTencent2OpenAI(response *ChatResponse) *openai.TextResponse { fullTextResponse := openai.TextResponse{ Object: "chat.completion", 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 { choice := openai.TextResponseChoice{ @@ -154,6 +150,7 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) { var TencentResponse ChatResponse + var responseP ChatResponseP responseBody, err := io.ReadAll(resp.Body) if err != nil { return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil @@ -162,10 +159,11 @@ func Handler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, * if err != 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 { return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil } + TencentResponse = responseP.Response if TencentResponse.Error.Code != 0 { return &model.ErrorWithStatusCode{ Error: model.Error{ @@ -202,29 +200,62 @@ func ParseConfig(config string) (appId int64, secretId string, secretKey string, return } -func GetSign(req ChatRequest, secretKey string) string { - params := make([]string, 0) - params = append(params, "app_id="+strconv.FormatInt(req.AppId, 10)) - params = append(params, "secret_id="+req.SecretId) - params = append(params, "timestamp="+strconv.FormatInt(req.Timestamp, 10)) - params = append(params, "query_id="+req.QueryID) - params = append(params, "temperature="+strconv.FormatFloat(req.Temperature, 'f', -1, 64)) - params = append(params, "top_p="+strconv.FormatFloat(req.TopP, 'f', -1, 64)) - params = append(params, "stream="+strconv.Itoa(req.Stream)) - params = append(params, "expired="+strconv.FormatInt(req.Expired, 10)) - - var messageStr string - for _, msg := range req.Messages { - messageStr += fmt.Sprintf(`{"role":"%s","content":"%s"},`, msg.Role, msg.Content) - } - messageStr = strings.TrimSuffix(messageStr, ",") - params = append(params, "messages=["+messageStr+"]") - - sort.Strings(params) - url := "hunyuan.cloud.tencent.com/hyllm/v1/chat/completions?" + strings.Join(params, "&") - mac := hmac.New(sha1.New, []byte(secretKey)) - signURL := url - mac.Write([]byte(signURL)) - sign := mac.Sum([]byte(nil)) - return base64.StdEncoding.EncodeToString(sign) +func sha256hex(s string) string { + b := sha256.Sum256([]byte(s)) + return hex.EncodeToString(b[:]) +} + +func hmacSha256(s, key string) string { + hashed := hmac.New(sha256.New, []byte(key)) + hashed.Write([]byte(s)) + return string(hashed.Sum(nil)) +} + +func GetSign(req ChatRequest, adaptor *Adaptor, secId, secKey string) string { + // build canonical request string + host := "hunyuan.tencentcloudapi.com" + httpRequestMethod := "POST" + canonicalURI := "/" + canonicalQueryString := "" + 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, + 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 } diff --git a/relay/adaptor/tencent/model.go b/relay/adaptor/tencent/model.go index 71286be9..fb97724e 100644 --- a/relay/adaptor/tencent/model.go +++ b/relay/adaptor/tencent/model.go @@ -1,63 +1,75 @@ package tencent -import ( - "github.com/songquanpeng/one-api/relay/model" -) - type Message struct { - Role string `json:"role"` - Content string `json:"content"` + Role string `json:"Role"` + Content string `json:"Content"` } type ChatRequest struct { - AppId int64 `json:"app_id"` // 腾讯云账号的 APPID - SecretId string `json:"secret_id"` // 官网 SecretId - // Timestamp当前 UNIX 时间戳,单位为秒,可记录发起 API 请求的时间。 - // 例如1529223702,如果与当前时间相差过大,会引起签名过期错误 - Timestamp int64 `json:"timestamp"` - // Expired 签名的有效期,是一个符合 UNIX Epoch 时间戳规范的数值, - // 单位为秒;Expired 必须大于 Timestamp 且 Expired-Timestamp 小于90天 - Expired int64 `json:"expired"` - QueryID string `json:"query_id"` //请求 Id,用于问题排查 - // Temperature 较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定 - // 默认 1.0,取值区间为[0.0,2.0],非必要不建议使用,不合理的取值会影响效果 - // 建议该参数和 top_p 只设置1个,不要同时更改 top_p - Temperature float64 `json:"temperature"` - // TopP 影响输出文本的多样性,取值越大,生成文本的多样性越强 - // 默认1.0,取值区间为[0.0, 1.0],非必要不建议使用, 不合理的取值会影响效果 - // 建议该参数和 temperature 只设置1个,不要同时更改 - TopP float64 `json:"top_p"` - // Stream 0:同步,1:流式 (默认,协议:SSE) - // 同步请求超时:60s,如果内容较长建议使用流式 - Stream int `json:"stream"` - // Messages 会话内容, 长度最多为40, 按对话时间从旧到新在数组中排列 - // 输入 content 总数最大支持 3000 token。 - Messages []Message `json:"messages"` + // 模型名称,可选值包括 hunyuan-lite、hunyuan-standard、hunyuan-standard-256K、hunyuan-pro。 + // 各模型介绍请阅读 [产品概述](https://cloud.tencent.com/document/product/1729/104753) 中的说明。 + // + // 注意: + // 不同的模型计费不同,请根据 [购买指南](https://cloud.tencent.com/document/product/1729/97731) 按需调用。 + Model *string `json:"Model"` + // 聊天上下文信息。 + // 说明: + // 1. 长度最多为 40,按对话时间从旧到新在数组中排列。 + // 2. Message.Role 可选值:system、user、assistant。 + // 其中,system 角色可选,如存在则必须位于列表的最开始。user 和 assistant 需交替出现(一问一答),以 user 提问开始和结束,且 Content 不能为空。Role 的顺序示例:[system(可选) user assistant user assistant user ...]。 + // 3. Messages 中 Content 总长度不能超过模型输入长度上限(可参考 [产品概述](https://cloud.tencent.com/document/product/1729/104753) 文档),超过则会截断最前面的内容,只保留尾部内容。 + Messages []*Message `json:"Messages"` + // 流式调用开关。 + // 说明: + // 1. 未传值时默认为非流式调用(false)。 + // 2. 流式调用时以 SSE 协议增量返回结果(返回值取 Choices[n].Delta 中的值,需要拼接增量数据才能获得完整结果)。 + // 3. 非流式调用时: + // 调用方式与普通 HTTP 请求无异。 + // 接口响应耗时较长,**如需更低时延建议设置为 true**。 + // 只返回一次最终结果(返回值取 Choices[n].Message 中的值)。 + // + // 注意: + // 通过 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 { - Code int `json:"code"` - Message string `json:"message"` + Code int `json:"Code"` + Message string `json:"Message"` } type Usage struct { - InputTokens int `json:"input_tokens"` - OutputTokens int `json:"output_tokens"` - TotalTokens int `json:"total_tokens"` + PromptTokens int `json:"PromptTokens"` + CompletionTokens int `json:"CompletionTokens"` + TotalTokens int `json:"TotalTokens"` } type ResponseChoices struct { - FinishReason string `json:"finish_reason,omitempty"` // 流式结束标志位,为 stop 则表示尾包 - Messages Message `json:"messages,omitempty"` // 内容,同步模式返回内容,流模式为 null 输出 content 内容总数最多支持 1024token。 - Delta Message `json:"delta,omitempty"` // 内容,流模式返回内容,同步模式为 null 输出 content 内容总数最多支持 1024token。 + FinishReason string `json:"FinishReason,omitempty"` // 流式结束标志位,为 stop 则表示尾包 + Messages Message `json:"Message,omitempty"` // 内容,同步模式返回内容,流模式为 null 输出 content 内容总数最多支持 1024token。 + Delta Message `json:"Delta,omitempty"` // 内容,流模式返回内容,同步模式为 null 输出 content 内容总数最多支持 1024token。 } type ChatResponse struct { - Choices []ResponseChoices `json:"choices,omitempty"` // 结果 - Created string `json:"created,omitempty"` // unix 时间戳的字符串 - Id string `json:"id,omitempty"` // 会话 id - Usage model.Usage `json:"usage,omitempty"` // token 数量 - Error Error `json:"error,omitempty"` // 错误信息 注意:此字段可能返回 null,表示取不到有效值 - Note string `json:"note,omitempty"` // 注释 - ReqID string `json:"req_id,omitempty"` // 唯一请求 Id,每次请求都会返回。用于反馈接口入参 + Choices []ResponseChoices `json:"Choices,omitempty"` // 结果 + Created int64 `json:"Created,omitempty"` // unix 时间戳的字符串 + Id string `json:"Id,omitempty"` // 会话 id + Usage Usage `json:"Usage,omitempty"` // token 数量 + Error Error `json:"Error,omitempty"` // 错误信息 注意:此字段可能返回 null,表示取不到有效值 + Note string `json:"Note,omitempty"` // 注释 + ReqID string `json:"Req_id,omitempty"` // 唯一请求 Id,每次请求都会返回。用于反馈接口入参 +} + +type ChatResponseP struct { + Response ChatResponse `json:"Response,omitempty"` } diff --git a/relay/adaptor/xunfei/adaptor.go b/relay/adaptor/xunfei/adaptor.go index 3af97831..b5967f26 100644 --- a/relay/adaptor/xunfei/adaptor.go +++ b/relay/adaptor/xunfei/adaptor.go @@ -27,14 +27,6 @@ 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 } @@ -69,6 +61,14 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Met if a.request == nil { return nil, openai.ErrorWrapper(errors.New("request is nil"), "request_is_nil", http.StatusBadRequest) } + version := parseAPIVersionByModelName(meta.ActualModelName) + if version == "" { + version = a.meta.Config.APIVersion + } + if version == "" { + version = "v1.1" + } + a.meta.Config.APIVersion = version if meta.IsStream { err, usage = StreamHandler(c, meta, *a.request, splits[0], splits[1], splits[2]) } else { diff --git a/relay/adaptor/xunfei/main.go b/relay/adaptor/xunfei/main.go index c3e768b7..39b76e27 100644 --- a/relay/adaptor/xunfei/main.go +++ b/relay/adaptor/xunfei/main.go @@ -5,7 +5,14 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" + "errors" "fmt" + "io" + "net/http" + "net/url" + "strings" + "time" + "github.com/gin-gonic/gin" "github.com/gorilla/websocket" "github.com/songquanpeng/one-api/common" @@ -16,11 +23,6 @@ import ( "github.com/songquanpeng/one-api/relay/constant" "github.com/songquanpeng/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/model" - "io" - "net/http" - "net/url" - "strings" - "time" ) // https://console.xfyun.cn/services/cbm @@ -28,11 +30,7 @@ import ( func requestOpenAI2Xunfei(request model.GeneralOpenAIRequest, xunfeiAppId string, domain string) *ChatRequest { messages := make([]Message, 0, len(request.Messages)) - var lastToolCalls []model.Tool for _, message := range request.Messages { - if message.ToolCalls != nil { - lastToolCalls = message.ToolCalls - } messages = append(messages, Message{ Role: message.Role, Content: message.StringContent(), @@ -45,9 +43,14 @@ func requestOpenAI2Xunfei(request model.GeneralOpenAIRequest, xunfeiAppId string xunfeiRequest.Parameter.Chat.TopK = request.N xunfeiRequest.Parameter.Chat.MaxTokens = request.MaxTokens xunfeiRequest.Payload.Message.Text = messages - if len(lastToolCalls) != 0 { - for _, toolCall := range lastToolCalls { - xunfeiRequest.Payload.Functions.Text = append(xunfeiRequest.Payload.Functions.Text, toolCall.Function) + + 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{ + Text: functions, } } @@ -203,7 +206,7 @@ func Handler(c *gin.Context, meta *meta.Meta, textRequest model.GeneralOpenAIReq } } if len(xunfeiResponse.Payload.Choices.Text) == 0 { - return openai.ErrorWrapper(err, "xunfei_empty_response_detected", http.StatusInternalServerError), nil + return openai.ErrorWrapper(errors.New("xunfei empty response detected"), "xunfei_empty_response_detected", http.StatusInternalServerError), nil } xunfeiResponse.Payload.Choices.Text[0].Content = content diff --git a/relay/adaptor/xunfei/model.go b/relay/adaptor/xunfei/model.go index 97a43154..1f37c046 100644 --- a/relay/adaptor/xunfei/model.go +++ b/relay/adaptor/xunfei/model.go @@ -9,6 +9,10 @@ type Message struct { Content string `json:"content"` } +type Functions struct { + Text []model.Function `json:"text,omitempty"` +} + type ChatRequest struct { Header struct { AppId string `json:"app_id"` @@ -26,9 +30,7 @@ type ChatRequest struct { Message struct { Text []Message `json:"text"` } `json:"message"` - Functions struct { - Text []model.Function `json:"text,omitempty"` - } `json:"functions,omitempty"` + Functions *Functions `json:"functions,omitempty"` } `json:"payload"` } diff --git a/relay/billing/ratio/image.go b/relay/billing/ratio/image.go index 5a29cddc..ced0c667 100644 --- a/relay/billing/ratio/image.go +++ b/relay/billing/ratio/image.go @@ -49,3 +49,8 @@ var ImagePromptLengthLimitations = map[string]int{ "wanx-v1": 4000, "cogview-3": 833, } + +var ImageOriginModelName = map[string]string{ + "ali-stable-diffusion-xl": "stable-diffusion-xl", + "ali-stable-diffusion-v1.5": "stable-diffusion-v1.5", +} diff --git a/relay/channeltype/define.go b/relay/channeltype/define.go index 60964565..d8885ae9 100644 --- a/relay/channeltype/define.go +++ b/relay/channeltype/define.go @@ -41,6 +41,6 @@ const ( Cloudflare DeepL TogetherAI - + Doubao Dummy ) diff --git a/relay/channeltype/url.go b/relay/channeltype/url.go index f5767f47..513d183b 100644 --- a/relay/channeltype/url.go +++ b/relay/channeltype/url.go @@ -24,7 +24,7 @@ var ChannelBaseURLs = []string{ "https://openrouter.ai/api", // 20 "https://api.aiproxy.io", // 21 "https://fastgpt.run/api/openapi", // 22 - "https://hunyuan.cloud.tencent.com", // 23 + "https://hunyuan.tencentcloudapi.com", // 23 "https://generativelanguage.googleapis.com", // 24 "https://api.moonshot.cn", // 25 "https://api.baichuan-ai.com", // 26 @@ -41,6 +41,7 @@ var ChannelBaseURLs = []string{ "https://api.cloudflare.com", // 37 "https://api-free.deepl.com", // 38 "https://api.together.xyz", // 39 + "https://ark.cn-beijing.volces.com", // 40 } func init() { diff --git a/relay/client/init.go b/relay/client/init.go deleted file mode 100644 index 4b59cba7..00000000 --- a/relay/client/init.go +++ /dev/null @@ -1,24 +0,0 @@ -package client - -import ( - "github.com/songquanpeng/one-api/common/config" - "net/http" - "time" -) - -var HTTPClient *http.Client -var ImpatientHTTPClient *http.Client - -func init() { - if config.RelayTimeout == 0 { - HTTPClient = &http.Client{} - } else { - HTTPClient = &http.Client{ - Timeout: time.Duration(config.RelayTimeout) * time.Second, - } - } - - ImpatientHTTPClient = &http.Client{ - Timeout: 5 * time.Second, - } -} diff --git a/relay/controller/audio.go b/relay/controller/audio.go index 15e74290..8f9708d0 100644 --- a/relay/controller/audio.go +++ b/relay/controller/audio.go @@ -9,6 +9,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/songquanpeng/one-api/common" + "github.com/songquanpeng/one-api/common/client" "github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/logger" @@ -17,7 +18,6 @@ import ( "github.com/songquanpeng/one-api/relay/billing" billingratio "github.com/songquanpeng/one-api/relay/billing/ratio" "github.com/songquanpeng/one-api/relay/channeltype" - "github.com/songquanpeng/one-api/relay/client" "github.com/songquanpeng/one-api/relay/meta" relaymodel "github.com/songquanpeng/one-api/relay/model" "github.com/songquanpeng/one-api/relay/relaymode" diff --git a/relay/controller/image.go b/relay/controller/image.go index 6620bef5..691c7c0e 100644 --- a/relay/controller/image.go +++ b/relay/controller/image.go @@ -55,6 +55,11 @@ func RelayImageHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatus return openai.ErrorWrapper(err, "get_image_cost_ratio_failed", http.StatusInternalServerError) } + imageModel := imageRequest.Model + // Convert the original image model + imageRequest.Model, _ = getMappedModelName(imageRequest.Model, billingratio.ImageOriginModelName) + c.Set("response_format", imageRequest.ResponseFormat) + var requestBody io.Reader if isModelMapped || meta.ChannelType == channeltype.Azure { // make Azure channel request body jsonStr, err := json.Marshal(imageRequest) @@ -89,7 +94,7 @@ func RelayImageHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatus requestBody = bytes.NewBuffer(jsonStr) } - modelRatio := billingratio.GetModelRatio(imageRequest.Model) + modelRatio := billingratio.GetModelRatio(imageModel) groupRatio := billingratio.GetGroupRatio(meta.Group) ratio := modelRatio * groupRatio userQuota, err := model.CacheGetUserQuota(ctx, meta.UserId) diff --git a/web/berry/package.json b/web/berry/package.json index 2edb2355..f8265ef7 100644 --- a/web/berry/package.json +++ b/web/berry/package.json @@ -26,7 +26,7 @@ "notistack": "^3.0.1", "prop-types": "^15.8.1", "react": "^18.2.0", - "react-apexcharts": "^1.4.0", + "react-apexcharts": "1.4.0", "react-device-detect": "^2.2.2", "react-dom": "^18.2.0", "react-perfect-scrollbar": "^1.5.8", diff --git a/web/berry/src/constants/ChannelConstants.js b/web/berry/src/constants/ChannelConstants.js index e6b0aed5..52d64083 100644 --- a/web/berry/src/constants/ChannelConstants.js +++ b/web/berry/src/constants/ChannelConstants.js @@ -11,12 +11,18 @@ export const CHANNEL_OPTIONS = { value: 14, color: 'primary' }, - // 33: { - // key: 33, - // text: 'AWS Claude', - // value: 33, - // color: 'primary' - // }, + 33: { + key: 33, + text: 'AWS Claude', + value: 33, + color: 'primary' + }, + 37: { + key: 37, + text: 'Cloudflare', + value: 37, + color: 'success' + }, 3: { key: 3, text: 'Azure OpenAI', @@ -41,6 +47,12 @@ export const CHANNEL_OPTIONS = { value: 28, color: 'warning' }, + 40: { + key: 40, + text: '字节跳动豆包', + value: 40, + color: 'primary' + }, 15: { key: 15, text: '百度文心千帆', @@ -119,12 +131,12 @@ export const CHANNEL_OPTIONS = { value: 32, color: 'primary' }, - // 34: { - // key: 34, - // text: 'Coze', - // value: 34, - // color: 'primary' - // }, + 34: { + key: 34, + text: 'Coze', + value: 34, + color: 'primary' + }, 35: { key: 35, text: 'Cohere', diff --git a/web/berry/src/constants/SnackbarConstants.js b/web/berry/src/constants/SnackbarConstants.js index 19523da1..05f79231 100644 --- a/web/berry/src/constants/SnackbarConstants.js +++ b/web/berry/src/constants/SnackbarConstants.js @@ -1,24 +1,56 @@ +import { closeSnackbar } from 'notistack'; +import { IconX } from '@tabler/icons-react'; +import { IconButton } from '@mui/material'; +const action = (snackbarId) => ( + <> + { + closeSnackbar(snackbarId); + }} + > + + + +); + export const snackbarConstants = { Common: { ERROR: { variant: 'error', - autoHideDuration: 5000 + autoHideDuration: 5000, + preventDuplicate: true, + action }, WARNING: { variant: 'warning', - autoHideDuration: 10000 + autoHideDuration: 10000, + preventDuplicate: true, + action }, SUCCESS: { variant: 'success', - autoHideDuration: 1500 + autoHideDuration: 1500, + preventDuplicate: true, + action }, INFO: { variant: 'info', - autoHideDuration: 3000 + autoHideDuration: 3000, + preventDuplicate: true, + action }, NOTICE: { variant: 'info', - autoHideDuration: 7000 + autoHideDuration: 20000, + preventDuplicate: true, + action + }, + COPY: { + variant: 'copy', + persist: true, + preventDuplicate: true, + allowDownload: true, + action } }, Mobile: { diff --git a/web/berry/src/utils/common.js b/web/berry/src/utils/common.js index 947df3bf..d74d032e 100644 --- a/web/berry/src/utils/common.js +++ b/web/berry/src/utils/common.js @@ -193,3 +193,40 @@ export function removeTrailingSlash(url) { return url; } } + +let channelModels = undefined; +export async function loadChannelModels() { + const res = await API.get('/api/models'); + const { success, data } = res.data; + if (!success) { + return; + } + channelModels = data; + localStorage.setItem('channel_models', JSON.stringify(data)); +} + +export function getChannelModels(type) { + if (channelModels !== undefined && type in channelModels) { + return channelModels[type]; + } + let models = localStorage.getItem('channel_models'); + if (!models) { + return []; + } + channelModels = JSON.parse(models); + if (type in channelModels) { + return channelModels[type]; + } + return []; +} + +export function copy(text, name = '') { + try { + navigator.clipboard.writeText(text); + } catch (error) { + text = `复制${name}失败,请手动复制:

${text}`; + enqueueSnackbar(, getSnackbarOptions('COPY')); + return; + } + showSuccess(`复制${name}成功!`); +} diff --git a/web/berry/src/views/Authentication/AuthForms/ResetPasswordForm.js b/web/berry/src/views/Authentication/AuthForms/ResetPasswordForm.js index eaa8dc95..a9f0f9e3 100644 --- a/web/berry/src/views/Authentication/AuthForms/ResetPasswordForm.js +++ b/web/berry/src/views/Authentication/AuthForms/ResetPasswordForm.js @@ -1,22 +1,22 @@ -import { useState, useEffect } from "react"; -import { useSearchParams } from "react-router-dom"; +import { useState, useEffect } from 'react'; +import { useSearchParams } from 'react-router-dom'; // material-ui -import { Button, Stack, Typography, Alert } from "@mui/material"; +import { Button, Stack, Typography, Alert } from '@mui/material'; // assets -import { showError, showInfo } from "utils/common"; -import { API } from "utils/api"; +import { showError, copy } from 'utils/common'; +import { API } from 'utils/api'; // ===========================|| FIREBASE - REGISTER ||=========================== // const ResetPasswordForm = () => { const [searchParams] = useSearchParams(); const [inputs, setInputs] = useState({ - email: "", - token: "", + email: '', + token: '' }); - const [newPassword, setNewPassword] = useState(""); + const [newPassword, setNewPassword] = useState(''); const submit = async () => { const res = await API.post(`/api/user/reset`, inputs); @@ -24,31 +24,25 @@ const ResetPasswordForm = () => { if (success) { let password = res.data.data; setNewPassword(password); - navigator.clipboard.writeText(password); - showInfo(`新密码已复制到剪贴板:${password}`); + copy(password, '新密码'); } else { showError(message); } }; useEffect(() => { - let email = searchParams.get("email"); - let token = searchParams.get("token"); + let email = searchParams.get('email'); + let token = searchParams.get('token'); setInputs({ token, - email, + email }); }, []); return ( - + {!inputs.email || !inputs.token ? ( - + 无效的链接 ) : newPassword ? ( @@ -57,14 +51,7 @@ const ResetPasswordForm = () => { 请登录后及时修改密码 ) : ( - )} diff --git a/web/berry/src/views/Channel/component/EditModal.js b/web/berry/src/views/Channel/component/EditModal.js index 03b4df57..29bd3de2 100644 --- a/web/berry/src/views/Channel/component/EditModal.js +++ b/web/berry/src/views/Channel/component/EditModal.js @@ -1,9 +1,9 @@ -import PropTypes from "prop-types"; -import { useState, useEffect } from "react"; -import { CHANNEL_OPTIONS } from "constants/ChannelConstants"; -import { useTheme } from "@mui/material/styles"; -import { API } from "utils/api"; -import { showError, showSuccess } from "utils/common"; +import PropTypes from 'prop-types'; +import { useState, useEffect } from 'react'; +import { CHANNEL_OPTIONS } from 'constants/ChannelConstants'; +import { useTheme } from '@mui/material/styles'; +import { API } from 'utils/api'; +import { showError, showSuccess, getChannelModels } from 'utils/common'; import { Dialog, DialogTitle, @@ -22,15 +22,15 @@ import { Autocomplete, FormHelperText, Switch, - Checkbox, -} from "@mui/material"; + Checkbox +} from '@mui/material'; -import { Formik } from "formik"; -import * as Yup from "yup"; -import { defaultConfig, typeConfig } from "../type/Config"; //typeConfig -import { createFilterOptions } from "@mui/material/Autocomplete"; -import CheckBoxOutlineBlankIcon from "@mui/icons-material/CheckBoxOutlineBlank"; -import CheckBoxIcon from "@mui/icons-material/CheckBox"; +import { Formik } from 'formik'; +import * as Yup from 'yup'; +import { defaultConfig, typeConfig } from '../type/Config'; //typeConfig +import { createFilterOptions } from '@mui/material/Autocomplete'; +import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank'; +import CheckBoxIcon from '@mui/icons-material/CheckBox'; const icon = ; const checkedIcon = ; @@ -38,38 +38,34 @@ const checkedIcon = ; const filter = createFilterOptions(); const validationSchema = Yup.object().shape({ is_edit: Yup.boolean(), - name: Yup.string().required("名称 不能为空"), - type: Yup.number().required("渠道 不能为空"), - key: Yup.string().when("is_edit", { - is: false, - then: Yup.string().required("密钥 不能为空"), + name: Yup.string().required('名称 不能为空'), + type: Yup.number().required('渠道 不能为空'), + key: Yup.string().when(['is_edit', 'type'], { + is: (is_edit, type) => !is_edit && type !== 33, + then: Yup.string().required('密钥 不能为空') }), other: Yup.string(), - models: Yup.array().min(1, "模型 不能为空"), - groups: Yup.array().min(1, "用户组 不能为空"), - base_url: Yup.string().when("type", { + models: Yup.array().min(1, '模型 不能为空'), + groups: Yup.array().min(1, '用户组 不能为空'), + base_url: Yup.string().when('type', { is: (value) => [3, 8].includes(value), - then: Yup.string().required("渠道API地址 不能为空"), // base_url 是必需的 - otherwise: Yup.string(), // 在其他情况下,base_url 可以是任意字符串 + then: Yup.string().required('渠道API地址 不能为空'), // base_url 是必需的 + otherwise: Yup.string() // 在其他情况下,base_url 可以是任意字符串 }), - model_mapping: Yup.string().test( - "is-json", - "必须是有效的JSON字符串", - function (value) { - try { - if (value === "" || value === null || value === undefined) { - return true; - } - const parsedValue = JSON.parse(value); - if (typeof parsedValue === "object") { - return true; - } - } catch (e) { - return false; + model_mapping: Yup.string().test('is-json', '必须是有效的JSON字符串', function (value) { + try { + if (value === '' || value === null || value === undefined) { + return true; } + const parsedValue = JSON.parse(value); + if (typeof parsedValue === 'object') { + return true; + } + } catch (e) { return false; } - ), + return false; + }) }); const EditModal = ({ open, channelId, onCancel, onOk }) => { @@ -81,12 +77,13 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { const [groupOptions, setGroupOptions] = useState([]); const [modelOptions, setModelOptions] = useState([]); const [batchAdd, setBatchAdd] = useState(false); + const [basicModels, setBasicModels] = useState([]); const initChannel = (typeValue) => { if (typeConfig[typeValue]?.inputLabel) { setInputLabel({ ...defaultConfig.inputLabel, - ...typeConfig[typeValue].inputLabel, + ...typeConfig[typeValue].inputLabel }); } else { setInputLabel(defaultConfig.inputLabel); @@ -95,7 +92,7 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { if (typeConfig[typeValue]?.prompt) { setInputPrompt({ ...defaultConfig.prompt, - ...typeConfig[typeValue].prompt, + ...typeConfig[typeValue].prompt }); } else { setInputPrompt(defaultConfig.prompt); @@ -104,40 +101,14 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { return typeConfig[typeValue]?.input; }; const handleTypeChange = (setFieldValue, typeValue, values) => { - const newInput = initChannel(typeValue); - - if (newInput) { - Object.keys(newInput).forEach((key) => { - if ( - (!Array.isArray(values[key]) && - values[key] !== null && - values[key] !== undefined && - values[key] !== "") || - (Array.isArray(values[key]) && values[key].length > 0) - ) { - return; - } - - if (key === "models") { - setFieldValue(key, initialModel(newInput[key])); - return; - } - setFieldValue(key, newInput[key]); - }); + initChannel(typeValue); + let localModels = getChannelModels(typeValue); + setBasicModels(localModels); + if (localModels.length > 0 && Array.isArray(values['models']) && values['models'].length == 0) { + setFieldValue('models', initialModel(localModels)); } - }; - const basicModels = (channelType) => { - let modelGroup = - typeConfig[channelType]?.modelGroup || defaultConfig.modelGroup; - // 循环 modelOptions,找到 modelGroup 对应的模型 - let modelList = []; - modelOptions.forEach((model) => { - if (model.group === modelGroup) { - modelList.push(model); - } - }); - return modelList; + setFieldValue('config', {}); }; const fetchGroups = async () => { @@ -155,7 +126,7 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { const { data } = res.data; data.forEach((item) => { if (!item.owned_by) { - item.owned_by = "未知"; + item.owned_by = '未知'; } }); // 先对data排序 @@ -171,7 +142,7 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { data.map((model) => { return { id: model.id, - group: model.owned_by, + group: model.owned_by }; }) ); @@ -182,33 +153,41 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { const submit = async (values, { setErrors, setStatus, setSubmitting }) => { setSubmitting(true); - if (values.base_url && values.base_url.endsWith("/")) { + if (values.base_url && values.base_url.endsWith('/')) { values.base_url = values.base_url.slice(0, values.base_url.length - 1); } - if (values.type === 3 && values.other === "") { - values.other = "2023-09-01-preview"; + if (values.type === 3 && values.other === '') { + values.other = '2023-09-01-preview'; } - if (values.type === 18 && values.other === "") { - values.other = "v2.1"; + if (values.type === 18 && values.other === '') { + values.other = 'v2.1'; } + if (values.key === '') { + if (values.config.ak && values.config.sk && values.config.region) { + values.key = `${values.config.ak}|${values.config.sk}|${values.config.region}`; + } + } + let res; - const modelsStr = values.models.map((model) => model.id).join(","); - values.group = values.groups.join(","); + const modelsStr = values.models.map((model) => model.id).join(','); + const configStr = JSON.stringify(values.config); + values.group = values.groups.join(','); if (channelId) { res = await API.put(`/api/channel/`, { ...values, id: parseInt(channelId), models: modelsStr, + config: configStr }); } else { - res = await API.post(`/api/channel/`, { ...values, models: modelsStr }); + res = await API.post(`/api/channel/`, { ...values, models: modelsStr, config: configStr }); } const { success, message } = res.data; if (success) { if (channelId) { - showSuccess("渠道更新成功!"); + showSuccess('渠道更新成功!'); } else { - showSuccess("渠道创建成功!"); + showSuccess('渠道创建成功!'); } setSubmitting(false); setStatus({ success: true }); @@ -226,15 +205,15 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { } // 如果 channelModel 是一个字符串 - if (typeof channelModel === "string") { - channelModel = channelModel.split(","); + if (typeof channelModel === 'string') { + channelModel = channelModel.split(','); } let modelList = channelModel.map((model) => { const modelOption = modelOptions.find((option) => option.id === model); if (modelOption) { return modelOption; } - return { id: model, group: "自定义:点击或回车输入" }; + return { id: model, group: '自定义:点击或回车输入' }; }); return modelList; } @@ -243,24 +222,24 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { let res = await API.get(`/api/channel/${channelId}`); const { success, message, data } = res.data; if (success) { - if (data.models === "") { + if (data.models === '') { data.models = []; } else { data.models = initialModel(data.models); } - if (data.group === "") { + if (data.group === '') { data.groups = []; } else { - data.groups = data.group.split(","); + data.groups = data.group.split(','); } - if (data.model_mapping !== "") { - data.model_mapping = JSON.stringify( - JSON.parse(data.model_mapping), - null, - 2 - ); + if (data.model_mapping !== '') { + data.model_mapping = JSON.stringify(JSON.parse(data.model_mapping), null, 2); } - data.base_url = data.base_url ?? ""; + if (data.config !== '') { + data.config = JSON.parse(data.config); + } + + data.base_url = data.base_url ?? ''; data.is_edit = true; initChannel(data.type); setInitialInput(data); @@ -286,45 +265,25 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => { }, [channelId]); return ( - + - {channelId ? "编辑渠道" : "新建渠道"} + {channelId ? '编辑渠道' : '新建渠道'} - - {({ - errors, - handleBlur, - handleChange, - handleSubmit, - isSubmitting, - touched, - values, - setFieldValue, - }) => ( + + {({ errors, handleBlur, handleChange, handleSubmit, isSubmitting, touched, values, setFieldValue }) => (
- - - {inputLabel.type} - + + {inputLabel.type}