ソースを参照

微信群

develop
H Vs 2週間前
コミット
11d5c5dbcb
46個のファイルの変更5768行の追加40行の削除
  1. +6
    -0
      .gitignore
  2. +13
    -3
      bot/chatgpt/chat_gpt_bot.py
  3. +1
    -1
      bot/chatgpt/chat_gpt_session.py
  4. +15
    -1
      bot/session_manager.py
  5. +1
    -0
      common/expired_dict.py
  6. +34
    -1
      common/log.py
  7. +37
    -0
      config-dev.json
  8. +37
    -0
      config-origin.json
  9. +44
    -0
      config-production.json
  10. +41
    -0
      config-test.json
  11. +35
    -0
      config.json
  12. +23
    -2
      config.py
  13. +4
    -1
      docker/Dockerfile.latest
  14. +2
    -2
      docker/build.latest.sh
  15. +51
    -0
      docker/entrypoint-bk.sh
  16. +12
    -29
      docker/entrypoint.sh
  17. +3
    -0
      plugins/banwords/config.json
  18. +1
    -0
      plugins/coze4upload/__init__.py
  19. +54
    -0
      plugins/coze4upload/config.json
  20. +308
    -0
      plugins/coze4upload/coze4upload-dev.txt
  21. +372
    -0
      plugins/coze4upload/coze4upload.py
  22. +271
    -0
      plugins/coze4upload/coze4upload.txt
  23. +279
    -0
      plugins/coze4upload/coze4upload2.txt
  24. +336
    -0
      plugins/coze4upload/coze4upload3.txt
  25. +374
    -0
      plugins/coze4upload/coze4upload4.txt
  26. +9
    -0
      plugins/coze4upload/requirements.txt
  27. +1
    -0
      plugins/file4upload/__init__.py
  28. +54
    -0
      plugins/file4upload/config.json
  29. +632
    -0
      plugins/file4upload/file4upload.py
  30. +619
    -0
      plugins/file4upload/file4upload.txt
  31. +523
    -0
      plugins/file4upload/file4upload0.txt
  32. +8
    -0
      plugins/file4upload/requirements.txt
  33. +4
    -0
      plugins/godcmd/config.json
  34. +1
    -0
      plugins/healthai/__init__.py
  35. +8
    -0
      plugins/healthai/config.json
  36. +456
    -0
      plugins/healthai/healthai.py
  37. +434
    -0
      plugins/healthai/healthai.txt
  38. +9
    -0
      plugins/healthai/requirements.txt
  39. +3
    -0
      plugins/keyword/config.json
  40. +1
    -0
      plugins/kimi4upload/__init__.py
  41. +54
    -0
      plugins/kimi4upload/config.json
  42. +572
    -0
      plugins/kimi4upload/kimi4upload.py
  43. +9
    -0
      plugins/kimi4upload/requirements.txt
  44. +2
    -0
      requirements.txt
  45. +8
    -0
      voice/ali/ali_voice.py
  46. +7
    -0
      voice/ali/config.json

+ 6
- 0
.gitignore ファイルの表示

@@ -30,4 +30,10 @@ plugins/banwords/lib/__pycache__
!plugins/role
!plugins/keyword
!plugins/linkai
!plugins/healthai
!plugins/coze4upload
!plugins/file4upload
!plugins/kimi4upload
!plugins/sum4all
client_config.json
!config.json

+ 13
- 3
bot/chatgpt/chat_gpt_bot.py ファイルの表示

@@ -5,6 +5,7 @@ import time
import openai
import openai.error
import requests
import json

from bot.bot import Bot
from bot.chatgpt.chat_gpt_session import ChatGPTSession
@@ -15,6 +16,7 @@ from bridge.reply import Reply, ReplyType
from common.log import logger
from common.token_bucket import TokenBucket
from config import conf, load_config
from channel.chat_message import ChatMessage


# OpenAI对话模型API (可用)
@@ -46,9 +48,13 @@ class ChatGPTBot(Bot, OpenAIImage):
def reply(self, query, context=None):
# acquire reply content
if context.type == ContextType.TEXT:
logger.info("[CHATGPT] query={}".format(query))

