import requests

API_KEY = 'app-zEgQZ2PD7dfLdrLTJglgTJmX'
API_URL = 'http://localhost/v1/chat-messages'  # 云端请替换为实际地址

headers = {
    'Authorization': f'Bearer {API_KEY}',
    'Content-Type': 'application/json'
}

# 你要发送的消息内容
query = "xxxxx"

# 如果你的应用有自定义 inputs，可以在这里填写
inputs = {
    # "input1": "value1",
    # "input2": "value2",
}

# 如果需要上传文件（如图片），可以这样写
files = [
    {
        "type": "image",
        "transfer_method": "remote_url",
        "url": "https://cloud.dify.ai/logo/logo-site.png"
    }
]

payload = {
    "inputs": inputs,
    "query": query,
    "response_mode": "blocking",  # 或 "streaming"
    "user": "abc-123",            # 可选，用户ID
    "conversation_id": "",        # 可选，首次对话留空，后续对话填前一次返回的 conversation_id
    "files": files                # 可选，文件输入
}

response = requests.post(API_URL, headers=headers, json=payload)

if response.status_code == 200:
    print("运行成功，原始返回内容如下：")
    print(response.text)  # 先打印原始内容
    try:
        print(response.json())
    except Exception as e:
        print("返回内容不是合法的JSON，错误信息：", e)
else:
    print("运行失败，状态码：", response.status_code)
    print(response.text)