adaptor.go 12 KB

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