Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tongyi_qwen_bot.py 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # encoding:utf-8
  2. import json
  3. import time
  4. from typing import List, Tuple
  5. import openai
  6. import openai.error
  7. import broadscope_bailian
  8. from broadscope_bailian import ChatQaMessage
  9. from bot.bot import Bot
  10. from bot.baidu.baidu_wenxin_session import BaiduWenxinSession
  11. from bot.session_manager import SessionManager
  12. from bridge.context import ContextType
  13. from bridge.reply import Reply, ReplyType
  14. from common.log import logger
  15. from config import conf, load_config
  16. class TongyiQwenBot(Bot):
  17. def __init__(self):
  18. super().__init__()
  19. self.access_key_id = conf().get("tongyi_access_key_id")
  20. self.access_key_secret = conf().get("tongyi_access_key_secret")
  21. self.agent_key = conf().get("tongyi_agent_key")
  22. self.app_id = conf().get("tongyi_app_id")
  23. self.node_id = conf().get("tongyi_node_id")
  24. self.api_key_client = broadscope_bailian.AccessTokenClient(access_key_id=self.access_key_id, access_key_secret=self.access_key_secret)
  25. self.api_key_expired_time = self.set_api_key()
  26. self.sessions = SessionManager(BaiduWenxinSession, model=conf().get("model") or "tongyi")
  27. self.temperature = conf().get("temperature", 0.2) # 值在[0,1]之间,越大表示回复越具有不确定性
  28. self.top_p = conf().get("top_p", 1)
  29. def reply(self, query, context=None):
  30. # acquire reply content
  31. if context.type == ContextType.TEXT:
  32. logger.info("[TONGYI] query={}".format(query))
  33. session_id = context["session_id"]
  34. reply = None
  35. clear_memory_commands = conf().get("clear_memory_commands", ["#清除记忆"])
  36. if query in clear_memory_commands:
  37. self.sessions.clear_session(session_id)
  38. reply = Reply(ReplyType.INFO, "记忆已清除")
  39. elif query == "#清除所有":
  40. self.sessions.clear_all_session()
  41. reply = Reply(ReplyType.INFO, "所有人记忆已清除")
  42. elif query == "#更新配置":
  43. load_config()
  44. reply = Reply(ReplyType.INFO, "配置已更新")
  45. if reply:
  46. return reply
  47. session = self.sessions.session_query(query, session_id)
  48. logger.debug("[TONGYI] session query={}".format(session.messages))
  49. reply_content = self.reply_text(session)
  50. logger.debug(
  51. "[TONGYI] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format(
  52. session.messages,
  53. session_id,
  54. reply_content["content"],
  55. reply_content["completion_tokens"],
  56. )
  57. )
  58. if reply_content["completion_tokens"] == 0 and len(reply_content["content"]) > 0:
  59. reply = Reply(ReplyType.ERROR, reply_content["content"])
  60. elif reply_content["completion_tokens"] > 0:
  61. self.sessions.session_reply(reply_content["content"], session_id, reply_content["total_tokens"])
  62. reply = Reply(ReplyType.TEXT, reply_content["content"])
  63. else:
  64. reply = Reply(ReplyType.ERROR, reply_content["content"])
  65. logger.debug("[TONGYI] reply {} used 0 tokens.".format(reply_content))
  66. return reply
  67. else:
  68. reply = Reply(ReplyType.ERROR, "Bot不支持处理{}类型的消息".format(context.type))
  69. return reply
  70. def reply_text(self, session: BaiduWenxinSession, retry_count=0) -> dict:
  71. """
  72. call bailian's ChatCompletion to get the answer
  73. :param session: a conversation session
  74. :param retry_count: retry count
  75. :return: {}
  76. """
  77. try:
  78. prompt, history = self.convert_messages_format(session.messages)
  79. self.update_api_key_if_expired()
  80. # NOTE 阿里百炼的call()函数参数比较奇怪, top_k参数表示top_p, top_p参数表示temperature, 可以参考文档 https://help.aliyun.com/document_detail/2587502.htm
  81. response = broadscope_bailian.Completions().call(app_id=self.app_id, prompt=prompt, history=history, top_k=self.top_p, top_p=self.temperature)
  82. completion_content = self.get_completion_content(response, self.node_id)
  83. completion_tokens, total_tokens = self.calc_tokens(session.messages, completion_content)
  84. return {
  85. "total_tokens": total_tokens,
  86. "completion_tokens": completion_tokens,
  87. "content": completion_content,
  88. }
  89. except Exception as e:
  90. need_retry = retry_count < 2
  91. result = {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"}
  92. if isinstance(e, openai.error.RateLimitError):
  93. logger.warn("[TONGYI] RateLimitError: {}".format(e))
  94. result["content"] = "提问太快啦,请休息一下再问我吧"
  95. if need_retry:
  96. time.sleep(20)
  97. elif isinstance(e, openai.error.Timeout):
  98. logger.warn("[TONGYI] Timeout: {}".format(e))
  99. result["content"] = "我没有收到你的消息"
  100. if need_retry:
  101. time.sleep(5)
  102. elif isinstance(e, openai.error.APIError):
  103. logger.warn("[TONGYI] Bad Gateway: {}".format(e))
  104. result["content"] = "请再问我一次"
  105. if need_retry:
  106. time.sleep(10)
  107. elif isinstance(e, openai.error.APIConnectionError):
  108. logger.warn("[TONGYI] APIConnectionError: {}".format(e))
  109. need_retry = False
  110. result["content"] = "我连接不到你的网络"
  111. else:
  112. logger.exception("[TONGYI] Exception: {}".format(e))
  113. need_retry = False
  114. self.sessions.clear_session(session.session_id)
  115. if need_retry:
  116. logger.warn("[TONGYI] 第{}次重试".format(retry_count + 1))
  117. return self.reply_text(session, retry_count + 1)
  118. else:
  119. return result
  120. def set_api_key(self):
  121. api_key, expired_time = self.api_key_client.create_token(agent_key=self.agent_key)
  122. broadscope_bailian.api_key = api_key
  123. return expired_time
  124. def update_api_key_if_expired(self):
  125. if time.time() > self.api_key_expired_time:
  126. self.api_key_expired_time = self.set_api_key()
  127. def convert_messages_format(self, messages) -> Tuple[str, List[ChatQaMessage]]:
  128. history = []
  129. user_content = ''
  130. assistant_content = ''
  131. for message in messages:
  132. role = message.get('role')
  133. if role == 'user':
  134. user_content += message.get('content')
  135. elif role == 'assistant':
  136. assistant_content = message.get('content')
  137. history.append(ChatQaMessage(user_content, assistant_content))
  138. user_content = ''
  139. assistant_content = ''
  140. if user_content == '':
  141. raise Exception('no user message')
  142. return user_content, history
  143. def get_completion_content(self, response, node_id):
  144. text = response['Data']['Text']
  145. if node_id == '':
  146. return text
  147. # TODO: 当使用流程编排创建大模型应用时,响应结构如下,最终结果在['finalResult'][node_id]['response']['text']中,暂时先这么写
  148. # {
  149. # 'Success': True,
  150. # 'Code': None,
  151. # 'Message': None,
  152. # 'Data': {
  153. # 'ResponseId': '9822f38dbacf4c9b8daf5ca03a2daf15',
  154. # 'SessionId': 'session_id',
  155. # 'Text': '{"finalResult":{"LLM_T7islK":{"params":{"modelId":"qwen-plus-v1","prompt":"${systemVars.query}${bizVars.Text}"},"response":{"text":"作为一个AI语言模型,我没有年龄,因为我没有生日。\n我只是一个程序,没有生命和身体。"}}}}',
  156. # 'Thoughts': [],
  157. # 'Debug': {},
  158. # 'DocReferences': []
  159. # },
  160. # 'RequestId': '8e11d31551ce4c3f83f49e6e0dd998b0',
  161. # 'Failed': None
  162. # }
  163. text_dict = json.loads(text)
  164. completion_content = text_dict['finalResult'][node_id]['response']['text']
  165. return completion_content
  166. def calc_tokens(self, messages, completion_content):
  167. completion_tokens = len(completion_content)
  168. prompt_tokens = 0
  169. for message in messages:
  170. prompt_tokens += len(message["content"])
  171. return completion_tokens, prompt_tokens + completion_tokens