ngcbot接入dify,实现微信机器人
ngcbot提供了个人微信聊天机器人,但是暂时没有办法实现复杂业务接口的交互(知识库,其它API等)利用dify配合ngcbot实现某些功能,这里需要改dify的代码即可。
ngcbot提供了个人微信聊天机器人,但是暂时没有办法实现复杂业务接口的交互(知识库,其它API等)
我想利用dify配合ngcbot实现某些功能,这里需要改dify的代码即可
一、ngcbot
开源地址:https://github.com/ngc660sec/NGCBot
当前安装的是 V2.3 版本
将代码直接clone下来即可,等会后面再用
注意,你需要安装ngcbot版本指定的微信软件
二、dify
1.安装
开源地址:https://github.com/langgenius/dify
代码直接clone下来,进入docker文件夹,复制一下配置文件
cd docker
cp .env.example .env
当前路径下执行编译
docker compose up -d
执行后,你将看到
我是windows的docker安装,如果出现pg db链接不上 2个容器启动不了,请修改此处
修改docker-compose.yaml 文件
重新执行
docker compose up -d
看到容器全部绿色即可成功
访问
http://localhost/signin 初始化设置即可
2.创建一个应用
初次使用,记得安装模型商插件,我这里使用的是deepseek
创建应用,且发布
创建key,复制其key跟url,等会dify要使用
app-RXeOEmsIaurObtxxjrkAmO7c
三、修改ngcbot代码且启动
修改的时候,请注意你的版本,还有代码大体位置即可,可能ngcbot版本不同,叫法跟代码位置不太一样,我用的V2.3
1.修改yaml文件
这个为刚刚我们在dify创建的api,跟key,url为dify默认启动的80端口,你启动的不是80端口自己更换
2.修改 AiDialogue.py
第一处
self.DifyConfig = {
'DifyApi': configData['AiConfig']['Dify']['DifyApi'],
'DifyKey': configData['AiConfig']['Dify']['DifyKey']
}
第二处
self.DifyMessages = [{"role": "system", "content": f'{self.systemAiRole}'}]
第三处,新建一个方法
def getDify(self, content, messages):
"""
deepSeek
:param content: 对话内容
:param messages: 消息列表
:return:
"""
op(f'[*]: 正在调用Dify对话接口... ...')
if not self.DifyConfig.get('DifyKey'):
op(f'[-]: Dify模型未配置, 请检查相关配置!!!')
return None, []
messages.append({"role": "user", "content": f'{content}'})
op(f'[-]: "Status Code:", {messages}')
data = {
"inputs": {},
"query": messages,
"response_mode": "blocking",
"conversation_id": "",
"user": "abc-123",
"files": [
{
"type": "image",
"transfer_method": "remote_url",
"url": "https://cloud.dify.ai/logo/logo-site.png"
}
]
}
headers = {
'Authorization': self.DifyConfig.get('DifyKey'),
'Content-Type': 'application/json'
}
try:
resp = requests.post(url=self.DifyConfig.get('DifyApi'), headers=headers, json=data,
timeout=3000)
json_data = resp.json()
op(f'[-]: "Status Code:", {resp.status_code}')
assistant_content = json_data['answer']
op(f'[-]: {json_data["answer"]}')
messages.append({"role": "assistant", "content": f"{assistant_content}"})
if len(messages) == 21:
del messages[1]
del messages[2]
return assistant_content, messages
except Exception as e:
op(f'[-]: Dify对话接口出现错误, 错误信息: {e}')
return None, [{"role": "system", "content": f'{self.systemAiRole}'}]
第四处
if aiModule == 'Dify':
result, self.DifyMessages = self.getDify(content, self.DifyMessages)
3.启动代码查看效果
登录你的微信
启动ngcbot main.py
聊天测试
ngcbot日志
dify日志

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)