# print(context.__dict__)
msg: ChatMessage = context.kwargs['msg']
# print(msg.from_user_nickname)
logger.info("[CHATGPT] {} query={}".format(msg.from_user_nickname,query))
session_id = context["session_id"]
# print(f'会话id:{session_id}')
reply = None
clear_memory_commands = conf().get("clear_memory_commands", ["#清除记忆"])
if query in clear_memory_commands:
@@ -121,7 +127,11 @@ class ChatGPTBot(Bot, OpenAIImage):
if args is None:
args = self.args
response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **args)
# logger.debug("[CHATGPT] response={}".format(response))
# print("{}".format(session.__dict__))
logger.info("[CHATGPT] 请求={}".format(session.messages))
# print(f'会话id:{session.session_id}')
# logger.info("[CHATGPT] 响应={}".format(response))
logger.info("[CHATGPT] 响应={}".format(json.dumps(response, separators=(',', ':'),ensure_ascii=False)))
# logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"]))
return {
"total_tokens": response["usage"]["total_tokens"],


+ 1
- 1
bot/chatgpt/chat_gpt_session.py ファイルの表示

@@ -66,7 +66,7 @@ def num_tokens_from_messages(messages, model):
return num_tokens_from_messages(messages, model="gpt-3.5-turbo")
elif model in ["gpt-4-0314", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", "gpt-35-turbo-16k", "gpt-4-turbo-preview",
"gpt-4-1106-preview", const.GPT4_TURBO_PREVIEW, const.GPT4_VISION_PREVIEW, const.GPT4_TURBO_01_25,
"gpt-4-1106-preview", '7374349217580056592',const.GPT4_TURBO_PREVIEW, const.GPT4_VISION_PREVIEW, const.GPT4_TURBO_01_25,
const.GPT_4o, const.GPT_4O_0806, const.GPT_4o_MINI, const.LINKAI_4o, const.LINKAI_4_TURBO]:
return num_tokens_from_messages(messages, model="gpt-4")
elif model.startswith("claude-3"):


+ 15
- 1
bot/session_manager.py ファイルの表示

@@ -1,6 +1,7 @@
from common.expired_dict import ExpiredDict
from common.log import logger
from config import conf
import json


class Session(object):
@@ -21,8 +22,21 @@ class Session(object):
self.system_prompt = system_prompt
self.reset()

# def add_query(self, query):
# user_item = {"role": "user", "content": query}
# self.messages.append(user_item)

def add_query(self, query):
user_item = {"role": "user", "content": query}
try:
# 判断是否为 JSON 字符串,如果是则转换为 Python 字典
json_data = json.loads(query)
if isinstance(json_data, dict) or isinstance(json_data, list): # 检查是否为字典格式
user_item = {"role": "user", "content": json_data}
else:
user_item = {"role": "user", "content": query}
except json.JSONDecodeError:
# 如果不是 JSON 字符串,直接保存为字符串
user_item = {"role": "user", "content": query}
self.messages.append(user_item)

def add_reply(self, reply):


+ 1
- 0
common/expired_dict.py ファイルの表示

@@ -16,6 +16,7 @@ class ExpiredDict(dict):

def __setitem__(self, key, value):
expiry_time = datetime.now() + timedelta(seconds=self.expires_in_seconds)
# print(f'{key} 缓存过期时间:{expiry_time}')
super().__setitem__(key, (value, expiry_time))

def get(self, key, default=None):


+ 34
- 1
common/log.py ファイルの表示

@@ -1,5 +1,29 @@
import logging
import sys
from datetime import datetime, timedelta
import os

LOG_DIR = "./logs" # 日志文件目录
LOG_RETENTION_DAYS = 7 # 日志保留天数


def _remove_old_logs(log_dir, retention_days):
"""删除超过保留天数的日志文件"""
if not os.path.exists(log_dir):
os.makedirs(log_dir)
now = datetime.now()
for filename in os.listdir(log_dir):
file_path = os.path.join(log_dir, filename)
if os.path.isfile(file_path) and filename.startswith("run_") and filename.endswith(".log"):
# 提取文件日期
try:
log_date_str = filename[4:14]
log_date = datetime.strptime(log_date_str, "%Y-%m-%d")
if now - log_date > timedelta(days=retention_days):
os.remove(file_path)
print(f"删除旧日志: {filename}")
except ValueError:
continue


def _reset_logger(log):
@@ -16,7 +40,13 @@ def _reset_logger(log):
datefmt="%Y-%m-%d %H:%M:%S",
)
)
file_handle = logging.FileHandler("run.log", encoding="utf-8")
# 获取当前日期并将其加入日志文件名
date_str = datetime.now().strftime("%Y-%m-%d")
log_file_path = os.path.join(LOG_DIR, f"run_{date_str}.log")
# 日志文件句柄
file_handle = logging.FileHandler(log_file_path, encoding="utf-8")
file_handle.setFormatter(
logging.Formatter(
"[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s",
@@ -26,6 +56,9 @@ def _reset_logger(log):
log.addHandler(file_handle)
log.addHandler(console_handle)

# 清理旧日志
_remove_old_logs(LOG_DIR, LOG_RETENTION_DAYS)


def _get_logger():
log = logging.getLogger("log")


+ 37
- 0
config-dev.json ファイルの表示

@@ -0,0 +1,37 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"open_ai_api_key": "sk-tPyolP9giSyBLhG6soygVf3rpFUFqdtqXobW1NkVnVps3QxGz2kWlhFuhZ7Zm4TQ",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
"proxy": "",
"hot_reload": false,
"debug": true,
"single_chat_reply_prefix": "[小蕴]",
"group_chat_prefix": [
"小蕴",
"@AI好蕴"
],
"group_name_white_list": [
"AI好蕴测试群",
"AI好蕴测试群2",
"AI好蕴测试群3",
"AI好蕴孕妈服务测试群"
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "您好,我是AI好蕴健康顾问,非常高兴能够陪伴您一起踏上这一段美妙而重要的旅程。怀孕是生命中的一个特殊阶段,不仅承载着新生命的期许,也意味着您和家庭将迎来新的挑战和变化。在这个过程中,了解怀孕的意义、注意事项和如何进行科学的健康管理,对您和宝宝的健康至关重要。\n怀孕期的每一天都是新生命成长的过程,每一次胎动都让人感受到生命的奇迹。对于准妈妈来说,这是一段与宝宝建立深厚联系的时期。与此同时,这段时间也会让您对生活、家庭和未来有更深层次的认识和规划。\n怀孕不仅是生理上的变化,更是心理和情感上的一次洗礼。一个健康乐观的妈妈才能诞生一个阳光天使宝宝!\n通过科学的健康管理和正确的生活方式,您可以为自己和宝宝创造一个健康、安全的环境。我们专门给您设立了独立的服务支撑保证体系,包括各个方面的专家将为您提供贴身呵护和陪伴,为您提供专业的指导和支持,愿您度过一个平安、健康且愉快的孕期。\n如果你有任何健康问题咨询,可@我或输入“小蕴”,呼唤我为你服务!\n 【如果你有任何健康问题咨询,可@我、或语音“小蕴”呼唤我为你服务!】 \n祝您怀孕顺利,宝宝健康成长!",
"trigger_by_self": true,
"speech_recognition": true,
"group_speech_recognition": false,
"voice_reply_voice": false,
"conversation_max_tokens": 2500,
"expires_in_seconds": 300,
"character_desc": "you are professional doctor",
"temperature": 0.9,
"subscribe_msg": "感谢您的关注!\n这里是AI智能助手,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
}

+ 37
- 0
config-origin.json ファイルの表示

@@ -0,0 +1,37 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"open_ai_api_key": "sk-bQSMCXqpB5sdTMksYUWSFusy1zOQeYPs5Qty0Scw5LoPuvSDa7UFQIOn97mGGsOW",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
"proxy": "",
"hot_reload": false,
"single_chat_reply_prefix": "[小蕴]",
"group_chat_prefix": [
"小蕴",
"@AI好蕴"
],
"group_name_white_list": [
"AI好蕴测试群",
"AI好蕴测试群2",
"AI好蕴孕妈服务测试群",
"AI好蕴智能体服务群",
"AI好蕴智能体"
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "您好,我是AI好蕴健康顾问,非常高兴能够陪伴您一起踏上这一段美妙而重要的旅程。怀孕是生命中的一个特殊阶段,不仅承载着新生命的期许,也意味着您和家庭将迎来新的挑战和变化。在这个过程中,了解怀孕的意义、注意事项和如何进行科学的健康管理,对您和宝宝的健康至关重要。\n怀孕期的每一天都是新生命成长的过程,每一次胎动都让人感受到生命的奇迹。对于准妈妈来说,这是一段与宝宝建立深厚联系的时期。与此同时,这段时间也会让您对生活、家庭和未来有更深层次的认识和规划。\n怀孕不仅是生理上的变化,更是心理和情感上的一次洗礼。一个健康乐观的妈妈才能诞生一个阳光天使宝宝!\n通过科学的健康管理和正确的生活方式,您可以为自己和宝宝创造一个健康、安全的环境。我们专门给您设立了独立的服务支撑保证体系,包括各个方面的专家将为您提供贴身呵护和陪伴,为您提供专业的指导和支持,愿您度过一个平安、健康且愉快的孕期。\n如果你有任何健康问题咨询,可@我或输入“小蕴”,呼唤我为你服务!\n 【如果你有任何健康问题咨询,可@我、或语音“小蕴”呼唤我为你服务!】 \n祝您怀孕顺利,宝宝健康成长!",
"trigger_by_self": true,
"speech_recognition": true,
"group_speech_recognition": false,
"voice_reply_voice": false,
"conversation_max_tokens": 2500,
"expires_in_seconds": 300,
"character_desc": "you are professional doctor",
"temperature": 0.9,
"subscribe_msg": "感谢您的关注!\n这里是AI智能助手,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
}

+ 44
- 0
config-production.json ファイルの表示

@@ -0,0 +1,44 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"open_ai_api_key": "sk-bQSMCXqpB5sdTMksYUWSFusy1zOQeYPs5Qty0Scw5LoPuvSDa7UFQIOn97mGGsOW",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
"proxy": "",
"hot_reload": false,
"single_chat_reply_prefix": "[小蕴]",
"group_chat_prefix": [
"小蕴",
"@AI好蕴",
"xiaoyun",
"xiaoyin",
"xiaoying",
"xiaoyue",
"xiaoyuan",
"xiaoxin"
],
"group_name_white_list": [
"AI好蕴测试群",
"AI好蕴孕妈服务测试群",
"AI好蕴智能体服务群",
"AI好蕴智能体",
"AI月子VIP服务群",
"星妈汇-好蕴人工智能月子"
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "您好,我是AI好蕴健康顾问,非常高兴能够陪伴您一起踏上这一段美妙而重要的旅程。怀孕是生命中的一个特殊阶段,不仅承载着新生命的期许,也意味着您和家庭将迎来新的挑战和变化。在这个过程中,了解怀孕的意义、注意事项和如何进行科学的健康管理,对您和宝宝的健康至关重要。\n怀孕期的每一天都是新生命成长的过程,每一次胎动都让人感受到生命的奇迹。对于准妈妈来说,这是一段与宝宝建立深厚联系的时期。与此同时,这段时间也会让您对生活、家庭和未来有更深层次的认识和规划。\n怀孕不仅是生理上的变化,更是心理和情感上的一次洗礼。一个健康乐观的妈妈才能诞生一个阳光天使宝宝!\n通过科学的健康管理和正确的生活方式,您可以为自己和宝宝创造一个健康、安全的环境。我们专门给您设立了独立的服务支撑保证体系,包括各个方面的专家将为您提供贴身呵护和陪伴,为您提供专业的指导和支持,愿您度过一个平安、健康且愉快的孕期。\n如果你有任何健康问题咨询,可@我或输入“小蕴”,呼唤我为你服务!\n 【如果你有任何健康问题咨询,可@我、或语音“小蕴”呼唤我为你服务!】 \n祝您怀孕顺利,宝宝健康成长!",
"trigger_by_self": true,
"speech_recognition": true,
"group_speech_recognition": false,
"voice_reply_voice": false,
"conversation_max_tokens": 2500,
"expires_in_seconds": 300,
"character_desc": "you are professional doctor",
"temperature": 0.9,
"subscribe_msg": "感谢您的关注!\n这里是AI智能助手,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
}

+ 41
- 0
config-test.json ファイルの表示

@@ -0,0 +1,41 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"open_ai_api_key": "sk-flDQg2UT0fYZZsKtmIXrRUhLySpSOgWedQof6Vw2iYB0la2iF44AD",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
"proxy": "",
"hot_reload": false,
"debug": false,
"single_chat_reply_prefix": "[小蕴]",
"group_chat_prefix": [
"小蕴",
"@AI好蕴",
"xiaoyun",
"xiaoyin",
"xiaoying",
"xiaoyue",
"xiaoyuan",
"xiaoxin"
],
"group_name_white_list": [
"AI好蕴测试群3"
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "您好,我是AI好蕴健康顾问,非常高兴能够陪伴您一起踏上这一段美妙而重要的旅程。怀孕是生命中的一个特殊阶段,不仅承载着新生命的期许,也意味着您和家庭将迎来新的挑战和变化。在这个过程中,了解怀孕的意义、注意事项和如何进行科学的健康管理,对您和宝宝的健康至关重要。\n怀孕期的每一天都是新生命成长的过程,每一次胎动都让人感受到生命的奇迹。对于准妈妈来说,这是一段与宝宝建立深厚联系的时期。与此同时,这段时间也会让您对生活、家庭和未来有更深层次的认识和规划。\n怀孕不仅是生理上的变化,更是心理和情感上的一次洗礼。一个健康乐观的妈妈才能诞生一个阳光天使宝宝!\n通过科学的健康管理和正确的生活方式,您可以为自己和宝宝创造一个健康、安全的环境。我们专门给您设立了独立的服务支撑保证体系,包括各个方面的专家将为您提供贴身呵护和陪伴,为您提供专业的指导和支持,愿您度过一个平安、健康且愉快的孕期。\n如果你有任何健康问题咨询,可@我或输入“小蕴”,呼唤我为你服务!\n 【如果你有任何健康问题咨询,可@我、或语音“小蕴”呼唤我为你服务!】 \n祝您怀孕顺利,宝宝健康成长!",
"trigger_by_self": true,
"voice_to_text":"ali",
"speech_recognition": true,
"group_speech_recognition": true,
"voice_reply_voice": false,
"conversation_max_tokens": 2500,
"expires_in_seconds": 300,
"character_desc": "you are professional doctor",
"temperature": 0.9,
"subscribe_msg": "感谢您的关注!\n这里是AI智能助手,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
}

+ 35
- 0
config.json ファイルの表示

@@ -0,0 +1,35 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"open_ai_api_key": "sk-tPyolP9giSyBLhG6soygVf3rpFUFqdtqXobW1NkVnVps3QxGz2kWlhFuhZ7Zm4TQ",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
"proxy": "",
"hot_reload": false,
"debug": false,
"single_chat_reply_prefix": "[小蕴]",
"group_chat_prefix": [
"小蕴",
"@AI好蕴"
],
"group_name_white_list": [
"AI好蕴测试群3"
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "您好,我是AI好蕴健康顾问,非常高兴能够陪伴您一起踏上这一段美妙而重要的旅程。怀孕是生命中的一个特殊阶段,不仅承载着新生命的期许,也意味着您和家庭将迎来新的挑战和变化。在这个过程中,了解怀孕的意义、注意事项和如何进行科学的健康管理,对您和宝宝的健康至关重要。\n怀孕期的每一天都是新生命成长的过程,每一次胎动都让人感受到生命的奇迹。对于准妈妈来说,这是一段与宝宝建立深厚联系的时期。与此同时,这段时间也会让您对生活、家庭和未来有更深层次的认识和规划。\n怀孕不仅是生理上的变化,更是心理和情感上的一次洗礼。一个健康乐观的妈妈才能诞生一个阳光天使宝宝!\n通过科学的健康管理和正确的生活方式,您可以为自己和宝宝创造一个健康、安全的环境。我们专门给您设立了独立的服务支撑保证体系,包括各个方面的专家将为您提供贴身呵护和陪伴,为您提供专业的指导和支持,愿您度过一个平安、健康且愉快的孕期。\n如果你有任何健康问题咨询,可@我或输入“小蕴”,呼唤我为你服务!\n 【如果你有任何健康问题咨询,可@我、或语音“小蕴”呼唤我为你服务!】 \n祝您怀孕顺利,宝宝健康成长!",
"trigger_by_self": true,
"voice_to_text":"ali",
"speech_recognition": true,
"group_speech_recognition": true,
"voice_reply_voice": false,
"conversation_max_tokens": 2500,
"expires_in_seconds": 300,
"character_desc": "you are professional doctor",
"temperature": 0.9,
"subscribe_msg": "感谢您的关注!\n这里是AI智能助手,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
}

+ 23
- 2
config.py ファイルの表示

@@ -265,9 +265,30 @@ def drag_sensitive(config):

def load_config():
global config
config_path = "./config.json"
# config_path = "./config.json"
# if not os.path.exists(config_path):
# logger.info("配置文件不存在,将使用config-template.json模板")
# config_path = "./config-template.json"

# 默认加载 config.json 或者 config-template.json
environment = os.environ.get('environment', 'default') # 默认是生产环境
logger.info(f"当前环境: {environment}")

if environment == "test":
config_path = "./config-test.json"
elif environment == "production":
config_path = "./config-production.json"
elif environment == "dev":
config_path = "./config-dev.json"
elif environment == "default":
config_path = "./config.json"
else:
logger.error("无效的环境配置,使用默认的 config-template.json")
config_path = "./config-template.json"

# 加载配置文件
if not os.path.exists(config_path):
logger.info("配置文件不存在,将使用config-template.json模板")
logger.info(f"配置文件 {config_path} 不存在,将使用 config-template.json 模板")
config_path = "./config-template.json"

config_str = read_file(config_path)


+ 4
- 1
docker/Dockerfile.latest ファイルの表示

@@ -18,7 +18,10 @@ RUN apt-get update \
&& /usr/local/bin/python -m pip install --no-cache --upgrade pip \
&& pip install --no-cache -r requirements.txt \
&& pip install --no-cache -r requirements-optional.txt \
&& pip install azure-cognitiveservices-speech
&& pip install azure-cognitiveservices-speech \
&& pip install --no-cache -r ./plugins/sum4all/requirements.txt \
&& pip install --no-cache -r ./plugins/file4upload/requirements.txt \
&& pip install --no-cache -r ./plugins/healthai/requirements.txt

WORKDIR ${BUILD_PREFIX}



+ 2
- 2
docker/build.latest.sh ファイルの表示

@@ -3,6 +3,6 @@
unset KUBECONFIG

cd .. && docker build -f docker/Dockerfile.latest \
-t zhayujie/chatgpt-on-wechat .
-t ssjl/chatgpt-on-wechat .

docker tag zhayujie/chatgpt-on-wechat zhayujie/chatgpt-on-wechat:$(date +%y%m%d)
docker tag ssjl/chatgpt-on-wechat ssjl/chatgpt-on-wechat:$(date +%y%m%d)

+ 51
- 0
docker/entrypoint-bk.sh ファイルの表示

@@ -0,0 +1,51 @@
#!/bin/bash
set -e

# build prefix
CHATGPT_ON_WECHAT_PREFIX=${CHATGPT_ON_WECHAT_PREFIX:-""}
# path to config.json
CHATGPT_ON_WECHAT_CONFIG_PATH=${CHATGPT_ON_WECHAT_CONFIG_PATH:-""}
# execution command line
CHATGPT_ON_WECHAT_EXEC=${CHATGPT_ON_WECHAT_EXEC:-""}

# use environment variables to pass parameters
# if you have not defined environment variables, set them below
# export OPEN_AI_API_KEY=${OPEN_AI_API_KEY:-'YOUR API KEY'}
# export OPEN_AI_PROXY=${OPEN_AI_PROXY:-""}
# export SINGLE_CHAT_PREFIX=${SINGLE_CHAT_PREFIX:-'["bot", "@bot"]'}
# export SINGLE_CHAT_REPLY_PREFIX=${SINGLE_CHAT_REPLY_PREFIX:-'"[bot] "'}
# export GROUP_CHAT_PREFIX=${GROUP_CHAT_PREFIX:-'["@bot"]'}
# export GROUP_NAME_WHITE_LIST=${GROUP_NAME_WHITE_LIST:-'["ChatGPT测试群", "ChatGPT测试群2"]'}
# export IMAGE_CREATE_PREFIX=${IMAGE_CREATE_PREFIX:-'["画", "看", "找"]'}
# export CONVERSATION_MAX_TOKENS=${CONVERSATION_MAX_TOKENS:-"1000"}
# export SPEECH_RECOGNITION=${SPEECH_RECOGNITION:-"False"}
# export CHARACTER_DESC=${CHARACTER_DESC:-"你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。"}
# export EXPIRES_IN_SECONDS=${EXPIRES_IN_SECONDS:-"3600"}

# CHATGPT_ON_WECHAT_PREFIX is empty, use /app
if [ "$CHATGPT_ON_WECHAT_PREFIX" == "" ] ; then
CHATGPT_ON_WECHAT_PREFIX=/app
fi

# CHATGPT_ON_WECHAT_CONFIG_PATH is empty, use '/app/config.json'
if [ "$CHATGPT_ON_WECHAT_CONFIG_PATH" == "" ] ; then
CHATGPT_ON_WECHAT_CONFIG_PATH=$CHATGPT_ON_WECHAT_PREFIX/config.json
fi

# CHATGPT_ON_WECHAT_EXEC is empty, use ‘python app.py’
if [ "$CHATGPT_ON_WECHAT_EXEC" == "" ] ; then
CHATGPT_ON_WECHAT_EXEC="python app.py"
fi

# modify content in config.json
# if [ "$OPEN_AI_API_KEY" == "YOUR API KEY" ] || [ "$OPEN_AI_API_KEY" == "" ]; then
# echo -e "\033[31m[Warning] You need to set OPEN_AI_API_KEY before running!\033[0m"
# fi


# go to prefix dir
cd $CHATGPT_ON_WECHAT_PREFIX
# excute
$CHATGPT_ON_WECHAT_EXEC



+ 12
- 29
docker/entrypoint.sh ファイルの表示

@@ -8,44 +8,27 @@ CHATGPT_ON_WECHAT_CONFIG_PATH=${CHATGPT_ON_WECHAT_CONFIG_PATH:-""}
# execution command line
CHATGPT_ON_WECHAT_EXEC=${CHATGPT_ON_WECHAT_EXEC:-""}

# use environment variables to pass parameters
# if you have not defined environment variables, set them below
# export OPEN_AI_API_KEY=${OPEN_AI_API_KEY:-'YOUR API KEY'}
# export OPEN_AI_PROXY=${OPEN_AI_PROXY:-""}
# export SINGLE_CHAT_PREFIX=${SINGLE_CHAT_PREFIX:-'["bot", "@bot"]'}
# export SINGLE_CHAT_REPLY_PREFIX=${SINGLE_CHAT_REPLY_PREFIX:-'"[bot] "'}
# export GROUP_CHAT_PREFIX=${GROUP_CHAT_PREFIX:-'["@bot"]'}
# export GROUP_NAME_WHITE_LIST=${GROUP_NAME_WHITE_LIST:-'["ChatGPT测试群", "ChatGPT测试群2"]'}
# export IMAGE_CREATE_PREFIX=${IMAGE_CREATE_PREFIX:-'["画", "看", "找"]'}
# export CONVERSATION_MAX_TOKENS=${CONVERSATION_MAX_TOKENS:-"1000"}
# export SPEECH_RECOGNITION=${SPEECH_RECOGNITION:-"False"}
# export CHARACTER_DESC=${CHARACTER_DESC:-"你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。"}
# export EXPIRES_IN_SECONDS=${EXPIRES_IN_SECONDS:-"3600"}
# Determine the environment and set the config file accordingly
if [ "$environment" == "test" ]; then
CHATGPT_ON_WECHAT_CONFIG_PATH=${CHATGPT_ON_WECHAT_CONFIG_PATH:-$CHATGPT_ON_WECHAT_PREFIX/config-test.json}
elif [ "$environment" == "production" ]; then
CHATGPT_ON_WECHAT_CONFIG_PATH=${CHATGPT_ON_WECHAT_CONFIG_PATH:-$CHATGPT_ON_WECHAT_PREFIX/config-production.json}
else
echo "Invalid environment specified. Please set environment to 'test' or 'prod'."
exit 1
fi

# CHATGPT_ON_WECHAT_PREFIX is empty, use /app
if [ "$CHATGPT_ON_WECHAT_PREFIX" == "" ] ; then
if [ "$CHATGPT_ON_WECHAT_PREFIX" == "" ]; then
CHATGPT_ON_WECHAT_PREFIX=/app
fi

# CHATGPT_ON_WECHAT_CONFIG_PATH is empty, use '/app/config.json'
if [ "$CHATGPT_ON_WECHAT_CONFIG_PATH" == "" ] ; then
CHATGPT_ON_WECHAT_CONFIG_PATH=$CHATGPT_ON_WECHAT_PREFIX/config.json
fi

# CHATGPT_ON_WECHAT_EXEC is empty, use ‘python app.py’
if [ "$CHATGPT_ON_WECHAT_EXEC" == "" ] ; then
if [ "$CHATGPT_ON_WECHAT_EXEC" == "" ]; then
CHATGPT_ON_WECHAT_EXEC="python app.py"
fi

# modify content in config.json
# if [ "$OPEN_AI_API_KEY" == "YOUR API KEY" ] || [ "$OPEN_AI_API_KEY" == "" ]; then
# echo -e "\033[31m[Warning] You need to set OPEN_AI_API_KEY before running!\033[0m"
# fi


# go to prefix dir
cd $CHATGPT_ON_WECHAT_PREFIX
# excute
# execute
$CHATGPT_ON_WECHAT_EXEC



+ 3
- 0
plugins/banwords/config.json ファイルの表示

@@ -0,0 +1,3 @@
{
"action": "ignore"
}

+ 1
- 0
plugins/coze4upload/__init__.py ファイルの表示

@@ -0,0 +1 @@
from .coze4upload import *

+ 54
- 0
plugins/coze4upload/config.json ファイルの表示

@@ -0,0 +1,54 @@
{
"url_sum": {
"enabled": true,
"service": "sum4all",
"group": true,
"qa_enabled":true,
"qa_prefix":"问",
"prompt": "你是一个新闻专家,我会给你发一些网页内容,请你用简单明了的语言做总结。格式如下:📌总结\n一句话讲清楚整篇文章的核心观点,控制在30字左右。\n\n💡要点\n用数字序号列出来3-5个文章的核心内容,尽量使用emoji让你的表达更生动"
},
"search_sum": {
"enabled": false,
"service": "sum4all",
"search_service": "duckduckgo",
"group": true,
"search_prefix":"搜",
"prompt": "你是一个信息检索专家,我会把我的问题和搜索结果发给你,请你根据问题,从搜索结果里找出能回答问题的相关内容,用简单明了的语言为我做回复,尽量使用emoji让你的表达更生动"
},
"file_sum": {
"enabled": true,
"service": "openai",
"max_file_size": "15000",
"group": true,
"qa_prefix":"问",
"prompt": "你是妇产科专业医生,同时,也是全科医生及健康管理顾问。以医生的口吻进行回复。格式如下:📌总结\n一句话讲清楚问题的核心观点,控制在30字左右。\n\n💡要点\n用数字序号列出来3-5个文章的核心内容,尽量使用emoji让你的表达更生动。"
},
"image_sum": {
"enabled": true,
"service": "openai",
"group": true,
"qa_prefix":"问",
"prompt": "先全局分析图片的主要内容,并按照逻辑分层次、段落,提炼出5个左右图片中的精华信息、关键要点,生动地向读者描述图片的主要内容。注意排版、换行、emoji、标签的合理搭配,清楚地展现图片讲了什么。"
},
"note": {
"enabled": false,
"service": "flomo",
"prefix":"记"
},
"keys": {
"sum4all_key": "",
"search1api_key": "",
"gemini_key": "",
"perplexity_key": "",
"open_ai_api_key": "sk-5dyg7PMUNeoSqHH807453eB06f434c34Ba6fB4764aC8358c",
"model": "moonshot-v1-32k",
"open_ai_api_base": "http://106.15.182.218:3001/v1",
"xunfei_app_id": "",
"xunfei_api_key": "",
"xunfei_api_secret": "",
"opensum_key": "",
"bibigpt_key": "",
"outputLanguage": "zh-CN",
"flomo_key":""
}
}

+ 308
- 0
plugins/coze4upload/coze4upload-dev.txt ファイルの表示

@@ -0,0 +1,308 @@
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 *


# 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"]
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]:
#先回应
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 not '' else '[小蕴]' +f"已经收到,立刻为你服务"
itchat.send(f"{itchat_content}", toUserName=receiver)

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

+ 372
- 0
plugins/coze4upload/coze4upload.py ファイルの表示

@@ -0,0 +1,372 @@
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)

