adaptor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package openai
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. "net/http"
  8. "one-api/common"
  9. "one-api/dto"
  10. "one-api/relay/channel"
  11. "one-api/relay/channel/ai360"
  12. "one-api/relay/channel/lingyiwanwu"
  13. "one-api/relay/channel/minimax"
  14. "one-api/relay/channel/moonshot"
  15. relaycommon "one-api/relay/common"
  16. "one-api/service"
  17. "strings"
  18. )
  19. type Adaptor struct {
  20. ChannelType int
  21. }
  22. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  23. return nil, nil
  24. }
  25. func (a *Adaptor) InitRerank(info *relaycommon.RelayInfo, request dto.RerankRequest) {
  26. }
  27. func (a *Adaptor) Init(info *relaycommon.RelayInfo, request dto.GeneralOpenAIRequest) {
  28. a.ChannelType = info.ChannelType
  29. }
  30. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  31. switch info.ChannelType {
  32. case common.ChannelTypeAzure:
  33. // https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?pivots=rest-api&tabs=command-line#rest-api
  34. requestURL := strings.Split(info.RequestURLPath, "?")[0]
  35. requestURL = fmt.Sprintf("%s?api-version=%s", requestURL, info.ApiVersion)
  36. task := strings.TrimPrefix(requestURL, "/v1/")
  37. model_ := info.UpstreamModelName
  38. model_ = strings.Replace(model_, ".", "", -1)
  39. // https://github.com/songquanpeng/one-api/issues/67
  40. requestURL = fmt.Sprintf("/openai/deployments/%s/%s", model_, task)
  41. return relaycommon.GetFullRequestURL(info.BaseUrl, requestURL, info.ChannelType), nil
  42. case common.ChannelTypeMiniMax:
  43. return minimax.GetRequestURL(info)
  44. case common.ChannelTypeCustom:
  45. url := info.BaseUrl
  46. url = strings.Replace(url, "{model}", info.UpstreamModelName, -1)
  47. return url, nil
  48. default:
  49. return relaycommon.GetFullRequestURL(info.BaseUrl, info.RequestURLPath, info.ChannelType), nil
  50. }
  51. }
  52. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
  53. channel.SetupApiRequestHeader(info, c, req)
  54. if info.ChannelType == common.ChannelTypeAzure {
  55. req.Header.Set("api-key", info.ApiKey)
  56. return nil
  57. }
  58. if info.ChannelType == common.ChannelTypeOpenAI && "" != info.Organization {
  59. req.Header.Set("OpenAI-Organization", info.Organization)
  60. }
  61. req.Header.Set("Authorization", "Bearer "+info.ApiKey)
  62. //if info.ChannelType == common.ChannelTypeOpenRouter {
  63. // req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api")
  64. // req.Header.Set("X-Title", "One API")
  65. //}
  66. return nil
  67. }
  68. func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *dto.GeneralOpenAIRequest) (any, error) {
  69. if request == nil {
  70. return nil, errors.New("request is nil")
  71. }
  72. return request, nil
  73. }
  74. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
  75. return channel.DoApiRequest(a, c, info, requestBody)
  76. }
  77. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage *dto.Usage, err *dto.OpenAIErrorWithStatusCode) {
  78. if info.IsStream {
  79. var responseText string
  80. var toolCount int
  81. err, responseText, toolCount = OpenaiStreamHandler(c, resp, info)
  82. usage, _ = service.ResponseText2Usage(responseText, info.UpstreamModelName, info.PromptTokens)
  83. usage.CompletionTokens += toolCount * 7
  84. } else {
  85. err, usage = OpenaiHandler(c, resp, info.PromptTokens, info.UpstreamModelName)
  86. }
  87. return
  88. }
  89. func (a *Adaptor) GetModelList() []string {
  90. switch a.ChannelType {
  91. case common.ChannelType360:
  92. return ai360.ModelList
  93. case common.ChannelTypeMoonshot:
  94. return moonshot.ModelList
  95. case common.ChannelTypeLingYiWanWu:
  96. return lingyiwanwu.ModelList
  97. case common.ChannelTypeMiniMax:
  98. return minimax.ModelList
  99. default:
  100. return ModelList
  101. }
  102. }
  103. func (a *Adaptor) GetChannelName() string {
  104. switch a.ChannelType {
  105. case common.ChannelType360:
  106. return ai360.ChannelName
  107. case common.ChannelTypeMoonshot:
  108. return moonshot.ChannelName
  109. case common.ChannelTypeLingYiWanWu:
  110. return lingyiwanwu.ChannelName
  111. case common.ChannelTypeMiniMax:
  112. return minimax.ChannelName
  113. default:
  114. return ChannelName
  115. }
  116. }