adaptor.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package moonshot
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. channelconstant "github.com/QuantumNous/new-api/constant"
  8. "github.com/QuantumNous/new-api/dto"
  9. "github.com/QuantumNous/new-api/relay/channel"
  10. "github.com/QuantumNous/new-api/relay/channel/claude"
  11. "github.com/QuantumNous/new-api/relay/channel/openai"
  12. relaycommon "github.com/QuantumNous/new-api/relay/common"
  13. "github.com/QuantumNous/new-api/relay/constant"
  14. "github.com/QuantumNous/new-api/types"
  15. "github.com/gin-gonic/gin"
  16. )
  17. type Adaptor struct {
  18. }
  19. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  20. //TODO implement me
  21. return nil, errors.New("not implemented")
  22. }
  23. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  24. adaptor := claude.Adaptor{}
  25. return adaptor.ConvertClaudeRequest(c, info, req)
  26. }
  27. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  28. //TODO implement me
  29. return nil, errors.New("not supported")
  30. }
  31. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  32. adaptor := openai.Adaptor{}
  33. return adaptor.ConvertImageRequest(c, info, request)
  34. }
  35. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  36. }
  37. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  38. baseURL := info.ChannelBaseUrl
  39. if specialPlan, ok := channelconstant.ChannelSpecialBases[baseURL]; ok {
  40. if info.RelayFormat == types.RelayFormatClaude {
  41. return fmt.Sprintf("%s/v1/messages", specialPlan.ClaudeBaseURL), nil
  42. }
  43. if info.RelayFormat == types.RelayFormatOpenAI {
  44. return fmt.Sprintf("%s/chat/completions", specialPlan.OpenAIBaseURL), nil
  45. }
  46. }
  47. switch info.RelayFormat {
  48. case types.RelayFormatClaude:
  49. return fmt.Sprintf("%s/anthropic/v1/messages", info.ChannelBaseUrl), nil
  50. default:
  51. if info.RelayMode == constant.RelayModeRerank {
  52. return fmt.Sprintf("%s/v1/rerank", info.ChannelBaseUrl), nil
  53. } else if info.RelayMode == constant.RelayModeEmbeddings {
  54. return fmt.Sprintf("%s/v1/embeddings", info.ChannelBaseUrl), nil
  55. } else if info.RelayMode == constant.RelayModeChatCompletions {
  56. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  57. } else if info.RelayMode == constant.RelayModeCompletions {
  58. return fmt.Sprintf("%s/v1/completions", info.ChannelBaseUrl), nil
  59. }
  60. return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil
  61. }
  62. }
  63. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  64. channel.SetupApiRequestHeader(info, c, req)
  65. req.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey))
  66. return nil
  67. }
  68. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  69. return request, nil
  70. }
  71. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  72. // TODO implement me
  73. return nil, errors.New("not implemented")
  74. }
  75. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  76. return channel.DoApiRequest(a, c, info, requestBody)
  77. }
  78. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  79. return request, nil
  80. }
  81. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  82. return request, nil
  83. }
  84. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  85. switch info.RelayFormat {
  86. case types.RelayFormatClaude:
  87. if info.IsStream {
  88. return claude.ClaudeStreamHandler(c, resp, info, claude.RequestModeMessage)
  89. } else {
  90. return claude.ClaudeHandler(c, resp, info, claude.RequestModeMessage)
  91. }
  92. default:
  93. adaptor := openai.Adaptor{}
  94. return adaptor.DoResponse(c, resp, info)
  95. }
  96. }
  97. func (a *Adaptor) GetModelList() []string {
  98. return ModelList
  99. }
  100. func (a *Adaptor) GetChannelName() string {
  101. return ChannelName
  102. }