adaptor.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package dify
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "one-api/dto"
  8. "one-api/relay/channel"
  9. relaycommon "one-api/relay/common"
  10. "one-api/types"
  11. "github.com/gin-gonic/gin"
  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. //}
  44. a.BotType = BotTypeChatFlow
  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(c, info, *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) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  77. // TODO implement me
  78. return nil, errors.New("not implemented")
  79. }
  80. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  81. return channel.DoApiRequest(a, c, info, requestBody)
  82. }
  83. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  84. if info.IsStream {
  85. return difyStreamHandler(c, info, resp)
  86. } else {
  87. return difyHandler(c, info, resp)
  88. }
  89. return
  90. }
  91. func (a *Adaptor) GetModelList() []string {
  92. return ModelList
  93. }
  94. func (a *Adaptor) GetChannelName() string {
  95. return ChannelName
  96. }