bridge_qwen.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import time
  2. import os
  3. from toolbox import update_ui, get_conf, update_ui_lastest_msg
  4. from toolbox import check_packages, report_exception
  5. model_name = 'Qwen'
  6. def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=[], console_slience=False):
  7. """
  8. ⭐多线程方法
  9. 函数的说明请见 request_llms/bridge_all.py
  10. """
  11. watch_dog_patience = 5
  12. response = ""
  13. from .com_qwenapi import QwenRequestInstance
  14. sri = QwenRequestInstance()
  15. for response in sri.generate(inputs, llm_kwargs, history, sys_prompt):
  16. if len(observe_window) >= 1:
  17. observe_window[0] = response
  18. if len(observe_window) >= 2:
  19. if (time.time()-observe_window[1]) > watch_dog_patience: raise RuntimeError("程序终止。")
  20. return response
  21. def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None):
  22. """
  23. ⭐单线程方法
  24. 函数的说明请见 request_llms/bridge_all.py
  25. """
  26. chatbot.append((inputs, ""))
  27. yield from update_ui(chatbot=chatbot, history=history)
  28. # 尝试导入依赖,如果缺少依赖,则给出安装建议
  29. try:
  30. check_packages(["dashscope"])
  31. except:
  32. yield from update_ui_lastest_msg(f"导入软件依赖失败。使用该模型需要额外依赖,安装方法```pip install --upgrade dashscope```。",
  33. chatbot=chatbot, history=history, delay=0)
  34. return
  35. # 检查DASHSCOPE_API_KEY
  36. if get_conf("DASHSCOPE_API_KEY") == "":
  37. yield from update_ui_lastest_msg(f"请配置 DASHSCOPE_API_KEY。",
  38. chatbot=chatbot, history=history, delay=0)
  39. return
  40. if additional_fn is not None:
  41. from core_functional import handle_core_functionality
  42. inputs, history = handle_core_functionality(additional_fn, inputs, history, chatbot)
  43. # 开始接收回复
  44. from .com_qwenapi import QwenRequestInstance
  45. sri = QwenRequestInstance()
  46. for response in sri.generate(inputs, llm_kwargs, history, system_prompt):
  47. chatbot[-1] = (inputs, response)
  48. yield from update_ui(chatbot=chatbot, history=history)
  49. # 总结输出
  50. if response == f"[Local Message] 等待{model_name}响应中 ...":
  51. response = f"[Local Message] {model_name}响应异常 ..."
  52. history.extend([inputs, response])
  53. yield from update_ui(chatbot=chatbot, history=history)