Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

67 строки
1.8 KiB

  1. package controller
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/middleware"
  7. "github.com/QuantumNous/new-api/model"
  8. relaycommon "github.com/QuantumNous/new-api/relay/common"
  9. "github.com/QuantumNous/new-api/setting/operation_setting"
  10. "github.com/QuantumNous/new-api/types"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func Playground(c *gin.Context) {
  14. var newAPIError *types.NewAPIError
  15. defer func() {
  16. if newAPIError != nil {
  17. c.JSON(newAPIError.StatusCode, gin.H{
  18. "error": newAPIError.ToOpenAIError(),
  19. })
  20. }
  21. }()
  22. useAccessToken := c.GetBool("use_access_token")
  23. if useAccessToken {
  24. newAPIError = types.NewError(errors.New("暂不支持使用 access token"), types.ErrorCodeAccessDenied, types.ErrOptionWithSkipRetry())
  25. return
  26. }
  27. relayInfo, err := relaycommon.GenRelayInfo(c, types.RelayFormatOpenAI, nil, nil)
  28. if err != nil {
  29. newAPIError = types.NewError(err, types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
  30. return
  31. }
  32. userId := c.GetInt("id")
  33. // Write user context to ensure acceptUnsetRatio is available
  34. userCache, err := model.GetUserCache(userId)
  35. if err != nil {
  36. newAPIError = types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry())
  37. return
  38. }
  39. userCache.WriteContext(c)
  40. tempToken := &model.Token{
  41. UserId: userId,
  42. Name: fmt.Sprintf("playground-%s", relayInfo.UsingGroup),
  43. Group: relayInfo.UsingGroup,
  44. }
  45. _ = middleware.SetupContextForToken(c, tempToken)
  46. Relay(c, types.RelayFormatOpenAI)
  47. }
  48. // GetPlaygroundConfig 获取 Playground 公开配置(无需认证)
  49. func GetPlaygroundConfig(c *gin.Context) {
  50. setting := operation_setting.GetPlaygroundSetting()
  51. common.ApiSuccess(c, gin.H{
  52. "mutual_exclusive_params": setting.MutualExclusiveParams,
  53. })
  54. }