adaptor.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package volcengine
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "path/filepath"
  10. "strings"
  11. channelconstant "github.com/QuantumNous/new-api/constant"
  12. "github.com/QuantumNous/new-api/dto"
  13. "github.com/QuantumNous/new-api/relay/channel"
  14. "github.com/QuantumNous/new-api/relay/channel/claude"
  15. "github.com/QuantumNous/new-api/relay/channel/openai"
  16. relaycommon "github.com/QuantumNous/new-api/relay/common"
  17. "github.com/QuantumNous/new-api/relay/constant"
  18. "github.com/QuantumNous/new-api/setting/model_setting"
  19. "github.com/QuantumNous/new-api/types"
  20. "github.com/gin-gonic/gin"
  21. "github.com/samber/lo"
  22. )
  23. const (
  24. contextKeyTTSRequest = "volcengine_tts_request"
  25. contextKeyResponseFormat = "response_format"
  26. )
  27. type Adaptor struct {
  28. }
  29. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  30. //TODO implement me
  31. return nil, errors.New("not implemented")
  32. }
  33. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  34. if _, ok := channelconstant.ChannelSpecialBases[info.ChannelBaseUrl]; ok {
  35. adaptor := claude.Adaptor{}
  36. return adaptor.ConvertClaudeRequest(c, info, req)
  37. }
  38. adaptor := openai.Adaptor{}
  39. return adaptor.ConvertClaudeRequest(c, info, req)
  40. }
  41. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  42. if info.RelayMode != constant.RelayModeAudioSpeech {
  43. return nil, errors.New("unsupported audio relay mode")
  44. }
  45. appID, token, err := parseVolcengineAuth(info.ApiKey)
  46. if err != nil {
  47. return nil, err
  48. }
  49. voiceType := mapVoiceType(request.Voice)
  50. speedRatio := lo.FromPtrOr(request.Speed, 0.0)
  51. encoding := mapEncoding(request.ResponseFormat)
  52. c.Set(contextKeyResponseFormat, encoding)
  53. volcRequest := VolcengineTTSRequest{
  54. App: VolcengineTTSApp{
  55. AppID: appID,
  56. Token: token,
  57. Cluster: "volcano_tts",
  58. },
  59. User: VolcengineTTSUser{
  60. UID: "openai_relay_user",
  61. },
  62. Audio: VolcengineTTSAudio{
  63. VoiceType: voiceType,
  64. Encoding: encoding,
  65. SpeedRatio: speedRatio,
  66. Rate: 24000,
  67. },
  68. Request: VolcengineTTSReqInfo{
  69. ReqID: generateRequestID(),
  70. Text: request.Input,
  71. Operation: "submit",
  72. Model: info.OriginModelName,
  73. },
  74. }
  75. if len(request.Metadata) > 0 {
  76. if err = json.Unmarshal(request.Metadata, &volcRequest); err != nil {
  77. return nil, fmt.Errorf("error unmarshalling metadata to volcengine request: %w", err)
  78. }
  79. }
  80. c.Set(contextKeyTTSRequest, volcRequest)
  81. if volcRequest.Request.Operation == "submit" {
  82. info.IsStream = true
  83. }
  84. jsonData, err := json.Marshal(volcRequest)
  85. if err != nil {
  86. return nil, fmt.Errorf("error marshalling volcengine request: %w", err)
  87. }
  88. return bytes.NewReader(jsonData), nil
  89. }
  90. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  91. switch info.RelayMode {
  92. case constant.RelayModeImagesGenerations:
  93. return request, nil
  94. // 根据官方文档,并没有发现豆包生图支持表单请求:https://www.volcengine.com/docs/82379/1824121
  95. //case constant.RelayModeImagesEdits:
  96. //
  97. // var requestBody bytes.Buffer
  98. // writer := multipart.NewWriter(&requestBody)
  99. //
  100. // writer.WriteField("model", request.Model)
  101. //
  102. // formData := c.Request.PostForm
  103. // for key, values := range formData {
  104. // if key == "model" {
  105. // continue
  106. // }
  107. // for _, value := range values {
  108. // writer.WriteField(key, value)
  109. // }
  110. // }
  111. //
  112. // if err := c.Request.ParseMultipartForm(32 << 20); err != nil {
  113. // return nil, errors.New("failed to parse multipart form")
  114. // }
  115. //
  116. // if c.Request.MultipartForm != nil && c.Request.MultipartForm.File != nil {
  117. // var imageFiles []*multipart.FileHeader
  118. // var exists bool
  119. //
  120. // if imageFiles, exists = c.Request.MultipartForm.File["image"]; !exists || len(imageFiles) == 0 {
  121. // if imageFiles, exists = c.Request.MultipartForm.File["image[]"]; !exists || len(imageFiles) == 0 {
  122. // foundArrayImages := false
  123. // for fieldName, files := range c.Request.MultipartForm.File {
  124. // if strings.HasPrefix(fieldName, "image[") && len(files) > 0 {
  125. // foundArrayImages = true
  126. // for _, file := range files {
  127. // imageFiles = append(imageFiles, file)
  128. // }
  129. // }
  130. // }
  131. //
  132. // if !foundArrayImages && (len(imageFiles) == 0) {
  133. // return nil, errors.New("image is required")
  134. // }
  135. // }
  136. // }
  137. //
  138. // for i, fileHeader := range imageFiles {
  139. // file, err := fileHeader.Open()
  140. // if err != nil {
  141. // return nil, fmt.Errorf("failed to open image file %d: %w", i, err)
  142. // }
  143. // defer file.Close()
  144. //
  145. // fieldName := "image"
  146. // if len(imageFiles) > 1 {
  147. // fieldName = "image[]"
  148. // }
  149. //
  150. // mimeType := detectImageMimeType(fileHeader.Filename)
  151. //
  152. // h := make(textproto.MIMEHeader)
  153. // h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldName, fileHeader.Filename))
  154. // h.Set("Content-Type", mimeType)
  155. //
  156. // part, err := writer.CreatePart(h)
  157. // if err != nil {
  158. // return nil, fmt.Errorf("create form part failed for image %d: %w", i, err)
  159. // }
  160. //
  161. // if _, err := io.Copy(part, file); err != nil {
  162. // return nil, fmt.Errorf("copy file failed for image %d: %w", i, err)
  163. // }
  164. // }
  165. //
  166. // if maskFiles, exists := c.Request.MultipartForm.File["mask"]; exists && len(maskFiles) > 0 {
  167. // maskFile, err := maskFiles[0].Open()
  168. // if err != nil {
  169. // return nil, errors.New("failed to open mask file")
  170. // }
  171. // defer maskFile.Close()
  172. //
  173. // mimeType := detectImageMimeType(maskFiles[0].Filename)
  174. //
  175. // h := make(textproto.MIMEHeader)
  176. // h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="mask"; filename="%s"`, maskFiles[0].Filename))
  177. // h.Set("Content-Type", mimeType)
  178. //
  179. // maskPart, err := writer.CreatePart(h)
  180. // if err != nil {
  181. // return nil, errors.New("create form file failed for mask")
  182. // }
  183. //
  184. // if _, err := io.Copy(maskPart, maskFile); err != nil {
  185. // return nil, errors.New("copy mask file failed")
  186. // }
  187. // }
  188. // } else {
  189. // return nil, errors.New("no multipart form data found")
  190. // }
  191. //
  192. // writer.Close()
  193. // c.Request.Header.Set("Content-Type", writer.FormDataContentType())
  194. // return bytes.NewReader(requestBody.Bytes()), nil
  195. default:
  196. return request, nil
  197. }
  198. }
  199. func detectImageMimeType(filename string) string {
  200. ext := strings.ToLower(filepath.Ext(filename))
  201. switch ext {
  202. case ".jpg", ".jpeg":
  203. return "image/jpeg"
  204. case ".png":
  205. return "image/png"
  206. case ".webp":
  207. return "image/webp"
  208. default:
  209. if strings.HasPrefix(ext, ".jp") {
  210. return "image/jpeg"
  211. }
  212. return "image/png"
  213. }
  214. }
  215. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  216. }
  217. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  218. baseUrl := info.ChannelBaseUrl
  219. if baseUrl == "" {
  220. baseUrl = channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeVolcEngine]
  221. }
  222. specialPlan, hasSpecialPlan := channelconstant.ChannelSpecialBases[baseUrl]
  223. switch info.RelayFormat {
  224. case types.RelayFormatClaude:
  225. if hasSpecialPlan && specialPlan.ClaudeBaseURL != "" {
  226. return fmt.Sprintf("%s/v1/messages", specialPlan.ClaudeBaseURL), nil
  227. }
  228. if strings.HasPrefix(info.UpstreamModelName, "bot") {
  229. return fmt.Sprintf("%s/api/v3/bots/chat/completions", baseUrl), nil
  230. }
  231. return fmt.Sprintf("%s/api/v3/chat/completions", baseUrl), nil
  232. default:
  233. switch info.RelayMode {
  234. case constant.RelayModeChatCompletions:
  235. if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
  236. return fmt.Sprintf("%s/chat/completions", specialPlan.OpenAIBaseURL), nil
  237. }
  238. if strings.HasPrefix(info.UpstreamModelName, "bot") {
  239. return fmt.Sprintf("%s/api/v3/bots/chat/completions", baseUrl), nil
  240. }
  241. return fmt.Sprintf("%s/api/v3/chat/completions", baseUrl), nil
  242. case constant.RelayModeEmbeddings:
  243. return fmt.Sprintf("%s/api/v3/embeddings", baseUrl), nil
  244. //豆包的图生图也走generations接口: https://www.volcengine.com/docs/82379/1824121
  245. case constant.RelayModeImagesGenerations, constant.RelayModeImagesEdits:
  246. return fmt.Sprintf("%s/api/v3/images/generations", baseUrl), nil
  247. //case constant.RelayModeImagesEdits:
  248. // return fmt.Sprintf("%s/api/v3/images/edits", baseUrl), nil
  249. case constant.RelayModeRerank:
  250. return fmt.Sprintf("%s/api/v3/rerank", baseUrl), nil
  251. case constant.RelayModeResponses:
  252. return fmt.Sprintf("%s/api/v3/responses", baseUrl), nil
  253. case constant.RelayModeAudioSpeech:
  254. if baseUrl == channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeVolcEngine] {
  255. return "wss://openspeech.bytedance.com/api/v1/tts/ws_binary", nil
  256. }
  257. return fmt.Sprintf("%s/v1/audio/speech", baseUrl), nil
  258. default:
  259. }
  260. }
  261. return "", fmt.Errorf("unsupported relay mode: %d", info.RelayMode)
  262. }
  263. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  264. channel.SetupApiRequestHeader(info, c, req)
  265. if info.RelayMode == constant.RelayModeAudioSpeech {
  266. parts := strings.Split(info.ApiKey, "|")
  267. if len(parts) == 2 {
  268. req.Set("Authorization", "Bearer;"+parts[1])
  269. }
  270. req.Set("Content-Type", "application/json")
  271. return nil
  272. } else if info.RelayMode == constant.RelayModeImagesEdits {
  273. req.Set("Content-Type", gin.MIMEJSON)
  274. }
  275. req.Set("Authorization", "Bearer "+info.ApiKey)
  276. return nil
  277. }
  278. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  279. if request == nil {
  280. return nil, errors.New("request is nil")
  281. }
  282. if !model_setting.ShouldPreserveThinkingSuffix(info.OriginModelName) &&
  283. strings.HasSuffix(info.UpstreamModelName, "-thinking") &&
  284. strings.HasPrefix(info.UpstreamModelName, "deepseek") {
  285. info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-thinking")
  286. request.Model = info.UpstreamModelName
  287. request.THINKING = json.RawMessage(`{"type": "enabled"}`)
  288. }
  289. return request, nil
  290. }
  291. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  292. return nil, nil
  293. }
  294. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  295. return request, nil
  296. }
  297. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  298. return request, nil
  299. }
  300. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  301. if info.RelayMode == constant.RelayModeAudioSpeech {
  302. baseUrl := info.ChannelBaseUrl
  303. if baseUrl == "" {
  304. baseUrl = channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeVolcEngine]
  305. }
  306. if baseUrl == channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeVolcEngine] {
  307. if info.IsStream {
  308. return nil, nil
  309. }
  310. }
  311. }
  312. return channel.DoApiRequest(a, c, info, requestBody)
  313. }
  314. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  315. if info.RelayFormat == types.RelayFormatClaude {
  316. if _, ok := channelconstant.ChannelSpecialBases[info.ChannelBaseUrl]; ok {
  317. adaptor := claude.Adaptor{}
  318. return adaptor.DoResponse(c, resp, info)
  319. }
  320. }
  321. if info.RelayMode == constant.RelayModeAudioSpeech {
  322. encoding := mapEncoding(c.GetString(contextKeyResponseFormat))
  323. if info.IsStream {
  324. volcRequestInterface, exists := c.Get(contextKeyTTSRequest)
  325. if !exists {
  326. return nil, types.NewErrorWithStatusCode(
  327. errors.New("volcengine TTS request not found in context"),
  328. types.ErrorCodeBadRequestBody,
  329. http.StatusInternalServerError,
  330. )
  331. }
  332. volcRequest, ok := volcRequestInterface.(VolcengineTTSRequest)
  333. if !ok {
  334. return nil, types.NewErrorWithStatusCode(
  335. errors.New("invalid volcengine TTS request type"),
  336. types.ErrorCodeBadRequestBody,
  337. http.StatusInternalServerError,
  338. )
  339. }
  340. // Get the WebSocket URL
  341. requestURL, urlErr := a.GetRequestURL(info)
  342. if urlErr != nil {
  343. return nil, types.NewErrorWithStatusCode(
  344. urlErr,
  345. types.ErrorCodeBadRequestBody,
  346. http.StatusInternalServerError,
  347. )
  348. }
  349. return handleTTSWebSocketResponse(c, requestURL, volcRequest, info, encoding)
  350. }
  351. return handleTTSResponse(c, resp, info, encoding)
  352. }
  353. adaptor := openai.Adaptor{}
  354. usage, err = adaptor.DoResponse(c, resp, info)
  355. return
  356. }
  357. func (a *Adaptor) GetModelList() []string {
  358. return ModelList
  359. }
  360. func (a *Adaptor) GetChannelName() string {
  361. return ChannelName
  362. }