微信扫码
添加专属顾问
我要投稿
探索Dify AI Agent与Dash应用的集成之道,掌握构建Agent Chain的实战技巧。 核心内容: 1. Dash与Dify平台集成的实战方法 2. 四个AI Agent的配置与功能介绍 3. 基于Dash构建智能体链的代码实现细节
这是一个Dash结合dify的项目,将详细介绍将Dify平台开发的多个AI Agent集成到Dash应用中的实战方法。基于Dify搭建的多个Agent不仅可以在Dash中无缝集成,还能实现Agent间的相互调用,构建复杂的工作链路,最终完成用户提出的综合目标任务。当然,这是最终目前?。在本篇中,将先实现Agent调度其他Agent的功能。文末可获取本篇中的Dash代码。
目前我们配置了4个智能体,分别为意图识别、制度专家、智能助手和创作大师。先通过意图识别,判断用户的意图,然后再调用相应的智能体进行回答。由于篇幅问题,本系列文章正文中仅提供智能体的概况,提示词不多赘述,后面会开一个相关的专题来专门讲解提示词的设计。
意图识别Agent用于判断用户的意图,根据具体意图调用相应的智能体。
制度专家是一个公司制度助手,基于RAG技术实现,用于解答用户与公司制度相关的问题。
创作大师是一个小红书文案创作大师,能够根据用户的主题创作小红书风格的文案。
智能助手为一个通用的聊天助手,如果用户的问题不是制度专家和创作大师能回答时,将由智能助手来回答。
我们的项目在前篇《用Dash搭建Dify多智能体应用》的基础上进一步改进,由意图识别Agent来自主调用智能体而不是通过手动切换。
AgentChain/
├── asserts/
│ ├── ├── imgs/
│ ├── callback.js
│ ├── style.css
├── apis/
│ ├── dify_api.py
├── views/
│ ├── commom.py
├── app.py
├── config.py
├── requirements.txt
在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",
},
}
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+中大型企业
2025-05-09
MinerU教程第一弹丨Dify插件超详细配置攻略和工作流搭建案例,不允许还有人不会
2025-05-09
Dify知识库调优浅聊
2025-05-08
在Dify中使用本地MCP工具
2025-05-08
Dify升级指南:1.1.13->1.3.1
2025-05-06
API-to-MCP,并在 Dify 实现调用的实践
2025-04-30
知识融合新范式:Cherry Studio×dify 知识库联动全解析
2025-04-29
Dify 平台集成 Palo Alto Networks 安全插件,为 AI 应用注入企业级安全防护
2025-04-29
Dify→ LLM 节点 说明
2024-12-24
2024-04-25
2024-07-16
2024-07-20
2024-04-24
2024-06-21
2024-05-08
2024-11-15
2024-08-06
2024-05-09
2025-04-27
2025-04-15
2025-03-20
2024-12-19
2024-09-13
2024-09-13
2024-08-28
2024-04-24