|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- 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
- from lib import itchat
- from lib.itchat.content import *
- import re
-
-
- # C:\Users\vsoni\source\repos\chatgpt-on-wechat\channel\wechat\wechat_channel.py
-
- @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"]
- # # logger.info(f'{e_context.__dict__}')
- # # logger.info('---------------------------------')
- # # logger.info(f'{ e_context["context"]}')
- # logger.info('---------------------------------')
- # logger.info(f'{e_context["context"]["msg"]}')
- # 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)
- # print(msg.actual_user_nickname)
- # itchat.send(f'@{msg.actual_user_nickname}立刻为你服务', toUserName=user_id)
-
- def on_handle_context(self, e_context: EventContext):
- context = e_context["context"]
- # logger.info(f'{e_context.__dict__}')
- # logger.info('---------------------------------')
- # logger.info(f'{ e_context["context"]}')
- # logger.info('---------------------------------')
- # logger.info(f'{e_context["context"]["msg"]}')
- 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)
-
- # itchat.send(f'@{msg.actual_user_nickname}立刻为你服务', toUserName=msg.actual_user_nickname)
-
- 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] and not e_context['context']['isgroup']:
-
- reply = Reply()
- reply.type = ReplyType.TEXT
- 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] and not e_context['context']['isgroup']:
- reply = Reply()
- reply.type = ReplyType.TEXT
- 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"不能处理这份文件"
- 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] and contains_keywords(self.params_cache[user_id]['previous_prompt']):
- #先回应
- logger.info('先回应')
- # reply2 = Reply()
- # reply2.type = ReplyType.TEXT
- # reply2.content = f"已经收到,立刻为你服务"
- # msg:ChatMessage = e_context['context']['msg']
- # e_context['reply'] = reply2
- # e_context.action = EventAction.BREAK # 事件结束
-
-
- # reply = Reply()
- # reply.type = ReplyType.TEXT
- # reply.content = f"已经收到,立刻为你服务"
- # e_context["reply"] = reply
- # e_context.action = EventAction.BREAK
- receiver=user_id
- print(receiver)
- # itchat_content= '' if e_context['context']['isgroup'] else '[小蕴]'+"已经收到,立刻为你服务"
- # if e_context['context']['isgroup']:
- # itchat_content =f'@{msg.actual_user_nickname}已经收到,立刻为你服务'
- # else:
- # itchat_content = '[小蕴]'+"已经收到,立刻为你服务"
-
- text=self.params_cache[user_id]['previous_prompt']
- logger.info(f'{text},{contains_keywords(text)}')
-
-
- itchat_content= f'@{msg.actual_user_nickname}' if e_context['context']['isgroup'] else '[小蕴]'
- itchat_content+="已经收到,立刻为您服务"
- flag=contains_keywords(text)
- if flag==True:
- print('发送'+itchat_content)
- itchat.send(itchat_content, toUserName=receiver)
-
- e_context.action = EventAction.BREAK
-
- if 'previous_prompt' in self.params_cache[user_id] and 'last_content' in self.params_cache[user_id]:
-
- if contains_keywords(self.params_cache[user_id]['previous_prompt']):
- e_context["context"].type = ContextType.TEXT
- e_context["context"].content ="<content>"+self.params_cache[user_id]['last_content']+"</content>"+'\n\t'+"<ask>"+self.params_cache[user_id]['previous_prompt']+"</ask>"
- logger.info(f'conze4upload 插件处理上传文件或图片')
- e_context.action = EventAction.CONTINUE
-
- # 清空清空缓存
- self.params_cache.clear()
- logger.info(f'清空缓存后:{self.params_cache}')
- else:
- if not e_context['context']['isgroup']:
- 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
-
-
- ## 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
-
- def contains_keywords_by_re(text):
- # 匹配<ask>标签中的内容
- # match = re.search(r'<ask>(.*?)</ask>', text)
- match = re.search(r'(.*?)', text)
- if match:
- content = match.group(1)
- # 检查关键词
- keywords = ['分析', '总结', '报告', '描述']
- for keyword in keywords:
- if keyword in content:
- return True
- return False
-
- def contains_keywords(text):
- keywords = ["分析", "总结", "报告", "描述"]
- return any(keyword in text for keyword in keywords)
|