llm_chatglm.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from wudao.api_request import executeEngine, getToken
  2. from plugins.common import settings
  3. import os
  4. # 能力类型
  5. ability_type = "chatGLM"
  6. # 引擎类型
  7. engine_type = "chatGLM"
  8. Api_key=''
  9. Public_key=''
  10. if os.environ['GLM_API_KEY']:
  11. Api_key=os.environ['GLM_API_KEY']
  12. Public_key=os.environ['GLM_Public_key']
  13. else:
  14. Api_key=settings.Api_key
  15. Public_key=settings.Public_key
  16. def chat_init(history):
  17. history_data = []
  18. if history is not None:
  19. history_data = []
  20. for i, old_chat in enumerate(history):
  21. history_data.append(old_chat['content'])
  22. return history_data
  23. def chat_one(prompt, history_formatted, max_length, top_p, temperature, data):
  24. # 请求参数样例
  25. data = {
  26. "top_p": top_p,
  27. "temperature": temperature,
  28. "prompt": prompt,
  29. "requestTaskNo": "1542097269879345154",
  30. "history":history_formatted
  31. }
  32. token_result = getToken(Api_key, Public_key)
  33. if token_result and token_result["code"] == 200:
  34. token = token_result["data"]
  35. resp = executeEngine(ability_type, engine_type, token, data)
  36. print(resp)
  37. if resp[ 'success']!=False:
  38. yield resp['data']['outputText']
  39. else:
  40. yield resp['msg']
  41. else:
  42. yield "获取token失败,请检查 API_KEY 和 PUBLIC_KEY"
  43. def load_model():
  44. pass
  45. class Lock:
  46. def __init__(self):
  47. pass
  48. def get_waiting_threads(self):
  49. return 0
  50. def __enter__(self):
  51. pass
  52. def __exit__(self, exc_type, exc_val, exc_tb):
  53. pass
粤ICP备19079148号