playground.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package controller
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/constant"
  8. "github.com/QuantumNous/new-api/middleware"
  9. "github.com/QuantumNous/new-api/model"
  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. group := common.GetContextKeyString(c, constant.ContextKeyUsingGroup)
  28. modelName := c.GetString("original_model")
  29. userId := c.GetInt("id")
  30. // Write user context to ensure acceptUnsetRatio is available
  31. userCache, err := model.GetUserCache(userId)
  32. if err != nil {
  33. newAPIError = types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry())
  34. return
  35. }
  36. userCache.WriteContext(c)
  37. tempToken := &model.Token{
  38. UserId: userId,
  39. Name: fmt.Sprintf("playground-%s", group),
  40. Group: group,
  41. }
  42. _ = middleware.SetupContextForToken(c, tempToken)
  43. _, newAPIError = getChannel(c, group, modelName, 0)
  44. if newAPIError != nil {
  45. return
  46. }
  47. //middleware.SetupContextForSelectedChannel(c, channel, playgroundRequest.Model)
  48. common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now())
  49. Relay(c, types.RelayFormatOpenAI)
  50. }