+ 271
- 0
plugins/coze4upload/coze4upload.txt ファイルの表示

@@ -0,0 +1,271 @@
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] = {}

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}")
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])
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

+ 279
- 0
plugins/coze4upload/coze4upload2.txt ファイルの表示

@@ -0,0 +1,279 @@
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

+ 336
- 0
plugins/coze4upload/coze4upload3.txt ファイルの表示

@@ -0,0 +1,336 @@
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 *


# 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]:

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]:
#先回应
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 = '[小蕴]'+"已经收到,立刻为你服务"
itchat_content= f'@{msg.actual_user_nickname}' if e_context['context']['isgroup'] else '[小蕴]'
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] :
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

+ 374
- 0
plugins/coze4upload/coze4upload4.txt ファイルの表示

@@ -0,0 +1,374 @@
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]:

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] 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:
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)

+ 9
- 0
plugins/coze4upload/requirements.txt ファイルの表示

@@ -0,0 +1,9 @@
python-docx
markdown
PyMuPDF
openpyxl
beautifulsoup4
python-pptx
Pillow
oss2


+ 1
- 0
plugins/file4upload/__init__.py ファイルの表示

@@ -0,0 +1 @@
from .file4upload import *

+ 54
- 0
plugins/file4upload/config.json ファイルの表示

@@ -0,0 +1,54 @@
{
"url_sum": {
"enabled": true,
"service": "sum4all",
"group": true,
"qa_enabled":true,
"qa_prefix":"问",
"prompt": "你是一个新闻专家,我会给你发一些网页内容,请你用简单明了的语言做总结。格式如下:📌总结\n一句话讲清楚整篇文章的核心观点,控制在30字左右。\n\n💡要点\n用数字序号列出来3-5个文章的核心内容,尽量使用emoji让你的表达更生动"
},
"search_sum": {
"enabled": false,
"service": "sum4all",
"search_service": "duckduckgo",
"group": true,
"search_prefix":"搜",
"prompt": "你是一个信息检索专家,我会把我的问题和搜索结果发给你,请你根据问题,从搜索结果里找出能回答问题的相关内容,用简单明了的语言为我做回复,尽量使用emoji让你的表达更生动"
},
"file_sum": {
"enabled": true,
"service": "openai",
"max_file_size": "15000",
"group": true,
"qa_prefix":"问",
"prompt": "你是妇产科专业医生,同时,也是全科医生及健康管理顾问。以医生的口吻进行回复。格式如下:📌总结\n一句话讲清楚问题的核心观点,控制在30字左右。\n\n💡要点\n用数字序号列出来3-5个文章的核心内容,尽量使用emoji让你的表达更生动。"
},
"image_sum": {
"enabled": true,
"service": "openai",
"group": true,
"qa_prefix":"问",
"prompt": "先全局分析图片的主要内容,并按照逻辑分层次、段落,提炼出5个左右图片中的精华信息、关键要点,生动地向读者描述图片的主要内容。注意排版、换行、emoji、标签的合理搭配,清楚地展现图片讲了什么。"
},
"note": {
"enabled": false,
"service": "flomo",
"prefix":"记"
},
"keys": {
"sum4all_key": "",
"search1api_key": "",
"gemini_key": "",
"perplexity_key": "",
"open_ai_api_key": "sk-5dyg7PMUNeoSqHH807453eB06f434c34Ba6fB4764aC8358c",
"model": "moonshot-v1-32k",
"open_ai_api_base": "http://106.15.182.218:3001/v1",
"xunfei_app_id": "",
"xunfei_api_key": "",
"xunfei_api_secret": "",
"opensum_key": "",
"bibigpt_key": "",
"outputLanguage": "zh-CN",
"flomo_key":""
}
}

+ 632
- 0
plugins/file4upload/file4upload.py ファイルの表示

@@ -0,0 +1,632 @@
import requests
import json
import re
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
from docx import Document
import markdown
import fitz
from openpyxl import load_workbook
import csv
from bs4 import BeautifulSoup
from pptx import Presentation
from PIL import Image
import base64
import html
import oss2

EXTENSION_TO_TYPE = {
'pdf': 'pdf',
'doc': 'docx', 'docx': 'docx',
'md': 'md',
'txt': 'txt',
'xls': 'excel', 'xlsx': 'excel',
'csv': 'csv',
'html': 'html', 'htm': 'html',
'ppt': 'ppt', 'pptx': 'ppt'
}

@plugins.register(
name="file4upload",
desire_priority=-1,
desc="A plugin for upload",
version="0.0.01",
author="",
)
class file4upload(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)
logger.info(f"user_id:{user_id},content:{content},isgroup:{isgroup}")
logger.info(f'上下文参数缓存键字典:{self.params_cache.keys}')
logger.info(f'user_id in self.params_cache: {user_id in self.params_cache}')
# logger.info(f'last_file_content in self.params_cache[user_id] :{'last_file_content' in self.params_cache[user_id]}')
# 上次提示
if context.type == ContextType.TEXT:
self.previous_prompt=msg.content

if context.type == ContextType.FILE:
logger.info('开始首次处理文件')
# logger.info(f'{self.params_cache}')
# # content = "The World"
# logger.info(f'file4upload 插件作画')
# e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

# e_context["context"].type = ContextType.TEXT
# e_context["context"].content = "The World"
# logger.info(f'file4upload 插件处理')
# e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

if isgroup and not self.file_sum_group:
# 群聊中忽略处理文件
logger.info("群聊消息,文件处理功能已禁用")
return
logger.info("on_handle_context: 处理上下文开始")
context.get("msg").prepare()
file_path = context.content
logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
# 检查是否应该进行文件总结
if self.file_sum_enabled:
# 更新params_cache中的last_file_content
self.params_cache[user_id] = {}
file_content = self.extract_content(file_path)
if file_content is None:
logger.info("文件内容无法提取,跳过处理")
else:
file_content=f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{file_content}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:'
self.params_cache[user_id]['last_file_content'] = file_content
logger.info(f'写入文件内容缓存')
# logger.info('Updated last_file_content in params_cache for user.')
# # self.handle_file(file_content, e_context)

# e_context["context"].type = ContextType.TEXT
# e_context["context"].content = file_content+'\n\t请总结分析这份数据文件。文件内容是HRV分析报告,请分析这个用户的心梗、脑梗的风险等级(如无风险、低风险、中风险、高风险),并提供健康建议'
# logger.info(f'file4upload 插件处理上传文件')
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
else:
logger.info("文件总结功能已禁用,不对文件内容进行处理")
# 删除文件
os.remove(file_path)
logger.info(f"文件 {file_path} 已删除")

elif context.type == ContextType.IMAGE:
logger.info('开始首次处理图片')
if isgroup and not self.image_sum_group:
# 群聊中忽略处理图片
logger.info("群聊消息,图片处理功能已禁用")
return
logger.info("on_handle_context: 开始处理图片")
context.get("msg").prepare()
image_path = context.content
logger.info(f"on_handle_context: 获取到图片路径 {image_path}")
# 检查是否应该进行图片总结
if self.image_sum_enabled:
# # 将图片路径转换为Base64编码的字符串
# 将图片上图到oss
# 阿里云账号AccessKey ID和AccessKey Secret
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=image_path
oss_file_name=f'cow/{os.path.basename(image_path)}'
logger.info(f'oss_file_name:{oss_file_name}\n local_file_path :{local_file_path}')
file_url = upload_oss(access_key_id, access_key_secret, endpoint, bucket_name, local_file_path, oss_file_name)
# file_url='https://cow-agent.oss-cn-shanghai.aliyuncs.com/cow/keys.jpg'
# file_url='http://cow-agent.oss-cn-shanghai.aliyuncs.com/cow/240920-170334.png'
# file_url='http://cow-agent.oss-cn-shanghai.aliyuncs.com/cow/240920-100213.png '
self.params_cache[user_id] = {}
oss_image=file_url
# self.params_cache[user_id]['last_image_oss'] =f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{oss_image}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:'
# 不总结图片,只告知上图
self.params_cache[user_id]['last_image_oss'] =f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{oss_image}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\t- If no words after \'Query\', do not describe any about the url, just said you have uploaded the image and ask what to do next. \n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:'
self.params_cache[user_id]['last_image_oss'] =f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{oss_image}</context>\n\t\n\tWhen answer to user:\n\t- If no words after \'Query\', do not describe any about the url, just said you have uploaded the image and ask what to do next. \n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:'

# 小结图片
# self.params_cache[user_id]['last_image_oss'] =f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{oss_image}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\t- If no words after \'Query\', describe the image in 10 chinese words , said you have uploaded the image and ask what to do next. \n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:'
logger.info(f'写入图片缓存oss 地址{file_url}')

# self.params_cache[user_id]['last_image_oss'] =f'{oss_image},分析以上影像检查图片并提供建议'
# self.params_cache[user_id]['last_image_oss'] =f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{oss_image}</context>\n\t\n\tWhen answer to user:\n\t- you are a professinal doctor,describe in detail about the image.\n\t- If no words after \'Query\', describe the image in 10 chinese words , said you have uploaded the image and ask what to do next. \n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:分析以上影像检查图片并提供建议'
# self.params_cache[user_id]['last_image_oss'] =f'{oss_image} 分析以上影像检查图片并提供建议'
e_context.action = EventAction.CONTINUE

