recognize_models.py 1.3 KB

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