支持私有化部署
AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


用Dash开发基于Dify的Agent Chain(1)

发布日期:2025-05-09 16:39:11 浏览次数: 1520 作者:AISpace研习所
推荐语

探索Dify AI Agent与Dash应用的集成之道,掌握构建Agent Chain的实战技巧。

核心内容:
1. Dash与Dify平台集成的实战方法
2. 四个AI Agent的配置与功能介绍
3. 基于Dash构建智能体链的代码实现细节

杨芳贤
53A创始人/腾讯云(TVP)最具价值专家

这是一个Dash结合dify的项目,将详细介绍将Dify平台开发的多个AI Agent集成到Dash应用中的实战方法。基于Dify搭建的多个Agent不仅可以在Dash中无缝集成,还能实现Agent间的相互调用,构建复杂的工作链路,最终完成用户提出的综合目标任务。当然,这是最终目前?。在本篇中,将先实现Agent调度其他Agent的功能。文末可获取本篇中的Dash代码。


基于Dify搭建智能体

目前我们配置了4个智能体,分别为意图识别、制度专家、智能助手和创作大师。先通过意图识别,判断用户的意图,然后再调用相应的智能体进行回答。由于篇幅问题,本系列文章正文中仅提供智能体的概况,提示词不多赘述,后面会开一个相关的专题来专门讲解提示词的设计。

意图识别Agent

意图识别Agent用于判断用户的意图,根据具体意图调用相应的智能体。

意图识别Agent
意图识别Agent工作示例

制度专家

制度专家是一个公司制度助手,基于RAG技术实现,用于解答用户与公司制度相关的问题。

制度专家
制度专家工作示例

创作大师

创作大师是一个小红书文案创作大师,能够根据用户的主题创作小红书风格的文案。

创作大师工作示例

智能助手

智能助手为一个通用的聊天助手,如果用户的问题不是制度专家和创作大师能回答时,将由智能助手来回答。

智能助手工作示例

基于Dash构建智能体链

我们的项目在前篇《用Dash搭建Dify多智能体应用》的基础上进一步改进,由意图识别Agent来自主调用智能体而不是通过手动切换。

AgentChain/
├── asserts/
│   ├── ├── imgs/
│   ├── callback.js
│   ├── style.css
├── apis/
│   ├── dify_api.py
├── views/
│   ├── commom.py
├── app.py
├── config.py
├── requirements.txt

Dify接口封装

dify_api.py中封装的Dify的接口chat-messages接口,Dify还有上传文件、获取会话历史等接口,目前阶段暂未用到,未做封装。

class difyServer():
    def __init__(self, dify_service_url):
        self.dify_service_url = dify_service_url

    
    def chat(self, query, api_key, conversation_id="", user="", stream=True):
        headers = {
            'Authorization'f'Bearer {api_key}',
            'Content-Type''application/json',
        }

        json_data = {
            'inputs': {},
            'query': query,
            'response_mode''streaming',
            'conversation_id'f"{conversation_id}",
            'user': user,
            'auto_generate_name':True
        }

        return requests.post(f'{dify_service_url}/v1/chat-messages', headers=headers, json=json_data, stream=stream)

智能体配置

config.py中配置之前提到的四个智能体信息,以及Dify的接口地址。

dify_service_url = "your_dify_api_host"


DifyAppGroup = {
    "Router": {
        "apikey":"app-your-dify-app-api-key",
        "title":"SpaceAI",
        "type":"Chatflow",
        "color""#357f37",
        },
    "RuleQA":{
        "apikey":"app-your-dify-app-api-key",
        "title":"制度专家",
        "type":"Chatflow",
        "color""#DB7093",
    },
    "GenealQA":{
        "apikey":"app-your-dify-app-api-key",
        "title":"智能助手",
        "type":"Agent",
        "color""#87CEFA",
    },
    "RedBookQA":{
        "apikey":"app-your-dify-app-api-key",
        "title":"创作大师",
        "type":"Agent",
        "color""#FF4981",
    },
}

处理Agent链

Dify以约定好的格式在直接回复节点传出参数,我们在Dash中截取这部分参数,从而获取到下一步调用的Agent,然后通过自动调用该Agent的接口来实现Agent Chain。

在Dash中获取约定参数,以routerParams来约定传出参数。

  // 提取工作流传出参数,并保存到session中
let paramsContent = '';

const paramsRegex = /```routerParams\n([\s\S]*?)\n```/g;
let match;
while ((match = paramsRegex.exec(newCache)) !== null) {
      paramsContent += match[1] + '\n'
      newCache = newCache.replace(match[0], ''); 
  }

window.dash_clientside.set_props(
      "app-router-store",
      {
          "data": paramsContent.trim()
      }
  )

根据获取的智能体编号,来调用相应的智能体

@app.callback(
    [
        Output("agent-call-container""children"),
        Output("is-finished-answer-store""data", allow_duplicate=True),
        Output("consume-agent-store"'data', allow_duplicate=True),
    ],
    [
        Input("app-router-store""data"),
        Input("is-finished-answer-store""data"),
    ],
    State("consume-agent-store""data"),
    prevent_initial_call=True,
)
def call_router_agent(app, is_finish, consume_info):
    '''根据路由结果调用agent '''
    if all([app, is_finish]):
        agent = json.loads(app).get("agent")
        consume_info.update({
            "is_consume":False,
            "app":agent,
        })

        agent_config = DifyAppGroup.get(agent)

        return [
            fac.AntdSpace([
                    fac.AntdText(
                        "智能体",
                        strong=True,
                        style={'fontSize'16}
                    ),

                    fac.AntdTag(
                        color=agent_config.get("color""#5846ff"), 
                        content=f'@ {agent_config.get("title""")}'),
                    
                    fac.AntdText(
                        "为您解答!",
                        strong=True,
                        style={'fontSize'16}
                    ),
                ], 
                size=2
                style={"marginTop":"8px"}
            ),
            False,
            consume_info
            ]

    return [dash.no_update, dash.no_update, dash.no_update]


53AI,企业落地大模型首选服务商

产品:场景落地咨询+大模型应用平台+行业解决方案

承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

添加专属顾问

回到顶部

加载中...

扫码咨询