adaptor.go 2.7 KB

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