| 104 | } |
| 105 | |
| 106 | func getAndValidateTextRequest(c *gin.Context, relayInfo *relaycommon.RelayInfo) (*dto.GeneralOpenAIRequest, error) { |
| 107 | textRequest := &dto.GeneralOpenAIRequest{} |
| 108 | err := common.UnmarshalBodyReusable(c, textRequest) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | if relayInfo.RelayMode == relayconstant.RelayModeModerations && textRequest.Model == "" { |
| 113 | textRequest.Model = "text-moderation-latest" |
| 114 | } |
| 115 | if relayInfo.RelayMode == relayconstant.RelayModeEmbeddings && textRequest.Model == "" { |
| 116 | textRequest.Model = c.Param("model") |
| 117 | } |
| 118 | |
| 119 | if textRequest.MaxTokens > math.MaxInt32/2 { |
| 120 | return nil, errors.New("max_tokens is invalid") |
| 121 | } |
| 122 | if textRequest.Model == "" { |
| 123 | return nil, errors.New("model is required") |
| 124 | } |
| 125 | if textRequest.WebSearchOptions != nil { |
| 126 | if textRequest.WebSearchOptions.SearchContextSize != "" { |
| 127 | validSizes := map[string]bool{ |
| 128 | "high": true, |
| 129 | "medium": true, |
| 130 | "low": true, |
| 131 | } |
| 132 | if !validSizes[textRequest.WebSearchOptions.SearchContextSize] { |
| 133 | return nil, errors.New("invalid search_context_size, must be one of: high, medium, low") |
| 134 | } |
| 135 | } else { |
| 136 | textRequest.WebSearchOptions.SearchContextSize = "medium" |
| 137 | } |
| 138 | } |
| 139 | switch relayInfo.RelayMode { |
| 140 | case relayconstant.RelayModeCompletions: |
| 141 | if textRequest.Prompt == "" { |
| 142 | return nil, errors.New("field prompt is required") |
| 143 | } |
| 144 | case relayconstant.RelayModeChatCompletions: |
| 145 | if len(textRequest.Messages) == 0 { |
| 146 | return nil, errors.New("field messages is required") |
| 147 | } |
| 148 | case relayconstant.RelayModeEmbeddings: |
| 149 | case relayconstant.RelayModeModerations: |
| 150 | if textRequest.Input == nil || textRequest.Input == "" { |
| 151 | return nil, errors.New("field input is required") |
| 152 | } |
| 153 | case relayconstant.RelayModeEdits: |
| 154 | if textRequest.Instruction == "" { |
| 155 | return nil, errors.New("field instruction is required") |
| 156 | } |
| 157 | } |
| 158 | relayInfo.IsStream = textRequest.Stream |
| 159 | return textRequest, nil |
| 160 | } |
| 161 | |
| 162 | func TextHelper(c *gin.Context) (newAPIError *types.NewAPIError) { |
| 163 | |