adaptor.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
  23. //TODO implement me
  24. panic("implement me")
  25. return nil, nil
  26. }
  27. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  28. //TODO implement me
  29. return nil, errors.New("not implemented")
  30. }
  31. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  32. //TODO implement me
  33. return nil, errors.New("not implemented")
  34. }
  35. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  36. if strings.HasPrefix(info.UpstreamModelName, "agent") {
  37. a.BotType = BotTypeAgent
  38. } else if strings.HasPrefix(info.UpstreamModelName, "workflow") {
  39. a.BotType = BotTypeWorkFlow
  40. } else if strings.HasPrefix(info.UpstreamModelName, "chat") {
  41. a.BotType = BotTypeCompletion
  42. } else {
  43. a.BotType = BotTypeChatFlow
  44. }
  45. }
  46. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  47. switch a.BotType {
  48. case BotTypeWorkFlow:
  49. return fmt.Sprintf("%s/v1/workflows/run", info.BaseUrl), nil
  50. case BotTypeCompletion:
  51. return fmt.Sprintf("%s/v1/completion-messages", info.BaseUrl), nil
  52. case BotTypeAgent:
  53. fallthrough
  54. default:
  55. return fmt.Sprintf("%s/v1/chat-messages", info.BaseUrl), nil
  56. }
  57. }
  58. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  59. channel.SetupApiRequestHeader(info, c, req)
  60. req.Set("Authorization", "Bearer "+info.ApiKey)
  61. return nil
  62. }
  63. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  64. if request == nil {
  65. return nil, errors.New("request is nil")
  66. }
  67. return requestOpenAI2Dify(*request), nil
  68. }
  69. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  70. return nil, nil
  71. }
  72. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  73. //TODO implement me
  74. return nil, errors.New("not implemented")
  75. }
  76. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  77. return channel.DoApiRequest(a, c, info, requestBody)
  78. }
  79. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *dto.OpenAIErrorWithStatusCode) {
  80. if info.IsStream {
  81. err, usage = difyStreamHandler(c, resp, info)
  82. } else {
  83. err, usage = difyHandler(c, resp, info)
  84. }
  85. return
  86. }
  87. func (a *Adaptor) GetModelList() []string {
  88. return ModelList
  89. }
  90. func (a *Adaptor) GetChannelName() string {
  91. return ChannelName
  92. }