|
- import requests
- import json
- import plugins
- from bridge.reply import Reply, ReplyType
- from bridge.context import ContextType
- from channel.chat_message import ChatMessage
- from plugins import *
- from common.log import logger
- from common.expired_dict import ExpiredDict
- import os
- import base64
- from pathlib import Path
- from PIL import Image
- import oss2
-
- @plugins.register(
- name="coze4upload",
- desire_priority=-1,
- desc="A plugin for upload",
- version="0.0.01",
- author="",
- )
-
- class coze4upload(Plugin):
- def __init__(self):
- super().__init__()
- try:
- curdir = os.path.dirname(__file__)
- config_path = os.path.join(curdir, "config.json")
- if os.path.exists(config_path):
- with open(config_path, "r", encoding="utf-8") as f:
- self.config = json.load(f)
- else:
- # 使用父类的方法来加载配置
- self.config = super().load_config()
-
- if not self.config:
- raise Exception("config.json not found")
- # 设置事件处理函数
- self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
- self.params_cache = ExpiredDict(300)
-
- # 从配置中提取所需的设置
- self.keys = self.config.get("keys", {})
- self.url_sum = self.config.get("url_sum", {})
- self.search_sum = self.config.get("search_sum", {})
- self.file_sum = self.config.get("file_sum", {})
- self.image_sum = self.config.get("image_sum", {})
- self.note = self.config.get("note", {})
-
- self.sum4all_key = self.keys.get("sum4all_key", "")
- self.search1api_key = self.keys.get("search1api_key", "")
- self.gemini_key = self.keys.get("gemini_key", "")
- self.bibigpt_key = self.keys.get("bibigpt_key", "")
- self.outputLanguage = self.keys.get("outputLanguage", "zh-CN")
- self.opensum_key = self.keys.get("opensum_key", "")
- self.open_ai_api_key = self.keys.get("open_ai_api_key", "")
- self.model = self.keys.get("model", "gpt-3.5-turbo")
- self.open_ai_api_base = self.keys.get("open_ai_api_base", "https://api.openai.com/v1")
- self.xunfei_app_id = self.keys.get("xunfei_app_id", "")
- self.xunfei_api_key = self.keys.get("xunfei_api_key", "")
- self.xunfei_api_secret = self.keys.get("xunfei_api_secret", "")
- self.perplexity_key = self.keys.get("perplexity_key", "")
- self.flomo_key = self.keys.get("flomo_key", "")
- # 之前提示
- self.previous_prompt=''
-
- self.file_sum_enabled = self.file_sum.get("enabled", False)
- self.file_sum_service = self.file_sum.get("service", "")
- self.max_file_size = self.file_sum.get("max_file_size", 15000)
- self.file_sum_group = self.file_sum.get("group", True)
- self.file_sum_qa_prefix = self.file_sum.get("qa_prefix", "问")
- self.file_sum_prompt = self.file_sum.get("prompt", "")
-
- self.image_sum_enabled = self.image_sum.get("enabled", False)
- self.image_sum_service = self.image_sum.get("service", "")
- self.image_sum_group = self.image_sum.get("group", True)
- self.image_sum_qa_prefix = self.image_sum.get("qa_prefix", "问")
- self.image_sum_prompt = self.image_sum.get("prompt", "")
-
- # 初始化成功日志
- logger.info("[file4upload] inited.")
- except Exception as e:
- # 初始化失败日志
- logger.warn(f"file4upload init failed: {e}")
-
-
- def on_handle_context(self, e_context: EventContext):
- context = e_context["context"]
- if context.type not in [ContextType.TEXT, ContextType.SHARING,ContextType.FILE,ContextType.IMAGE]:
- return
- msg: ChatMessage = e_context["context"]["msg"]
- user_id = msg.from_user_id
- content = context.content
- isgroup = e_context["context"].get("isgroup", False)
-
- if isgroup and not self.file_sum_group:
- # 群聊中忽略处理文件
- logger.info("群聊消息,文件处理功能已禁用")
- return
- logger.info("on_handle_context: 处理上下文开始")
- context.get("msg").prepare()
-
- api_key='sk-5z2L4zy9T1w90j6e3T90ANZdyN2zLWClRwFnBzWgzdrG4onx'
-
-
- logger.info(f'当前缓存:self.params_cache:{self.params_cache}')
- if user_id not in self.params_cache:
- self.params_cache[user_id] = {}
- logger.info(f'初始化缓存:{self.params_cache}')
-
- if context.type == ContextType.TEXT and user_id in self.params_cache:
- self.params_cache[user_id]['previous_prompt']=msg.content
-
-
- # print(f'{msg.__dict__}')
- if context.type == ContextType.IMAGE:
- logger.info('处理图片')
- file_path = context.content
- logger.info(f"on_handle_context: 获取到图片路径 {file_path},{user_id in self.params_cache}")
- if user_id in self.params_cache:
- if 'previous_prompt' not in self.params_cache[user_id]:
-
- reply = Reply()
- reply.type = ReplyType.TEXT
- # reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
- reply.content = f"您刚刚上传了一张图片,请问我有什么可以帮您的呢?"
- e_context["reply"] = reply
- e_context.action = EventAction.BREAK
-
- # else:
- print(f'准备抽取文字')
- file_content=extract_content_by_llm(file_path,api_key)
-
- if file_content is None:
- logger.info('不能抽取文字,使用图片oss请求LLM')
- access_key_id = 'LTAI5tRTG6pLhTpKACJYoPR5'
- access_key_secret = 'E7dMzeeMxq4VQvLg7Tq7uKf3XWpYfN'
- # OSS区域对应的Endpoint
- endpoint = 'http://oss-cn-shanghai.aliyuncs.com' # 根据你的区域选择
- # Bucket名称
- bucket_name = 'cow-agent'
- local_file_path=file_path
- oss_file_name=f'cow/{os.path.basename(file_path)}'
- logger.info(f'oss_file_name:{oss_file_name}\n local_file_path :{local_file_path}')
- file_content = upload_oss(access_key_id, access_key_secret, endpoint, bucket_name, local_file_path, oss_file_name)
- logger.info(f'写入图片缓存oss 地址{file_content}')
-
- self.params_cache[user_id]['last_content']=file_content
-
- # else:
- # logger.warn(f'还没有建立会话')
- logger.info('删除图片')
- os.remove(file_path)
-
- if context.type == ContextType.FILE:
- logger.info('处理图片')
- file_path = context.content
- logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
- if user_id in self.params_cache:
- if 'previous_prompt' not in self.params_cache[user_id]:
- reply = Reply()
- reply.type = ReplyType.TEXT
- # reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
- reply.content = f"您刚刚上传了一份文件,请问我有什么可以帮您的呢?"
- e_context["reply"] = reply
- e_context.action = EventAction.BREAK
- # else:
- print(f'准备抽取文字')
- file_content=extract_content_by_llm(file_path,api_key)
- if file_content is None:
- reply = Reply()
- reply.type = ReplyType.TEXT
- # reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
- reply.content = f"不能处理这份文件"
- e_context["reply"] = reply
- e_context.action = EventAction.BREAK
- return
- else:
- self.params_cache[user_id]['last_content']=file_content
-
- logger.info('删除图片')
- os.remove(file_path)
-
- # logger.info('previous_prompt' in self.params_cache[user_id])
- # logger.info('last_content' in self.params_cache[user_id])
- is_previous_prompt='previous_prompt' in self.params_cache[user_id]
- is_last_content='last_content' in self.params_cache[user_id]
- logger.info(f"存在提示词 previous_prompt:{is_previous_prompt}")
- logger.info(f'存在内容 last_content:{is_last_content}' )
- if 'previous_prompt' in self.params_cache[user_id] and 'last_content' in self.params_cache[user_id] :
- e_context["context"].type = ContextType.TEXT
- e_context["context"].content = self.params_cache[user_id]['last_content']+'\n\t'+self.params_cache[user_id]['previous_prompt']
- logger.info(f'conze4upload 插件处理上传文件或图片')
- e_context.action = EventAction.CONTINUE
-
- # 清空清空缓存
- self.params_cache.clear()
- logger.info(f'清空缓存后:{self.params_cache}')
-
- # e_context.action = EventAction.BREAK
-
-
- def remove_markdown(text):
- # 替换Markdown的粗体标记
- text = text.replace("**", "")
- # 替换Markdown的标题标记
- text = text.replace("### ", "").replace("## ", "").replace("# ", "")
- return text
-
- def extract_content_by_llm(file_path: str, api_key: str) -> str:
- logger.info(f'大模型开始抽取文字')
- try:
- headers = {
- 'Authorization': f'Bearer {api_key}'
- }
- data = {
- 'purpose': 'file-extract',
- }
- file_name=os.path.basename(file_path)
- files = {
- 'file': (file_name, open(Path(file_path), 'rb')),
- }
- # print(files)
- api_url='https://api.moonshot.cn/v1/files'
- response = requests.post(api_url, headers=headers, files=files, data=data)
- response_data = response.json()
- file_id = response_data.get('id')
- response=requests.get(url=f"https://api.moonshot.cn/v1/files/{file_id}/content", headers=headers)
- print(response.text)
- response_data = response.json()
- content = response_data.get('content')
- return content
- except requests.exceptions.RequestException as e:
- logger.error(f"Error calling LLM API: {e}")
- return None
-
- def upload_oss(access_key_id, access_key_secret, endpoint, bucket_name, local_file_path, oss_file_name, expiration_days=7):
- """
- 上传文件到阿里云OSS并设置生命周期规则,同时返回文件的公共访问地址。
-
- :param access_key_id: 阿里云AccessKey ID
- :param access_key_secret: 阿里云AccessKey Secret
- :param endpoint: OSS区域对应的Endpoint
- :param bucket_name: OSS中的Bucket名称
- :param local_file_path: 本地文件路径
- :param oss_file_name: OSS中的文件存储路径
- :param expiration_days: 文件保存天数,默认7天后删除
- :return: 文件的公共访问地址
- """
-
- # 创建Bucket实例
- auth = oss2.Auth(access_key_id, access_key_secret)
- bucket = oss2.Bucket(auth, endpoint, bucket_name)
-
- ### 1. 设置生命周期规则 ###
- rule_id = f'delete_after_{expiration_days}_days' # 规则ID
- prefix = oss_file_name.split('/')[0] + '/' # 设置规则应用的前缀为文件所在目录
-
- # 定义生命周期规则
- rule = oss2.models.LifecycleRule(rule_id, prefix, status=oss2.models.LifecycleRule.ENABLED,
- expiration=oss2.models.LifecycleExpiration(days=expiration_days))
-
- # 设置Bucket的生命周期
- lifecycle = oss2.models.BucketLifecycle([rule])
- bucket.put_bucket_lifecycle(lifecycle)
-
- print(f"已设置生命周期规则:文件将在{expiration_days}天后自动删除")
-
- ### 2. 上传文件到OSS ###
- bucket.put_object_from_file(oss_file_name, local_file_path)
-
- ### 3. 构建公共访问URL ###
- file_url = f"http://{bucket_name}.{endpoint.replace('http://', '')}/{oss_file_name}"
-
- print(f"文件上传成功,公共访问地址:{file_url}")
-
- return file_url
-
|