adaptor.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package zhipu_4v
  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. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  14. "github.com/QuantumNous/new-api/types"
  15. "github.com/samber/lo"
  16. "github.com/gin-gonic/gin"
  17. )
  18. type Adaptor struct {
  19. }
  20. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  21. //TODO implement me
  22. return nil, errors.New("not implemented")
  23. }
  24. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
  25. return req, nil
  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 implemented")
  30. }
  31. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  32. return request, nil
  33. }
  34. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  35. }
  36. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  37. baseURL := info.ChannelBaseUrl
  38. if baseURL == "" {
  39. baseURL = channelconstant.ChannelBaseURLs[channelconstant.ChannelTypeZhipu_v4]
  40. }
  41. specialPlan, hasSpecialPlan := channelconstant.ChannelSpecialBases[baseURL]
  42. switch info.RelayFormat {
  43. case types.RelayFormatClaude:
  44. if hasSpecialPlan && specialPlan.ClaudeBaseURL != "" {
  45. return fmt.Sprintf("%s/v1/messages", specialPlan.ClaudeBaseURL), nil
  46. }
  47. return fmt.Sprintf("%s/api/anthropic/v1/messages", baseURL), nil
  48. default:
  49. switch info.RelayMode {
  50. case relayconstant.RelayModeEmbeddings:
  51. if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
  52. return fmt.Sprintf("%s/embeddings", specialPlan.OpenAIBaseURL), nil
  53. }
  54. return fmt.Sprintf("%s/api/paas/v4/embeddings", baseURL), nil
  55. case relayconstant.RelayModeImagesGenerations:
  56. if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
  57. return fmt.Sprintf("%s/images/generations", specialPlan.OpenAIBaseURL), nil
  58. }
  59. return fmt.Sprintf("%s/api/paas/v4/images/generations", baseURL), nil
  60. default:
  61. if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
  62. return fmt.Sprintf("%s/chat/completions", specialPlan.OpenAIBaseURL), nil
  63. }
  64. return fmt.Sprintf("%s/api/paas/v4/chat/completions", baseURL), nil
  65. }
  66. }
  67. }
  68. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  69. channel.SetupApiRequestHeader(info, c, req)
  70. req.Set("Authorization", "Bearer "+info.ApiKey)
  71. return nil
  72. }
  73. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  74. if request == nil {
  75. return nil, errors.New("request is nil")
  76. }
  77. if lo.FromPtrOr(request.TopP, 0) >= 1 {
  78. request.TopP = lo.ToPtr(0.99)
  79. }
  80. return requestOpenAI2Zhipu(*request), nil
  81. }
  82. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  83. return nil, nil
  84. }
  85. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  86. return request, nil
  87. }
  88. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  89. // TODO implement me
  90. return nil, errors.New("not implemented")
  91. }
  92. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  93. return channel.DoApiRequest(a, c, info, requestBody)
  94. }
  95. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  96. switch info.RelayFormat {
  97. case types.RelayFormatClaude:
  98. adaptor := claude.Adaptor{}
  99. return adaptor.DoResponse(c, resp, info)
  100. default:
  101. if info.RelayMode == relayconstant.RelayModeImagesGenerations {
  102. return zhipu4vImageHandler(c, resp, info)
  103. }
  104. adaptor := openai.Adaptor{}
  105. return adaptor.DoResponse(c, resp, info)
  106. }
  107. }
  108. func (a *Adaptor) GetModelList() []string {
  109. return ModelList
  110. }
  111. func (a *Adaptor) GetChannelName() string {
  112. return ChannelName
  113. }