adaptor.go 3.8 KB

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