gemini_handler.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package relay
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "github.com/QuantumNous/new-api/common"
  9. "github.com/QuantumNous/new-api/constant"
  10. "github.com/QuantumNous/new-api/dto"
  11. "github.com/QuantumNous/new-api/logger"
  12. "github.com/QuantumNous/new-api/relay/channel/gemini"
  13. relaycommon "github.com/QuantumNous/new-api/relay/common"
  14. "github.com/QuantumNous/new-api/relay/helper"
  15. "github.com/QuantumNous/new-api/service"
  16. "github.com/QuantumNous/new-api/setting/model_setting"
  17. "github.com/QuantumNous/new-api/types"
  18. "github.com/gin-gonic/gin"
  19. )
  20. func isNoThinkingRequest(req *dto.GeminiChatRequest) bool {
  21. if req.GenerationConfig.ThinkingConfig != nil && req.GenerationConfig.ThinkingConfig.ThinkingBudget != nil {
  22. configBudget := req.GenerationConfig.ThinkingConfig.ThinkingBudget
  23. if configBudget != nil && *configBudget == 0 {
  24. // 如果思考预算为 0,则认为是非思考请求
  25. return true
  26. }
  27. }
  28. return false
  29. }
  30. func trimModelThinking(modelName string) string {
  31. // 去除模型名称中的 -nothinking 后缀
  32. if strings.HasSuffix(modelName, "-nothinking") {
  33. return strings.TrimSuffix(modelName, "-nothinking")
  34. }
  35. // 去除模型名称中的 -thinking 后缀
  36. if strings.HasSuffix(modelName, "-thinking") {
  37. return strings.TrimSuffix(modelName, "-thinking")
  38. }
  39. // 去除模型名称中的 -thinking-number
  40. if strings.Contains(modelName, "-thinking-") {
  41. parts := strings.Split(modelName, "-thinking-")
  42. if len(parts) > 1 {
  43. return parts[0] + "-thinking"
  44. }
  45. }
  46. return modelName
  47. }
  48. func GeminiHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
  49. info.InitChannelMeta(c)
  50. geminiReq, ok := info.Request.(*dto.GeminiChatRequest)
  51. if !ok {
  52. return types.NewErrorWithStatusCode(fmt.Errorf("invalid request type, expected *dto.GeminiChatRequest, got %T", info.Request), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
  53. }
  54. request, err := common.DeepCopy(geminiReq)
  55. if err != nil {
  56. return types.NewError(fmt.Errorf("failed to copy request to GeminiChatRequest: %w", err), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
  57. }
  58. // model mapped 模型映射
  59. err = helper.ModelMappedHelper(c, info, request)
  60. if err != nil {
  61. return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
  62. }
  63. if model_setting.GetGeminiSettings().ThinkingAdapterEnabled {
  64. if isNoThinkingRequest(request) {
  65. // check is thinking
  66. if !strings.Contains(info.OriginModelName, "-nothinking") {
  67. // try to get no thinking model price
  68. noThinkingModelName := info.OriginModelName + "-nothinking"
  69. containPrice := helper.ContainPriceOrRatio(noThinkingModelName)
  70. if containPrice {
  71. info.OriginModelName = noThinkingModelName
  72. info.UpstreamModelName = noThinkingModelName
  73. }
  74. }
  75. }
  76. if request.GenerationConfig.ThinkingConfig == nil {
  77. gemini.ThinkingAdaptor(request, info)
  78. }
  79. }
  80. adaptor := GetAdaptor(info.ApiType)
  81. if adaptor == nil {
  82. return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry())
  83. }
  84. adaptor.Init(info)
  85. if info.ChannelSetting.SystemPrompt != "" {
  86. if request.SystemInstructions == nil {
  87. request.SystemInstructions = &dto.GeminiChatContent{
  88. Parts: []dto.GeminiPart{
  89. {Text: info.ChannelSetting.SystemPrompt},
  90. },
  91. }
  92. } else if len(request.SystemInstructions.Parts) == 0 {
  93. request.SystemInstructions.Parts = []dto.GeminiPart{{Text: info.ChannelSetting.SystemPrompt}}
  94. } else if info.ChannelSetting.SystemPromptOverride {
  95. common.SetContextKey(c, constant.ContextKeySystemPromptOverride, true)
  96. merged := false
  97. for i := range request.SystemInstructions.Parts {
  98. if request.SystemInstructions.Parts[i].Text == "" {
  99. continue
  100. }
  101. request.SystemInstructions.Parts[i].Text = info.ChannelSetting.SystemPrompt + "\n" + request.SystemInstructions.Parts[i].Text
  102. merged = true
  103. break
  104. }
  105. if !merged {
  106. request.SystemInstructions.Parts = append([]dto.GeminiPart{{Text: info.ChannelSetting.SystemPrompt}}, request.SystemInstructions.Parts...)
  107. }
  108. }
  109. }
  110. // Clean up empty system instruction
  111. if request.SystemInstructions != nil {
  112. hasContent := false
  113. for _, part := range request.SystemInstructions.Parts {
  114. if part.Text != "" {
  115. hasContent = true
  116. break
  117. }
  118. }
  119. if !hasContent {
  120. request.SystemInstructions = nil
  121. }
  122. }
  123. var requestBody io.Reader
  124. if model_setting.GetGlobalSettings().PassThroughRequestEnabled || info.ChannelSetting.PassThroughBodyEnabled {
  125. body, err := common.GetRequestBody(c)
  126. if err != nil {
  127. return types.NewErrorWithStatusCode(err, types.ErrorCodeReadRequestBodyFailed, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
  128. }
  129. requestBody = bytes.NewReader(body)
  130. } else {
  131. // 使用 ConvertGeminiRequest 转换请求格式
  132. convertedRequest, err := adaptor.ConvertGeminiRequest(c, info, request)
  133. if err != nil {
  134. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  135. }
  136. jsonData, err := common.Marshal(convertedRequest)
  137. if err != nil {
  138. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  139. }
  140. // apply param override
  141. if len(info.ParamOverride) > 0 {
  142. jsonData, err = relaycommon.ApplyParamOverride(jsonData, info.ParamOverride, relaycommon.BuildParamOverrideContext(info))
  143. if err != nil {
  144. return types.NewError(err, types.ErrorCodeChannelParamOverrideInvalid, types.ErrOptionWithSkipRetry())
  145. }
  146. }
  147. logger.LogDebug(c, "Gemini request body: "+string(jsonData))
  148. requestBody = bytes.NewReader(jsonData)
  149. }
  150. resp, err := adaptor.DoRequest(c, info, requestBody)
  151. if err != nil {
  152. logger.LogError(c, "Do gemini request failed: "+err.Error())
  153. return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
  154. }
  155. statusCodeMappingStr := c.GetString("status_code_mapping")
  156. var httpResp *http.Response
  157. if resp != nil {
  158. httpResp = resp.(*http.Response)
  159. info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream")
  160. if httpResp.StatusCode != http.StatusOK {
  161. newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false)
  162. // reset status code 重置状态码
  163. service.ResetStatusCode(newAPIError, statusCodeMappingStr)
  164. return newAPIError
  165. }
  166. }
  167. usage, openaiErr := adaptor.DoResponse(c, resp.(*http.Response), info)
  168. if openaiErr != nil {
  169. service.ResetStatusCode(openaiErr, statusCodeMappingStr)
  170. return openaiErr
  171. }
  172. postConsumeQuota(c, info, usage.(*dto.Usage), "")
  173. return nil
  174. }
  175. func GeminiEmbeddingHandler(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types.NewAPIError) {
  176. info.InitChannelMeta(c)
  177. isBatch := strings.HasSuffix(c.Request.URL.Path, "batchEmbedContents")
  178. info.IsGeminiBatchEmbedding = isBatch
  179. var req dto.Request
  180. var err error
  181. var inputTexts []string
  182. if isBatch {
  183. batchRequest := &dto.GeminiBatchEmbeddingRequest{}
  184. err = common.UnmarshalBodyReusable(c, batchRequest)
  185. if err != nil {
  186. return types.NewError(err, types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
  187. }
  188. req = batchRequest
  189. for _, r := range batchRequest.Requests {
  190. for _, part := range r.Content.Parts {
  191. if part.Text != "" {
  192. inputTexts = append(inputTexts, part.Text)
  193. }
  194. }
  195. }
  196. } else {
  197. singleRequest := &dto.GeminiEmbeddingRequest{}
  198. err = common.UnmarshalBodyReusable(c, singleRequest)
  199. if err != nil {
  200. return types.NewError(err, types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
  201. }
  202. req = singleRequest
  203. for _, part := range singleRequest.Content.Parts {
  204. if part.Text != "" {
  205. inputTexts = append(inputTexts, part.Text)
  206. }
  207. }
  208. }
  209. err = helper.ModelMappedHelper(c, info, req)
  210. if err != nil {
  211. return types.NewError(err, types.ErrorCodeChannelModelMappedError, types.ErrOptionWithSkipRetry())
  212. }
  213. req.SetModelName("models/" + info.UpstreamModelName)
  214. adaptor := GetAdaptor(info.ApiType)
  215. if adaptor == nil {
  216. return types.NewError(fmt.Errorf("invalid api type: %d", info.ApiType), types.ErrorCodeInvalidApiType, types.ErrOptionWithSkipRetry())
  217. }
  218. adaptor.Init(info)
  219. var requestBody io.Reader
  220. jsonData, err := common.Marshal(req)
  221. if err != nil {
  222. return types.NewError(err, types.ErrorCodeConvertRequestFailed, types.ErrOptionWithSkipRetry())
  223. }
  224. // apply param override
  225. if len(info.ParamOverride) > 0 {
  226. reqMap := make(map[string]interface{})
  227. _ = common.Unmarshal(jsonData, &reqMap)
  228. for key, value := range info.ParamOverride {
  229. reqMap[key] = value
  230. }
  231. jsonData, err = common.Marshal(reqMap)
  232. if err != nil {
  233. return types.NewError(err, types.ErrorCodeChannelParamOverrideInvalid, types.ErrOptionWithSkipRetry())
  234. }
  235. }
  236. logger.LogDebug(c, "Gemini embedding request body: "+string(jsonData))
  237. requestBody = bytes.NewReader(jsonData)
  238. resp, err := adaptor.DoRequest(c, info, requestBody)
  239. if err != nil {
  240. logger.LogError(c, "Do gemini request failed: "+err.Error())
  241. return types.NewOpenAIError(err, types.ErrorCodeDoRequestFailed, http.StatusInternalServerError)
  242. }
  243. statusCodeMappingStr := c.GetString("status_code_mapping")
  244. var httpResp *http.Response
  245. if resp != nil {
  246. httpResp = resp.(*http.Response)
  247. if httpResp.StatusCode != http.StatusOK {
  248. newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false)
  249. service.ResetStatusCode(newAPIError, statusCodeMappingStr)
  250. return newAPIError
  251. }
  252. }
  253. usage, openaiErr := adaptor.DoResponse(c, resp.(*http.Response), info)
  254. if openaiErr != nil {
  255. service.ResetStatusCode(openaiErr, statusCodeMappingStr)
  256. return openaiErr
  257. }
  258. postConsumeQuota(c, info, usage.(*dto.Usage), "")
  259. return nil
  260. }