else:
logger.info("图片总结功能已禁用,不对图片内容进行处理")
# 删除文件
os.remove(image_path)
logger.info(f"本地文件 {image_path} 已删除")

if user_id in self.params_cache and ('last_file_content' in self.params_cache[user_id] or 'last_image_base64' in self.params_cache[user_id] or 'last_image_oss' in self.params_cache[user_id]):
# logger.info('上传过文件或图片')
if 'last_file_content' in self.params_cache[user_id]:
logger.info('上次文件内容开始')
e_context["context"].type = ContextType.TEXT
e_context["context"].content = self.params_cache[user_id]['last_file_content']+f'\n\t{self.previous_prompt}'
logger.info(f'file4upload 插件处理上传文件')
self.previous_prompt=''
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

elif 'last_image_base64' in self.params_cache[user_id]:
logger.info('上次last_image_base64图片开始')

elif 'last_image_oss' in self.params_cache[user_id]:
logger.info('上次last_image_oss图片开始')
logger.info(self.params_cache[user_id]['last_image_oss'])
e_context["context"].type = ContextType.TEXT
e_context["context"].content = self.params_cache[user_id]['last_image_oss']+f'\n\t{self.previous_prompt}'
logger.info(f'file4upload 插件处理上传图片')
self.previous_prompt=''
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑



def handle_file(self, content, e_context):
logger.info("handle_file: 向LLM发送内容总结请求")
# 根据sum_service的值选择API密钥和基础URL
if self.file_sum_service == "openai":
api_key = self.open_ai_api_key
api_base = self.open_ai_api_base
model = self.model
elif self.file_sum_service == "sum4all":
api_key = self.sum4all_key
api_base = "https://pro.sum4all.site/v1"
model = "sum4all"
elif self.file_sum_service == "gemini":
api_key = self.gemini_key
model = "gemini"
api_base = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
else:
logger.error(f"未知的sum_service配置: {self.file_sum_service}")
return
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.file_sum_prompt)
if model == "gemini":
headers = {
'Content-Type': 'application/json',
'x-goog-api-key': api_key
}
data = {
"contents": [
{"role": "user", "parts": [{"text": prompt}]},
{"role": "model", "parts": [{"text": "okay"}]},
{"role": "user", "parts": [{"text": content}]}
],
"generationConfig": {
"maxOutputTokens": 800
}
}
api_url = api_base
else:
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
# content=f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{content}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query'
if self.previous_prompt!='':
prompt=self.previous_prompt
logger.info(f"改变提示,使用上次提示")
data = {
"model": model,
"messages": [
{"role": "system", "content": prompt},
{"role": "user", "content": content}
]
}
api_url = f"{api_base}/chat/completions"
try:
logger.info(f'handle_file: 请求文件内容{json.dumps(data, ensure_ascii=False)}')
response = requests.post(api_url, headers=headers, data=json.dumps(data))
response.raise_for_status()
response_data = response.json()
# 解析 JSON 并获取 content
if model == "gemini":
if "candidates" in response_data and len(response_data["candidates"]) > 0:
first_candidate = response_data["candidates"][0]
if "content" in first_candidate:
if "parts" in first_candidate["content"] and len(first_candidate["content"]["parts"]) > 0:
response_content = first_candidate["content"]["parts"][0]["text"].strip() # 获取响应内容
logger.info(f"Gemini API response content: {response_content}") # 记录响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
else:
logger.error("Parts not found in the Gemini API response content")
reply_content = "Parts not found in the Gemini API response content"
else:
logger.error("Content not found in the Gemini API response candidate")
reply_content = "Content not found in the Gemini API response candidate"
else:
logger.error("No candidates available in the Gemini API response")
reply_content = "No candidates available in the Gemini API response"
else:
if "choices" in response_data and len(response_data["choices"]) > 0:
first_choice = response_data["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip() # 获取响应内容
logger.info(f"LLM API response content") # 记录响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
if msg.ctype == ContextType.FILE and self.previous_prompt =='':
reply_content="您刚刚上传了一个文件,请问我有什么可以帮您的呢?"
self.previous_prompt=''
# 已上传过,重置 previous_prompt
elif self.previous_prompt !='' and 'last_file_content' in self.params_cache[user_id]:
logger.info(f'last_file_content 已经回答最后的提示,清空previous_prompt')
self.previous_prompt =''
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"

except requests.exceptions.RequestException as e:
logger.error(f"Error calling LLM API: {e}")
reply_content = f"An error occurred while calling LLM API"

reply = Reply()
reply.type = ReplyType.TEXT
# reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
def handle_image(self, base64_image, e_context):
logger.info("handle_image: 解析图像处理API的响应")
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.image_sum_prompt)

if self.image_sum_service == "openai":
api_key = self.open_ai_api_key
api_base = f"{self.open_ai_api_base}/chat/completions"
model = "gpt-4o-mini"
elif self.image_sum_service == "xunfei":
api_key = self.xunfei_api_key
api_base = "https://spark.sum4all.site/v1/chat/completions"
model = "spark-chat-vision"
elif self.image_sum_service == "sum4all":
api_key = self.sum4all_key
api_base = "https://pro.sum4all.site/v1/chat/completions"
model = "sum4all-vision"
elif self.image_sum_service == "gemini":
api_key = self.gemini_key
api_base = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
payload = {
"contents": [
{
"parts": [
{"text": prompt},
{
"inline_data": {
"mime_type":"image/png",
"data": base64_image
}
}
]
}
]
}
headers = {
"Content-Type": "application/json",
"x-goog-api-key": api_key
}
logger.info(f"准备发送请求. Payload大小: {len(json.dumps(payload))} 字节")

else:
logger.error(f"未知的image_sum_service配置: {self.image_sum_service}")
return
if self.previous_prompt!='':
prompt=self.previous_prompt
logger.info(f"改变提示,使用上次提示")

if self.image_sum_service != "gemini":
payload = {
"model": model,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 3000
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}

try:
logger.info(f'handle_image: 请求图片内容{json.dumps(payload, ensure_ascii=False)}')
response = requests.post(api_base, headers=headers, json=payload)
logger.info(f"API请求已发送. 状态码: {response.status_code}")
response.raise_for_status()
logger.info("API响应状态码正常,开始解析JSON")
response_json = response.json()
logger.info("JSON解析完成")

if self.image_sum_service == "gemini":
reply_content = response_json.get('candidates', [{}])[0].get('content', {}).get('parts', [{}])[0].get('text', 'No text found in the response')
logger.info(f"成功解析Gemini响应. 回复内容长度: {len(reply_content)}")
else:
if "choices" in response_json and len(response_json["choices"]) > 0:
first_choice = response_json["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip()
logger.info("LLM API response content")
reply_content = response_content
if msg.ctype == ContextType.IMAGE and self.previous_prompt =='':
reply_content="您刚刚上传了一张图片,请问我有什么可以帮您的呢?"
self.previous_prompt=''
elif self.previous_prompt !='' and 'last_image_base64' in self.params_cache[user_id]:
self.previous_prompt =''
logger.info(f'last_image_base64 已经回答最后的提示,清空previous_prompt')
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"
except Exception as e:
logger.error(f"Error processing LLM API response: {e}")
reply_content = f"An error occurred while processing LLM API response"

reply = Reply()
reply.type = ReplyType.TEXT
# reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.image_sum_qa_prefix}+问题,可继续追问"
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS

def read_pdf(self, file_path):
logger.info(f"开始读取PDF文件:{file_path}")
doc = fitz.open(file_path)
content = ' '.join([page.get_text() for page in doc])
logger.info(f"PDF文件读取完成:{file_path}")

return content
def read_word(self, file_path):
doc = Document(file_path)
return ' '.join([p.text for p in doc.paragraphs])
def read_markdown(self, file_path):
with open(file_path, 'r', encoding='utf-8') as file:
md_content = file.read()
return markdown.markdown(md_content)
def read_excel(self, file_path):
workbook = load_workbook(file_path)
content = ''
for sheet in workbook:
for row in sheet.iter_rows():
content += ' '.join([str(cell.value) for cell in row])
content += '\n'
return content
def read_txt(self, file_path):
logger.debug(f"开始读取TXT文件: {file_path}")
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
logger.debug(f"TXT文件读取完成: {file_path}")
logger.debug("TXT文件内容的前50个字符:")
logger.debug(content[:50]) # 打印文件内容的前50个字符
return content
except Exception as e:
logger.error(f"读取TXT文件时出错: {file_path},错误信息: {str(e)}")
return ""
def read_csv(self, file_path):
content = ''
with open(file_path, 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
content += ' '.join(row) + '\n'
return content
def read_html(self, file_path):
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'html.parser')
return soup.get_text()
def read_ppt(self, file_path):
presentation = Presentation(file_path)
content = ''
for slide in presentation.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
content += shape.text + '\n'
return content
def extract_content(self, file_path):
logger.info(f"extract_content: 提取文件内容,文件路径: {file_path}")
file_size = os.path.getsize(file_path) // 1000 # 将文件大小转换为KB
if file_size > int(self.max_file_size):
logger.warning(f"文件大小超过限制({self.max_file_size}KB),不进行处理。文件大小: {file_size}KB")
return None
file_extension = os.path.splitext(file_path)[1][1:].lower()
logger.info(f"extract_content: 文件类型为 {file_extension}")

file_type = EXTENSION_TO_TYPE.get(file_extension)

if not file_type:
logger.error(f"不支持的文件扩展名: {file_extension}")
return None

read_func = {
'pdf': self.read_pdf,
'docx': self.read_word,
'md': self.read_markdown,
'txt': self.read_txt,
'excel': self.read_excel,
'csv': self.read_csv,
'html': self.read_html,
'ppt': self.read_ppt
}.get(file_type)

if not read_func:
logger.error(f"不支持的文件类型: {file_type}")
return None
logger.info("extract_content: 文件内容提取完成")
return read_func(file_path)
def encode_image_to_base64(self, image_path):
logger.info(f"开始处理图片: {image_path}")
try:
with Image.open(image_path) as img:
logger.info(f"成功打开图片. 原始大小: {img.size}")
if img.width > 1024:
new_size = (1024, int(img.height*1024/img.width))
img = img.resize(new_size)
img.save(image_path) # 保存调整大小后的图片
logger.info(f"调整图片大小至: {new_size}")

with open(image_path, "rb") as image_file:
img_byte_arr = image_file.read()
logger.info(f"读取图片完成. 大小: {len(img_byte_arr)} 字节")

encoded = base64.b64encode(img_byte_arr).decode('ascii')
logger.info(f"Base64编码完成. 编码后长度: {len(encoded)}")
return encoded
except Exception as e:
logger.error(f"图片编码过程中发生错误: {str(e)}", exc_info=True)
raise
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 remove_markdown(text):
# 替换Markdown的粗体标记
text = text.replace("**", "")
# 替换Markdown的标题标记
text = text.replace("### ", "").replace("## ", "").replace("# ", "")
return text

+ 619
- 0
plugins/file4upload/file4upload.txt ファイルの表示

@@ -0,0 +1,619 @@
import requests
import json
import re
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
from docx import Document
import markdown
import fitz
from openpyxl import load_workbook
import csv
from bs4 import BeautifulSoup
from pptx import Presentation
from PIL import Image
import base64
import html
import oss2

EXTENSION_TO_TYPE = {
'pdf': 'pdf',
'doc': 'docx', 'docx': 'docx',
'md': 'md',
'txt': 'txt',
'xls': 'excel', 'xlsx': 'excel',
'csv': 'csv',
'html': 'html', 'htm': 'html',
'ppt': 'ppt', 'pptx': 'ppt'
}

@plugins.register(
name="file4upload",
desire_priority=-1,
desc="A plugin for upload",
version="0.0.01",
author="",
)
class file4upload(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)
logger.info(f"user_id:{user_id},content:{content},isgroup:{isgroup}")
logger.info(f'上下文参数缓存键字典:{self.params_cache.keys}')
logger.info(f'user_id in self.params_cache: {user_id in self.params_cache}')
# logger.info(f'last_file_content in self.params_cache[user_id] :{'last_file_content' in self.params_cache[user_id]}')
# 上次提示
if context.type == ContextType.TEXT:
self.previous_prompt=msg.content

if context.type == ContextType.FILE:
logger.info('开始首次处理文件')
# logger.info(f'{self.params_cache}')
# # content = "The World"
# logger.info(f'file4upload 插件作画')
# e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

# e_context["context"].type = ContextType.TEXT
# e_context["context"].content = "The World"
# logger.info(f'file4upload 插件处理')
# e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

if isgroup and not self.file_sum_group:
# 群聊中忽略处理文件
logger.info("群聊消息,文件处理功能已禁用")
return
logger.info("on_handle_context: 处理上下文开始")
context.get("msg").prepare()
file_path = context.content
logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
# 检查是否应该进行文件总结
if self.file_sum_enabled:
# 更新params_cache中的last_file_content
self.params_cache[user_id] = {}
file_content = self.extract_content(file_path)
if file_content is None:
logger.info("文件内容无法提取,跳过处理")
else:
file_content=f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{file_content}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:'
self.params_cache[user_id]['last_file_content'] = file_content
logger.info(f'写入文件内容缓存')
# logger.info('Updated last_file_content in params_cache for user.')
# # self.handle_file(file_content, e_context)

# e_context["context"].type = ContextType.TEXT
# e_context["context"].content = file_content+'\n\t请总结分析这份数据文件。文件内容是HRV分析报告,请分析这个用户的心梗、脑梗的风险等级(如无风险、低风险、中风险、高风险),并提供健康建议'
# logger.info(f'file4upload 插件处理上传文件')
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
else:
logger.info("文件总结功能已禁用,不对文件内容进行处理")
# 删除文件
os.remove(file_path)
logger.info(f"文件 {file_path} 已删除")

elif context.type == ContextType.IMAGE:
logger.info('开始首次处理图片')
if isgroup and not self.image_sum_group:
# 群聊中忽略处理图片
logger.info("群聊消息,图片处理功能已禁用")
return
logger.info("on_handle_context: 开始处理图片")
context.get("msg").prepare()
image_path = context.content
logger.info(f"on_handle_context: 获取到图片路径 {image_path}")
# 检查是否应该进行图片总结
if self.image_sum_enabled:
# # 将图片路径转换为Base64编码的字符串
# base64_image = self.encode_image_to_base64(image_path)
# # 更新params_cache中的last_image_path
# self.params_cache[user_id] = {}
# self.params_cache[user_id]['last_image_base64'] = base64_image
# logger.info('Updated last_image_base64 in params_cache for user.')
# self.handle_image(base64_image, e_context)
# 将图片上图到oss
# 阿里云账号AccessKey ID和AccessKey Secret
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=image_path
oss_file_name=f'cow/{''}'
logger.info(f'oss_file_name:{oss_file_name}\n local_file_path :{local_file_path}')
#file_url = upload_oss(access_key_id, access_key_secret, endpoint, bucket_name, local_file_path, oss_file_name)

self.params_cache[user_id] = {}
oss_image='' #file_url
self.params_cache[user_id]['last_image_oss'] = oss_image
logger.info(f'写入图片缓存')
e_context.action = EventAction.CONTINUE

else:
logger.info("图片总结功能已禁用,不对图片内容进行处理")
# 删除文件
os.remove(image_path)
logger.info(f"本地文件 {image_path} 已删除")

if user_id in self.params_cache and ('last_file_content' in self.params_cache[user_id] or 'last_image_base64' in self.params_cache[user_id] or 'last_image_oss' in self.params_cache[user_id]):
# logger.info('上传过文件或图片')
if 'last_file_content' in self.params_cache[user_id]:
logger.info('上次文件内容开始')
e_context["context"].type = ContextType.TEXT
e_context["context"].content = self.params_cache[user_id]['last_file_content']+f'\n\t{self.previous_prompt}'
logger.info(f'file4upload 插件处理上传文件')
self.previous_prompt=''
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

elif 'last_image_base64' in self.params_cache[user_id]:
logger.info('上次last_image_base64图片开始')

elif 'last_image_oss' in self.params_cache[user_id]:
logger.info('上次last_image_oss图片开始')
logger.info(self.params_cache[user_id]['last_image_oss'])
# self.previous_prompt=''
# e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑



def handle_file(self, content, e_context):
logger.info("handle_file: 向LLM发送内容总结请求")
# 根据sum_service的值选择API密钥和基础URL
if self.file_sum_service == "openai":
api_key = self.open_ai_api_key
api_base = self.open_ai_api_base
model = self.model
elif self.file_sum_service == "sum4all":
api_key = self.sum4all_key
api_base = "https://pro.sum4all.site/v1"
model = "sum4all"
elif self.file_sum_service == "gemini":
api_key = self.gemini_key
model = "gemini"
api_base = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
else:
logger.error(f"未知的sum_service配置: {self.file_sum_service}")
return
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.file_sum_prompt)
if model == "gemini":
headers = {
'Content-Type': 'application/json',
'x-goog-api-key': api_key
}
data = {
"contents": [
{"role": "user", "parts": [{"text": prompt}]},
{"role": "model", "parts": [{"text": "okay"}]},
{"role": "user", "parts": [{"text": content}]}
],
"generationConfig": {
"maxOutputTokens": 800
}
}
api_url = api_base
else:
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
# content=f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{content}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query'
if self.previous_prompt!='':
prompt=self.previous_prompt
logger.info(f"改变提示,使用上次提示")
data = {
"model": model,
"messages": [
{"role": "system", "content": prompt},
{"role": "user", "content": content}
]
}
api_url = f"{api_base}/chat/completions"
try:
logger.info(f'handle_file: 请求文件内容{json.dumps(data, ensure_ascii=False)}')
response = requests.post(api_url, headers=headers, data=json.dumps(data))
response.raise_for_status()
response_data = response.json()
# 解析 JSON 并获取 content
if model == "gemini":
if "candidates" in response_data and len(response_data["candidates"]) > 0:
first_candidate = response_data["candidates"][0]
if "content" in first_candidate:
if "parts" in first_candidate["content"] and len(first_candidate["content"]["parts"]) > 0:
response_content = first_candidate["content"]["parts"][0]["text"].strip() # 获取响应内容
logger.info(f"Gemini API response content: {response_content}") # 记录响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
else:
logger.error("Parts not found in the Gemini API response content")
reply_content = "Parts not found in the Gemini API response content"
else:
logger.error("Content not found in the Gemini API response candidate")
reply_content = "Content not found in the Gemini API response candidate"
else:
logger.error("No candidates available in the Gemini API response")
reply_content = "No candidates available in the Gemini API response"
else:
if "choices" in response_data and len(response_data["choices"]) > 0:
first_choice = response_data["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip() # 获取响应内容
logger.info(f"LLM API response content") # 记录响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
if msg.ctype == ContextType.FILE and self.previous_prompt =='':
reply_content="您刚刚上传了一个文件,请问我有什么可以帮您的呢?"
self.previous_prompt=''
# 已上传过,重置 previous_prompt
elif self.previous_prompt !='' and 'last_file_content' in self.params_cache[user_id]:
logger.info(f'last_file_content 已经回答最后的提示,清空previous_prompt')
self.previous_prompt =''
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"

except requests.exceptions.RequestException as e:
logger.error(f"Error calling LLM API: {e}")
reply_content = f"An error occurred while calling LLM API"

reply = Reply()
reply.type = ReplyType.TEXT
# reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
def handle_image(self, base64_image, e_context):
logger.info("handle_image: 解析图像处理API的响应")
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.image_sum_prompt)

