adaptor.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package codex
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "github.com/QuantumNous/new-api/common"
  9. "github.com/QuantumNous/new-api/dto"
  10. "github.com/QuantumNous/new-api/relay/channel"
  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(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
  20. return nil, errors.New("codex channel: endpoint not supported")
  21. }
  22. func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
  23. return nil, errors.New("codex channel: endpoint not supported")
  24. }
  25. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  26. return nil, errors.New("codex channel: endpoint not supported")
  27. }
  28. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  29. return nil, errors.New("codex channel: endpoint not supported")
  30. }
  31. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  32. }
  33. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  34. return nil, errors.New("codex channel: endpoint not supported")
  35. }
  36. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  37. return nil, errors.New("codex channel: endpoint not supported")
  38. }
  39. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  40. return nil, errors.New("codex channel: endpoint not supported")
  41. }
  42. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  43. isCompact := info != nil && info.RelayMode == relayconstant.RelayModeResponsesCompact
  44. if info != nil && info.ChannelSetting.SystemPrompt != "" {
  45. systemPrompt := info.ChannelSetting.SystemPrompt
  46. if len(request.Instructions) == 0 {
  47. if b, err := common.Marshal(systemPrompt); err == nil {
  48. request.Instructions = b
  49. } else {
  50. return nil, err
  51. }
  52. } else if info.ChannelSetting.SystemPromptOverride {
  53. var existing string
  54. if err := common.Unmarshal(request.Instructions, &existing); err == nil {
  55. existing = strings.TrimSpace(existing)
  56. if existing == "" {
  57. if b, err := common.Marshal(systemPrompt); err == nil {
  58. request.Instructions = b
  59. } else {
  60. return nil, err
  61. }
  62. } else {
  63. if b, err := common.Marshal(systemPrompt + "\n" + existing); err == nil {
  64. request.Instructions = b
  65. } else {
  66. return nil, err
  67. }
  68. }
  69. } else {
  70. if b, err := common.Marshal(systemPrompt); err == nil {
  71. request.Instructions = b
  72. } else {
  73. return nil, err
  74. }
  75. }
  76. }
  77. }
  78. if isCompact {
  79. return request, nil
  80. }
  81. // codex: store must be false
  82. request.Store = json.RawMessage("false")
  83. // rm max_output_tokens
  84. request.MaxOutputTokens = 0
  85. request.Temperature = nil
  86. return request, nil
  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. if info.RelayMode != relayconstant.RelayModeResponses && info.RelayMode != relayconstant.RelayModeResponsesCompact {
  93. return nil, types.NewError(errors.New("codex channel: endpoint not supported"), types.ErrorCodeInvalidRequest)
  94. }
  95. if info.RelayMode == relayconstant.RelayModeResponsesCompact {
  96. return openai.OaiResponsesCompactionHandler(c, resp)
  97. }
  98. if info.IsStream {
  99. return openai.OaiResponsesStreamHandler(c, info, resp)
  100. }
  101. return openai.OaiResponsesHandler(c, info, resp)
  102. }
  103. func (a *Adaptor) GetModelList() []string {
  104. return ModelList
  105. }
  106. func (a *Adaptor) GetChannelName() string {
  107. return ChannelName
  108. }
  109. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  110. if info.RelayMode != relayconstant.RelayModeResponses && info.RelayMode != relayconstant.RelayModeResponsesCompact {
  111. return "", errors.New("codex channel: only /v1/responses and /v1/responses/compact are supported")
  112. }
  113. path := "/backend-api/codex/responses"
  114. if info.RelayMode == relayconstant.RelayModeResponsesCompact {
  115. path = "/backend-api/codex/responses/compact"
  116. }
  117. return relaycommon.GetFullRequestURL(info.ChannelBaseUrl, path, info.ChannelType), nil
  118. }
  119. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  120. channel.SetupApiRequestHeader(info, c, req)
  121. key := strings.TrimSpace(info.ApiKey)
  122. if !strings.HasPrefix(key, "{") {
  123. return errors.New("codex channel: key must be a JSON object")
  124. }
  125. oauthKey, err := ParseOAuthKey(key)
  126. if err != nil {
  127. return err
  128. }
  129. accessToken := strings.TrimSpace(oauthKey.AccessToken)
  130. accountID := strings.TrimSpace(oauthKey.AccountID)
  131. if accessToken == "" {
  132. return errors.New("codex channel: access_token is required")
  133. }
  134. if accountID == "" {
  135. return errors.New("codex channel: account_id is required")
  136. }
  137. req.Set("Authorization", "Bearer "+accessToken)
  138. req.Set("chatgpt-account-id", accountID)
  139. if req.Get("OpenAI-Beta") == "" {
  140. req.Set("OpenAI-Beta", "responses=experimental")
  141. }
  142. if req.Get("originator") == "" {
  143. req.Set("originator", "codex_cli_rs")
  144. }
  145. return nil
  146. }