# # curl -X POST 'http://192.168.2.240/v1/chat-messages' \ # --header 'Authorization: Bearer {api_key}' \ # --header 'Content-Type: application/json' \ # --data-raw '{ # "inputs": {}, # "query": "What are the specs of the iPhone 13 Pro Max?", # "response_mode": "streaming", # "conversation_id": "", # "user": "abc-123", # "files": [ # { # "type": "image", # "transfer_method": "remote_url", # "url": "https://cloud.dify.ai/logo/logo-site.png" # } # ] # }' import json import requests def dify_news_title_analyze(content): # 设置Authorization和URL authorization = "Bearer app-rhhKkbvHd2IAQoGX7xTzXZJj" # 请替换为真实的Authorization token url = 'http://192.168.2.240/v1/chat-messages' data = { "response_mode": "blocking", "conversation_id": "", "inputs": {}, "query": content, "user": "a-bot" } # 设置请求头 headers = { "Content-Type": "application/json; charset=utf-8", "Authorization": authorization } # 发送POST请求 response = requests.post(url, headers=headers, data=json.dumps(data), ) response.encoding = 'utf-8' # 输出响应内容 print(response.status_code) print(response.json()) return extract_content(response.json()) def extract_content(data): """解析API响应内容 Args: data: API返回的响应数据,可以是字典或字符串 Returns: str: 提取的answer内容 """ try: # 如果是字符串,尝试解析为字典 if isinstance(data, str): data = json.dumps(data) # 如果是字典,直接获取answer if isinstance(data, dict): answer = data.get('answer', '') if answer: return answer return None except Exception as e: print(f"解析响应失败: {str(e)}") return None