From b6f95dca417d48bef759bbb1c0121dda59b3fd67 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Mon, 24 Feb 2025 14:18:30 +0800 Subject: [PATCH] feat: Add support for different Dify bot types and request URLs --- relay/channel/dify/adaptor.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/relay/channel/dify/adaptor.go b/relay/channel/dify/adaptor.go index ce73c78c..2626dd7d 100644 --- a/relay/channel/dify/adaptor.go +++ b/relay/channel/dify/adaptor.go @@ -9,9 +9,18 @@ import ( "one-api/dto" "one-api/relay/channel" relaycommon "one-api/relay/common" + "strings" +) + +const ( + BotTypeChatFlow = 1 // chatflow default + BotTypeAgent = 2 + BotTypeWorkFlow = 3 + BotTypeCompletion = 4 ) type Adaptor struct { + BotType int } func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) { @@ -25,10 +34,28 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf } func (a *Adaptor) Init(info *relaycommon.RelayInfo) { + if strings.HasPrefix(info.UpstreamModelName, "agent") { + a.BotType = BotTypeAgent + } else if strings.HasPrefix(info.UpstreamModelName, "workflow") { + a.BotType = BotTypeWorkFlow + } else if strings.HasPrefix(info.UpstreamModelName, "chat") { + a.BotType = BotTypeCompletion + } else { + a.BotType = BotTypeChatFlow + } } func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { - return fmt.Sprintf("%s/v1/chat-messages", info.BaseUrl), nil + switch a.BotType { + case BotTypeWorkFlow: + return fmt.Sprintf("%s/v1/workflows/run", info.BaseUrl), nil + case BotTypeCompletion: + return fmt.Sprintf("%s/v1/completion-messages", info.BaseUrl), nil + case BotTypeAgent: + fallthrough + default: + return fmt.Sprintf("%s/v1/chat-messages", info.BaseUrl), nil + } } func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error { @@ -53,7 +80,6 @@ func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.Rela return nil, errors.New("not implemented") } - func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) { return channel.DoApiRequest(a, c, info, requestBody) }