mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-17 13:43:42 +08:00
🔖 chore: Rename relay/util to relay/relay_util package and add utils package
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"one-api/relay/util"
|
||||
"one-api/relay/relay_util"
|
||||
"one-api/types"
|
||||
|
||||
providersBase "one-api/providers/base"
|
||||
@@ -14,7 +14,7 @@ type relayBase struct {
|
||||
provider providersBase.ProviderInterface
|
||||
originalModel string
|
||||
modelName string
|
||||
cache *util.ChatCacheProps
|
||||
cache *relay_util.ChatCacheProps
|
||||
}
|
||||
|
||||
type RelayBaseInterface interface {
|
||||
@@ -28,14 +28,14 @@ type RelayBaseInterface interface {
|
||||
getModelName() string
|
||||
getContext() *gin.Context
|
||||
SetChatCache(allow bool)
|
||||
GetChatCache() *util.ChatCacheProps
|
||||
GetChatCache() *relay_util.ChatCacheProps
|
||||
}
|
||||
|
||||
func (r *relayBase) SetChatCache(allow bool) {
|
||||
r.cache = util.NewChatCacheProps(r.c, allow)
|
||||
r.cache = relay_util.NewChatCacheProps(r.c, allow)
|
||||
}
|
||||
|
||||
func (r *relayBase) GetChatCache() *util.ChatCacheProps {
|
||||
func (r *relayBase) GetChatCache() *relay_util.ChatCacheProps {
|
||||
return r.cache
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/common/requester"
|
||||
"one-api/common/utils"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
@@ -100,9 +101,9 @@ func (r *relayChat) send() (err *types.OpenAIErrorWithStatusCode, done bool) {
|
||||
func (r *relayChat) getUsageResponse() string {
|
||||
if r.chatRequest.StreamOptions != nil && r.chatRequest.StreamOptions.IncludeUsage {
|
||||
usageResponse := types.ChatCompletionStreamResponse{
|
||||
ID: fmt.Sprintf("chatcmpl-%s", common.GetUUID()),
|
||||
ID: fmt.Sprintf("chatcmpl-%s", utils.GetUUID()),
|
||||
Object: "chat.completion.chunk",
|
||||
Created: common.GetTimestamp(),
|
||||
Created: utils.GetTimestamp(),
|
||||
Model: r.chatRequest.Model,
|
||||
Choices: []types.ChatCompletionStreamChoice{},
|
||||
Usage: r.provider.GetUsage(),
|
||||
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/common/requester"
|
||||
"one-api/common/utils"
|
||||
"one-api/controller"
|
||||
"one-api/model"
|
||||
"one-api/providers"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/relay/util"
|
||||
"one-api/relay/relay_util"
|
||||
"one-api/types"
|
||||
"strings"
|
||||
|
||||
@@ -142,7 +143,7 @@ func responseJsonClient(c *gin.Context, data interface{}) *types.OpenAIErrorWith
|
||||
|
||||
type StreamEndHandler func() string
|
||||
|
||||
func responseStreamClient(c *gin.Context, stream requester.StreamReaderInterface[string], cache *util.ChatCacheProps, endHandler StreamEndHandler) (errWithOP *types.OpenAIErrorWithStatusCode) {
|
||||
func responseStreamClient(c *gin.Context, stream requester.StreamReaderInterface[string], cache *relay_util.ChatCacheProps, endHandler StreamEndHandler) (errWithOP *types.OpenAIErrorWithStatusCode) {
|
||||
requester.SetEventStreamHeaders(c)
|
||||
dataChan, errChan := stream.Recv()
|
||||
|
||||
@@ -257,7 +258,7 @@ func processChannelRelayError(ctx context.Context, channelId int, channelName st
|
||||
|
||||
func relayResponseWithErr(c *gin.Context, err *types.OpenAIErrorWithStatusCode) {
|
||||
requestId := c.GetString(common.RequestIdKey)
|
||||
err.OpenAIError.Message = common.MessageWithRequestId(err.OpenAIError.Message, requestId)
|
||||
err.OpenAIError.Message = utils.MessageWithRequestId(err.OpenAIError.Message, requestId)
|
||||
c.JSON(err.StatusCode, gin.H{
|
||||
"error": err.OpenAIError,
|
||||
})
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/common/requester"
|
||||
"one-api/common/utils"
|
||||
providersBase "one-api/providers/base"
|
||||
"one-api/types"
|
||||
|
||||
@@ -93,9 +94,9 @@ func (r *relayCompletions) send() (err *types.OpenAIErrorWithStatusCode, done bo
|
||||
func (r *relayCompletions) getUsageResponse() string {
|
||||
if r.request.StreamOptions != nil && r.request.StreamOptions.IncludeUsage {
|
||||
usageResponse := types.CompletionResponse{
|
||||
ID: fmt.Sprintf("chatcmpl-%s", common.GetUUID()),
|
||||
ID: fmt.Sprintf("chatcmpl-%s", utils.GetUUID()),
|
||||
Object: "chat.completion.chunk",
|
||||
Created: common.GetTimestamp(),
|
||||
Created: utils.GetTimestamp(),
|
||||
Model: r.request.Model,
|
||||
Choices: []types.CompletionChoice{},
|
||||
Usage: r.provider.GetUsage(),
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
"one-api/relay/util"
|
||||
"one-api/relay/relay_util"
|
||||
"one-api/types"
|
||||
"time"
|
||||
|
||||
@@ -96,8 +96,8 @@ func RelayHandler(relay RelayBaseInterface) (err *types.OpenAIErrorWithStatusCod
|
||||
|
||||
relay.getProvider().SetUsage(usage)
|
||||
|
||||
var quota *util.Quota
|
||||
quota, err = util.NewQuota(relay.getContext(), relay.getModelName(), promptTokens)
|
||||
var quota *relay_util.Quota
|
||||
quota, err = relay_util.NewQuota(relay.getContext(), relay.getModelName(), promptTokens)
|
||||
if err != nil {
|
||||
done = true
|
||||
return
|
||||
@@ -119,7 +119,7 @@ func RelayHandler(relay RelayBaseInterface) (err *types.OpenAIErrorWithStatusCod
|
||||
return
|
||||
}
|
||||
|
||||
func cacheProcessing(c *gin.Context, cacheProps *util.ChatCacheProps) {
|
||||
func cacheProcessing(c *gin.Context, cacheProps *relay_util.ChatCacheProps) {
|
||||
responseCache(c, cacheProps.Response)
|
||||
|
||||
// 写入日志
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"one-api/model"
|
||||
provider "one-api/providers/midjourney"
|
||||
"one-api/relay"
|
||||
"one-api/relay/util"
|
||||
"one-api/relay/relay_util"
|
||||
"one-api/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -539,10 +539,10 @@ func getMjRequestPath(path string) string {
|
||||
return requestURL
|
||||
}
|
||||
|
||||
func getQuota(c *gin.Context, action string) (*util.Quota, *types.OpenAIErrorWithStatusCode) {
|
||||
func getQuota(c *gin.Context, action string) (*relay_util.Quota, *types.OpenAIErrorWithStatusCode) {
|
||||
modelName := CoverActionToModelName(action)
|
||||
|
||||
return util.NewQuota(c, modelName, 1000)
|
||||
return relay_util.NewQuota(c, modelName, 1000)
|
||||
}
|
||||
|
||||
func getMJProviderWithRequest(c *gin.Context, relayMode int, request *provider.MidjourneyRequest) (*provider.MidjourneyProvider, *provider.MidjourneyResponse) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/model"
|
||||
"one-api/relay/util"
|
||||
"one-api/relay/relay_util"
|
||||
"one-api/types"
|
||||
"sort"
|
||||
|
||||
@@ -90,7 +90,7 @@ func ListModels(c *gin.Context) {
|
||||
}
|
||||
|
||||
func ListModelsForAdmin(c *gin.Context) {
|
||||
prices := util.PricingInstance.GetAllPrices()
|
||||
prices := relay_util.PricingInstance.GetAllPrices()
|
||||
var openAIModels []OpenAIModels
|
||||
for modelId, price := range prices {
|
||||
openAIModels = append(openAIModels, OpenAIModels{
|
||||
@@ -123,7 +123,7 @@ func ListModelsForAdmin(c *gin.Context) {
|
||||
func RetrieveModel(c *gin.Context) {
|
||||
modelName := c.Param("model")
|
||||
openaiModel := getOpenAIModelWithName(modelName)
|
||||
if *openaiModel.OwnedBy != util.UnknownOwnedBy {
|
||||
if *openaiModel.OwnedBy != relay_util.UnknownOwnedBy {
|
||||
c.JSON(200, openaiModel)
|
||||
} else {
|
||||
openAIError := types.OpenAIError{
|
||||
@@ -139,15 +139,15 @@ func RetrieveModel(c *gin.Context) {
|
||||
}
|
||||
|
||||
func getModelOwnedBy(channelType int) (ownedBy *string) {
|
||||
if ownedByName, ok := util.ModelOwnedBy[channelType]; ok {
|
||||
if ownedByName, ok := relay_util.ModelOwnedBy[channelType]; ok {
|
||||
return &ownedByName
|
||||
}
|
||||
|
||||
return &util.UnknownOwnedBy
|
||||
return &relay_util.UnknownOwnedBy
|
||||
}
|
||||
|
||||
func getOpenAIModelWithName(modelName string) *OpenAIModels {
|
||||
price := util.PricingInstance.GetPrice(modelName)
|
||||
price := relay_util.PricingInstance.GetPrice(modelName)
|
||||
|
||||
return &OpenAIModels{
|
||||
Id: modelName,
|
||||
@@ -164,6 +164,6 @@ func GetModelOwnedBy(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"message": "",
|
||||
"data": util.ModelOwnedBy,
|
||||
"data": relay_util.ModelOwnedBy,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package util
|
||||
package relay_util
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"one-api/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -37,7 +38,7 @@ func GetDebugList(userId int) ([]*ChatCacheProps, error) {
|
||||
|
||||
var props []*ChatCacheProps
|
||||
for _, cache := range caches {
|
||||
prop, err := common.UnmarshalString[ChatCacheProps](cache.Data)
|
||||
prop, err := utils.UnmarshalString[ChatCacheProps](cache.Data)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -77,7 +78,7 @@ func (p *ChatCacheProps) SetHash(request any) {
|
||||
return
|
||||
}
|
||||
|
||||
p.hash(common.Marshal(request))
|
||||
p.hash(utils.Marshal(request))
|
||||
}
|
||||
|
||||
func (p *ChatCacheProps) SetResponse(response any) {
|
||||
@@ -90,7 +91,7 @@ func (p *ChatCacheProps) SetResponse(response any) {
|
||||
return
|
||||
}
|
||||
|
||||
responseStr := common.Marshal(response)
|
||||
responseStr := utils.Marshal(response)
|
||||
if responseStr == "" {
|
||||
return
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package util
|
||||
package relay_util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"one-api/model"
|
||||
"time"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ func (db *ChatCacheDB) Get(hash string, userId int) *ChatCacheProps {
|
||||
return nil
|
||||
}
|
||||
|
||||
props, err := common.UnmarshalString[ChatCacheProps](cache.Data)
|
||||
props, err := utils.UnmarshalString[ChatCacheProps](cache.Data)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -28,7 +28,7 @@ func (db *ChatCacheDB) Set(hash string, props *ChatCacheProps, expire int64) err
|
||||
}
|
||||
|
||||
func SetCacheDB(hash string, props *ChatCacheProps, expire int64) error {
|
||||
data := common.Marshal(props)
|
||||
data := utils.Marshal(props)
|
||||
if data == "" {
|
||||
return errors.New("marshal error")
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package util
|
||||
package relay_util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -17,7 +18,7 @@ func (r *ChatCacheRedis) Get(hash string, userId int) *ChatCacheProps {
|
||||
return nil
|
||||
}
|
||||
|
||||
props, err := common.UnmarshalString[ChatCacheProps](cache)
|
||||
props, err := utils.UnmarshalString[ChatCacheProps](cache)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -31,7 +32,7 @@ func (r *ChatCacheRedis) Set(hash string, props *ChatCacheProps, expire int64) e
|
||||
return nil
|
||||
}
|
||||
|
||||
data := common.Marshal(&props)
|
||||
data := utils.Marshal(&props)
|
||||
if data == "" {
|
||||
return errors.New("marshal error")
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package util
|
||||
package relay_util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"one-api/model"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -98,7 +99,7 @@ func (p *Pricing) GetPrice(modelName string) *model.Price {
|
||||
return price
|
||||
}
|
||||
|
||||
matchModel := common.GetModelsWithMatch(&p.Match, modelName)
|
||||
matchModel := utils.GetModelsWithMatch(&p.Match, modelName)
|
||||
if price, ok := p.Prices[matchModel]; ok {
|
||||
return price
|
||||
}
|
||||
@@ -281,7 +282,7 @@ func (p *Pricing) BatchSetPrices(batchPrices *BatchPrices, originalModels []stri
|
||||
var updatePrices []string
|
||||
|
||||
for _, model := range originalModels {
|
||||
if !common.Contains(model, batchPrices.Models) {
|
||||
if !utils.Contains(model, batchPrices.Models) {
|
||||
deletePrices = append(deletePrices, model)
|
||||
} else {
|
||||
updatePrices = append(updatePrices, model)
|
||||
@@ -289,7 +290,7 @@ func (p *Pricing) BatchSetPrices(batchPrices *BatchPrices, originalModels []stri
|
||||
}
|
||||
|
||||
for _, model := range batchPrices.Models {
|
||||
if !common.Contains(model, originalModels) {
|
||||
if !utils.Contains(model, originalModels) {
|
||||
addPrice := batchPrices.Price
|
||||
addPrice.Model = model
|
||||
addPrices = append(addPrices, &addPrice)
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package relay_util
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package relay_util
|
||||
|
||||
import "one-api/common"
|
||||
|
||||
Reference in New Issue
Block a user