adaptor.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. req.Header.Set("Authorization", "Bearer "+info.ApiKey)
  47. //if info.ChannelType == common.ChannelTypeOpenRouter {
  48. // req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api")
  49. // req.Header.Set("X-Title", "One API")
  50. //}
  51. return nil
  52. }
  53. func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *dto.GeneralOpenAIRequest) (any, error) {
  54. if request == nil {
  55. return nil, errors.New("request is nil")
  56. }
  57. return request, nil
  58. }
  59. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
  60. return channel.DoApiRequest(a, c, info, requestBody)
  61. }
  62. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage *dto.Usage, err *dto.OpenAIErrorWithStatusCode) {
  63. if info.IsStream {
  64. var responseText string
  65. err, responseText = OpenaiStreamHandler(c, resp, info.RelayMode)
  66. usage = service.ResponseText2Usage(responseText, info.UpstreamModelName, info.PromptTokens)
  67. } else {
  68. err, usage = OpenaiHandler(c, resp, info.PromptTokens, info.UpstreamModelName)
  69. }
  70. return
  71. }
  72. func (a *Adaptor) GetModelList() []string {
  73. switch a.ChannelType {
  74. case common.ChannelType360:
  75. return ai360.ModelList
  76. case common.ChannelTypeMoonshot:
  77. return moonshot.ModelList
  78. default:
  79. return ModelList
  80. }
  81. }
  82. func (a *Adaptor) GetChannelName() string {
  83. return ChannelName
  84. }