adaptor.go 3.2 KB

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