mirror of
https://github.com/songquanpeng/one-api.git
synced 2026-03-04 02:34:25 +08:00
Compare commits
15 Commits
v0.6.9-alp
...
a424e26c2d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a424e26c2d | ||
|
|
cba82404ae | ||
|
|
c9ac670ba1 | ||
|
|
15f815c23c | ||
|
|
89b63ca96f | ||
|
|
8cc54489b9 | ||
|
|
58bf60805e | ||
|
|
6714cf96d6 | ||
|
|
c44928b3a7 | ||
|
|
02da017791 | ||
|
|
7ef45ee28f | ||
|
|
1430152a90 | ||
|
|
dd73f5b6b1 | ||
|
|
1264ddcef5 | ||
|
|
f71e4ef151 |
@@ -3,6 +3,7 @@ package ctxkey
|
|||||||
const (
|
const (
|
||||||
Config = "config"
|
Config = "config"
|
||||||
Id = "id"
|
Id = "id"
|
||||||
|
RequestId = "X-Oneapi-Request-Id"
|
||||||
Username = "username"
|
Username = "username"
|
||||||
Role = "role"
|
Role = "role"
|
||||||
Status = "status"
|
Status = "status"
|
||||||
@@ -15,6 +16,7 @@ const (
|
|||||||
Group = "group"
|
Group = "group"
|
||||||
ModelMapping = "model_mapping"
|
ModelMapping = "model_mapping"
|
||||||
ChannelName = "channel_name"
|
ChannelName = "channel_name"
|
||||||
|
ContentType = "content_type"
|
||||||
TokenId = "token_id"
|
TokenId = "token_id"
|
||||||
TokenName = "token_name"
|
TokenName = "token_name"
|
||||||
BaseURL = "base_url"
|
BaseURL = "base_url"
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRequestBody(c *gin.Context) ([]byte, error) {
|
func GetRequestBody(c *gin.Context) ([]byte, error) {
|
||||||
@@ -28,18 +28,16 @@ func UnmarshalBodyReusable(c *gin.Context, v any) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
contentType := c.Request.Header.Get("Content-Type")
|
|
||||||
if strings.HasPrefix(contentType, "application/json") {
|
|
||||||
err = json.Unmarshal(requestBody, &v)
|
|
||||||
} else {
|
|
||||||
// skip for now
|
|
||||||
// TODO: someday non json request have variant model, we will need to implementation this
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Reset request body
|
// Reset request body
|
||||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
||||||
|
defer func() {
|
||||||
|
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err = c.Bind(v); err != nil {
|
||||||
|
return errors.Wrap(err, "bind request body failed")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ import (
|
|||||||
func relayHelper(c *gin.Context, relayMode int) *model.ErrorWithStatusCode {
|
func relayHelper(c *gin.Context, relayMode int) *model.ErrorWithStatusCode {
|
||||||
var err *model.ErrorWithStatusCode
|
var err *model.ErrorWithStatusCode
|
||||||
switch relayMode {
|
switch relayMode {
|
||||||
case relaymode.ImagesGenerations:
|
case relaymode.ImagesGenerations,
|
||||||
|
relaymode.ImagesEdits:
|
||||||
err = controller.RelayImageHelper(c, relayMode)
|
err = controller.RelayImageHelper(c, relayMode)
|
||||||
case relaymode.AudioSpeech:
|
case relaymode.AudioSpeech:
|
||||||
fallthrough
|
fallthrough
|
||||||
@@ -45,10 +46,6 @@ func relayHelper(c *gin.Context, relayMode int) *model.ErrorWithStatusCode {
|
|||||||
func Relay(c *gin.Context) {
|
func Relay(c *gin.Context) {
|
||||||
ctx := c.Request.Context()
|
ctx := c.Request.Context()
|
||||||
relayMode := relaymode.GetByPath(c.Request.URL.Path)
|
relayMode := relaymode.GetByPath(c.Request.URL.Path)
|
||||||
if config.DebugEnabled {
|
|
||||||
requestBody, _ := common.GetRequestBody(c)
|
|
||||||
logger.Debugf(ctx, "request body: %s", string(requestBody))
|
|
||||||
}
|
|
||||||
channelId := c.GetInt(ctxkey.ChannelId)
|
channelId := c.GetInt(ctxkey.ChannelId)
|
||||||
userId := c.GetInt(ctxkey.Id)
|
userId := c.GetInt(ctxkey.Id)
|
||||||
bizErr := relayHelper(c, relayMode)
|
bizErr := relayHelper(c, relayMode)
|
||||||
@@ -60,6 +57,8 @@ func Relay(c *gin.Context) {
|
|||||||
channelName := c.GetString(ctxkey.ChannelName)
|
channelName := c.GetString(ctxkey.ChannelName)
|
||||||
group := c.GetString(ctxkey.Group)
|
group := c.GetString(ctxkey.Group)
|
||||||
originalModel := c.GetString(ctxkey.OriginalModel)
|
originalModel := c.GetString(ctxkey.OriginalModel)
|
||||||
|
|
||||||
|
// BUG: bizErr is shared, should not run this function in goroutine to avoid race
|
||||||
go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
|
go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
|
||||||
requestId := c.GetString(helper.RequestIdKey)
|
requestId := c.GetString(helper.RequestIdKey)
|
||||||
retryTimes := config.RetryTimes
|
retryTimes := config.RetryTimes
|
||||||
@@ -90,6 +89,7 @@ func Relay(c *gin.Context) {
|
|||||||
// BUG: bizErr is in race condition
|
// BUG: bizErr is in race condition
|
||||||
go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
|
go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if bizErr != nil {
|
if bizErr != nil {
|
||||||
if bizErr.StatusCode == http.StatusTooManyRequests {
|
if bizErr.StatusCode == http.StatusTooManyRequests {
|
||||||
bizErr.Error.Message = "当前分组上游负载已饱和,请稍后再试"
|
bizErr.Error.Message = "当前分组上游负载已饱和,请稍后再试"
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ModelRequest struct {
|
type ModelRequest struct {
|
||||||
Model string `json:"model"`
|
Model string `json:"model" form:"model"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Distribute() func(c *gin.Context) {
|
func Distribute() func(c *gin.Context) {
|
||||||
@@ -61,6 +61,7 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
|
|||||||
c.Set(ctxkey.Channel, channel.Type)
|
c.Set(ctxkey.Channel, channel.Type)
|
||||||
c.Set(ctxkey.ChannelId, channel.Id)
|
c.Set(ctxkey.ChannelId, channel.Id)
|
||||||
c.Set(ctxkey.ChannelName, channel.Name)
|
c.Set(ctxkey.ChannelName, channel.Name)
|
||||||
|
c.Set(ctxkey.ContentType, c.Request.Header.Get("Content-Type"))
|
||||||
c.Set(ctxkey.ModelMapping, channel.GetModelMapping())
|
c.Set(ctxkey.ModelMapping, channel.GetModelMapping())
|
||||||
c.Set(ctxkey.OriginalModel, modelName) // for retry
|
c.Set(ctxkey.OriginalModel, modelName) // for retry
|
||||||
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
|
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/common/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/common/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
"github.com/songquanpeng/one-api/common/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func abortWithMessage(c *gin.Context, statusCode int, message string) {
|
func abortWithMessage(c *gin.Context, statusCode int, message string) {
|
||||||
|
|||||||
13
model/log.go
13
model/log.go
@@ -3,6 +3,7 @@ package model
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
"github.com/songquanpeng/one-api/common/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
@@ -152,7 +153,11 @@ func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int) (quota int64) {
|
func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int) (quota int64) {
|
||||||
tx := LOG_DB.Table("logs").Select("ifnull(sum(quota),0)")
|
ifnull := "ifnull"
|
||||||
|
if common.UsingPostgreSQL {
|
||||||
|
ifnull = "COALESCE"
|
||||||
|
}
|
||||||
|
tx := LOG_DB.Table("logs").Select(fmt.Sprintf("%s(sum(quota),0)", ifnull))
|
||||||
if username != "" {
|
if username != "" {
|
||||||
tx = tx.Where("username = ?", username)
|
tx = tx.Where("username = ?", username)
|
||||||
}
|
}
|
||||||
@@ -176,7 +181,11 @@ func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelNa
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (token int) {
|
func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (token int) {
|
||||||
tx := LOG_DB.Table("logs").Select("ifnull(sum(prompt_tokens),0) + ifnull(sum(completion_tokens),0)")
|
ifnull := "ifnull"
|
||||||
|
if common.UsingPostgreSQL {
|
||||||
|
ifnull = "COALESCE"
|
||||||
|
}
|
||||||
|
tx := LOG_DB.Table("logs").Select(fmt.Sprintf("%s(sum(prompt_tokens),0) + %s(sum(completion_tokens),0)", ifnull, ifnull))
|
||||||
if username != "" {
|
if username != "" {
|
||||||
tx = tx.Where("username = ?", username)
|
tx = tx.Where("username = ?", username)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package monitor
|
package monitor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
|
||||||
"github.com/songquanpeng/one-api/relay/model"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
|
"github.com/songquanpeng/one-api/relay/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ShouldDisableChannel(err *model.Error, statusCode int) bool {
|
func ShouldDisableChannel(err *model.Error, statusCode int) bool {
|
||||||
@@ -18,31 +19,23 @@ func ShouldDisableChannel(err *model.Error, statusCode int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
switch err.Type {
|
switch err.Type {
|
||||||
case "insufficient_quota":
|
case "insufficient_quota", "authentication_error", "permission_error", "forbidden":
|
||||||
return true
|
|
||||||
// https://docs.anthropic.com/claude/reference/errors
|
|
||||||
case "authentication_error":
|
|
||||||
return true
|
|
||||||
case "permission_error":
|
|
||||||
return true
|
|
||||||
case "forbidden":
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if err.Code == "invalid_api_key" || err.Code == "account_deactivated" {
|
if err.Code == "invalid_api_key" || err.Code == "account_deactivated" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(err.Message, "Your credit balance is too low") { // anthropic
|
|
||||||
return true
|
lowerMessage := strings.ToLower(err.Message)
|
||||||
} else if strings.HasPrefix(err.Message, "This organization has been disabled.") {
|
if strings.Contains(lowerMessage, "your access was terminated") ||
|
||||||
return true
|
strings.Contains(lowerMessage, "violation of our policies") ||
|
||||||
}
|
strings.Contains(lowerMessage, "your credit balance is too low") ||
|
||||||
//if strings.Contains(err.Message, "quota") {
|
strings.Contains(lowerMessage, "organization has been disabled") ||
|
||||||
// return true
|
strings.Contains(lowerMessage, "credit") ||
|
||||||
//}
|
strings.Contains(lowerMessage, "balance") ||
|
||||||
if strings.Contains(err.Message, "credit") {
|
strings.Contains(lowerMessage, "permission denied") ||
|
||||||
return true
|
strings.Contains(lowerMessage, "organization has been restricted") || // groq
|
||||||
}
|
strings.Contains(lowerMessage, "已欠费") {
|
||||||
if strings.Contains(err.Message, "balance") {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package ali
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||||
"github.com/songquanpeng/one-api/common/render"
|
"github.com/songquanpeng/one-api/common/render"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -59,7 +60,7 @@ func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
|
|||||||
|
|
||||||
func ConvertEmbeddingRequest(request model.GeneralOpenAIRequest) *EmbeddingRequest {
|
func ConvertEmbeddingRequest(request model.GeneralOpenAIRequest) *EmbeddingRequest {
|
||||||
return &EmbeddingRequest{
|
return &EmbeddingRequest{
|
||||||
Model: "text-embedding-v1",
|
Model: request.Model,
|
||||||
Input: struct {
|
Input: struct {
|
||||||
Texts []string `json:"texts"`
|
Texts []string `json:"texts"`
|
||||||
}{
|
}{
|
||||||
@@ -102,8 +103,9 @@ func EmbeddingHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStat
|
|||||||
StatusCode: resp.StatusCode,
|
StatusCode: resp.StatusCode,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
requestModel := c.GetString(ctxkey.RequestModel)
|
||||||
fullTextResponse := embeddingResponseAli2OpenAI(&aliResponse)
|
fullTextResponse := embeddingResponseAli2OpenAI(&aliResponse)
|
||||||
|
fullTextResponse.Model = requestModel
|
||||||
jsonResponse, err := json.Marshal(fullTextResponse)
|
jsonResponse, err := json.Marshal(fullTextResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ package adaptor
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/songquanpeng/one-api/common/client"
|
|
||||||
"github.com/songquanpeng/one-api/relay/meta"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/songquanpeng/one-api/common/client"
|
||||||
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||||
|
"github.com/songquanpeng/one-api/relay/meta"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) {
|
func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) {
|
||||||
@@ -27,6 +29,9 @@ func DoRequestHelper(a Adaptor, c *gin.Context, meta *meta.Meta, requestBody io.
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("new request failed: %w", err)
|
return nil, fmt.Errorf("new request failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", c.GetString(ctxkey.ContentType))
|
||||||
|
|
||||||
err = a.SetupRequestHeader(c, req, meta)
|
err = a.SetupRequestHeader(c, req, meta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("setup request header failed: %w", err)
|
return nil, fmt.Errorf("setup request header failed: %w", err)
|
||||||
|
|||||||
@@ -104,10 +104,13 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Met
|
|||||||
switch meta.Mode {
|
switch meta.Mode {
|
||||||
case relaymode.ImagesGenerations:
|
case relaymode.ImagesGenerations:
|
||||||
err, _ = ImageHandler(c, resp)
|
err, _ = ImageHandler(c, resp)
|
||||||
|
case relaymode.ImagesEdits:
|
||||||
|
err, _ = ImagesEditsHandler(c, resp)
|
||||||
default:
|
default:
|
||||||
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,30 @@ package openai
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/songquanpeng/one-api/relay/model"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/songquanpeng/one-api/relay/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ImagesEditsHandler just copy response body to client
|
||||||
|
//
|
||||||
|
// https://platform.openai.com/docs/api-reference/images/createEdit
|
||||||
|
func ImagesEditsHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||||
|
c.Writer.WriteHeader(resp.StatusCode)
|
||||||
|
for k, v := range resp.Header {
|
||||||
|
c.Writer.Header().Set(k, v[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
|
||||||
|
return ErrorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError), nil
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ImageHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
func ImageHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||||
var imageResponse ImageResponse
|
var imageResponse ImageResponse
|
||||||
responseBody, err := io.ReadAll(resp.Body)
|
responseBody, err := io.ReadAll(resp.Body)
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
package stepfun
|
package stepfun
|
||||||
|
|
||||||
var ModelList = []string{
|
var ModelList = []string{
|
||||||
|
"step-1-8k",
|
||||||
"step-1-32k",
|
"step-1-32k",
|
||||||
|
"step-1-128k",
|
||||||
|
"step-1-256k",
|
||||||
|
"step-1-flash",
|
||||||
|
"step-2-16k",
|
||||||
|
"step-1v-8k",
|
||||||
"step-1v-32k",
|
"step-1v-32k",
|
||||||
"step-1-200k",
|
"step-1x-medium",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,14 @@ var ImageSizeRatios = map[string]map[string]float64{
|
|||||||
"720x1280": 1,
|
"720x1280": 1,
|
||||||
"1280x720": 1,
|
"1280x720": 1,
|
||||||
},
|
},
|
||||||
|
"step-1x-medium": {
|
||||||
|
"256x256": 1,
|
||||||
|
"512x512": 1,
|
||||||
|
"768x768": 1,
|
||||||
|
"1024x1024": 1,
|
||||||
|
"1280x800": 1,
|
||||||
|
"800x1280": 1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var ImageGenerationAmounts = map[string][2]int{
|
var ImageGenerationAmounts = map[string][2]int{
|
||||||
@@ -39,6 +47,7 @@ var ImageGenerationAmounts = map[string][2]int{
|
|||||||
"ali-stable-diffusion-v1.5": {1, 4}, // Ali
|
"ali-stable-diffusion-v1.5": {1, 4}, // Ali
|
||||||
"wanx-v1": {1, 4}, // Ali
|
"wanx-v1": {1, 4}, // Ali
|
||||||
"cogview-3": {1, 1},
|
"cogview-3": {1, 1},
|
||||||
|
"step-1x-medium": {1, 1},
|
||||||
}
|
}
|
||||||
|
|
||||||
var ImagePromptLengthLimitations = map[string]int{
|
var ImagePromptLengthLimitations = map[string]int{
|
||||||
@@ -48,6 +57,7 @@ var ImagePromptLengthLimitations = map[string]int{
|
|||||||
"ali-stable-diffusion-v1.5": 4000,
|
"ali-stable-diffusion-v1.5": 4000,
|
||||||
"wanx-v1": 4000,
|
"wanx-v1": 4000,
|
||||||
"cogview-3": 833,
|
"cogview-3": 833,
|
||||||
|
"step-1x-medium": 4000,
|
||||||
}
|
}
|
||||||
|
|
||||||
var ImageOriginModelName = map[string]string{
|
var ImageOriginModelName = map[string]string{
|
||||||
|
|||||||
@@ -171,10 +171,15 @@ var ModelRatio = map[string]float64{
|
|||||||
"yi-34b-chat-0205": 2.5 / 1000 * RMB,
|
"yi-34b-chat-0205": 2.5 / 1000 * RMB,
|
||||||
"yi-34b-chat-200k": 12.0 / 1000 * RMB,
|
"yi-34b-chat-200k": 12.0 / 1000 * RMB,
|
||||||
"yi-vl-plus": 6.0 / 1000 * RMB,
|
"yi-vl-plus": 6.0 / 1000 * RMB,
|
||||||
// stepfun todo
|
// https://platform.stepfun.com/docs/pricing/details
|
||||||
"step-1v-32k": 0.024 * RMB,
|
"step-1-8k": 0.005 / 1000 * RMB,
|
||||||
"step-1-32k": 0.024 * RMB,
|
"step-1-32k": 0.015 / 1000 * RMB,
|
||||||
"step-1-200k": 0.15 * RMB,
|
"step-1-128k": 0.040 / 1000 * RMB,
|
||||||
|
"step-1-256k": 0.095 / 1000 * RMB,
|
||||||
|
"step-1-flash": 0.001 / 1000 * RMB,
|
||||||
|
"step-2-16k": 0.038 / 1000 * RMB,
|
||||||
|
"step-1v-8k": 0.005 / 1000 * RMB,
|
||||||
|
"step-1v-32k": 0.015 / 1000 * RMB,
|
||||||
// aws llama3 https://aws.amazon.com/cn/bedrock/pricing/
|
// aws llama3 https://aws.amazon.com/cn/bedrock/pricing/
|
||||||
"llama3-8b-8192(33)": 0.0003 / 0.002, // $0.0003 / 1K tokens
|
"llama3-8b-8192(33)": 0.0003 / 0.002, // $0.0003 / 1K tokens
|
||||||
"llama3-70b-8192(33)": 0.00265 / 0.002, // $0.00265 / 1K tokens
|
"llama3-70b-8192(33)": 0.00265 / 0.002, // $0.00265 / 1K tokens
|
||||||
@@ -200,8 +205,10 @@ var CompletionRatio = map[string]float64{
|
|||||||
"llama3-70b-8192(33)": 0.0035 / 0.00265,
|
"llama3-70b-8192(33)": 0.0035 / 0.00265,
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultModelRatio map[string]float64
|
var (
|
||||||
var DefaultCompletionRatio map[string]float64
|
DefaultModelRatio map[string]float64
|
||||||
|
DefaultCompletionRatio map[string]float64
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
DefaultModelRatio = make(map[string]float64)
|
DefaultModelRatio = make(map[string]float64)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/songquanpeng/one-api/common"
|
"github.com/songquanpeng/one-api/common"
|
||||||
@@ -134,7 +135,8 @@ func RelayImageHelper(c *gin.Context, relayMode int) *relaymodel.ErrorWithStatus
|
|||||||
c.Set("response_format", imageRequest.ResponseFormat)
|
c.Set("response_format", imageRequest.ResponseFormat)
|
||||||
|
|
||||||
var requestBody io.Reader
|
var requestBody io.Reader
|
||||||
if isModelMapped || meta.ChannelType == channeltype.Azure { // make Azure channel request body
|
if strings.ToLower(c.GetString(ctxkey.ContentType)) == "application/json" &&
|
||||||
|
isModelMapped || meta.ChannelType == channeltype.Azure { // make Azure channel request body
|
||||||
jsonStr, err := json.Marshal(imageRequest)
|
jsonStr, err := json.Marshal(imageRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return openai.ErrorWrapper(err, "marshal_image_request_failed", http.StatusInternalServerError)
|
return openai.ErrorWrapper(err, "marshal_image_request_failed", http.StatusInternalServerError)
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
type ResponseFormat struct {
|
type ResponseFormat struct {
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
|
JsonSchema *JSONSchema `json:"json_schema,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONSchema struct {
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Schema map[string]interface{} `json:"schema,omitempty"`
|
||||||
|
Strict *bool `json:"strict,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GeneralOpenAIRequest struct {
|
type GeneralOpenAIRequest struct {
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
type ImageRequest struct {
|
type ImageRequest struct {
|
||||||
Model string `json:"model"`
|
Model string `json:"model" form:"model"`
|
||||||
Prompt string `json:"prompt" binding:"required"`
|
Prompt string `json:"prompt" binding:"required" form:"prompt"`
|
||||||
N int `json:"n,omitempty"`
|
N int `json:"n,omitempty" form:"n"`
|
||||||
Size string `json:"size,omitempty"`
|
Size string `json:"size,omitempty" form:"size"`
|
||||||
Quality string `json:"quality,omitempty"`
|
Quality string `json:"quality,omitempty" form:"quality"`
|
||||||
ResponseFormat string `json:"response_format,omitempty"`
|
ResponseFormat string `json:"response_format,omitempty" form:"response_format"`
|
||||||
Style string `json:"style,omitempty"`
|
Style string `json:"style,omitempty" form:"style"`
|
||||||
User string `json:"user,omitempty"`
|
User string `json:"user,omitempty" form:"user"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const (
|
|||||||
AudioSpeech
|
AudioSpeech
|
||||||
AudioTranscription
|
AudioTranscription
|
||||||
AudioTranslation
|
AudioTranslation
|
||||||
|
ImagesEdits
|
||||||
// Proxy is a special relay mode for proxying requests to custom upstream
|
// Proxy is a special relay mode for proxying requests to custom upstream
|
||||||
Proxy
|
Proxy
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -24,8 +24,11 @@ func GetByPath(path string) int {
|
|||||||
relayMode = AudioTranscription
|
relayMode = AudioTranscription
|
||||||
} else if strings.HasPrefix(path, "/v1/audio/translations") {
|
} else if strings.HasPrefix(path, "/v1/audio/translations") {
|
||||||
relayMode = AudioTranslation
|
relayMode = AudioTranslation
|
||||||
|
} else if strings.HasPrefix(path, "/v1/images/edits") {
|
||||||
|
relayMode = ImagesEdits
|
||||||
} else if strings.HasPrefix(path, "/v1/oneapi/proxy") {
|
} else if strings.HasPrefix(path, "/v1/oneapi/proxy") {
|
||||||
relayMode = Proxy
|
relayMode = Proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
return relayMode
|
return relayMode
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func SetRelayRouter(router *gin.Engine) {
|
|||||||
relayV1Router.POST("/chat/completions", controller.Relay)
|
relayV1Router.POST("/chat/completions", controller.Relay)
|
||||||
relayV1Router.POST("/edits", controller.Relay)
|
relayV1Router.POST("/edits", controller.Relay)
|
||||||
relayV1Router.POST("/images/generations", controller.Relay)
|
relayV1Router.POST("/images/generations", controller.Relay)
|
||||||
relayV1Router.POST("/images/edits", controller.RelayNotImplemented)
|
relayV1Router.POST("/images/edits", controller.Relay)
|
||||||
relayV1Router.POST("/images/variations", controller.RelayNotImplemented)
|
relayV1Router.POST("/images/variations", controller.RelayNotImplemented)
|
||||||
relayV1Router.POST("/embeddings", controller.Relay)
|
relayV1Router.POST("/embeddings", controller.Relay)
|
||||||
relayV1Router.POST("/engines/:model/embeddings", controller.Relay)
|
relayV1Router.POST("/engines/:model/embeddings", controller.Relay)
|
||||||
|
|||||||
@@ -11,12 +11,14 @@ import EditToken from '../pages/Token/EditToken';
|
|||||||
const COPY_OPTIONS = [
|
const COPY_OPTIONS = [
|
||||||
{ key: 'next', text: 'ChatGPT Next Web', value: 'next' },
|
{ key: 'next', text: 'ChatGPT Next Web', value: 'next' },
|
||||||
{ key: 'ama', text: 'ChatGPT Web & Midjourney', value: 'ama' },
|
{ key: 'ama', text: 'ChatGPT Web & Midjourney', value: 'ama' },
|
||||||
{ key: 'opencat', text: 'OpenCat', value: 'opencat' }
|
{ key: 'opencat', text: 'OpenCat', value: 'opencat' },
|
||||||
|
{ key: 'lobechat', text: 'LobeChat', value: 'lobechat' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const OPEN_LINK_OPTIONS = [
|
const OPEN_LINK_OPTIONS = [
|
||||||
{ key: 'ama', text: 'ChatGPT Web & Midjourney', value: 'ama' },
|
{ key: 'ama', text: 'ChatGPT Web & Midjourney', value: 'ama' },
|
||||||
{ key: 'opencat', text: 'OpenCat', value: 'opencat' }
|
{ key: 'opencat', text: 'OpenCat', value: 'opencat' },
|
||||||
|
{ key: 'lobechat', text: 'LobeChat', value: 'lobechat' }
|
||||||
];
|
];
|
||||||
|
|
||||||
function renderTimestamp(timestamp) {
|
function renderTimestamp(timestamp) {
|
||||||
@@ -60,7 +62,12 @@ const TokensTable = () => {
|
|||||||
onOpenLink('next-mj');
|
onOpenLink('next-mj');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ node: 'item', key: 'opencat', name: 'OpenCat', value: 'opencat' }
|
{ node: 'item', key: 'opencat', name: 'OpenCat', value: 'opencat' },
|
||||||
|
{
|
||||||
|
node: 'item', key: 'lobechat', name: 'LobeChat', onClick: () => {
|
||||||
|
onOpenLink('lobechat');
|
||||||
|
}
|
||||||
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const columns = [
|
const columns = [
|
||||||
@@ -177,6 +184,11 @@ const TokensTable = () => {
|
|||||||
node: 'item', key: 'opencat', name: 'OpenCat', onClick: () => {
|
node: 'item', key: 'opencat', name: 'OpenCat', onClick: () => {
|
||||||
onOpenLink('opencat', record.key);
|
onOpenLink('opencat', record.key);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
node: 'item', key: 'lobechat', name: 'LobeChat', onClick: () => {
|
||||||
|
onOpenLink('lobechat');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -382,6 +394,9 @@ const TokensTable = () => {
|
|||||||
case 'next-mj':
|
case 'next-mj':
|
||||||
url = mjLink + `/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
url = mjLink + `/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
||||||
break;
|
break;
|
||||||
|
case 'lobechat':
|
||||||
|
url = chatLink + `/?settings={"keyVaults":{"openai":{"apiKey":"sk-${key}","baseURL":"${serverAddress}"/v1"}}}`;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
if (!chatLink) {
|
if (!chatLink) {
|
||||||
showError('管理员未设置聊天链接');
|
showError('管理员未设置聊天链接');
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ const COPY_OPTIONS = [
|
|||||||
encode: false
|
encode: false
|
||||||
},
|
},
|
||||||
{ key: 'ama', text: 'BotGem', url: 'ama://set-api-key?server={serverAddress}&key=sk-{key}', encode: true },
|
{ key: 'ama', text: 'BotGem', url: 'ama://set-api-key?server={serverAddress}&key=sk-{key}', encode: true },
|
||||||
{ key: 'opencat', text: 'OpenCat', url: 'opencat://team/join?domain={serverAddress}&token=sk-{key}', encode: true }
|
{ key: 'opencat', text: 'OpenCat', url: 'opencat://team/join?domain={serverAddress}&token=sk-{key}', encode: true },
|
||||||
|
{ key: 'lobechat', text: 'LobeChat', url: 'https://lobehub.com/?settings={"keyVaults":{"openai":{"apiKey":"user-key","baseURL":"https://your-proxy.com/v1"}}}', encode: true }
|
||||||
];
|
];
|
||||||
|
|
||||||
function replacePlaceholders(text, key, serverAddress) {
|
function replacePlaceholders(text, key, serverAddress) {
|
||||||
|
|||||||
@@ -10,12 +10,14 @@ const COPY_OPTIONS = [
|
|||||||
{ key: 'next', text: 'ChatGPT Next Web', value: 'next' },
|
{ key: 'next', text: 'ChatGPT Next Web', value: 'next' },
|
||||||
{ key: 'ama', text: 'BotGem', value: 'ama' },
|
{ key: 'ama', text: 'BotGem', value: 'ama' },
|
||||||
{ key: 'opencat', text: 'OpenCat', value: 'opencat' },
|
{ key: 'opencat', text: 'OpenCat', value: 'opencat' },
|
||||||
|
{ key: 'lobechat', text: 'LobeChat', value: 'lobechat' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const OPEN_LINK_OPTIONS = [
|
const OPEN_LINK_OPTIONS = [
|
||||||
{ key: 'next', text: 'ChatGPT Next Web', value: 'next' },
|
{ key: 'next', text: 'ChatGPT Next Web', value: 'next' },
|
||||||
{ key: 'ama', text: 'BotGem', value: 'ama' },
|
{ key: 'ama', text: 'BotGem', value: 'ama' },
|
||||||
{ key: 'opencat', text: 'OpenCat', value: 'opencat' },
|
{ key: 'opencat', text: 'OpenCat', value: 'opencat' },
|
||||||
|
{ key: 'lobechat', text: 'LobeChat', value: 'lobechat' },
|
||||||
];
|
];
|
||||||
|
|
||||||
function renderTimestamp(timestamp) {
|
function renderTimestamp(timestamp) {
|
||||||
@@ -114,6 +116,9 @@ const TokensTable = () => {
|
|||||||
case 'next':
|
case 'next':
|
||||||
url = nextUrl;
|
url = nextUrl;
|
||||||
break;
|
break;
|
||||||
|
case 'lobechat':
|
||||||
|
url = nextLink + `/?settings={"keyVaults":{"openai":{"apiKey":"sk-${key}","baseURL":"${serverAddress}"/v1"}}}`;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
url = `sk-${key}`;
|
url = `sk-${key}`;
|
||||||
}
|
}
|
||||||
@@ -153,7 +158,11 @@ const TokensTable = () => {
|
|||||||
case 'opencat':
|
case 'opencat':
|
||||||
url = `opencat://team/join?domain=${encodedServerAddress}&token=sk-${key}`;
|
url = `opencat://team/join?domain=${encodedServerAddress}&token=sk-${key}`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'lobechat':
|
||||||
|
url = chatLink + `/?settings={"keyVaults":{"openai":{"apiKey":"sk-${key}","baseURL":"${serverAddress}"/v1"}}}`;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
url = defaultUrl;
|
url = defaultUrl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user