adaptor.go 3.2 KB

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