adaptor.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package xai
  2. import (
  3. "errors"
  4. "io"
  5. "net/http"
  6. "strings"
  7. "github.com/QuantumNous/new-api/dto"
  8. "github.com/QuantumNous/new-api/relay/channel"
  9. "github.com/QuantumNous/new-api/relay/channel/openai"
  10. relaycommon "github.com/QuantumNous/new-api/relay/common"
  11. "github.com/QuantumNous/new-api/types"
  12. "github.com/QuantumNous/new-api/relay/constant"
  13. "github.com/gin-gonic/gin"
  14. "github.com/samber/lo"
  15. )
  16. type Adaptor struct {
  17. }
  18. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  19. //TODO implement me
  20. return nil, errors.New("not implemented")
  21. }
  22. func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
  23. //TODO implement me
  24. //panic("implement me")
  25. return nil, errors.New("not available")
  26. }
  27. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  28. //not available
  29. return nil, errors.New("not available")
  30. }
  31. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  32. xaiRequest := ImageRequest{
  33. Model: request.Model,
  34. Prompt: request.Prompt,
  35. N: int(lo.FromPtrOr(request.N, uint(1))),
  36. ResponseFormat: request.ResponseFormat,
  37. }
  38. return xaiRequest, nil
  39. }
  40. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  41. }
  42. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  43. return relaycommon.GetFullRequestURL(info.ChannelBaseUrl, info.RequestURLPath, info.ChannelType), nil
  44. }
  45. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  46. channel.SetupApiRequestHeader(info, c, req)
  47. req.Set("Authorization", "Bearer "+info.ApiKey)
  48. return nil
  49. }
  50. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  51. if request == nil {
  52. return nil, errors.New("request is nil")
  53. }
  54. if strings.HasSuffix(info.UpstreamModelName, "-search") {
  55. info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-search")
  56. request.Model = info.UpstreamModelName
  57. toMap := request.ToMap()
  58. toMap["search_parameters"] = map[string]any{
  59. "mode": "on",
  60. }
  61. return toMap, nil
  62. }
  63. if strings.HasPrefix(request.Model, "grok-3-mini") {
  64. if lo.FromPtrOr(request.MaxCompletionTokens, uint(0)) == 0 && lo.FromPtrOr(request.MaxTokens, uint(0)) != 0 {
  65. request.MaxCompletionTokens = request.MaxTokens
  66. request.MaxTokens = lo.ToPtr(uint(0))
  67. }
  68. if strings.HasSuffix(request.Model, "-high") {
  69. request.ReasoningEffort = "high"
  70. request.Model = strings.TrimSuffix(request.Model, "-high")
  71. } else if strings.HasSuffix(request.Model, "-low") {
  72. request.ReasoningEffort = "low"
  73. request.Model = strings.TrimSuffix(request.Model, "-low")
  74. }
  75. info.ReasoningEffort = request.ReasoningEffort
  76. info.UpstreamModelName = request.Model
  77. }
  78. return request, nil
  79. }
  80. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  81. return nil, nil
  82. }
  83. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  84. //not available
  85. return nil, errors.New("not available")
  86. }
  87. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  88. if request.Model == "" && info != nil {
  89. request.Model = info.UpstreamModelName
  90. }
  91. return request, nil
  92. }
  93. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  94. return channel.DoApiRequest(a, c, info, requestBody)
  95. }
  96. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  97. switch info.RelayMode {
  98. case constant.RelayModeImagesGenerations, constant.RelayModeImagesEdits:
  99. usage, err = openai.OpenaiHandlerWithUsage(c, info, resp)
  100. case constant.RelayModeResponses:
  101. if info.IsStream {
  102. usage, err = openai.OaiResponsesStreamHandler(c, info, resp)
  103. } else {
  104. usage, err = openai.OaiResponsesHandler(c, info, resp)
  105. }
  106. default:
  107. if info.IsStream {
  108. usage, err = xAIStreamHandler(c, info, resp)
  109. } else {
  110. usage, err = xAIHandler(c, info, resp)
  111. }
  112. }
  113. return
  114. }
  115. func (a *Adaptor) GetModelList() []string {
  116. return ModelList
  117. }
  118. func (a *Adaptor) GetChannelName() string {
  119. return ChannelName
  120. }