之前搭建本地deepseek+Dify,搭建本地知识库,现在进行python编写一个调用本地Dify的API使用
之前搭建本地deepseek+Dify的文章:
DeepSeek本地部署与Dify结合创建私有知识库指南-CSDN博客
1、准备API地址和key
127.0.0.1为本机地址,如果需要其他电脑调用,换成IP地址,我的API地址和key为:
DIFY_API_URL = "http://192.168.3.211/v1/chat-messages" # Dify API 地址
DIFY_API_KEY = "app-pHMJZ61lI28ok1LOYZz4NoLH" # 你的 Dify API Key
2、编写代码访问API地址测试效果
import requests
DIFY_API_URL = "http://192.168.3.211/v1/chat-messages" # Dify API 地址
DIFY_API_KEY = "app-pHMJZ61lI28ok1LOYZz4NoLH" # 你的 Dify API Key
payload = {
"inputs": {},
"query": "你好",
"response_mode": "blocking",
"conversation_id": "",
"user": "abc-123"
}
headers = {
"Authorization": f"Bearer {DIFY_API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(DIFY_API_URL, json=payload, headers=headers)
print(response.json())
运行代码:
PS D:\python> & C:/Users/det/AppData/Local/Programs/Python/Python312/python.exe "d:/python/csAI.py"
返回了响应:
{'event': 'message', 'task_id': 'c0055502-6f22-4249-a02e-462dcf30387a', 'id': 'e5c61bec-6608-4de9-90a4-65f7c6c381e2', 'message_id': 'e5c61bec-6608-4de9-90a4-65f7c6c381e2', 'conversation_id': '06a78f4c-b516-4e3c-bf0c-4277bfe36ad9', 'mode': 'chat', 'answer': '<think>\n好的,用户发来的是“你好”,看起来像是中文问候。我需要根据上下文来生成回复。\n\n首先,查看上下文中的内 容,主要是关于问题处理流程和如何创建项目问题的指导,还有工作周日志的说明。这些信息都是技术性的,属于内部使用的指南或帮助文档。\n\n用户可能是在询问如何操作系统中的某个功能,比如如何创建问题或者使用高级查询。也有可能是想确认是否有相关的