if self.image_sum_service == "openai":
api_key = self.open_ai_api_key
api_base = f"{self.open_ai_api_base}/chat/completions"
model = "gpt-4o-mini"
elif self.image_sum_service == "xunfei":
api_key = self.xunfei_api_key
api_base = "https://spark.sum4all.site/v1/chat/completions"
model = "spark-chat-vision"
elif self.image_sum_service == "sum4all":
api_key = self.sum4all_key
api_base = "https://pro.sum4all.site/v1/chat/completions"
model = "sum4all-vision"
elif self.image_sum_service == "gemini":
api_key = self.gemini_key
api_base = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
payload = {
"contents": [
{
"parts": [
{"text": prompt},
{
"inline_data": {
"mime_type":"image/png",
"data": base64_image
}
}
]
}
]
}
headers = {
"Content-Type": "application/json",
"x-goog-api-key": api_key
}
logger.info(f"准备发送请求. Payload大小: {len(json.dumps(payload))} 字节")

else:
logger.error(f"未知的image_sum_service配置: {self.image_sum_service}")
return
if self.previous_prompt!='':
prompt=self.previous_prompt
logger.info(f"改变提示,使用上次提示")

if self.image_sum_service != "gemini":
payload = {
"model": model,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 3000
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}

try:
logger.info(f'handle_image: 请求图片内容{json.dumps(payload, ensure_ascii=False)}')
response = requests.post(api_base, headers=headers, json=payload)
logger.info(f"API请求已发送. 状态码: {response.status_code}")
response.raise_for_status()
logger.info("API响应状态码正常,开始解析JSON")
response_json = response.json()
logger.info("JSON解析完成")

if self.image_sum_service == "gemini":
reply_content = response_json.get('candidates', [{}])[0].get('content', {}).get('parts', [{}])[0].get('text', 'No text found in the response')
logger.info(f"成功解析Gemini响应. 回复内容长度: {len(reply_content)}")
else:
if "choices" in response_json and len(response_json["choices"]) > 0:
first_choice = response_json["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip()
logger.info("LLM API response content")
reply_content = response_content
if msg.ctype == ContextType.IMAGE and self.previous_prompt =='':
reply_content="您刚刚上传了一张图片,请问我有什么可以帮您的呢?"
self.previous_prompt=''
elif self.previous_prompt !='' and 'last_image_base64' in self.params_cache[user_id]:
self.previous_prompt =''
logger.info(f'last_image_base64 已经回答最后的提示,清空previous_prompt')
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"
except Exception as e:
logger.error(f"Error processing LLM API response: {e}")
reply_content = f"An error occurred while processing LLM API response"

reply = Reply()
reply.type = ReplyType.TEXT
# reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.image_sum_qa_prefix}+问题,可继续追问"
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS

def read_pdf(self, file_path):
logger.info(f"开始读取PDF文件:{file_path}")
doc = fitz.open(file_path)
content = ' '.join([page.get_text() for page in doc])
logger.info(f"PDF文件读取完成:{file_path}")

return content
def read_word(self, file_path):
doc = Document(file_path)
return ' '.join([p.text for p in doc.paragraphs])
def read_markdown(self, file_path):
with open(file_path, 'r', encoding='utf-8') as file:
md_content = file.read()
return markdown.markdown(md_content)
def read_excel(self, file_path):
workbook = load_workbook(file_path)
content = ''
for sheet in workbook:
for row in sheet.iter_rows():
content += ' '.join([str(cell.value) for cell in row])
content += '\n'
return content
def read_txt(self, file_path):
logger.debug(f"开始读取TXT文件: {file_path}")
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
logger.debug(f"TXT文件读取完成: {file_path}")
logger.debug("TXT文件内容的前50个字符:")
logger.debug(content[:50]) # 打印文件内容的前50个字符
return content
except Exception as e:
logger.error(f"读取TXT文件时出错: {file_path},错误信息: {str(e)}")
return ""
def read_csv(self, file_path):
content = ''
with open(file_path, 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
content += ' '.join(row) + '\n'
return content
def read_html(self, file_path):
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'html.parser')
return soup.get_text()
def read_ppt(self, file_path):
presentation = Presentation(file_path)
content = ''
for slide in presentation.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
content += shape.text + '\n'
return content
def extract_content(self, file_path):
logger.info(f"extract_content: 提取文件内容,文件路径: {file_path}")
file_size = os.path.getsize(file_path) // 1000 # 将文件大小转换为KB
if file_size > int(self.max_file_size):
logger.warning(f"文件大小超过限制({self.max_file_size}KB),不进行处理。文件大小: {file_size}KB")
return None
file_extension = os.path.splitext(file_path)[1][1:].lower()
logger.info(f"extract_content: 文件类型为 {file_extension}")

file_type = EXTENSION_TO_TYPE.get(file_extension)

if not file_type:
logger.error(f"不支持的文件扩展名: {file_extension}")
return None

read_func = {
'pdf': self.read_pdf,
'docx': self.read_word,
'md': self.read_markdown,
'txt': self.read_txt,
'excel': self.read_excel,
'csv': self.read_csv,
'html': self.read_html,
'ppt': self.read_ppt
}.get(file_type)

if not read_func:
logger.error(f"不支持的文件类型: {file_type}")
return None
logger.info("extract_content: 文件内容提取完成")
return read_func(file_path)
def encode_image_to_base64(self, image_path):
logger.info(f"开始处理图片: {image_path}")
try:
with Image.open(image_path) as img:
logger.info(f"成功打开图片. 原始大小: {img.size}")
if img.width > 1024:
new_size = (1024, int(img.height*1024/img.width))
img = img.resize(new_size)
img.save(image_path) # 保存调整大小后的图片
logger.info(f"调整图片大小至: {new_size}")

with open(image_path, "rb") as image_file:
img_byte_arr = image_file.read()
logger.info(f"读取图片完成. 大小: {len(img_byte_arr)} 字节")

encoded = base64.b64encode(img_byte_arr).decode('ascii')
logger.info(f"Base64编码完成. 编码后长度: {len(encoded)}")
return encoded
except Exception as e:
logger.error(f"图片编码过程中发生错误: {str(e)}", exc_info=True)
raise
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 remove_markdown(text):
# 替换Markdown的粗体标记
text = text.replace("**", "")
# 替换Markdown的标题标记
text = text.replace("### ", "").replace("## ", "").replace("# ", "")
return text

+ 523
- 0
plugins/file4upload/file4upload0.txt ファイルの表示

@@ -0,0 +1,523 @@
import requests
import json
import re
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
from docx import Document
import markdown
import fitz
from openpyxl import load_workbook
import csv
from bs4 import BeautifulSoup
from pptx import Presentation
from PIL import Image
import base64
import html

EXTENSION_TO_TYPE = {
'pdf': 'pdf',
'doc': 'docx', 'docx': 'docx',
'md': 'md',
'txt': 'txt',
'xls': 'excel', 'xlsx': 'excel',
'csv': 'csv',
'html': 'html', 'htm': 'html',
'ppt': 'ppt', 'pptx': 'ppt'
}

@plugins.register(
name="file4upload",
desire_priority=-1,
desc="A plugin for upload",
version="0.0.01",
author="",
)
class file4upload(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)
logger.info(f"user_id:{user_id},content:{content},isgroup:{isgroup}")
logger.info(f'上下文参数缓存键字典:{self.params_cache.keys}')
logger.info(f'user_id in self.params_cache: {user_id in self.params_cache}')
# logger.info(f'last_file_content in self.params_cache[user_id] :{'last_file_content' in self.params_cache[user_id]}')
# 上次提示
if context.type == ContextType.TEXT:
self.previous_prompt=msg.content

if context.type == ContextType.FILE:
logger.info('开始首次处理文件')
# logger.info(f'{self.params_cache}')
# # content = "The World"
# logger.info(f'file4upload 插件作画')
# e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

# e_context["context"].type = ContextType.TEXT
# e_context["context"].content = "The World"
# logger.info(f'file4upload 插件处理')
# e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

if isgroup and not self.file_sum_group:
# 群聊中忽略处理文件
logger.info("群聊消息,文件处理功能已禁用")
return
logger.info("on_handle_context: 处理上下文开始")
context.get("msg").prepare()
file_path = context.content
logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
# 检查是否应该进行文件总结
if self.file_sum_enabled:
# 更新params_cache中的last_file_content
self.params_cache[user_id] = {}
file_content = self.extract_content(file_path)
if file_content is None:
logger.info("文件内容无法提取,跳过处理")
else:
file_content=f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{file_content}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query.\n\tQuery:'
self.params_cache[user_id]['last_file_content'] = file_content
logger.info(f'写入文件内容缓存')
# logger.info('Updated last_file_content in params_cache for user.')
# # self.handle_file(file_content, e_context)

# e_context["context"].type = ContextType.TEXT
# e_context["context"].content = file_content+'\n\t请总结分析这份数据文件。文件内容是HRV分析报告,请分析这个用户的心梗、脑梗的风险等级(如无风险、低风险、中风险、高风险),并提供健康建议'
# logger.info(f'file4upload 插件处理上传文件')
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
else:
logger.info("文件总结功能已禁用,不对文件内容进行处理")

elif context.type == ContextType.IMAGE:
logger.info('开始首次处理图片')

if user_id in self.params_cache and ('last_file_content' in self.params_cache[user_id] or 'last_image_base64' in self.params_cache[user_id] ):
# logger.info('上传过文件或图片')
if 'last_file_content' in self.params_cache[user_id]:
logger.info('上次文件内容开始')
e_context["context"].type = ContextType.TEXT
e_context["context"].content = self.params_cache[user_id]['last_file_content']+f'\n\t{self.previous_prompt}'
logger.info(f'file4upload 插件处理上传文件')
self.previous_prompt=''
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

elif 'last_image_base64' in self.params_cache[user_id]:
logger.info('上次图片开始')


def handle_file(self, content, e_context):
logger.info("handle_file: 向LLM发送内容总结请求")
# 根据sum_service的值选择API密钥和基础URL
if self.file_sum_service == "openai":
api_key = self.open_ai_api_key
api_base = self.open_ai_api_base
model = self.model
elif self.file_sum_service == "sum4all":
api_key = self.sum4all_key
api_base = "https://pro.sum4all.site/v1"
model = "sum4all"
elif self.file_sum_service == "gemini":
api_key = self.gemini_key
model = "gemini"
api_base = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
else:
logger.error(f"未知的sum_service配置: {self.file_sum_service}")
return
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.file_sum_prompt)
if model == "gemini":
headers = {
'Content-Type': 'application/json',
'x-goog-api-key': api_key
}
data = {
"contents": [
{"role": "user", "parts": [{"text": prompt}]},
{"role": "model", "parts": [{"text": "okay"}]},
{"role": "user", "parts": [{"text": content}]}
],
"generationConfig": {
"maxOutputTokens": 800
}
}
api_url = api_base
else:
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
# content=f'Use the following context as your learned knowledge, inside <context></context> XML tags.\n\t<context>{content}</context>\n\t\n\tWhen answer to user:\n\t- If you don\'t know, just say that you don\'t know.\n\t- If you don\'t know when you are not sure, ask for clarification.\n\tAvoid mentioning that you obtained the information from the context.\n\tAnd answer according to the language of the user\'s question.\n\t\t\t\n\tGiven the context information, answer the query'
if self.previous_prompt!='':
prompt=self.previous_prompt
logger.info(f"改变提示,使用上次提示")
data = {
"model": model,
"messages": [
{"role": "system", "content": prompt},
{"role": "user", "content": content}
]
}
api_url = f"{api_base}/chat/completions"
try:
logger.info(f'handle_file: 请求文件内容{json.dumps(data, ensure_ascii=False)}')
response = requests.post(api_url, headers=headers, data=json.dumps(data))
response.raise_for_status()
response_data = response.json()
# 解析 JSON 并获取 content
if model == "gemini":
if "candidates" in response_data and len(response_data["candidates"]) > 0:
first_candidate = response_data["candidates"][0]
if "content" in first_candidate:
if "parts" in first_candidate["content"] and len(first_candidate["content"]["parts"]) > 0:
response_content = first_candidate["content"]["parts"][0]["text"].strip() # 获取响应内容
logger.info(f"Gemini API response content: {response_content}") # 记录响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
else:
logger.error("Parts not found in the Gemini API response content")
reply_content = "Parts not found in the Gemini API response content"
else:
logger.error("Content not found in the Gemini API response candidate")
reply_content = "Content not found in the Gemini API response candidate"
else:
logger.error("No candidates available in the Gemini API response")
reply_content = "No candidates available in the Gemini API response"
else:
if "choices" in response_data and len(response_data["choices"]) > 0:
first_choice = response_data["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip() # 获取响应内容
logger.info(f"LLM API response content") # 记录响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
if msg.ctype == ContextType.FILE and self.previous_prompt =='':
reply_content="您刚刚上传了一个文件,请问我有什么可以帮您的呢?"
self.previous_prompt=''
# 已上传过,重置 previous_prompt
elif self.previous_prompt !='' and 'last_file_content' in self.params_cache[user_id]:
logger.info(f'last_file_content 已经回答最后的提示,清空previous_prompt')
self.previous_prompt =''
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"

except requests.exceptions.RequestException as e:
logger.error(f"Error calling LLM API: {e}")
reply_content = f"An error occurred while calling LLM API"

reply = Reply()
reply.type = ReplyType.TEXT
# reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.file_sum_qa_prefix}+问题,可继续追问"
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
def handle_image(self, base64_image, e_context):
logger.info("handle_image: 解析图像处理API的响应")
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.image_sum_prompt)

