adaptor.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. } else if strings.HasSuffix(request.Model, "-medium") {
  74. request.ReasoningEffort = "medium"
  75. request.Model = strings.TrimSuffix(request.Model, "-medium")
  76. }
  77. info.ReasoningEffort = request.ReasoningEffort
  78. info.UpstreamModelName = request.Model
  79. }
  80. return request, nil
  81. }
  82. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  83. return nil, nil
  84. }
  85. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  86. //not available
  87. return nil, errors.New("not available")
  88. }
  89. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  90. // TODO implement me
  91. return nil, errors.New("not implemented")
  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. default:
  101. if info.IsStream {
  102. usage, err = xAIStreamHandler(c, info, resp)
  103. } else {
  104. usage, err = xAIHandler(c, info, resp)
  105. }
  106. }
  107. return
  108. }
  109. func (a *Adaptor) GetModelList() []string {
  110. return ModelList
  111. }
  112. func (a *Adaptor) GetChannelName() string {
  113. return ChannelName
  114. }