選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

133 行
4.3 KiB

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