adaptor.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dify
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. "net/http"
  8. "one-api/dto"
  9. "one-api/relay/channel"
  10. relaycommon "one-api/relay/common"
  11. "strings"
  12. )
  13. const (
  14. BotTypeChatFlow = 1 // chatflow default
  15. BotTypeAgent = 2
  16. BotTypeWorkFlow = 3
  17. BotTypeCompletion = 4
  18. )
  19. type Adaptor struct {
  20. BotType int
  21. }
  22. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  23. //TODO implement me
  24. return nil, errors.New("not implemented")
  25. }
  26. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  27. //TODO implement me
  28. return nil, errors.New("not implemented")
  29. }
  30. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  31. if strings.HasPrefix(info.UpstreamModelName, "agent") {
  32. a.BotType = BotTypeAgent
  33. } else if strings.HasPrefix(info.UpstreamModelName, "workflow") {
  34. a.BotType = BotTypeWorkFlow
  35. } else if strings.HasPrefix(info.UpstreamModelName, "chat") {
  36. a.BotType = BotTypeCompletion
  37. } else {
  38. a.BotType = BotTypeChatFlow
  39. }
  40. }
  41. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  42. switch a.BotType {
  43. case BotTypeWorkFlow:
  44. return fmt.Sprintf("%s/v1/workflows/run", info.BaseUrl), nil
  45. case BotTypeCompletion:
  46. return fmt.Sprintf("%s/v1/completion-messages", info.BaseUrl), nil
  47. case BotTypeAgent:
  48. fallthrough
  49. default:
  50. return fmt.Sprintf("%s/v1/chat-messages", info.BaseUrl), nil
  51. }
  52. }
  53. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  54. channel.SetupApiRequestHeader(info, c, req)
  55. req.Set("Authorization", "Bearer "+info.ApiKey)
  56. return nil
  57. }
  58. func (a *Adaptor) ConvertRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  59. if request == nil {
  60. return nil, errors.New("request is nil")
  61. }
  62. return requestOpenAI2Dify(*request), nil
  63. }
  64. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  65. return nil, nil
  66. }
  67. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  68. //TODO implement me
  69. return nil, errors.New("not implemented")
  70. }
  71. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  72. return channel.DoApiRequest(a, c, info, requestBody)
  73. }
  74. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *dto.OpenAIErrorWithStatusCode) {
  75. if info.IsStream {
  76. err, usage = difyStreamHandler(c, resp, info)
  77. } else {
  78. err, usage = difyHandler(c, resp, info)
  79. }
  80. return
  81. }
  82. func (a *Adaptor) GetModelList() []string {
  83. return ModelList
  84. }
  85. func (a *Adaptor) GetChannelName() string {
  86. return ChannelName
  87. }