if self.image_sum_service == "openai":
api_key = self.open_ai_api_key
api_base = f"{self.open_ai_api_base}/chat/completions"
model = "gpt-4o-mini"
elif self.image_sum_service == "xunfei":
api_key = self.xunfei_api_key
api_base = "https://spark.sum4all.site/v1/chat/completions"
model = "spark-chat-vision"
elif self.image_sum_service == "sum4all":
api_key = self.sum4all_key
api_base = "https://pro.sum4all.site/v1/chat/completions"
model = "sum4all-vision"
elif self.image_sum_service == "gemini":
api_key = self.gemini_key
api_base = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
payload = {
"contents": [
{
"parts": [
{"text": prompt},
{
"inline_data": {
"mime_type":"image/png",
"data": base64_image
}
}
]
}
]
}
headers = {
"Content-Type": "application/json",
"x-goog-api-key": api_key
}
logger.info(f"准备发送请求. Payload大小: {len(json.dumps(payload))} 字节")

else:
logger.error(f"未知的image_sum_service配置: {self.image_sum_service}")
return
if self.previous_prompt!='':
prompt=self.previous_prompt
logger.info(f"改变提示,使用上次提示")

if self.image_sum_service != "gemini":
payload = {
"model": model,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 3000
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}

try:
logger.info(f'handle_image: 请求图片内容{json.dumps(payload, ensure_ascii=False)}')
response = requests.post(api_base, headers=headers, json=payload)
logger.info(f"API请求已发送. 状态码: {response.status_code}")
response.raise_for_status()
logger.info("API响应状态码正常,开始解析JSON")
response_json = response.json()
logger.info("JSON解析完成")

if self.image_sum_service == "gemini":
reply_content = response_json.get('candidates', [{}])[0].get('content', {}).get('parts', [{}])[0].get('text', 'No text found in the response')
logger.info(f"成功解析Gemini响应. 回复内容长度: {len(reply_content)}")
else:
if "choices" in response_json and len(response_json["choices"]) > 0:
first_choice = response_json["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip()
logger.info("LLM API response content")
reply_content = response_content
if msg.ctype == ContextType.IMAGE and self.previous_prompt =='':
reply_content="您刚刚上传了一张图片,请问我有什么可以帮您的呢?"
self.previous_prompt=''
elif self.previous_prompt !='' and 'last_image_base64' in self.params_cache[user_id]:
self.previous_prompt =''
logger.info(f'last_image_base64 已经回答最后的提示,清空previous_prompt')
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"
except Exception as e:
logger.error(f"Error processing LLM API response: {e}")
reply_content = f"An error occurred while processing LLM API response"

reply = Reply()
reply.type = ReplyType.TEXT
# reply.content = f"{remove_markdown(reply_content)}\n\n💬5min内输入{self.image_sum_qa_prefix}+问题,可继续追问"
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS

def read_pdf(self, file_path):
logger.info(f"开始读取PDF文件:{file_path}")
doc = fitz.open(file_path)
content = ' '.join([page.get_text() for page in doc])
logger.info(f"PDF文件读取完成:{file_path}")

return content
def read_word(self, file_path):
doc = Document(file_path)
return ' '.join([p.text for p in doc.paragraphs])
def read_markdown(self, file_path):
with open(file_path, 'r', encoding='utf-8') as file:
md_content = file.read()
return markdown.markdown(md_content)
def read_excel(self, file_path):
workbook = load_workbook(file_path)
content = ''
for sheet in workbook:
for row in sheet.iter_rows():
content += ' '.join([str(cell.value) for cell in row])
content += '\n'
return content
def read_txt(self, file_path):
logger.debug(f"开始读取TXT文件: {file_path}")
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
logger.debug(f"TXT文件读取完成: {file_path}")
logger.debug("TXT文件内容的前50个字符:")
logger.debug(content[:50]) # 打印文件内容的前50个字符
return content
except Exception as e:
logger.error(f"读取TXT文件时出错: {file_path},错误信息: {str(e)}")
return ""
def read_csv(self, file_path):
content = ''
with open(file_path, 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
content += ' '.join(row) + '\n'
return content
def read_html(self, file_path):
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'html.parser')
return soup.get_text()
def read_ppt(self, file_path):
presentation = Presentation(file_path)
content = ''
for slide in presentation.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
content += shape.text + '\n'
return content
def extract_content(self, file_path):
logger.info(f"extract_content: 提取文件内容,文件路径: {file_path}")
file_size = os.path.getsize(file_path) // 1000 # 将文件大小转换为KB
if file_size > int(self.max_file_size):
logger.warning(f"文件大小超过限制({self.max_file_size}KB),不进行处理。文件大小: {file_size}KB")
return None
file_extension = os.path.splitext(file_path)[1][1:].lower()
logger.info(f"extract_content: 文件类型为 {file_extension}")

file_type = EXTENSION_TO_TYPE.get(file_extension)

if not file_type:
logger.error(f"不支持的文件扩展名: {file_extension}")
return None

read_func = {
'pdf': self.read_pdf,
'docx': self.read_word,
'md': self.read_markdown,
'txt': self.read_txt,
'excel': self.read_excel,
'csv': self.read_csv,
'html': self.read_html,
'ppt': self.read_ppt
}.get(file_type)

if not read_func:
logger.error(f"不支持的文件类型: {file_type}")
return None
logger.info("extract_content: 文件内容提取完成")
return read_func(file_path)
def encode_image_to_base64(self, image_path):
logger.info(f"开始处理图片: {image_path}")
try:
with Image.open(image_path) as img:
logger.info(f"成功打开图片. 原始大小: {img.size}")
if img.width > 1024:
new_size = (1024, int(img.height*1024/img.width))
img = img.resize(new_size)
img.save(image_path) # 保存调整大小后的图片
logger.info(f"调整图片大小至: {new_size}")

with open(image_path, "rb") as image_file:
img_byte_arr = image_file.read()
logger.info(f"读取图片完成. 大小: {len(img_byte_arr)} 字节")

encoded = base64.b64encode(img_byte_arr).decode('ascii')
logger.info(f"Base64编码完成. 编码后长度: {len(encoded)}")
return encoded
except Exception as e:
logger.error(f"图片编码过程中发生错误: {str(e)}", exc_info=True)
raise
def remove_markdown(text):
# 替换Markdown的粗体标记
text = text.replace("**", "")
# 替换Markdown的标题标记
text = text.replace("### ", "").replace("## ", "").replace("# ", "")
return text

+ 8
- 0
plugins/file4upload/requirements.txt ファイルの表示

@@ -0,0 +1,8 @@
python-docx
markdown
PyMuPDF
openpyxl
beautifulsoup4
python-pptx
Pillow
oss2

+ 4
- 0
plugins/godcmd/config.json ファイルの表示

@@ -0,0 +1,4 @@
{
"password": "",
"admin_users": []
}

+ 1
- 0
plugins/healthai/__init__.py ファイルの表示

@@ -0,0 +1 @@
from .healthai import *

+ 8
- 0
plugins/healthai/config.json ファイルの表示

@@ -0,0 +1,8 @@
{
"oss": {
"access_key_id": "LTAI5tRTG6pLhTpKACJYoPR5",
"access_key_secret":"E7dMzeeMxq4VQvLg7Tq7uKf3XWpYfN",
"endpoint":"http://oss-cn-shanghai.aliyuncs.com",
"bucket_name":"cow-agent"
}
}

+ 456
- 0
plugins/healthai/healthai.py ファイルの表示

@@ -0,0 +1,456 @@
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
from bot.session_manager import Session
from bot.session_manager import SessionManager

from bot.chatgpt.chat_gpt_session import ChatGPTSession

@plugins.register(
name="healthai",
desire_priority=-1,
desc="A plugin for upload",
version="0.0.01",
author="",
)

class healthai(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.oss = self.config.get("oss", {})
self.oss_access_key_id=self.oss.get("access_key_id","LTAI5tRTG6pLhTpKACJYoPR5")
self.oss_access_key_secret=self.oss.get("access_key_secret","E7dMzeeMxq4VQvLg7Tq7uKf3XWpYfN")
self.oss_endpoint=self.oss.get("endpoint","http://oss-cn-shanghai.aliyuncs.com")
self.oss_bucket_name=self.oss.get("bucket_name","cow-agent")
# 之前提示
self.previous_prompt=''

self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "gpt-3.5-turbo")

# 初始化成功日志
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)

context.get("msg").prepare()

logger.info(f'当前缓存:self.params_cache:{self.params_cache}')
print(f'输入内容:{content}')
print(f'类型:{context.type}')
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']=content
logger.info(f'上次提示缓存:{self.params_cache}')

# if context.type == ContextType.TEXT and user_id in self.params_cache and contains_keywords(content):
# self.params_cache[user_id]['previous_prompt']=content
# logger.info(f'上次提示缓存:{self.params_cache}')
# session_id = context["session_id"]
# session = self.sessions.session_query(content, session_id)
# print(f'session 消息{session.messages}')
# if 'last_content' not in self.params_cache[user_id]:
# reply = Reply()
# reply.type = ReplyType.TEXT
# reply.content = f"请上传相关报告或图片"
# e_context["reply"] = reply
# e_context.action = EventAction.BREAK_PASS
session_id = context["session_id"]
print(f'会话id:{session_id}')

# friends=itchat.get_friends(update=True)[1:]

# # logger.info(f'好友列表{friends}')
# # 提取所有好友的 NickName
# nicknames = [friend['NickName'] for friend in friends]
# print(nicknames)
# 打印所有 NickName
# for nickname in nicknames:
# print(nickname)

