Files
new-api/controller/playground.go

61 lines
1.4 KiB
Go
Raw Normal View History

2024-12-28 16:47:56 +08:00
package controller
import (
"errors"
"fmt"
"one-api/common"
2024-12-29 01:03:02 +08:00
"one-api/constant"
2024-12-28 16:47:56 +08:00
"one-api/middleware"
"one-api/model"
"one-api/types"
2024-12-29 01:03:02 +08:00
"time"
2025-06-16 22:15:12 +08:00
"github.com/gin-gonic/gin"
2024-12-28 16:47:56 +08:00
)
func Playground(c *gin.Context) {
var newAPIError *types.NewAPIError
2024-12-28 16:47:56 +08:00
defer func() {
if newAPIError != nil {
c.JSON(newAPIError.StatusCode, gin.H{
"error": newAPIError.ToOpenAIError(),
2024-12-28 16:47:56 +08:00
})
}
}()
useAccessToken := c.GetBool("use_access_token")
if useAccessToken {
2025-07-30 22:35:31 +08:00
newAPIError = types.NewError(errors.New("暂不支持使用 access token"), types.ErrorCodeAccessDenied, types.ErrOptionWithSkipRetry())
2024-12-28 16:47:56 +08:00
return
}
2025-08-08 11:59:04 +08:00
group := c.GetString("group")
modelName := c.GetString("original_model")
userId := c.GetInt("id")
2025-07-17 22:26:38 +08:00
// Write user context to ensure acceptUnsetRatio is available
userCache, err := model.GetUserCache(userId)
if err != nil {
2025-07-30 22:35:31 +08:00
newAPIError = types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry())
2025-07-17 22:26:38 +08:00
return
}
userCache.WriteContext(c)
tempToken := &model.Token{
UserId: userId,
Name: fmt.Sprintf("playground-%s", group),
Group: group,
}
_ = middleware.SetupContextForToken(c, tempToken)
2025-08-08 11:59:04 +08:00
_, newAPIError = getChannel(c, group, modelName, 0)
2025-07-16 23:41:31 +08:00
if newAPIError != nil {
2024-12-28 16:47:56 +08:00
return
}
//middleware.SetupContextForSelectedChannel(c, channel, playgroundRequest.Model)
common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now())
2024-12-28 16:47:56 +08:00
Relay(c)
}