adaptor.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. )
  15. type Adaptor struct {
  16. }
  17. func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dto.GeminiChatRequest) (any, error) {
  18. //TODO implement me
  19. return nil, errors.New("not implemented")
  20. }
  21. func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
  22. //TODO implement me
  23. //panic("implement me")
  24. return nil, errors.New("not available")
  25. }
  26. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  27. //not available
  28. return nil, errors.New("not available")
  29. }
  30. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  31. xaiRequest := ImageRequest{
  32. Model: request.Model,
  33. Prompt: request.Prompt,
  34. N: int(request.N),
  35. ResponseFormat: request.ResponseFormat,
  36. }
  37. return xaiRequest, nil
  38. }
  39. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  40. }
  41. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  42. return relaycommon.GetFullRequestURL(info.ChannelBaseUrl, info.RequestURLPath, info.ChannelType), nil
  43. }
  44. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  45. channel.SetupApiRequestHeader(info, c, req)
  46. req.Set("Authorization", "Bearer "+info.ApiKey)
  47. return nil
  48. }
  49. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  50. if request == nil {
  51. return nil, errors.New("request is nil")
  52. }
  53. if strings.HasSuffix(info.UpstreamModelName, "-search") {
  54. info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-search")
  55. request.Model = info.UpstreamModelName
  56. toMap := request.ToMap()
  57. toMap["search_parameters"] = map[string]any{
  58. "mode": "on",
  59. }
  60. return toMap, nil
  61. }
  62. if strings.HasPrefix(request.Model, "grok-3-mini") {
  63. if request.MaxCompletionTokens == 0 && request.MaxTokens != 0 {
  64. request.MaxCompletionTokens = request.MaxTokens
  65. request.MaxTokens = 0
  66. }
  67. if strings.HasSuffix(request.Model, "-high") {
  68. request.ReasoningEffort = "high"
  69. request.Model = strings.TrimSuffix(request.Model, "-high")
  70. } else if strings.HasSuffix(request.Model, "-low") {
  71. request.ReasoningEffort = "low"
  72. request.Model = strings.TrimSuffix(request.Model, "-low")
  73. }
  74. info.ReasoningEffort = request.ReasoningEffort
  75. info.UpstreamModelName = request.Model
  76. }
  77. return 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. //not available
  84. return nil, errors.New("not available")
  85. }
  86. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  87. if request.Model == "" && info != nil {
  88. request.Model = info.UpstreamModelName
  89. }
  90. return request, nil
  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.RelayMode {
  97. case constant.RelayModeImagesGenerations, constant.RelayModeImagesEdits:
  98. usage, err = openai.OpenaiHandlerWithUsage(c, info, resp)
  99. case constant.RelayModeResponses:
  100. if info.IsStream {
  101. usage, err = openai.OaiResponsesStreamHandler(c, info, resp)
  102. } else {
  103. usage, err = openai.OaiResponsesHandler(c, info, resp)
  104. }
  105. default:
  106. if info.IsStream {
  107. usage, err = xAIStreamHandler(c, info, resp)
  108. } else {
  109. usage, err = xAIHandler(c, info, resp)
  110. }
  111. }
  112. return
  113. }
  114. func (a *Adaptor) GetModelList() []string {
  115. return ModelList
  116. }
  117. func (a *Adaptor) GetChannelName() string {
  118. return ChannelName
  119. }