recognize_intention.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import requests
  2. import time
  3. from config.config.dify_config import difyconfig as dify_config
  4. def chat_with_dify(question='你看到了什么', image_path=''):
  5. s1_time = time.time()
  6. headers = {
  7. "Authorization": f"Bearer {dify_config.get_recognize_api_key()}",
  8. "Content-Type": "application/json"
  9. }
  10. payload = {
  11. "inputs": {}, # 可以留空,或者传其他上下文
  12. "query": question,
  13. "response_mode": "blocking",
  14. "conversation_id": "", # 如果是新会话可以留空
  15. "user": dify_config.get_user(),
  16. }
  17. # 发送请求
  18. try:
  19. s3_time = time.time()
  20. resp = requests.post(dify_config.get_recognize_url(), headers=headers, json=payload)
  21. s4_time = time.time()
  22. res = resp.json().get("answer", '请求出错')
  23. print(f"请求完成耗时: {s4_time - s3_time:.2f}秒")
  24. print(f"总耗时: {s4_time - s1_time:.2f}秒")
  25. return res
  26. except Exception as e:
  27. print(f"请求出错: {e}")
  28. return ''
  29. if __name__ == '__main__':
  30. # chat_with_dify(question='请用 30 字以内分析你看到的',image_path='demo_pic.png')
  31. res = chat_with_dify(question='你看到了什么', image_path='')
  32. print(11111,res)