adaptor.go 3.4 KB

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