選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

280 行
12KB

  1. import requests
  2. import json
  3. import plugins
  4. from bridge.reply import Reply, ReplyType
  5. from bridge.context import ContextType
  6. from channel.chat_message import ChatMessage
  7. from plugins import *
  8. from common.log import logger
  9. from common.expired_dict import ExpiredDict
  10. import os
  11. import base64
  12. from pathlib import Path
  13. from PIL import Image
  14. import oss2
  15. @plugins.register(
  16. name="coze4upload",
  17. desire_priority=-1,
  18. desc="A plugin for upload",
  19. version="0.0.01",
  20. author="",
  21. )
  22. class coze4upload(Plugin):
  23. def __init__(self):
  24. super().__init__()
  25. try:
  26. curdir = os.path.dirname(__file__)
  27. config_path = os.path.join(curdir, "config.json")
  28. if os.path.exists(config_path):
  29. with open(config_path, "r", encoding="utf-8") as f:
  30. self.config = json.load(f)
  31. else:
  32. # 使用父类的方法来加载配置
  33. self.config = super().load_config()
  34. if not self.config:
  35. raise Exception("config.json not found")
  36. # 设置事件处理函数
  37. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  38. self.params_cache = ExpiredDict(300)
  39. # 从配置中提取所需的设置
  40. self.keys = self.config.get("keys", {})
  41. self.url_sum = self.config.get("url_sum", {})
  42. self.search_sum = self.config.get("search_sum", {})
  43. self.file_sum = self.config.get("file_sum", {})
  44. self.image_sum = self.config.get("image_sum", {})
  45. self.note = self.config.get("note", {})
  46. self.sum4all_key = self.keys.get("sum4all_key", "")
  47. self.search1api_key = self.keys.get("search1api_key", "")
  48. self.gemini_key = self.keys.get("gemini_key", "")
  49. self.bibigpt_key = self.keys.get("bibigpt_key", "")
  50. self.outputLanguage = self.keys.get("outputLanguage", "zh-CN")
  51. self.opensum_key = self.keys.get("opensum_key", "")
  52. self.open_ai_api_key = self.keys.get("open_ai_api_key", "")
  53. self.model = self.keys.get("model", "gpt-3.5-turbo")
  54. self.open_ai_api_base = self.keys.get("open_ai_api_base", "https://api.openai.com/v1")
  55. self.xunfei_app_id = self.keys.get("xunfei_app_id", "")
  56. self.xunfei_api_key = self.keys.get("xunfei_api_key", "")
  57. self.xunfei_api_secret = self.keys.get("xunfei_api_secret", "")
  58. self.perplexity_key = self.keys.get("perplexity_key", "")
  59. self.flomo_key = self.keys.get("flomo_key", "")
  60. # 之前提示
  61. self.previous_prompt=''
  62. self.file_sum_enabled = self.file_sum.get("enabled", False)
  63. self.file_sum_service = self.file_sum.get("service", "")
  64. self.max_file_size = self.file_sum.get("max_file_size", 15000)
  65. self.file_sum_group = self.file_sum.get("group", True)
  66. self.file_sum_qa_prefix = self.file_sum.get("qa_prefix", "问")
  67. self.file_sum_prompt = self.file_sum.get("prompt", "")
  68. self.image_sum_enabled = self.image_sum.get("enabled", False)
  69. self.image_sum_service = self.image_sum.get("service", "")
  70. self.image_sum_group = self.image_sum.get("group", True)
  71. self.image_sum_qa_prefix = self.image_sum.get("qa_prefix", "问")
  72. self.image_sum_prompt = self.image_sum.get("prompt", "")
  73. # 初始化成功日志
  74. logger.info("[file4upload] inited.")
  75. except Exception as e:
  76. # 初始化失败日志
  77. logger.warn(f"file4upload init failed: {e}")
  78. def on_handle_context(self, e_context: EventContext):
  79. context = e_context["context"]
  80. if context.type not in [ContextType.TEXT, ContextType.SHARING,ContextType.FILE,ContextType.IMAGE]:
  81. return
  82. msg: ChatMessage = e_context["context"]["msg"]
  83. user_id = msg.from_user_id
  84. content = context.content
  85. isgroup = e_context["context"].get("isgroup", False)
  86. if isgroup and not self.file_sum_group:
  87. # 群聊中忽略处理文件
  88. logger.info("群聊消息,文件处理功能已禁用")
  89. return
  90. logger.info("on_handle_context: 处理上下文开始")
  91. context.get("msg").prepare()
  92. api_key='sk-5z2L4zy9T1w90j6e3T90ANZdyN2zLWClRwFnBzWgzdrG4onx'
  93. logger.info(f'当前缓存:self.params_cache:{self.params_cache}')
  94. if user_id not in self.params_cache:
  95. self.params_cache[user_id] = {}
  96. logger.info(f'初始化缓存:{self.params_cache}')
  97. if context.type == ContextType.TEXT and user_id in self.params_cache:
  98. self.params_cache[user_id]['previous_prompt']=msg.content
  99. # print(f'{msg.__dict__}')
  100. if context.type == ContextType.IMAGE:
  101. logger.info('处理图片')
  102. file_path = context.content
  103. logger.info(f"on_handle_context: 获取到图片路径 {file_path},{user_id in self.params_cache}")
  104. if user_id in self.params_cache:
  105. if 'previous_prompt' not in self.params_cache[user_id]:
  106. reply = Reply()
  107. reply.type = ReplyType.TEXT
  108. # reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
  109. reply.content = f"您刚刚上传了一张图片,请问我有什么可以帮您的呢?"
  110. e_context["reply"] = reply
  111. e_context.action = EventAction.BREAK
  112. # else:
  113. print(f'准备抽取文字')
  114. file_content=extract_content_by_llm(file_path,api_key)
  115. if file_content is None:
  116. logger.info('不能抽取文字,使用图片oss请求LLM')
  117. access_key_id = 'LTAI5tRTG6pLhTpKACJYoPR5'
  118. access_key_secret = 'E7dMzeeMxq4VQvLg7Tq7uKf3XWpYfN'
  119. # OSS区域对应的Endpoint
  120. endpoint = 'http://oss-cn-shanghai.aliyuncs.com' # 根据你的区域选择
  121. # Bucket名称
  122. bucket_name = 'cow-agent'
  123. local_file_path=file_path
  124. oss_file_name=f'cow/{os.path.basename(file_path)}'
  125. logger.info(f'oss_file_name:{oss_file_name}\n local_file_path :{local_file_path}')
  126. file_content = upload_oss(access_key_id, access_key_secret, endpoint, bucket_name, local_file_path, oss_file_name)
  127. logger.info(f'写入图片缓存oss 地址{file_content}')
  128. self.params_cache[user_id]['last_content']=file_content
  129. # else:
  130. # logger.warn(f'还没有建立会话')
  131. logger.info('删除图片')
  132. os.remove(file_path)
  133. if context.type == ContextType.FILE:
  134. logger.info('处理图片')
  135. file_path = context.content
  136. logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
  137. if user_id in self.params_cache:
  138. if 'previous_prompt' not in self.params_cache[user_id]:
  139. reply = Reply()
  140. reply.type = ReplyType.TEXT
  141. # reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
  142. reply.content = f"您刚刚上传了一份文件,请问我有什么可以帮您的呢?"
  143. e_context["reply"] = reply
  144. e_context.action = EventAction.BREAK
  145. # else:
  146. print(f'准备抽取文字')
  147. file_content=extract_content_by_llm(file_path,api_key)
  148. if file_content is None:
  149. reply = Reply()
  150. reply.type = ReplyType.TEXT
  151. # reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
  152. reply.content = f"不能处理这份文件"
  153. e_context["reply"] = reply
  154. e_context.action = EventAction.BREAK
  155. return
  156. else:
  157. self.params_cache[user_id]['last_content']=file_content
  158. logger.info('删除图片')
  159. os.remove(file_path)
  160. # logger.info('previous_prompt' in self.params_cache[user_id])
  161. # logger.info('last_content' in self.params_cache[user_id])
  162. is_previous_prompt='previous_prompt' in self.params_cache[user_id]
  163. is_last_content='last_content' in self.params_cache[user_id]
  164. logger.info(f"存在提示词 previous_prompt:{is_previous_prompt}")
  165. logger.info(f'存在内容 last_content:{is_last_content}' )
  166. if 'previous_prompt' in self.params_cache[user_id] and 'last_content' in self.params_cache[user_id] :
  167. e_context["context"].type = ContextType.TEXT
  168. e_context["context"].content = self.params_cache[user_id]['last_content']+'\n\t'+self.params_cache[user_id]['previous_prompt']
  169. logger.info(f'conze4upload 插件处理上传文件或图片')
  170. e_context.action = EventAction.CONTINUE
  171. # 清空清空缓存
  172. self.params_cache.clear()
  173. logger.info(f'清空缓存后:{self.params_cache}')
  174. # e_context.action = EventAction.BREAK
  175. def remove_markdown(text):
  176. # 替换Markdown的粗体标记
  177. text = text.replace("**", "")
  178. # 替换Markdown的标题标记
  179. text = text.replace("### ", "").replace("## ", "").replace("# ", "")
  180. return text
  181. def extract_content_by_llm(file_path: str, api_key: str) -> str:
  182. logger.info(f'大模型开始抽取文字')
  183. try:
  184. headers = {
  185. 'Authorization': f'Bearer {api_key}'
  186. }
  187. data = {
  188. 'purpose': 'file-extract',
  189. }
  190. file_name=os.path.basename(file_path)
  191. files = {
  192. 'file': (file_name, open(Path(file_path), 'rb')),
  193. }
  194. # print(files)
  195. api_url='https://api.moonshot.cn/v1/files'
  196. response = requests.post(api_url, headers=headers, files=files, data=data)
  197. response_data = response.json()
  198. file_id = response_data.get('id')
  199. response=requests.get(url=f"https://api.moonshot.cn/v1/files/{file_id}/content", headers=headers)
  200. print(response.text)
  201. response_data = response.json()
  202. content = response_data.get('content')
  203. return content
  204. except requests.exceptions.RequestException as e:
  205. logger.error(f"Error calling LLM API: {e}")
  206. return None
  207. def upload_oss(access_key_id, access_key_secret, endpoint, bucket_name, local_file_path, oss_file_name, expiration_days=7):
  208. """
  209. 上传文件到阿里云OSS并设置生命周期规则,同时返回文件的公共访问地址。
  210. :param access_key_id: 阿里云AccessKey ID
  211. :param access_key_secret: 阿里云AccessKey Secret
  212. :param endpoint: OSS区域对应的Endpoint
  213. :param bucket_name: OSS中的Bucket名称
  214. :param local_file_path: 本地文件路径
  215. :param oss_file_name: OSS中的文件存储路径
  216. :param expiration_days: 文件保存天数,默认7天后删除
  217. :return: 文件的公共访问地址
  218. """
  219. # 创建Bucket实例
  220. auth = oss2.Auth(access_key_id, access_key_secret)
  221. bucket = oss2.Bucket(auth, endpoint, bucket_name)
  222. ### 1. 设置生命周期规则 ###
  223. rule_id = f'delete_after_{expiration_days}_days' # 规则ID
  224. prefix = oss_file_name.split('/')[0] + '/' # 设置规则应用的前缀为文件所在目录
  225. # 定义生命周期规则
  226. rule = oss2.models.LifecycleRule(rule_id, prefix, status=oss2.models.LifecycleRule.ENABLED,
  227. expiration=oss2.models.LifecycleExpiration(days=expiration_days))
  228. # 设置Bucket的生命周期
  229. lifecycle = oss2.models.BucketLifecycle([rule])
  230. bucket.put_bucket_lifecycle(lifecycle)
  231. print(f"已设置生命周期规则:文件将在{expiration_days}天后自动删除")
  232. ### 2. 上传文件到OSS ###
  233. bucket.put_object_from_file(oss_file_name, local_file_path)
  234. ### 3. 构建公共访问URL ###
  235. file_url = f"http://{bucket_name}.{endpoint.replace('http://', '')}/{oss_file_name}"
  236. print(f"文件上传成功,公共访问地址:{file_url}")
  237. return file_url