(c *gin.Context)
| 16 | ) |
| 17 | |
| 18 | func Playground(c *gin.Context) { |
| 19 | var newAPIError *types.NewAPIError |
| 20 | |
| 21 | defer func() { |
| 22 | if newAPIError != nil { |
| 23 | c.JSON(newAPIError.StatusCode, gin.H{ |
| 24 | "error": newAPIError.ToOpenAIError(), |
| 25 | }) |
| 26 | } |
| 27 | }() |
| 28 | |
| 29 | useAccessToken := c.GetBool("use_access_token") |
| 30 | if useAccessToken { |
| 31 | newAPIError = types.NewError(errors.New("暂不支持使用 access token"), types.ErrorCodeAccessDenied) |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | playgroundRequest := &dto.PlayGroundRequest{} |
| 36 | err := common.UnmarshalBodyReusable(c, playgroundRequest) |
| 37 | if err != nil { |
| 38 | newAPIError = types.NewError(err, types.ErrorCodeInvalidRequest) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | if playgroundRequest.Model == "" { |
| 43 | newAPIError = types.NewError(errors.New("请选择模型"), types.ErrorCodeInvalidRequest) |
| 44 | return |
| 45 | } |
| 46 | c.Set("original_model", playgroundRequest.Model) |
| 47 | group := playgroundRequest.Group |
| 48 | userGroup := c.GetString("group") |
| 49 | |
| 50 | if group == "" { |
| 51 | group = userGroup |
| 52 | } else { |
| 53 | if !setting.GroupInUserUsableGroups(group) && group != userGroup { |
| 54 | newAPIError = types.NewError(errors.New("无权访问该分组"), types.ErrorCodeAccessDenied) |
| 55 | return |
| 56 | } |
| 57 | c.Set("group", group) |
| 58 | } |
| 59 | |
| 60 | userId := c.GetInt("id") |
| 61 | |
| 62 | // Write user context to ensure acceptUnsetRatio is available |
| 63 | userCache, err := model.GetUserCache(userId) |
| 64 | if err != nil { |
| 65 | newAPIError = types.NewError(err, types.ErrorCodeQueryDataError) |
| 66 | return |
| 67 | } |
| 68 | userCache.WriteContext(c) |
| 69 | |
| 70 | tempToken := &model.Token{ |
| 71 | UserId: userId, |
| 72 | Name: fmt.Sprintf("playground-%s", group), |
| 73 | Group: group, |
| 74 | } |
| 75 | _ = middleware.SetupContextForToken(c, tempToken) |
nothing calls this directly
no test coverage detected