session = self.sessions.build_session(session_id)
print(f'session 消息{session.messages}')
# if context.type == ContextType.TEXT and user_id in self.params_cache and contains_keywords(content):
# self.params_cache[user_id]['previous_prompt']=content
# logger.info(f'上次提示缓存:{self.params_cache}')
# session_id = context["session_id"]
# session = self.sessions.session_query(content, session_id)
# print(f'session 消息{session.messages}')
# if 'last_content' not in self.params_cache[user_id]:
# reply = Reply()
# reply.type = ReplyType.TEXT
# reply.content = f"请上传相关报告或图片"
# e_context["reply"] = reply
# e_context.action = EventAction.BREAK_PASS


if context.type in [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

file_content = upload_oss(self.oss_access_key_id, self.oss_access_key_secret, self.oss_endpoint, self.oss_bucket_name, file_path, f'cow/{os.path.basename(file_path)}')
# 确保 'last_content' 键存在,并且是一个列表
if 'last_content' not in self.params_cache[user_id]:
self.params_cache[user_id]['last_content'] = []

# 添加文件内容到 'urls' 列表
self.params_cache[user_id]['last_content'].append(file_content)

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,"sk-5z2L4zy9T1w90j6e3T90ANZdyN2zLWClRwFnBzWgzdrG4onx")
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)

# 先回应
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('先回应')
receiver=user_id
print(receiver)
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
last_content=self.params_cache[user_id]['last_content']
prompt=self.params_cache[user_id]['previous_prompt']

# if isinstance(last_content, list):
# e_context["context"].content =self.generate_openai_messages_content(last_content,prompt)
# elif isinstance(last_content, str):
# e_context["context"].content ="<content>"+last_content+"</content>"+'\n\t'+"<ask>"+prompt+"</ask>"
# else:
# return "urls is neither a list nor a string"
e_context["context"].content =self.generate_openai_messages_content(last_content,prompt)
logger.info(f'插件处理上传文件或图片')
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



def on_handle_context2(self, e_context: EventContext):
context = e_context["context"]

# 检查 context 类型
if context.type not in {ContextType.TEXT, ContextType.SHARING, ContextType.FILE, ContextType.IMAGE}:
return

msg: ChatMessage = context["msg"]
user_id = msg.from_user_id
content = context.content
is_group = context.get("isgroup", False)

# 准备消息
context.get("msg").prepare()

logger.info(f'当前缓存:self.params_cache:{self.params_cache}')

# 初始化用户缓存
user_cache = self.params_cache.setdefault(user_id, {})
if not user_cache:
logger.info(f'初始化缓存:{self.params_cache}')

previous_prompt = user_cache.get('previous_prompt')
last_content = user_cache.get('last_content')

# 更新 previous_prompt
if context.type == ContextType.TEXT and previous_prompt and contains_keywords(previous_prompt):
user_cache['previous_prompt'] = msg.content

# 处理 previous_prompt 和 last_content
if previous_prompt and last_content and contains_keywords(previous_prompt):
logger.info('先回应')
receiver = user_id
itchat_content = f'@{msg.actual_user_nickname}' if is_group else '[小蕴]'
itchat_content += "已经收到,立刻为您服务"

if contains_keywords(previous_prompt):
logger.info(f'发送消息: {itchat_content}')
itchat.send(itchat_content, toUserName=receiver)
e_context.action = EventAction.BREAK

# 清空缓存
self.params_cache.clear()
logger.info(f'清空缓存后:{self.params_cache}')
else:
if not is_group:
reply = Reply()
reply.type = ReplyType.TEXT
reply.content = "您刚刚上传了,请问我有什么可以帮您的呢?"
e_context["reply"] = reply
e_context.action = EventAction.BREAK

if context.type in [ContextType.FILE,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
if context.type==ContextType.FILE:
reply.content = f"您刚刚上传文件,请问我有什么可以帮您的呢?"
else:
reply.content = f"您刚刚上传图片,请问我有什么可以帮您的呢?"
e_context["reply"] = reply
e_context.action = EventAction.BREAK

file_content = upload_oss(self.oss_access_key_id, self.oss_access_key_secret, self.oss_endpoint, self.oss_bucket_name, file_path, f'cow/{os.path.basename(file_path)}')
# 确保 'urls' 键存在,并且是一个列表
if 'urls' not in self.params_cache[user_id]:
self.params_cache[user_id]['urls'] = []

# 添加文件内容到 'urls' 列表
self.params_cache[user_id]['urls'].append(file_content)

logger.info('删除图片')
os.remove(file_path)

def generate_openai_messages_content(self, last_content,prompt):
content = []

if isinstance(last_content, list):
# 遍历每个 URL,生成对应的消息结构
for url in last_content:
if url.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
# 对于图片,生成 "image_url" 类型的消息
content.append({
"type": "image_url",
"image_url": {
"url": url
}
})
else:
# 对于其他文件,生成 "file_url" 或类似的处理方式
content.append({
"type": "file_url",
"file_url": {
"url": url
}
})
else:
prompt="<content>"+last_content+"</content>"+'\n\t'+"<ask>"+prompt+"</ask>"


# 遍历每个 URL,生成对应的消息结构
# for url in urls:
# if url.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
# # 对于图片,生成 "image_url" 类型的消息
# content.append({
# "type": "image_url",
# "image_url": {
# "url": url
# }
# })
# else:
# # 对于其他文件,生成 "file_url" 或类似的处理方式
# content.append({
# "type": "file_url",
# "file_url": {
# "url": url
# }
# })

# 添加额外的文本说明
content.append({
"type": "text",
"text": prompt
})

return json.dumps(content, ensure_ascii=False)

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)

+ 434
- 0
plugins/healthai/healthai.txt ファイルの表示

@@ -0,0 +1,434 @@
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

@plugins.register(
name="healthai",
desire_priority=-1,
desc="A plugin for upload",
version="0.0.01",
author="",
)

class healthai(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.oss = self.config.get("oss", {})
self.oss_access_key_id=self.oss.get("access_key_id","LTAI5tRTG6pLhTpKACJYoPR5")
self.oss_access_key_secret=self.oss.get("access_key_secret","E7dMzeeMxq4VQvLg7Tq7uKf3XWpYfN")
self.oss_endpoint=self.oss.get("endpoint","http://oss-cn-shanghai.aliyuncs.com")
self.oss_bucket_name=self.oss.get("bucket_name","cow-agent")
# 之前提示
self.previous_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)

context.get("msg").prepare()

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 and contains_keywords(content):
self.params_cache[user_id]['previous_prompt']=msg.content
logger.info(f'上次提示缓存:{self.params_cache}')


# if context.type in [ContextType.FILE,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
# if context.type==ContextType.FILE:
# reply.content = f"您刚刚上传文件,请问我有什么可以帮您的呢?"
# else:
# reply.content = f"您刚刚上传图片,请问我有什么可以帮您的呢?"
# e_context["reply"] = reply
# e_context.action = EventAction.BREAK

# file_content = upload_oss(self.oss_access_key_id, self.oss_access_key_secret, self.oss_endpoint, self.oss_bucket_name, file_path, f'cow/{os.path.basename(file_path)}')
# # 确保 'urls' 键存在,并且是一个列表
# if 'urls' not in self.params_cache[user_id]:
# self.params_cache[user_id]['urls'] = []

# # 添加文件内容到 'urls' 列表
# self.params_cache[user_id]['urls'].append(file_content)

# logger.info('删除图片')
# os.remove(file_path)

if context.type in [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

file_content = upload_oss(self.oss_access_key_id, self.oss_access_key_secret, self.oss_endpoint, self.oss_bucket_name, file_path, f'cow/{os.path.basename(file_path)}')
# 确保 'last_content' 键存在,并且是一个列表
if 'last_content' not in self.params_cache[user_id]:
self.params_cache[user_id]['last_content'] = []

# 添加文件内容到 'urls' 列表
self.params_cache[user_id]['last_content'].append(file_content)

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,"sk-5z2L4zy9T1w90j6e3T90ANZdyN2zLWClRwFnBzWgzdrG4onx")
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)

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('先回应')
receiver=user_id
print(receiver)
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
last_content=self.params_cache[user_id]['last_content']
prompt=self.params_cache[user_id]['previous_prompt']

# if isinstance(last_content, list):
# e_context["context"].content =self.generate_openai_messages_content(last_content,prompt)
# elif isinstance(last_content, str):
# e_context["context"].content ="<content>"+last_content+"</content>"+'\n\t'+"<ask>"+prompt+"</ask>"
# else:
# return "urls is neither a list nor a string"
e_context["context"].content =self.generate_openai_messages_content(last_content,prompt)
logger.info(f'插件处理上传文件或图片')
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

def on_handle_context2(self, e_context: EventContext):
context = e_context["context"]

# 检查 context 类型
if context.type not in {ContextType.TEXT, ContextType.SHARING, ContextType.FILE, ContextType.IMAGE}:
return

msg: ChatMessage = context["msg"]
user_id = msg.from_user_id
content = context.content
is_group = context.get("isgroup", False)

# 准备消息
context.get("msg").prepare()

logger.info(f'当前缓存:self.params_cache:{self.params_cache}')

# 初始化用户缓存
user_cache = self.params_cache.setdefault(user_id, {})
if not user_cache:
logger.info(f'初始化缓存:{self.params_cache}')

previous_prompt = user_cache.get('previous_prompt')
last_content = user_cache.get('last_content')

# 更新 previous_prompt
if context.type == ContextType.TEXT and previous_prompt and contains_keywords(previous_prompt):
user_cache['previous_prompt'] = msg.content

# 处理 previous_prompt 和 last_content
if previous_prompt and last_content and contains_keywords(previous_prompt):
logger.info('先回应')
receiver = user_id
itchat_content = f'@{msg.actual_user_nickname}' if is_group else '[小蕴]'
itchat_content += "已经收到,立刻为您服务"

if contains_keywords(previous_prompt):
logger.info(f'发送消息: {itchat_content}')
itchat.send(itchat_content, toUserName=receiver)
e_context.action = EventAction.BREAK

# 清空缓存
self.params_cache.clear()
logger.info(f'清空缓存后:{self.params_cache}')
else:
if not is_group:
reply = Reply()
reply.type = ReplyType.TEXT
reply.content = "您刚刚上传了,请问我有什么可以帮您的呢?"
e_context["reply"] = reply
e_context.action = EventAction.BREAK

if context.type in [ContextType.FILE,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
if context.type==ContextType.FILE:
reply.content = f"您刚刚上传文件,请问我有什么可以帮您的呢?"
else:
reply.content = f"您刚刚上传图片,请问我有什么可以帮您的呢?"
e_context["reply"] = reply
e_context.action = EventAction.BREAK

file_content = upload_oss(self.oss_access_key_id, self.oss_access_key_secret, self.oss_endpoint, self.oss_bucket_name, file_path, f'cow/{os.path.basename(file_path)}')
# 确保 'urls' 键存在,并且是一个列表
if 'urls' not in self.params_cache[user_id]:
self.params_cache[user_id]['urls'] = []

# 添加文件内容到 'urls' 列表
self.params_cache[user_id]['urls'].append(file_content)

logger.info('删除图片')
os.remove(file_path)

def generate_openai_messages_content(self, last_content,prompt):
content = []

if isinstance(last_content, list):
# 遍历每个 URL,生成对应的消息结构
for url in last_content:
if url.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
# 对于图片,生成 "image_url" 类型的消息
content.append({
"type": "image_url",
"image_url": {
"url": url
}
})
else:
# 对于其他文件,生成 "file_url" 或类似的处理方式
content.append({
"type": "file_url",
"file_url": {
"url": url
}
})
else:
prompt="<content>"+last_content+"</content>"+'\n\t'+"<ask>"+prompt+"</ask>"


# 遍历每个 URL,生成对应的消息结构
# for url in urls:
# if url.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
# # 对于图片,生成 "image_url" 类型的消息
# content.append({
# "type": "image_url",
# "image_url": {
# "url": url
# }
# })
# else:
# # 对于其他文件,生成 "file_url" 或类似的处理方式
# content.append({
# "type": "file_url",
# "file_url": {
# "url": url
# }
# })

# 添加额外的文本说明
content.append({
"type": "text",
"text": prompt
})

return json.dumps(content, ensure_ascii=False)

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)

+ 9
- 0
plugins/healthai/requirements.txt ファイルの表示

@@ -0,0 +1,9 @@
python-docx
markdown
PyMuPDF
openpyxl
beautifulsoup4
python-pptx
Pillow
oss2
pypinyin

+ 3
- 0
plugins/keyword/config.json ファイルの表示

@@ -0,0 +1,3 @@
{
"keyword": {}
}

+ 1
- 0
plugins/kimi4upload/__init__.py ファイルの表示

@@ -0,0 +1 @@
from .kimi4upload import *

+ 54
- 0
plugins/kimi4upload/config.json ファイルの表示

@@ -0,0 +1,54 @@
{
"url_sum": {
"enabled": true,
"service": "sum4all",
"group": true,
"qa_enabled":true,
"qa_prefix":"问",
"prompt": "你是一个新闻专家,我会给你发一些网页内容,请你用简单明了的语言做总结。格式如下:📌总结\n一句话讲清楚整篇文章的核心观点,控制在30字左右。\n\n💡要点\n用数字序号列出来3-5个文章的核心内容,尽量使用emoji让你的表达更生动"
},
"search_sum": {
"enabled": false,
"service": "sum4all",
"search_service": "duckduckgo",
"group": true,
"search_prefix":"搜",
"prompt": "你是一个信息检索专家,我会把我的问题和搜索结果发给你,请你根据问题,从搜索结果里找出能回答问题的相关内容,用简单明了的语言为我做回复,尽量使用emoji让你的表达更生动"
},
"file_sum": {
"enabled": true,
"service": "openai",
"max_file_size": "15000",
"group": true,
"qa_prefix":"问",
"prompt": "你是妇产科专业医生,同时,也是全科医生及健康管理顾问。以医生的口吻进行回复。格式如下:📌总结\n一句话讲清楚问题的核心观点,控制在30字左右。\n\n💡要点\n用数字序号列出来3-5个文章的核心内容,尽量使用emoji让你的表达更生动。"
},
"image_sum": {
"enabled": true,
"service": "openai",
"group": true,
"qa_prefix":"问",
"prompt": "先全局分析图片的主要内容,并按照逻辑分层次、段落,提炼出5个左右图片中的精华信息、关键要点,生动地向读者描述图片的主要内容。注意排版、换行、emoji、标签的合理搭配,清楚地展现图片讲了什么。"
},
"note": {
"enabled": false,
"service": "flomo",
"prefix":"记"
},
"keys": {
"sum4all_key": "",
"search1api_key": "",
"gemini_key": "",
"perplexity_key": "",
"open_ai_api_key": "sk-5dyg7PMUNeoSqHH807453eB06f434c34Ba6fB4764aC8358c",
"model": "moonshot-v1-32k",
"open_ai_api_base": "http://106.15.182.218:3001/v1",
"xunfei_app_id": "",
"xunfei_api_key": "",
"xunfei_api_secret": "",
"opensum_key": "",
"bibigpt_key": "",
"outputLanguage": "zh-CN",
"flomo_key":""
}
}

