dify中工作流代码执行节点:根据上一节点的提示词生成一张图片,传给下一个节点,下一个节对该图片进行识别描述成中文。。。
代码节点执行的代码如下,运行返回:"Output image is missing"
import requests
import json
import os
def main(prompt: str, apikey: str) -> dict:
"""
通过阿里云百炼生成图片(适配Dify代码执行节点)
更新说明:
1. 修正model字段位置
2. 增加代理清除逻辑
3. 完善错误处理
"""
# 强制清除代理(防止容器环境变量干扰)
os.environ.pop('HTTP_PROXY', None)
os.environ.pop('HTTPS_PROXY', None)
url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis"
headers = {
"Authorization": f"Bearer {apikey}",
"Content-Type": "application/json",
"X-DashScope-Async": "enable" # 明确启用异步
}
payload = {
"model": "wanx-v1",
"input": {
"prompt": prompt,
"size": "1024x1024" # 标准尺寸选项:1024x1024/720x1280/1280x720
}
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
# 调试日志(在Dify日志中可见)
print(f"API响应: {json.dumps(result, indent=2)}")
if "output" in result:
if "results" in result["output"]: # 同步响应
image_url = result["output"]["results"][0]["url"]
return {"result": f""}
elif "task_id" in result["output"]: # 异步响应
return {"result": f"任务已提交,Task ID: {result['output']['task_id']}"}
return {"error": f"未知响应格式: {json.dumps(result)}"}
except requests.exceptions.RequestException as e:
return {"error": f"请求失败: {str(e)}"}
except Exception as e:
return {"error": f"运行时错误: {str(e)}"}
代码中的api-key使用的有效值,在宿主电脑上运行时,没有报错,但是直接运行结束,没有返回值,请老师帮
忙查看一下,多谢!diy源码已经根据网上大人的信息进行了稍微更改:
#### 修改api源码
1. api/services/file_service.py 中 FileService类upload_file函数中,在storage.save之前新增代码
```
# 新增此行
source_url = '/app/api/storage/' + file_key if not source_url else source_url
# save file to storage
storage.save(file_key, content)
```
#### 修改沙盒配置
1. 修改 docker/volumes/sandbox/conf/config.yaml,为什么加这个请看 up 主之前沙盒的视频
```
allowed_syscalls: [0,1,2,3,4,5,6,7,8,9,14,15,21,22,25,26,29,30,31,32,33,34,35,38,39,43,44,45,46,56,57,61,62,63,64,71,72,79,80,94,98,101,131,132,134,135,139,144,146,172,215,222,226,318,334,307,262,16,8,217,1,3,257,0,202,9,12,10,11,15,25,105,106,102,39,110,186,60,231,234,13,16,24,273,274,334,228,96,35,291,233,230,270,201,14,131,318,56,258,83,41,42,49,50,43,44,45,51,47,52,54,271,63,46,307,55,5,72,138,7,281]
```
#### 修改 docker-compose.yaml
```
api:
build: ../api
restart: always
```
2. 修改 sandbox 节点中的volumes
```
volumes:
- ./volumes/sandbox/dependencies:/dependencies
- ./volumes/sandbox/conf:/conf
- ./volumes/app/storage:/var/sandbox/sandbox-python/usr/local/storage
```
及对squid的代理设置,允许方位阿里云,并导出容器log:
D:\dify-main\dify-main\docker>docker logs docker-sandbox-1 --tail 20
2025/04/29 10:22:43 env.go:30: [WARN]python lib path /usr/lib/python3.10 is not available
2025/04/29 10:22:43 env.go:30: [WARN]python lib path /usr/lib/python3 is not available
2025/04/29 10:22:47 env.go:30: [WARN]python lib path /run/systemd/resolve/stub-resolv.conf is not available
2025/04/29 10:22:47 env.go:30: [WARN]python lib path /run/resolvconf/resolv.conf is not available
2025/04/29 10:22:50 server.go:89: [INFO]Python dependencies updated successfully.
[GIN] 2025/04/29 - 10:23:02 | 200 | 22.516µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:23:33 | 200 | 20.624µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:24:03 | 200 | 24.774µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:24:33 | 200 | 115.492µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:25:03 | 200 | 26.201µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:25:33 | 200 | 29.369µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:26:03 | 200 | 22.347µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:26:33 | 200 | 89.713µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:27:03 | 200 | 30.881µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:27:33 | 200 | 61.489µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:28:03 | 200 | 25.922µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:28:34 | 200 | 29.293µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:29:04 | 200 | 39.988µs | ::1 | GET "/health"
[GIN] 2025/04/29 - 10:29:07 | 200 | 239.362745ms | 172.18.0.5 | POST "/v1/sandbox/run"
[GIN] 2025/04/29 - 10:29:34 | 200 | 22.853µs | ::1 | GET "/health"