2024-12-28 16:47:56 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2025-06-16 22:15:12 +08:00
|
|
|
|
2025-10-11 15:30:09 +08:00
|
|
|
"github.com/QuantumNous/new-api/middleware"
|
|
|
|
|
"github.com/QuantumNous/new-api/model"
|
2025-12-12 22:04:38 +08:00
|
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
2025-10-11 15:30:09 +08:00
|
|
|
"github.com/QuantumNous/new-api/types"
|
|
|
|
|
|
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) {
|
2025-07-10 15:02:40 +08:00
|
|
|
var newAPIError *types.NewAPIError
|
2024-12-28 16:47:56 +08:00
|
|
|
|
|
|
|
|
defer func() {
|
2025-07-10 15:02:40 +08:00
|
|
|
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-12-12 22:04:38 +08:00
|
|
|
relayInfo, err := relaycommon.GenRelayInfo(c, types.RelayFormatOpenAI, nil, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
newAPIError = types.NewError(err, types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-07-06 12:37:56 +08:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2025-07-06 12:37:56 +08:00
|
|
|
tempToken := &model.Token{
|
|
|
|
|
UserId: userId,
|
2025-12-12 22:04:38 +08:00
|
|
|
Name: fmt.Sprintf("playground-%s", relayInfo.UsingGroup),
|
|
|
|
|
Group: relayInfo.UsingGroup,
|
2025-07-06 12:37:56 +08:00
|
|
|
}
|
|
|
|
|
_ = middleware.SetupContextForToken(c, tempToken)
|
2025-06-18 10:56:33 +08:00
|
|
|
|
2025-08-14 21:10:04 +08:00
|
|
|
Relay(c, types.RelayFormatOpenAI)
|
2024-12-28 16:47:56 +08:00
|
|
|
}
|