adaptor.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package volcengine
  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. channelconstant "github.com/QuantumNous/new-api/constant"
  14. "github.com/QuantumNous/new-api/dto"
  15. "github.com/QuantumNous/new-api/relay/channel"
  16. "github.com/QuantumNous/new-api/relay/channel/openai"
  17. relaycommon "github.com/QuantumNous/new-api/relay/common"
  18. "github.com/QuantumNous/new-api/relay/constant"
  19. "github.com/QuantumNous/new-api/types"
  20. "github.com/gin-gonic/gin"
  21. )
  22. type Adaptor struct {
  23. }
  24. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  25. //TODO implement me
  26. return nil, errors.New("not implemented")
  27. }
  28. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  29. adaptor := openai.Adaptor{}
  30. return adaptor.ConvertClaudeRequest(c, info, req)
  31. }
  32. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  33. if info.RelayMode != constant.RelayModeAudioSpeech {
  34. return nil, errors.New("unsupported audio relay mode")
  35. }
  36. appID, token, err := parseVolcengineAuth(info.ApiKey)
  37. if err != nil {
  38. return nil, err
  39. }
  40. voiceType := mapVoiceType(request.Voice)
  41. speedRatio := mapSpeedRatio(request.Speed)
  42. encoding := mapEncoding(request.ResponseFormat)
  43. c.Set("response_format", encoding)
  44. volcRequest := VolcengineTTSRequest{
  45. App: VolcengineTTSApp{
  46. AppID: appID,
  47. Token: token,
  48. Cluster: "volcano_tts",
  49. },
  50. User: VolcengineTTSUser{
  51. UID: "openai_relay_user",
  52. },
  53. Audio: VolcengineTTSAudio{
  54. VoiceType: voiceType,
  55. Encoding: encoding,
  56. SpeedRatio: speedRatio,
  57. Rate: 24000,
  58. },
  59. Request: VolcengineTTSReqInfo{
  60. ReqID: generateRequestID(),
  61. Text: request.Input,
  62. Operation: "query",
  63. Model: info.OriginModelName,
  64. },
  65. }
  66. jsonData, err := json.Marshal(volcRequest)
  67. if err != nil {
  68. return nil, fmt.Errorf("error marshalling volcengine request: %w", err)
  69. }
  70. return bytes.NewReader(jsonData), nil
  71. }
  72. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  73. switch info.RelayMode {
  74. case constant.RelayModeImagesGenerations:
  75. return request, nil
  76. case constant.RelayModeImagesEdits:
  77. var requestBody bytes.Buffer
  78. writer := multipart.NewWriter(&requestBody)
  79. writer.WriteField("model", request.Model)
  80. // 获取所有表单字段
  81. formData := c.Request.PostForm
  82. // 遍历表单字段并打印输出
  83. for key, values := range formData {
  84. if key == "model" {
  85. continue
  86. }
  87. for _, value := range values {
  88. writer.WriteField(key, value)
  89. }
  90. }
  91. // Parse the multipart form to handle both single image and multiple images
  92. if err := c.Request.ParseMultipartForm(32 << 20); err != nil { // 32MB max memory
  93. return nil, errors.New("failed to parse multipart form")
  94. }
  95. if c.Request.MultipartForm != nil && c.Request.MultipartForm.File != nil {
  96. // Check if "image" field exists in any form, including array notation
  97. var imageFiles []*multipart.FileHeader
  98. var exists bool
  99. // First check for standard "image" field
  100. if imageFiles, exists = c.Request.MultipartForm.File["image"]; !exists || len(imageFiles) == 0 {
  101. // If not found, check for "image[]" field
  102. if imageFiles, exists = c.Request.MultipartForm.File["image[]"]; !exists || len(imageFiles) == 0 {
  103. // If still not found, iterate through all fields to find any that start with "image["
  104. foundArrayImages := false
  105. for fieldName, files := range c.Request.MultipartForm.File {
  106. if strings.HasPrefix(fieldName, "image[") && len(files) > 0 {
  107. foundArrayImages = true
  108. for _, file := range files {
  109. imageFiles = append(imageFiles, file)
  110. }
  111. }
  112. }
  113. // If no image fields found at all
  114. if !foundArrayImages && (len(imageFiles) == 0) {
  115. return nil, errors.New("image is required")
  116. }
  117. }
  118. }
  119. // Process all image files
  120. for i, fileHeader := range imageFiles {
  121. file, err := fileHeader.Open()
  122. if err != nil {
  123. return nil, fmt.Errorf("failed to open image file %d: %w", i, err)
  124. }
  125. defer file.Close()
  126. // If multiple images, use image[] as the field name
  127. fieldName := "image"
  128. if len(imageFiles) > 1 {
  129. fieldName = "image[]"
  130. }
  131. // Determine MIME type based on file extension
  132. mimeType := detectImageMimeType(fileHeader.Filename)
  133. // Create a form file with the appropriate content type
  134. h := make(textproto.MIMEHeader)
  135. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldName, fileHeader.Filename))
  136. h.Set("Content-Type", mimeType)
  137. part, err := writer.CreatePart(h)
  138. if err != nil {
  139. return nil, fmt.Errorf("create form part failed for image %d: %w", i, err)
  140. }
  141. if _, err := io.Copy(part, file); err != nil {
  142. return nil, fmt.Errorf("copy file failed for image %d: %w", i, err)
  143. }
  144. }
  145. // Handle mask file if present
  146. if maskFiles, exists := c.Request.MultipartForm.File["mask"]; exists && len(maskFiles) > 0 {
  147. maskFile, err := maskFiles[0].Open()
  148. if err != nil {
  149. return nil, errors.New("failed to open mask file")
  150. }
  151. defer maskFile.Close()
  152. // Determine MIME type for mask file
  153. mimeType := detectImageMimeType(maskFiles[0].Filename)
  154. // Create a form file with the appropriate content type
  155. h := make(textproto.MIMEHeader)
  156. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="mask"; filename="%s"`, maskFiles[0].Filename))
  157. h.Set("Content-Type", mimeType)
  158. maskPart, err := writer.CreatePart(h)
  159. if err != nil {
  160. return nil, errors.New("create form file failed for mask")
  161. }
  162. if _, err := io.Copy(maskPart, maskFile); err != nil {
  163. return nil, errors.New("copy mask file failed")
  164. }
  165. }
  166. } else {
  167. return nil, errors.New("no multipart form data found")
  168. }
  169. // 关闭 multipart 编写器以设置分界线
  170. writer.Close()
  171. c.Request.Header.Set("Content-Type", writer.FormDataContentType())
  172. return bytes.NewReader(requestBody.Bytes()), nil
  173. default:
  174. return request, nil
  175. }
  176. }
  177. // detectImageMimeType determines the MIME type based on the file extension
  178. func detectImageMimeType(filename string) string {
  179. ext := strings.ToLower(filepath.Ext(filename))
  180. switch ext {
  181. case ".jpg", ".jpeg":
  182. return "image/jpeg"
  183. case ".png":
  184. return "image/png"
  185. case ".webp":
  186. return "image/webp"
  187. default:
  188. // Try to detect from extension if possible
  189. if strings.HasPrefix(ext, ".jp") {
  190. return "image/jpeg"
  191. }
  192. // Default to png as a fallback
  193. return "image/png"
  194. }
  195. }
  196. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  197. }
  198. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  199. baseUrl := info.ChannelBaseUrl
  200. if baseUrl == "" {
  201. baseUrl = channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeVolcEngine]
  202. }
  203. switch info.RelayFormat {
  204. case types.RelayFormatClaude:
  205. if strings.HasPrefix(info.UpstreamModelName, "bot") {
  206. return fmt.Sprintf("%s/api/v3/bots/chat/completions", baseUrl), nil
  207. }
  208. return fmt.Sprintf("%s/api/v3/chat/completions", baseUrl), nil
  209. default:
  210. switch info.RelayMode {
  211. case constant.RelayModeChatCompletions:
  212. if strings.HasPrefix(info.UpstreamModelName, "bot") {
  213. return fmt.Sprintf("%s/api/v3/bots/chat/completions", baseUrl), nil
  214. }
  215. return fmt.Sprintf("%s/api/v3/chat/completions", baseUrl), nil
  216. case constant.RelayModeEmbeddings:
  217. return fmt.Sprintf("%s/api/v3/embeddings", baseUrl), nil
  218. case constant.RelayModeImagesGenerations:
  219. return fmt.Sprintf("%s/api/v3/images/generations", baseUrl), nil
  220. case constant.RelayModeImagesEdits:
  221. return fmt.Sprintf("%s/api/v3/images/edits", baseUrl), nil
  222. case constant.RelayModeRerank:
  223. return fmt.Sprintf("%s/api/v3/rerank", baseUrl), nil
  224. case constant.RelayModeAudioSpeech:
  225. // 只有当 baseUrl 是火山默认的官方Url时才改为官方的的TTS接口,否则走透传的New接口
  226. if baseUrl == channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeVolcEngine] {
  227. return "https://openspeech.bytedance.com/api/v1/tts", nil
  228. }
  229. return fmt.Sprintf("%s/v1/audio/speech", baseUrl), nil
  230. default:
  231. }
  232. }
  233. return "", fmt.Errorf("unsupported relay mode: %d", info.RelayMode)
  234. }
  235. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  236. channel.SetupApiRequestHeader(info, c, req)
  237. if info.RelayMode == constant.RelayModeAudioSpeech {
  238. parts := strings.Split(info.ApiKey, "|")
  239. if len(parts) == 2 {
  240. req.Set("Authorization", "Bearer;"+parts[1])
  241. }
  242. req.Set("Content-Type", "application/json")
  243. return nil
  244. }
  245. req.Set("Authorization", "Bearer "+info.ApiKey)
  246. return nil
  247. }
  248. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  249. if request == nil {
  250. return nil, errors.New("request is nil")
  251. }
  252. // 适配 方舟deepseek混合模型 的 thinking 后缀
  253. if strings.HasSuffix(info.UpstreamModelName, "-thinking") && strings.HasPrefix(info.UpstreamModelName, "deepseek") {
  254. info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-thinking")
  255. request.Model = info.UpstreamModelName
  256. request.THINKING = json.RawMessage(`{"type": "enabled"}`)
  257. }
  258. return request, nil
  259. }
  260. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  261. return nil, nil
  262. }
  263. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  264. return request, nil
  265. }
  266. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  267. // TODO implement me
  268. return nil, errors.New("not implemented")
  269. }
  270. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  271. return channel.DoApiRequest(a, c, info, requestBody)
  272. }
  273. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  274. if info.RelayMode == constant.RelayModeAudioSpeech {
  275. encoding := mapEncoding(c.GetString("response_format"))
  276. return handleTTSResponse(c, resp, info, encoding)
  277. }
  278. adaptor := openai.Adaptor{}
  279. usage, err = adaptor.DoResponse(c, resp, info)
  280. return
  281. }
  282. func (a *Adaptor) GetModelList() []string {
  283. return ModelList
  284. }
  285. func (a *Adaptor) GetChannelName() string {
  286. return ChannelName
  287. }