adaptor.go 4.1 KB

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