adaptor.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package openai
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "mime/multipart"
  9. "net/http"
  10. "net/textproto"
  11. "path/filepath"
  12. "strings"
  13. "github.com/QuantumNous/new-api/common"
  14. "github.com/QuantumNous/new-api/constant"
  15. "github.com/QuantumNous/new-api/dto"
  16. "github.com/QuantumNous/new-api/logger"
  17. "github.com/QuantumNous/new-api/relay/channel"
  18. "github.com/QuantumNous/new-api/relay/channel/ai360"
  19. "github.com/QuantumNous/new-api/relay/channel/lingyiwanwu"
  20. //"github.com/QuantumNous/new-api/relay/channel/minimax"
  21. "github.com/QuantumNous/new-api/relay/channel/openrouter"
  22. "github.com/QuantumNous/new-api/relay/channel/xinference"
  23. relaycommon "github.com/QuantumNous/new-api/relay/common"
  24. "github.com/QuantumNous/new-api/relay/common_handler"
  25. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  26. "github.com/QuantumNous/new-api/service"
  27. "github.com/QuantumNous/new-api/setting/model_setting"
  28. "github.com/QuantumNous/new-api/types"
  29. "github.com/gin-gonic/gin"
  30. )
  31. type Adaptor struct {
  32. ChannelType int
  33. ResponseFormat string
  34. }
  35. // parseReasoningEffortFromModelSuffix 从模型名称中解析推理级别
  36. // support OAI models: o1-mini/o3-mini/o4-mini/o1/o3 etc...
  37. // minimal effort only available in gpt-5
  38. func parseReasoningEffortFromModelSuffix(model string) (string, string) {
  39. effortSuffixes := []string{"-high", "-minimal", "-low", "-medium", "-none", "-xhigh"}
  40. for _, suffix := range effortSuffixes {
  41. if strings.HasSuffix(model, suffix) {
  42. effort := strings.TrimPrefix(suffix, "-")
  43. originModel := strings.TrimSuffix(model, suffix)
  44. return effort, originModel
  45. }
  46. }
  47. return "", model
  48. }
  49. func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
  50. // 使用 service.GeminiToOpenAIRequest 转换请求格式
  51. openaiRequest, err := service.GeminiToOpenAIRequest(request, info)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return a.ConvertOpenAIRequest(c, info, openaiRequest)
  56. }
  57. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
  58. //if !strings.Contains(request.Model, "claude") {
  59. // return nil, fmt.Errorf("you are using openai channel type with path /v1/messages, only claude model supported convert, but got %s", request.Model)
  60. //}
  61. //if common.DebugEnabled {
  62. // bodyBytes := []byte(common.GetJsonString(request))
  63. // err := os.WriteFile(fmt.Sprintf("claude_request_%s.txt", c.GetString(common.RequestIdKey)), bodyBytes, 0644)
  64. // if err != nil {
  65. // println(fmt.Sprintf("failed to save request body to file: %v", err))
  66. // }
  67. //}
  68. aiRequest, err := service.ClaudeToOpenAIRequest(*request, info)
  69. if err != nil {
  70. return nil, err
  71. }
  72. //if common.DebugEnabled {
  73. // println(fmt.Sprintf("convert claude to openai request result: %s", common.GetJsonString(aiRequest)))
  74. // // Save request body to file for debugging
  75. // bodyBytes := []byte(common.GetJsonString(aiRequest))
  76. // err = os.WriteFile(fmt.Sprintf("claude_to_openai_request_%s.txt", c.GetString(common.RequestIdKey)), bodyBytes, 0644)
  77. // if err != nil {
  78. // println(fmt.Sprintf("failed to save request body to file: %v", err))
  79. // }
  80. //}
  81. if info.SupportStreamOptions && info.IsStream {
  82. aiRequest.StreamOptions = &dto.StreamOptions{
  83. IncludeUsage: true,
  84. }
  85. }
  86. return a.ConvertOpenAIRequest(c, info, aiRequest)
  87. }
  88. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  89. a.ChannelType = info.ChannelType
  90. // initialize ThinkingContentInfo when thinking_to_content is enabled
  91. if info.ChannelSetting.ThinkingToContent {
  92. info.ThinkingContentInfo = relaycommon.ThinkingContentInfo{
  93. IsFirstThinkingContent: true,
  94. SendLastThinkingContent: false,
  95. HasSentThinkingContent: false,
  96. }
  97. }
  98. }
  99. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  100. if info.RelayMode == relayconstant.RelayModeRealtime {
  101. if strings.HasPrefix(info.ChannelBaseUrl, "https://") {
  102. baseUrl := strings.TrimPrefix(info.ChannelBaseUrl, "https://")
  103. baseUrl = "wss://" + baseUrl
  104. info.ChannelBaseUrl = baseUrl
  105. } else if strings.HasPrefix(info.ChannelBaseUrl, "http://") {
  106. baseUrl := strings.TrimPrefix(info.ChannelBaseUrl, "http://")
  107. baseUrl = "ws://" + baseUrl
  108. info.ChannelBaseUrl = baseUrl
  109. }
  110. }
  111. switch info.ChannelType {
  112. case constant.ChannelTypeAzure:
  113. apiVersion := info.ApiVersion
  114. if apiVersion == "" {
  115. apiVersion = constant.AzureDefaultAPIVersion
  116. }
  117. // https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?pivots=rest-api&tabs=command-line#rest-api
  118. requestURL := strings.Split(info.RequestURLPath, "?")[0]
  119. requestURL = fmt.Sprintf("%s?api-version=%s", requestURL, apiVersion)
  120. task := strings.TrimPrefix(requestURL, "/v1/")
  121. if info.RelayFormat == types.RelayFormatClaude {
  122. task = strings.TrimPrefix(task, "messages")
  123. task = "chat/completions" + task
  124. }
  125. // 特殊处理 responses API
  126. if info.RelayMode == relayconstant.RelayModeResponses {
  127. responsesApiVersion := "preview"
  128. subUrl := "/openai/v1/responses"
  129. if strings.Contains(info.ChannelBaseUrl, "cognitiveservices.azure.com") {
  130. subUrl = "/openai/responses"
  131. responsesApiVersion = apiVersion
  132. }
  133. if info.ChannelOtherSettings.AzureResponsesVersion != "" {
  134. responsesApiVersion = info.ChannelOtherSettings.AzureResponsesVersion
  135. }
  136. requestURL = fmt.Sprintf("%s?api-version=%s", subUrl, responsesApiVersion)
  137. return relaycommon.GetFullRequestURL(info.ChannelBaseUrl, requestURL, info.ChannelType), nil
  138. }
  139. model_ := info.UpstreamModelName
  140. // 2025年5月10日后创建的渠道不移除.
  141. if info.ChannelCreateTime < constant.AzureNoRemoveDotTime {
  142. model_ = strings.Replace(model_, ".", "", -1)
  143. }
  144. // https://github.com/songquanpeng/one-api/issues/67
  145. requestURL = fmt.Sprintf("/openai/deployments/%s/%s", model_, task)
  146. if info.RelayMode == relayconstant.RelayModeRealtime {
  147. requestURL = fmt.Sprintf("/openai/realtime?deployment=%s&api-version=%s", model_, apiVersion)
  148. }
  149. return relaycommon.GetFullRequestURL(info.ChannelBaseUrl, requestURL, info.ChannelType), nil
  150. //case constant.ChannelTypeMiniMax:
  151. // return minimax.GetRequestURL(info)
  152. case constant.ChannelTypeCustom:
  153. url := info.ChannelBaseUrl
  154. url = strings.Replace(url, "{model}", info.UpstreamModelName, -1)
  155. return url, nil
  156. default:
  157. if (info.RelayFormat == types.RelayFormatClaude || info.RelayFormat == types.RelayFormatGemini) &&
  158. info.RelayMode != relayconstant.RelayModeResponses &&
  159. info.RelayMode != relayconstant.RelayModeResponsesCompact {
  160. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  161. }
  162. return relaycommon.GetFullRequestURL(info.ChannelBaseUrl, info.RequestURLPath, info.ChannelType), nil
  163. }
  164. }
  165. func (a *Adaptor) SetupRequestHeader(c *gin.Context, header *http.Header, info *relaycommon.RelayInfo) error {
  166. channel.SetupApiRequestHeader(info, c, header)
  167. if info.ChannelType == constant.ChannelTypeAzure {
  168. header.Set("api-key", info.ApiKey)
  169. return nil
  170. }
  171. if info.ChannelType == constant.ChannelTypeOpenAI && "" != info.Organization {
  172. header.Set("OpenAI-Organization", info.Organization)
  173. }
  174. // 检查 Header Override 是否已设置 Authorization,如果已设置则跳过默认设置
  175. // 这样可以避免在 Header Override 应用时被覆盖(虽然 Header Override 会在之后应用,但这里作为额外保护)
  176. hasAuthOverride := false
  177. if len(info.HeadersOverride) > 0 {
  178. for k := range info.HeadersOverride {
  179. if strings.EqualFold(k, "Authorization") {
  180. hasAuthOverride = true
  181. break
  182. }
  183. }
  184. }
  185. if info.RelayMode == relayconstant.RelayModeRealtime {
  186. swp := c.Request.Header.Get("Sec-WebSocket-Protocol")
  187. if swp != "" {
  188. items := []string{
  189. "realtime",
  190. "openai-insecure-api-key." + info.ApiKey,
  191. "openai-beta.realtime-v1",
  192. }
  193. header.Set("Sec-WebSocket-Protocol", strings.Join(items, ","))
  194. //req.Header.Set("Sec-WebSocket-Key", c.Request.Header.Get("Sec-WebSocket-Key"))
  195. //req.Header.Set("Sec-Websocket-Extensions", c.Request.Header.Get("Sec-Websocket-Extensions"))
  196. //req.Header.Set("Sec-Websocket-Version", c.Request.Header.Get("Sec-Websocket-Version"))
  197. } else {
  198. header.Set("openai-beta", "realtime=v1")
  199. if !hasAuthOverride {
  200. header.Set("Authorization", "Bearer "+info.ApiKey)
  201. }
  202. }
  203. } else {
  204. if !hasAuthOverride {
  205. header.Set("Authorization", "Bearer "+info.ApiKey)
  206. }
  207. }
  208. if info.ChannelType == constant.ChannelTypeOpenRouter {
  209. header.Set("HTTP-Referer", "https://www.newapi.ai")
  210. header.Set("X-Title", "New API")
  211. }
  212. return nil
  213. }
  214. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  215. if request == nil {
  216. return nil, errors.New("request is nil")
  217. }
  218. if info.ChannelType != constant.ChannelTypeOpenAI && info.ChannelType != constant.ChannelTypeAzure {
  219. request.StreamOptions = nil
  220. }
  221. if info.ChannelType == constant.ChannelTypeOpenRouter {
  222. if len(request.Usage) == 0 {
  223. request.Usage = json.RawMessage(`{"include":true}`)
  224. }
  225. // 适配 OpenRouter 的 thinking 后缀
  226. if !model_setting.ShouldPreserveThinkingSuffix(info.OriginModelName) &&
  227. strings.HasSuffix(info.UpstreamModelName, "-thinking") {
  228. info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-thinking")
  229. request.Model = info.UpstreamModelName
  230. if len(request.Reasoning) == 0 {
  231. reasoning := map[string]any{
  232. "enabled": true,
  233. }
  234. if request.ReasoningEffort != "" && request.ReasoningEffort != "none" {
  235. reasoning["effort"] = request.ReasoningEffort
  236. }
  237. marshal, err := common.Marshal(reasoning)
  238. if err != nil {
  239. return nil, fmt.Errorf("error marshalling reasoning: %w", err)
  240. }
  241. request.Reasoning = marshal
  242. }
  243. // 清空多余的ReasoningEffort
  244. request.ReasoningEffort = ""
  245. } else {
  246. if len(request.Reasoning) == 0 {
  247. // 适配 OpenAI 的 ReasoningEffort 格式
  248. if request.ReasoningEffort != "" {
  249. reasoning := map[string]any{
  250. "enabled": true,
  251. }
  252. if request.ReasoningEffort != "none" {
  253. reasoning["effort"] = request.ReasoningEffort
  254. marshal, err := common.Marshal(reasoning)
  255. if err != nil {
  256. return nil, fmt.Errorf("error marshalling reasoning: %w", err)
  257. }
  258. request.Reasoning = marshal
  259. }
  260. }
  261. }
  262. request.ReasoningEffort = ""
  263. }
  264. // https://docs.anthropic.com/en/api/openai-sdk#extended-thinking-support
  265. // 没有做排除3.5Haiku等,要出问题再加吧,最佳兼容性(不是
  266. if request.THINKING != nil && strings.HasPrefix(info.UpstreamModelName, "anthropic") {
  267. var thinking dto.Thinking // Claude标准Thinking格式
  268. if err := json.Unmarshal(request.THINKING, &thinking); err != nil {
  269. return nil, fmt.Errorf("error Unmarshal thinking: %w", err)
  270. }
  271. // 只有当 thinking.Type 是 "enabled" 时才处理
  272. if thinking.Type == "enabled" {
  273. // 检查 BudgetTokens 是否为 nil
  274. if thinking.BudgetTokens == nil {
  275. return nil, fmt.Errorf("BudgetTokens is nil when thinking is enabled")
  276. }
  277. reasoning := openrouter.RequestReasoning{
  278. MaxTokens: *thinking.BudgetTokens,
  279. }
  280. marshal, err := common.Marshal(reasoning)
  281. if err != nil {
  282. return nil, fmt.Errorf("error marshalling reasoning: %w", err)
  283. }
  284. request.Reasoning = marshal
  285. }
  286. // 清空 THINKING
  287. request.THINKING = nil
  288. }
  289. }
  290. if strings.HasPrefix(info.UpstreamModelName, "o") || strings.HasPrefix(info.UpstreamModelName, "gpt-5") {
  291. if request.MaxCompletionTokens == 0 && request.MaxTokens != 0 {
  292. request.MaxCompletionTokens = request.MaxTokens
  293. request.MaxTokens = 0
  294. }
  295. if strings.HasPrefix(info.UpstreamModelName, "o") {
  296. request.Temperature = nil
  297. }
  298. // gpt-5系列模型适配 归零不再支持的参数
  299. if strings.HasPrefix(info.UpstreamModelName, "gpt-5") {
  300. request.Temperature = nil
  301. request.TopP = 0 // oai 的 top_p 默认值是 1.0,但是为了 omitempty 属性直接不传,这里显式设置为 0
  302. request.LogProbs = false
  303. }
  304. // 转换模型推理力度后缀
  305. effort, originModel := parseReasoningEffortFromModelSuffix(info.UpstreamModelName)
  306. if effort != "" {
  307. request.ReasoningEffort = effort
  308. info.UpstreamModelName = originModel
  309. request.Model = originModel
  310. }
  311. info.ReasoningEffort = request.ReasoningEffort
  312. // o系列模型developer适配(o1-mini除外)
  313. if !strings.HasPrefix(info.UpstreamModelName, "o1-mini") && !strings.HasPrefix(info.UpstreamModelName, "o1-preview") {
  314. //修改第一个Message的内容,将system改为developer
  315. if len(request.Messages) > 0 && request.Messages[0].Role == "system" {
  316. request.Messages[0].Role = "developer"
  317. }
  318. }
  319. }
  320. return request, nil
  321. }
  322. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  323. return request, nil
  324. }
  325. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  326. return request, nil
  327. }
  328. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  329. a.ResponseFormat = request.ResponseFormat
  330. if info.RelayMode == relayconstant.RelayModeAudioSpeech {
  331. jsonData, err := json.Marshal(request)
  332. if err != nil {
  333. return nil, fmt.Errorf("error marshalling object: %w", err)
  334. }
  335. return bytes.NewReader(jsonData), nil
  336. } else {
  337. var requestBody bytes.Buffer
  338. writer := multipart.NewWriter(&requestBody)
  339. writer.WriteField("model", request.Model)
  340. formData, err2 := common.ParseMultipartFormReusable(c)
  341. if err2 != nil {
  342. return nil, fmt.Errorf("error parsing multipart form: %w", err2)
  343. }
  344. // 打印类似 curl 命令格式的信息
  345. logger.LogDebug(c.Request.Context(), fmt.Sprintf("--form 'model=\"%s\"'", request.Model))
  346. // 遍历表单字段并打印输出
  347. for key, values := range formData.Value {
  348. if key == "model" {
  349. continue
  350. }
  351. for _, value := range values {
  352. writer.WriteField(key, value)
  353. logger.LogDebug(c.Request.Context(), fmt.Sprintf("--form '%s=\"%s\"'", key, value))
  354. }
  355. }
  356. // 从 formData 中获取文件
  357. fileHeaders := formData.File["file"]
  358. if len(fileHeaders) == 0 {
  359. return nil, errors.New("file is required")
  360. }
  361. // 使用 formData 中的第一个文件
  362. fileHeader := fileHeaders[0]
  363. logger.LogDebug(c.Request.Context(), fmt.Sprintf("--form 'file=@\"%s\"' (size: %d bytes, content-type: %s)",
  364. fileHeader.Filename, fileHeader.Size, fileHeader.Header.Get("Content-Type")))
  365. file, err := fileHeader.Open()
  366. if err != nil {
  367. return nil, fmt.Errorf("error opening audio file: %v", err)
  368. }
  369. defer file.Close()
  370. part, err := writer.CreateFormFile("file", fileHeader.Filename)
  371. if err != nil {
  372. return nil, errors.New("create form file failed")
  373. }
  374. if _, err := io.Copy(part, file); err != nil {
  375. return nil, errors.New("copy file failed")
  376. }
  377. // 关闭 multipart 编写器以设置分界线
  378. writer.Close()
  379. c.Request.Header.Set("Content-Type", writer.FormDataContentType())
  380. logger.LogDebug(c.Request.Context(), fmt.Sprintf("--header 'Content-Type: %s'", writer.FormDataContentType()))
  381. return &requestBody, nil
  382. }
  383. }
  384. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  385. switch info.RelayMode {
  386. case relayconstant.RelayModeImagesEdits:
  387. var requestBody bytes.Buffer
  388. writer := multipart.NewWriter(&requestBody)
  389. writer.WriteField("model", request.Model)
  390. // 使用已解析的 multipart 表单,避免重复解析
  391. mf := c.Request.MultipartForm
  392. if mf == nil {
  393. if _, err := c.MultipartForm(); err != nil {
  394. return nil, errors.New("failed to parse multipart form")
  395. }
  396. mf = c.Request.MultipartForm
  397. }
  398. // 写入所有非文件字段
  399. if mf != nil {
  400. for key, values := range mf.Value {
  401. if key == "model" {
  402. continue
  403. }
  404. for _, value := range values {
  405. writer.WriteField(key, value)
  406. }
  407. }
  408. }
  409. if mf != nil && mf.File != nil {
  410. // Check if "image" field exists in any form, including array notation
  411. var imageFiles []*multipart.FileHeader
  412. var exists bool
  413. // First check for standard "image" field
  414. if imageFiles, exists = mf.File["image"]; !exists || len(imageFiles) == 0 {
  415. // If not found, check for "image[]" field
  416. if imageFiles, exists = mf.File["image[]"]; !exists || len(imageFiles) == 0 {
  417. // If still not found, iterate through all fields to find any that start with "image["
  418. foundArrayImages := false
  419. for fieldName, files := range mf.File {
  420. if strings.HasPrefix(fieldName, "image[") && len(files) > 0 {
  421. foundArrayImages = true
  422. imageFiles = append(imageFiles, files...)
  423. }
  424. }
  425. // If no image fields found at all
  426. if !foundArrayImages && (len(imageFiles) == 0) {
  427. return nil, errors.New("image is required")
  428. }
  429. }
  430. }
  431. // Process all image files
  432. for i, fileHeader := range imageFiles {
  433. file, err := fileHeader.Open()
  434. if err != nil {
  435. return nil, fmt.Errorf("failed to open image file %d: %w", i, err)
  436. }
  437. // If multiple images, use image[] as the field name
  438. fieldName := "image"
  439. if len(imageFiles) > 1 {
  440. fieldName = "image[]"
  441. }
  442. // Determine MIME type based on file extension
  443. mimeType := detectImageMimeType(fileHeader.Filename)
  444. // Create a form file with the appropriate content type
  445. h := make(textproto.MIMEHeader)
  446. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldName, fileHeader.Filename))
  447. h.Set("Content-Type", mimeType)
  448. part, err := writer.CreatePart(h)
  449. if err != nil {
  450. return nil, fmt.Errorf("create form part failed for image %d: %w", i, err)
  451. }
  452. if _, err := io.Copy(part, file); err != nil {
  453. return nil, fmt.Errorf("copy file failed for image %d: %w", i, err)
  454. }
  455. // 复制完立即关闭,避免在循环内使用 defer 占用资源
  456. _ = file.Close()
  457. }
  458. // Handle mask file if present
  459. if maskFiles, exists := mf.File["mask"]; exists && len(maskFiles) > 0 {
  460. maskFile, err := maskFiles[0].Open()
  461. if err != nil {
  462. return nil, errors.New("failed to open mask file")
  463. }
  464. // 复制完立即关闭,避免在循环内使用 defer 占用资源
  465. // Determine MIME type for mask file
  466. mimeType := detectImageMimeType(maskFiles[0].Filename)
  467. // Create a form file with the appropriate content type
  468. h := make(textproto.MIMEHeader)
  469. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="mask"; filename="%s"`, maskFiles[0].Filename))
  470. h.Set("Content-Type", mimeType)
  471. maskPart, err := writer.CreatePart(h)
  472. if err != nil {
  473. return nil, errors.New("create form file failed for mask")
  474. }
  475. if _, err := io.Copy(maskPart, maskFile); err != nil {
  476. return nil, errors.New("copy mask file failed")
  477. }
  478. _ = maskFile.Close()
  479. }
  480. } else {
  481. return nil, errors.New("no multipart form data found")
  482. }
  483. // 关闭 multipart 编写器以设置分界线
  484. writer.Close()
  485. c.Request.Header.Set("Content-Type", writer.FormDataContentType())
  486. return &requestBody, nil
  487. default:
  488. return request, nil
  489. }
  490. }
  491. // detectImageMimeType determines the MIME type based on the file extension
  492. func detectImageMimeType(filename string) string {
  493. ext := strings.ToLower(filepath.Ext(filename))
  494. switch ext {
  495. case ".jpg", ".jpeg":
  496. return "image/jpeg"
  497. case ".png":
  498. return "image/png"
  499. case ".webp":
  500. return "image/webp"
  501. default:
  502. // Try to detect from extension if possible
  503. if strings.HasPrefix(ext, ".jp") {
  504. return "image/jpeg"
  505. }
  506. // Default to png as a fallback
  507. return "image/png"
  508. }
  509. }
  510. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  511. // 转换模型推理力度后缀
  512. effort, originModel := parseReasoningEffortFromModelSuffix(request.Model)
  513. if effort != "" {
  514. if request.Reasoning == nil {
  515. request.Reasoning = &dto.Reasoning{
  516. Effort: effort,
  517. }
  518. } else {
  519. request.Reasoning.Effort = effort
  520. }
  521. request.Model = originModel
  522. }
  523. if info != nil && request.Reasoning != nil && request.Reasoning.Effort != "" {
  524. info.ReasoningEffort = request.Reasoning.Effort
  525. }
  526. return request, nil
  527. }
  528. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  529. if info.RelayMode == relayconstant.RelayModeAudioTranscription ||
  530. info.RelayMode == relayconstant.RelayModeAudioTranslation ||
  531. info.RelayMode == relayconstant.RelayModeImagesEdits {
  532. return channel.DoFormRequest(a, c, info, requestBody)
  533. } else if info.RelayMode == relayconstant.RelayModeRealtime {
  534. return channel.DoWssRequest(a, c, info, requestBody)
  535. } else {
  536. return channel.DoApiRequest(a, c, info, requestBody)
  537. }
  538. }
  539. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  540. switch info.RelayMode {
  541. case relayconstant.RelayModeRealtime:
  542. err, usage = OpenaiRealtimeHandler(c, info)
  543. case relayconstant.RelayModeAudioSpeech:
  544. usage = OpenaiTTSHandler(c, resp, info)
  545. case relayconstant.RelayModeAudioTranslation:
  546. fallthrough
  547. case relayconstant.RelayModeAudioTranscription:
  548. err, usage = OpenaiSTTHandler(c, resp, info, a.ResponseFormat)
  549. case relayconstant.RelayModeImagesGenerations, relayconstant.RelayModeImagesEdits:
  550. usage, err = OpenaiHandlerWithUsage(c, info, resp)
  551. case relayconstant.RelayModeRerank:
  552. usage, err = common_handler.RerankHandler(c, info, resp)
  553. case relayconstant.RelayModeResponses:
  554. if info.IsStream {
  555. usage, err = OaiResponsesStreamHandler(c, info, resp)
  556. } else {
  557. usage, err = OaiResponsesHandler(c, info, resp)
  558. }
  559. case relayconstant.RelayModeResponsesCompact:
  560. usage, err = OaiResponsesCompactionHandler(c, resp)
  561. default:
  562. if info.IsStream {
  563. usage, err = OaiStreamHandler(c, info, resp)
  564. } else {
  565. usage, err = OpenaiHandler(c, info, resp)
  566. }
  567. }
  568. return
  569. }
  570. func (a *Adaptor) GetModelList() []string {
  571. switch a.ChannelType {
  572. case constant.ChannelType360:
  573. return ai360.ModelList
  574. case constant.ChannelTypeLingYiWanWu:
  575. return lingyiwanwu.ModelList
  576. //case constant.ChannelTypeMiniMax:
  577. // return minimax.ModelList
  578. case constant.ChannelTypeXinference:
  579. return xinference.ModelList
  580. case constant.ChannelTypeOpenRouter:
  581. return openrouter.ModelList
  582. default:
  583. return ModelList
  584. }
  585. }
  586. func (a *Adaptor) GetChannelName() string {
  587. switch a.ChannelType {
  588. case constant.ChannelType360:
  589. return ai360.ChannelName
  590. case constant.ChannelTypeLingYiWanWu:
  591. return lingyiwanwu.ChannelName
  592. //case constant.ChannelTypeMiniMax:
  593. // return minimax.ChannelName
  594. case constant.ChannelTypeXinference:
  595. return xinference.ChannelName
  596. case constant.ChannelTypeOpenRouter:
  597. return openrouter.ChannelName
  598. default:
  599. return ChannelName
  600. }
  601. }