adaptor.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. return fmt.Sprintf("%s/api/paas/v4/images/generations", baseURL), nil
  57. default:
  58. if hasSpecialPlan && specialPlan.OpenAIBaseURL != "" {
  59. return fmt.Sprintf("%s/chat/completions", specialPlan.OpenAIBaseURL), nil
  60. }
  61. return fmt.Sprintf("%s/api/paas/v4/chat/completions", baseURL), nil
  62. }
  63. }
  64. }
  65. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  66. channel.SetupApiRequestHeader(info, c, req)
  67. req.Set("Authorization", "Bearer "+info.ApiKey)
  68. return nil
  69. }
  70. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  71. if request == nil {
  72. return nil, errors.New("request is nil")
  73. }
  74. if lo.FromPtrOr(request.TopP, 0) >= 1 {
  75. request.TopP = lo.ToPtr(0.99)
  76. }
  77. return requestOpenAI2Zhipu(*request), nil
  78. }
  79. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  80. return nil, nil
  81. }
  82. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  83. return request, nil
  84. }
  85. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  86. // TODO implement me
  87. return nil, errors.New("not implemented")
  88. }
  89. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  90. return channel.DoApiRequest(a, c, info, requestBody)
  91. }
  92. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  93. switch info.RelayFormat {
  94. case types.RelayFormatClaude:
  95. adaptor := claude.Adaptor{}
  96. return adaptor.DoResponse(c, resp, info)
  97. default:
  98. if info.RelayMode == relayconstant.RelayModeImagesGenerations {
  99. return zhipu4vImageHandler(c, resp, info)
  100. }
  101. adaptor := openai.Adaptor{}
  102. return adaptor.DoResponse(c, resp, info)
  103. }
  104. }
  105. func (a *Adaptor) GetModelList() []string {
  106. return ModelList
  107. }
  108. func (a *Adaptor) GetChannelName() string {
  109. return ChannelName
  110. }