+ 572
- 0
plugins/kimi4upload/kimi4upload.py ファイルの表示

@@ -0,0 +1,572 @@
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


EXTENSION_TO_TYPE = {
'pdf': 'pdf',
'doc': 'docx', 'docx': 'docx',
'md': 'md',
'txt': 'txt',
'xls': 'excel', 'xlsx': 'excel',
'csv': 'csv',
'html': 'html', 'htm': 'html',
'ppt': 'ppt', 'pptx': 'ppt'
}

@plugins.register(
name="kimi4upload",
desire_priority=-1,
desc="A plugin for upload",
version="0.0.01",
author="",
)


class file4upload(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)
# logger.info(f"user_id:{user_id},content:{content},isgroup:{isgroup}")
# logger.info(f'上下文参数缓存键字典:{self.params_cache.keys}')
# logger.info(f'user_id in self.params_cache: {user_id in self.params_cache}')
# 上次提示
if context.type == ContextType.TEXT:
self.previous_prompt=msg.content

if isgroup and not self.file_sum_group:
# 群聊中忽略处理文件
logger.info("群聊消息,文件处理功能已禁用")
return
logger.info("on_handle_context: 处理上下文开始")
context.get("msg").prepare()
# file_path = context.content
# logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
api_key='sk-5z2L4zy9T1w90j6e3T90ANZdyN2zLWClRwFnBzWgzdrG4onx'

if context.type == ContextType.IMAGE:
file_path = context.content
logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
print(f'处理首次上次的图片,准备抽取文字')
file_content=self.extract_content_by_llm(file_path,api_key)
self.params_cache[user_id] = {}
if file_content is not None:
logger.info('图片中抽取文字,使用使用图片的文字请求LLM')
messages = [{
"role": "system",
"content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。",
},{
"role": "system",
"content": file_content,
}]
self.params_cache[user_id]['last_word_messages']=messages
self.params_cache[user_id]['last_image_oss']=None
else:
logger.info('不能抽取文字,使用图片oss请求LLM')
# logger.info(f"on_handle_context: 获取到图片路径 {file_path}")
# base64_image=self.encode_image_to_base64(file_path)
# self.params_cache[user_id]['last_image_oss']=base64_image
# self.params_cache[user_id]['last_word_messages']=None
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_url = upload_oss(access_key_id, access_key_secret, endpoint, bucket_name, local_file_path, oss_file_name)
logger.info(f'写入图片缓存oss 地址{file_url}')
self.params_cache[user_id]['last_image_oss']=file_url
self.params_cache[user_id]['last_word_messages']=None


if self.previous_prompt == '':
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:
e_context.action = EventAction.CONTINUE

if context.type == ContextType.FILE:
file_path = context.content
logger.info(f"on_handle_context: 获取到文件路径 {file_path}")
print(f'处理首次上次的文件')
file_content=self.extract_content_by_llm(file_path,api_key)
if file_content is not None:
self.params_cache[user_id] = {}
messages = [{
"role": "system",
"content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。",
},{
"role": "system",
"content": file_content,
}]
self.params_cache[user_id]['last_word_messages']=messages
self.params_cache[user_id]['last_image_oss']=None

if self.previous_prompt == '':
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:
e_context.action = EventAction.CONTINUE
# if user_id in self.params_cache and (self.params_cache[user_id]['last_word_messages']!=None):
if user_id in self.params_cache:
if 'last_word_messages' in self.params_cache[user_id] and self.params_cache[user_id]['last_word_messages'] is not None:
print(f'缓存处理已经上传的文件')
# last_word_messages=self.params_cache[user_id]['last_word_messages']
# cache_messages=last_word_messages[:2]
cache_messages=self.params_cache[user_id]['last_word_messages']
messages = [
*cache_messages,
{
"role": "user",
"content": self.previous_prompt ,#msg.content,
},
]
self.handle_file_upload(messages, e_context)

# if user_id in self.params_cache and ('last_image_oss' in self.params_cache[user_id] or self.params_cache[user_id]['last_image_oss']!=None):
if user_id in self.params_cache:
if 'last_image_oss' in self.params_cache[user_id] and self.params_cache[user_id]['last_image_oss'] is not None:
print(f'缓存处理已经oss图片的文件')
file_url=self.params_cache[user_id]['last_image_oss']
messages = [{
"role": "system",
"content": "你是一个能能描述任何图片的智能助手",
},
{
"role": "user",
"content": f'{file_url}\n{self.previous_prompt}',
}]
# messages=[
# {
# "role": "user",
# "content": [
# {
# "type": "image_url",
# "image_url": {
# "url": f"{file_url}"
# }
# },
# {
# "type": "text",
# "text": f"{self.previous_prompt}"
# }
# ]
# }
# ]


self.handle_images_oos(messages, e_context)
def handle_file_upload(self, messages, e_context):
logger.info("handle_file: 向LLM发送内容总结请求")
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.file_sum_prompt)
self.params_cache[user_id] = {}


try:
api_key = "sk-5z2L4zy9T1w90j6e3T90ANZdyN2zLWClRwFnBzWgzdrG4onx"
# base_url = "https://api.moonshot.cn/v1",
api_url = "https://api.moonshot.cn/v1/chat/completions"
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}

data={
"model": "moonshot-v1-128k",
"messages":messages,
# "temperature": 0.3
}
response = requests.post(url=api_url, headers=headers, data=json.dumps(data))
logger.info(f'handle_file_upload: 请求文件内容{json.dumps(messages, ensure_ascii=False)}')
response.raise_for_status()
response_data = response.json()
if "choices" in response_data and len(response_data["choices"]) > 0:
first_choice = response_data["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip() # 获取响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
# self.params_cache[user_id]['last_word_messages']=messages
# if self.params_cache[user_id]['last_word_messages']!=None:
# self.params_cache[user_id]['last_word_messages']=messages

self.previous_prompt =''
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"
except requests.exceptions.RequestException as e:
logger.error(f"Error calling LLM API: {e}")
reply_content = f"An error occurred while calling LLM API"

reply = Reply()
reply.type = ReplyType.TEXT
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS

def handle_images_base64(self, messages, e_context):
logger.info("handle_file: 向LLM发送内容总结请求")
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.file_sum_prompt)


try:
# api_key = "sk-5z2L4zy9T1w90j6e3T90ANZdyN2zLWClRwFnBzWgzdrG4onx"
# # base_url = "https://api.moonshot.cn/v1",
# api_url = "https://api.moonshot.cn/v1/chat/completions"


# api_key = "sk-5dyg7PMUNeoSqHH807453eB06f434c34Ba6fB4764aC8358c"
# api_url = "http://106.15.182.218:3001/v1/chat/completions"
# headers = {
# 'Content-Type': 'application/json',
# 'Authorization': f'Bearer {api_key}'
# }

# data={
# "model": "moonshot-v1-128k",
# "messages":messages,
# # "temperature": 0.3
# }
# response = requests.post(url=api_url, headers=headers, json=data)
base64_image=self.encode_image_to_base64('tmp/240926-164856.png')
api_key = self.open_ai_api_key
api_base = f"{self.open_ai_api_base}/chat/completions"
logger.info(api_base)
payload = {
"model": "moonshot-v1-128k",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": self.previous_prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 3000
}

# payload = {
# "model": "moonshot-v1-128k",
# "messages": messages,
# "max_tokens": 3000
# }
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
logger.info('开始')
response = requests.post(api_base, headers=headers, json=payload)


# logger.info(f'handle_file_upload: 请求文件内容{json.dumps(messages, ensure_ascii=False)}')
response.raise_for_status()
response_data = response.json()
if "choices" in response_data and len(response_data["choices"]) > 0:
first_choice = response_data["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip() # 获取响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
# self.params_cache[user_id]['last_word_messages']=messages
# if self.params_cache[user_id]['last_word_messages']!=None:
# self.params_cache[user_id]['last_word_messages']=messages

self.previous_prompt =''
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"
except requests.exceptions.RequestException as e:
logger.error(f"Error calling LLM API: {e}")
reply_content = f"An error occurred while calling LLM API"

reply = Reply()
reply.type = ReplyType.TEXT
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
def handle_images_oos(self, messages, e_context):
logger.info("handle_file: 向LLM发送内容总结请求")
msg: ChatMessage = e_context["context"]["msg"]
user_id = msg.from_user_id
user_params = self.params_cache.get(user_id, {})
prompt = user_params.get('prompt', self.file_sum_prompt)
self.params_cache[user_id] = {}


try:
api_key = self.open_ai_api_key
api_base = f"{self.open_ai_api_base}/chat/completions"
logger.info(api_base)
payload = {
"model": "7374349217580056592",
"messages":messages,
"max_tokens": 3000
}

# payload = {
# "model": "moonshot-v1-128k",
# "messages": messages,
# "max_tokens": 3000
# }
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# logger.info('开始')
response = requests.post(api_base, headers=headers, json=payload)
logger.info(f'handle_file_upload: 请求文件内容{json.dumps(messages, ensure_ascii=False)}')
response.raise_for_status()
response_data = response.json()
if "choices" in response_data and len(response_data["choices"]) > 0:
first_choice = response_data["choices"][0]
if "message" in first_choice and "content" in first_choice["message"]:
response_content = first_choice["message"]["content"].strip() # 获取响应内容
reply_content = response_content.replace("\\n", "\n") # 替换 \\n 为 \n
# self.params_cache[user_id]['last_word_messages']=messages
# if self.params_cache[user_id]['last_word_messages']!=None:
# self.params_cache[user_id]['last_word_messages']=messages

self.previous_prompt =''
else:
logger.error("Content not found in the response")
reply_content = "Content not found in the LLM API response"
else:
logger.error("No choices available in the response")
reply_content = "No choices available in the LLM API response"
except requests.exceptions.RequestException as e:
logger.error(f"Error calling LLM API: {e}")
reply_content = f"An error occurred while calling LLM API"

reply = Reply()
reply.type = ReplyType.TEXT
reply.content = f"{remove_markdown(reply_content)}"
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
def extract_content_by_llm(self, 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)
# print(response.text)
response_data = response.json()
file_id = response_data.get('id')
# print(f'文件id:{file_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 encode_image_to_base64(self, image_path):
logger.info(f"开始处理图片: {image_path}")
try:
with Image.open(image_path) as img:
logger.info(f"成功打开图片. 原始大小: {img.size}")
if img.width > 1024:
new_size = (1024, int(img.height*1024/img.width))
img = img.resize(new_size)
img.save(image_path) # 保存调整大小后的图片
logger.info(f"调整图片大小至: {new_size}")

with open(image_path, "rb") as image_file:
img_byte_arr = image_file.read()
logger.info(f"读取图片完成. 大小: {len(img_byte_arr)} 字节")

encoded = base64.b64encode(img_byte_arr).decode('ascii')
logger.info(f"Base64编码完成. 编码后长度: {len(encoded)}")
return encoded
except Exception as e:
logger.error(f"图片编码过程中发生错误: {str(e)}", exc_info=True)
raise

def remove_markdown(text):
# 替换Markdown的粗体标记
text = text.replace("**", "")
# 替换Markdown的标题标记
text = text.replace("### ", "").replace("## ", "").replace("# ", "")
return text


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

+ 9
- 0
plugins/kimi4upload/requirements.txt ファイルの表示

@@ -0,0 +1,9 @@
python-docx
markdown
PyMuPDF
openpyxl
beautifulsoup4
python-pptx
Pillow
oss2


+ 2
- 0
requirements.txt ファイルの表示

@@ -8,3 +8,5 @@ Pillow
pre-commit
web.py
linkai>=0.0.6.0
pypng
pypinyin

+ 8
- 0
voice/ali/ali_voice.py ファイルの表示

@@ -20,6 +20,7 @@ from voice.voice import Voice
from voice.ali.ali_api import AliyunTokenGenerator, speech_to_text_aliyun, text_to_speech_aliyun
from config import conf

from pypinyin import pinyin, Style

class AliVoice(Voice):
def __init__(self):
@@ -76,6 +77,13 @@ class AliVoice(Voice):
text = speech_to_text_aliyun(self.api_url_voice_to_text, pcm, self.app_key, token_id)
if text:
logger.info("[Ali] VoicetoText = {}".format(text))
first_two_chars = text[:2]
pinyin_text = ''.join([item[0] for item in pinyin(first_two_chars, style=Style.NORMAL)])
if(pinyin_text in ['xiaoyun','xiaoyin','xiaoying','xiaoyue','xiaoyuan','xiaoxin']):
print(f'触发词:{first_two_chars};拼音:{pinyin_text}')
text= pinyin_text+''+text[2:]
logger.info("触发词转拼音 [Ali] VoicetoText = {}".format(text))
reply = Reply(ReplyType.TEXT, text)
else:
reply = Reply(ReplyType.ERROR, "抱歉,语音识别失败")


+ 7
- 0
voice/ali/config.json ファイルの表示

@@ -0,0 +1,7 @@
{
"api_url_text_to_voice": "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/tts",
"api_url_voice_to_text": "https://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr",
"app_key": "F3VB6magxpjpPgKH",
"access_key_id": "LTAI5tJS8kD1mh2fLzVJ4u3w",
"access_key_secret": "ahiLuHLiSeqBDMCgtmc9Qe3uvgo6pJ"
}

読み込み中…
キャンセル
保存