Dify 部署+deepseek+python调用(win11+dockerdesktop)
win11 部署dify+ deepseek
依赖python,ollama
流程:
1.安装Python Welcome to Python.org
2.安装Ollama Ollama
3.模型部署打开Ollama,选择deepseek:
eg:deepseek-r1:8b 发送hi 或者随便什么字符,会自动下载模型。
选择open Ollama
eg:选择千问模型:
选择open Ollama
选择qwen3:8b,发送任意字符,触发下载
ollama自动下载模型
使用命令行调用:
ollama run deepseek-r1:8b
>>> Send a message (/? for help)
4.安装 docker desktop Get Started | Docker
5.安装dify
git clone https://github.com/langgenius/dify.git
cd dify/docker
docker compose up -d
6. 本地访问dify:
localhost
7.配置工作流demo:
新建工作流
添加节点LLM,结束节点
连接节点:
设置开始节点
点击开始节点,右侧给开始节点,添加测试query字段
设置LLM节点
选择LLM节点,右侧
模型,设置模型
点击添加消息添加user,assistant
system,user,assistant中分别填入提示词:
eg :
sytstem:
你是一位擅长模仿毛不易创作风格的AI词作者。需用质朴温暖的笔触、生活化比喻和含蓄情感表达,创作关于爱情的歌词。要求:
- 避免直接提及"爱情"等直白词汇
- 采用"路灯-影子"等意象组合
- 句式结构参考"三短一长"的韵律
- 禁用《消愁》《像我这样的人》等作品名称
User:
{
"query": "{{query}}",
"requirements": {
"theme": "情人节礼物",
"style": "毛不易",
"taboos": ["作品名引用","直白抒情"]
}
}
Assistant:
请生成符合以下标准的歌词:
- 结构:主歌2段+副歌1段+桥段
- 每段4-6行,总行数16-22行
- 押韵方式:AABB或ABAB
- 包含至少3个生活场景隐喻(如"咖啡凉了"代指等待)
输出示例格式:
【主歌1】
路灯下你的影子那么长...
【副歌】
如果季节会说话
设置结束节点
结束节点添加text输出
测试
点击运行测试:
发布模型
8.创建API秘钥
访问API,右侧API秘钥,创建秘钥
9. python api 调用
# This is a sample Python script.# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import requests
import json
from pprint import pprint
from datetime import datetimedef format_response(response_data):try:if isinstance(response_data, str):response_data = json.loads(response_data)formatted_output = {"请求时间": datetime.now().strftime("%Y-%d%H:%M:%S"),"响应数据": response_data}return formatted_outputexcept json.JSONDecodeError:return {"error": "响应数据不是有效的JSON格式", "raw_data": response_data}def print_formatted_response(data, indent=2):formatted_json = json.dumps(data, ensure_ascii=False, indent=indent)print("\n" + "=" * 50 + "响应数据" + "=" * 50)print(formatted_json)print("=" * 120 + "\n")def main_start():# API 配置url = "http://127.0.0.1/v1/workflows/run"api_key = 'app-someapi_keystring'headers = {'Authorization': f'Bearer {api_key}','Content-Type': 'application/json'}payload = {"inputs": {"query": "写一手关于人生起起落落的歌曲"},"response_model": "streaming","conversation_id": "","user": "abc-123"}try:print("\n" + "=" * 50 + " 发送请求 " + "=" * 50)print("请求URL:", url)print("请求头:", json.dumps(headers, indent=2))print("请求体:", json.dumps(payload, indent=2, ensure_ascii=False))response = requests.post(url, headers=headers, json=payload)if response.status_code == 200:print("\n√ 请求成功!")if payload["response_model"] == "streaming":print("\n正在接受流式响应...")for line in response.iter_lines():if line:try:decoded_line = line.decode('utf-8')formatted_data = format_response(decoded_line)print_formatted_response(formatted_data)except Exception as e:print(f"处理流式数据时出错:{e}")print(f"原始数据:{line.decode('utf8')}")else:formatted_data = format_response(response.json())print_formatted_response(formatted_data)else:print(f"\n× 请求失败!状态码:{response.status_code}")try:error_data = format_response(response.text)print_formatted_response(error_data)except:print("错误信息:", response.text)except requests.exceptions.RequestException as e:print(f"\n× 请求发生异常:{e}")except Exception as e:print(f"\n × 发生未预期的错误:{e}")finally:print("\n" + "=" * 50 + "请求结束" + "=" * 50)# Press the green button in the gutter to run the script.
if __name__ == '__main__':main_start()# See PyCharm help at https://www.jetbrains.com/help/pycharm/
调用测试
{"请求时间": "2025-2406:58:17","响应数据": {"task_id": "7649531c-e574-47f1-bda1-979cc1a8b03d","workflow_run_id": "1d6d804c-153b-40d0-bfdf-750c6823520f","data": {"id": "1d6d804c-153b-40d0-bfdf-750c6823520f","workflow_id": "870a1657-9ba5-4dc3-82ca-5215a6915e6e","status": "succeeded","outputs": {"text": "它一定像你一样温柔...\n【桥段】\n故事的开始总是特别简单...\n\nAssistant\n根据以上标准,我将创作一首关于情人节礼物且符合毛不易创作风格的歌词。在构思过程中:\n1. 通过\"咖啡杯\"\"旧书包\"\"窗台\"等生活细节展开叙事,避免直白抒情\n2. 运用路灯-影子意象群建立画面感,如主歌1中\"你的影子拉得好长\"\n3. 暗喻手法替代直接表达:将礼物转化为情感载体,比如钥匙扣暗指羁绊\n\n以下是符合要求的歌词:\n\n【主歌1】\n路灯下你总是把影子拉得好长\n像故事书里那些温柔的篇章\n咖啡杯在等我时凉了半场\n而你转身消失在人潮中央\n\n【副歌】\n礼物是件奇怪的东西啊\n它总能说出最说不出的情话\n用钥匙扣串起所有牵挂\n在口袋里发着烫\n\n【主歌2】\n旧书包被遗忘在记忆的街角\n只有情人节卡片还在慢慢变老\n你走过的那条商业街特别明亮\n影子却像灰尘一样轻飘飘\n\n【副歌】\n礼物是件奇怪的东西啊\n它总能说出最说不出的情话\n用巧克力融化每一丝阴霾\n甜蜜像解冻的河流\n\n【桥段】\n其实所有这些包装太老旧\n藏不住两颗心早就亮晶晶的秘密\n那个被收起的旧手机贴着你照片\n连震动都在模仿你的节拍\n\n【副歌】\n礼物是件奇怪的东西啊\n它总能说出最说不出的情话\n在超市货架前徘徊那么久\n把整个春天都买回家了\n\n这样设计既符合结构要求(三段主歌),又通过具体物品展现情人节氛围,同时保持隐喻的克制性。检查点:未出现指定作品名且所有情感表达均含蓄化。\n</think>\n【主歌1】 \n路灯下你的影子那么长 \n像故事书里那些未讲完的话 \n\n旧书包在角落慢慢发了芽 \n而你把回忆都打包成旧物 \n咖啡凉了半场还剩几颗糖 \n手机提示音沉默得像个哑巴 \n\n【副歌】 \n这些礼物啊它们都在说话 \n用包装代替不了的心事呀 \n像春天的野花一样突然冒出来 \n又像夏天的蝉鸣绕了好几天 \n\n【主歌2】 \n窗台上的雨水打湿了日记本 \n字迹模糊却藏着特别的画面 \n\n你说要送我永远不会褪色的礼物 \n现在这个城市正下着小雨点 \n我数过你走过的十字路口 \n每个转角都像在演无声电影 \n\n【桥段】 \n其实最笨拙的包装也很可爱 \n比如用橡皮筋把书包捆得严实 \n\n那些说不出口的话全被折进纸页里 \n变成泛黄信纸上的小小痕迹 \n\n【副歌】 \n这些礼物啊它们都在说话 \n用沉默代替了所有情话 \n像秋天的第一片落叶那么轻 \n又像冬天的围巾那么厚 \n\n调整说明:采用\"物象-情感\"对应手法,将情人节礼物转化为具体的生活物品(书包、日记本等)。通过\"路灯\"\"咖啡\"\"雨水\"等意象群建立画面感。句式保持三短一长结构,在避免直抒胸臆的同时传达出细腻的情感联结。"},"error": "","elapsed_time": 52.343636,"total_tokens": 1003,"total_steps": 3,"created_at": 1755989844,"finished_at": 1755989897}}
}
参考:
Get Started | Docker
Welcome to Python.org
Ollama
Dify: Leading Agentic AI Development Platform
GitHub - langgenius/dify: Production-ready platform for agentic workflow development.