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