Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

134 lines
5.6KB

  1. # encoding:utf-8
  2. import time
  3. import openai
  4. import openai.error
  5. import anthropic
  6. from bot.bot import Bot
  7. from bot.openai.open_ai_image import OpenAIImage
  8. from bot.chatgpt.chat_gpt_session import ChatGPTSession
  9. from bot.gemini.google_gemini_bot import GoogleGeminiBot
  10. from bot.session_manager import SessionManager
  11. from bridge.context import ContextType
  12. from bridge.reply import Reply, ReplyType
  13. from common.log import logger
  14. from config import conf
  15. user_session = dict()
  16. # OpenAI对话模型API (可用)
  17. class ClaudeAPIBot(Bot, OpenAIImage):
  18. def __init__(self):
  19. super().__init__()
  20. self.claudeClient = anthropic.Anthropic(
  21. api_key=conf().get("claude_api_key")
  22. )
  23. openai.api_key = conf().get("open_ai_api_key")
  24. if conf().get("open_ai_api_base"):
  25. openai.api_base = conf().get("open_ai_api_base")
  26. proxy = conf().get("proxy")
  27. if proxy:
  28. openai.proxy = proxy
  29. self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "text-davinci-003")
  30. def reply(self, query, context=None):
  31. # acquire reply content
  32. if context and context.type:
  33. if context.type == ContextType.TEXT:
  34. logger.info("[CLAUDE_API] query={}".format(query))
  35. session_id = context["session_id"]
  36. reply = None
  37. if query == "#清除记忆":
  38. self.sessions.clear_session(session_id)
  39. reply = Reply(ReplyType.INFO, "记忆已清除")
  40. elif query == "#清除所有":
  41. self.sessions.clear_all_session()
  42. reply = Reply(ReplyType.INFO, "所有人记忆已清除")
  43. else:
  44. session = self.sessions.session_query(query, session_id)
  45. result = self.reply_text(session)
  46. logger.info(result)
  47. total_tokens, completion_tokens, reply_content = (
  48. result["total_tokens"],
  49. result["completion_tokens"],
  50. result["content"],
  51. )
  52. logger.debug(
  53. "[CLAUDE_API] new_query={}, session_id={}, reply_cont={}, completion_tokens={}".format(str(session), session_id, reply_content, completion_tokens)
  54. )
  55. if total_tokens == 0:
  56. reply = Reply(ReplyType.ERROR, reply_content)
  57. else:
  58. self.sessions.session_reply(reply_content, session_id, total_tokens)
  59. reply = Reply(ReplyType.TEXT, reply_content)
  60. return reply
  61. elif context.type == ContextType.IMAGE_CREATE:
  62. ok, retstring = self.create_img(query, 0)
  63. reply = None
  64. if ok:
  65. reply = Reply(ReplyType.IMAGE_URL, retstring)
  66. else:
  67. reply = Reply(ReplyType.ERROR, retstring)
  68. return reply
  69. def reply_text(self, session: ChatGPTSession, retry_count=0):
  70. try:
  71. actual_model = self._model_mapping(conf().get("model"))
  72. response = self.claudeClient.messages.create(
  73. model=actual_model,
  74. max_tokens=1024,
  75. # system=conf().get("system"),
  76. messages=GoogleGeminiBot.filter_messages(session.messages)
  77. )
  78. # response = openai.Completion.create(prompt=str(session), **self.args)
  79. res_content = response.content[0].text.strip().replace("<|endoftext|>", "")
  80. total_tokens = response.usage.input_tokens+response.usage.output_tokens
  81. completion_tokens = response.usage.output_tokens
  82. logger.info("[CLAUDE_API] reply={}".format(res_content))
  83. return {
  84. "total_tokens": total_tokens,
  85. "completion_tokens": completion_tokens,
  86. "content": res_content,
  87. }
  88. except Exception as e:
  89. need_retry = retry_count < 2
  90. result = {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"}
  91. if isinstance(e, openai.error.RateLimitError):
  92. logger.warn("[CLAUDE_API] RateLimitError: {}".format(e))
  93. result["content"] = "提问太快啦,请休息一下再问我吧"
  94. if need_retry:
  95. time.sleep(20)
  96. elif isinstance(e, openai.error.Timeout):
  97. logger.warn("[CLAUDE_API] Timeout: {}".format(e))
  98. result["content"] = "我没有收到你的消息"
  99. if need_retry:
  100. time.sleep(5)
  101. elif isinstance(e, openai.error.APIConnectionError):
  102. logger.warn("[CLAUDE_API] APIConnectionError: {}".format(e))
  103. need_retry = False
  104. result["content"] = "我连接不到你的网络"
  105. else:
  106. logger.warn("[CLAUDE_API] Exception: {}".format(e))
  107. need_retry = False
  108. self.sessions.clear_session(session.session_id)
  109. if need_retry:
  110. logger.warn("[CLAUDE_API] 第{}次重试".format(retry_count + 1))
  111. return self.reply_text(session, retry_count + 1)
  112. else:
  113. return result
  114. def _model_mapping(self, model) -> str:
  115. if model == "claude-3-opus":
  116. return "claude-3-opus-20240229"
  117. elif model == "claude-3-sonnet":
  118. return "claude-3-sonnet-20240229"
  119. elif model == "claude-3-haiku":
  120. return "claude-3-haiku-20240307"
  121. return model