Browse Source

用户选择

develop
H Vs 1 month ago
parent
commit
fce2a8006d
37 changed files with 265 additions and 5048 deletions
  1. +0
    -3
      .gitignore
  2. +58
    -7
      bot/chatgpt/chat_gpt_bot.py
  3. +1
    -1
      bot/chatgpt/chat_gpt_session.py
  4. +39
    -33
      channel/wechat/wechat_channel.py
  5. +11
    -8
      common/kafka_helper.py
  6. +114
    -0
      common/kafka_helper.txt
  7. +2
    -1
      common/memory.py
  8. +15
    -6
      config-dev.json
  9. +2
    -2
      config-origin.json
  10. +1
    -1
      config-production.json
  11. +2
    -2
      config-test.json
  12. +4
    -4
      config.json
  13. +11
    -3
      docker/Dockerfile.latest
  14. +0
    -51
      docker/entrypoint-bk.sh
  15. +3
    -1
      docker/entrypoint.sh
  16. +0
    -1
      plugins/coze4upload/__init__.py
  17. +0
    -54
      plugins/coze4upload/config.json
  18. +0
    -308
      plugins/coze4upload/coze4upload-dev.txt
  19. +0
    -372
      plugins/coze4upload/coze4upload.py
  20. +0
    -271
      plugins/coze4upload/coze4upload.txt
  21. +0
    -279
      plugins/coze4upload/coze4upload2.txt
  22. +0
    -336
      plugins/coze4upload/coze4upload3.txt
  23. +0
    -374
      plugins/coze4upload/coze4upload4.txt
  24. +0
    -9
      plugins/coze4upload/requirements.txt
  25. +0
    -1
      plugins/file4upload/__init__.py
  26. +0
    -54
      plugins/file4upload/config.json
  27. +0
    -632
      plugins/file4upload/file4upload.py
  28. +0
    -619
      plugins/file4upload/file4upload.txt
  29. +0
    -523
      plugins/file4upload/file4upload0.txt
  30. +0
    -8
      plugins/file4upload/requirements.txt
  31. +2
    -2
      plugins/healthai/healthai.py
  32. +0
    -434
      plugins/healthai/healthai.txt
  33. +0
    -1
      plugins/kimi4upload/__init__.py
  34. +0
    -54
      plugins/kimi4upload/config.json
  35. +0
    -572
      plugins/kimi4upload/kimi4upload.py
  36. +0
    -9
      plugins/kimi4upload/requirements.txt
  37. +0
    -12
      plugins/plugins.json

+ 0
- 3
.gitignore View File

@@ -31,9 +31,6 @@ plugins/banwords/lib/__pycache__
!plugins/keyword
!plugins/linkai
!plugins/healthai
!plugins/coze4upload
!plugins/file4upload
!plugins/kimi4upload
client_config.json
!config.json
!plugins.json

+ 58
- 7
bot/chatgpt/chat_gpt_bot.py View File

@@ -18,6 +18,8 @@ from common.token_bucket import TokenBucket
from config import conf, load_config
from channel.chat_message import ChatMessage

from common import memory


# OpenAI对话模型API (可用)
class ChatGPTBot(Bot, OpenAIImage):
@@ -126,19 +128,68 @@ class ChatGPTBot(Bot, OpenAIImage):
# if api_key == None, the default openai.api_key will be used
if args is None:
args = self.args
response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **args)

# Define additional parameters
additional_params = {
"chatId": session.session_id,
"detail": True
}

# Combine the additional params with the existing args (if any)
args.update(additional_params)
# msgs=session.messages

# cache_data = memory.USER_INTERACTIVE_CACHE.get(session.session_id)

# # Determine messages to send based on cache data
# messages_to_send = msgs[-1] if cache_data and cache_data.get('interactive') else msgs
# print(msgs[-1])
# print('----------------')
# # Send the response using OpenAI API
# response = openai.ChatCompletion.create(api_key=api_key, messages=messages_to_send, **args)
messages_to_send=session.messages

cache_data = memory.USER_INTERACTIVE_CACHE.get(session.session_id)
if cache_data and cache_data.get('interactive'):
messages_to_send=[session.messages[-1]]
print(messages_to_send)
response = openai.ChatCompletion.create(api_key=api_key, messages=messages_to_send, **args)
# print("{}".format(session.__dict__))
logger.info("[CHATGPT] 请求={}".format(session.messages))
logger.info("[CHATGPT] 请求={}".format(messages_to_send))
# 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"]))
content=response.choices[0]["message"]["content"]
return {
"total_tokens": response["usage"]["total_tokens"],
"completion_tokens": response["usage"]["completion_tokens"],
"content": content.lstrip("\n"),
}
description = ''
userSelectOptions = []
if isinstance(content, list):
# print(content)
for item in content:
if item["type"] == "interactive" and item["interactive"]["type"] == "userSelect":
params = item["interactive"]["params"]
description = params.get("description")
userSelectOptions = params.get("userSelectOptions", [])
values_string = "\n".join(option["value"] for option in userSelectOptions)
if description is not None:
memory.USER_INTERACTIVE_CACHE[session.session_id] = {
"interactive":True
}
return {
"total_tokens": response["usage"]["total_tokens"],
"completion_tokens": response["usage"]["completion_tokens"],
"content": description + '------------------------------\n'+values_string,
}
else:
memory.USER_INTERACTIVE_CACHE[session.session_id] = {
"interactive":False
}
return {
"total_tokens": response["usage"]["total_tokens"],
"completion_tokens": response["usage"]["completion_tokens"],
"content": content.lstrip("\n"),
}
except Exception as e:
need_retry = retry_count < 2
result = {"completion_tokens": 0, "content": "我现在有点累了,等会再来吧"}


+ 1
- 1
bot/chatgpt/chat_gpt_session.py View File

@@ -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", '7374349217580056592',const.GPT4_TURBO_PREVIEW, const.GPT4_VISION_PREVIEW, const.GPT4_TURBO_01_25,
"gpt-4-1106-preview",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"):


+ 39
- 33
channel/wechat/wechat_channel.py View File

@@ -26,7 +26,7 @@ from lib import itchat
from lib.itchat.content import *
from urllib.parse import urlparse

import asyncio
import threading

from common import kafka_helper, redis_helper
@@ -36,6 +36,8 @@ import json,time,re
import pickle
from datetime import datetime
import oss2
import random



# from common.kafka_client import KafkaClient
@@ -156,7 +158,7 @@ class WechatChannel(ChatChannel):

# 好友定时同步
agent_nickname=self.name
friend_thread =threading.Thread(target=hourly_change_save_friends, args=(agent_nickname,))
friend_thread =threading.Thread(target=ten_mins_change_save_friends, args=(agent_nickname,))
friend_thread.start()

# 立刻同步
@@ -272,29 +274,12 @@ class WechatChannel(ChatChannel):
def send(self, reply: Reply, context: Context):
receiver = context["receiver"]
if reply.type == ReplyType.TEXT:
itchat.send(reply.content, toUserName=receiver)
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
# logger.info(context)
# logger.info(context["msg"])
# // 发送kafka
# msg=context["msg"]
sent_res=itchat.send(reply.content, toUserName=receiver)
logger.info("[WX] sendMsg={}, receiver={} {}".format(reply, receiver,sent_res.get('BaseResponse',{}).get('RawMsg')))
msg: ChatMessage = context["msg"]
# content=msg["content"]
is_group=msg.is_group
if not is_group:
# print(f'响应:{content}')
# 用户输入
# input_content=msg.content
# input_from_user_nickname=msg.from_user_nickname
# input_to_user_nickname=msg.to_user_nickname

# input_wx_content_dialogue_message=[{"type": "text", "text": input_content}]
# input_message=dialogue_message(input_from_user_nickname,input_to_user_nickname,input_wx_content_dialogue_message)
# kafka_helper.kafka_client.produce_message(input_message)
# logger.info("发送对话 %s", json.dumps(input_message, separators=(',', ':'), ensure_ascii=False))

# 响应用户
output_content=reply.content
output_from_user_nickname=msg.to_user_nickname # 回复翻转
@@ -456,6 +441,20 @@ def hourly_change_save_friends(agent_nickname):
last_hour = current_hour
time.sleep(1) # 每秒检查一次

def ten_mins_change_save_friends(agent_nickname):
last_check_minute = datetime.now().minute # 获取当前分钟
while True:
current_minute = datetime.now().minute
if current_minute % 10 == 0 and current_minute != last_check_minute: # 检测每10分钟变化
friends = itchat.get_friends(update=True)[1:]

agent_info = fetch_agent_info(agent_nickname)
agent_tel = agent_info.get("agent_tel", None)
save_friends_to_redis(agent_tel, agent_nickname, friends)
last_check_minute = current_minute # 更新最后检查的分钟

time.sleep(60) # 每分钟检查一次

def wx_messages_process_callback(user_nickname,message):
"""
处理消费到的 Kafka 消息(基础示例)
@@ -491,19 +490,20 @@ def wx_messages_process_callback(user_nickname,message):
if friend.get("NickName",None) == nickname:
wx_content_list=content_data.get("wx_content",[])
for wx_content in wx_content_list:
# 处理文
# 处理文
if wx_content.get("type",None) == 'text':
wx_content_text=wx_content.get("text",None)
itchat.send(wx_content_text, toUserName=friend.get("UserName",None))
logger.info(f"{user_nickname} 向 {nickname} 发送文字【 {wx_content_text} 】")
sent_res=itchat.send(wx_content_text, toUserName=friend.get("UserName",None))
logger.info(f"{user_nickname} 向 {nickname} 发送文字【 {wx_content_text} 】 {sent_res.get('BaseResponse',{}).get('RawMsg')}")

# // 发送kafka
wx_content_dialogue_message=[{"type": "text", "text": wx_content_text}]
message=dialogue_message(agent_nickname_data,friend.get("NickName",None),wx_content_dialogue_message)
kafka_helper.kafka_client.produce_message(message)
logger.info("发送对话 %s",message)
time.sleep(10)
# 等待随机时间
time.sleep(random.uniform(30, 60))
# 处理图片
elif wx_content.get("type",None) == 'image_url':
print('发送图片')
@@ -527,15 +527,16 @@ def wx_messages_process_callback(user_nickname,message):
logger.error(f"Failed to convert image: {e}")
return

itchat.send_image(image_storage, toUserName=friend.get("UserName",None))
logger.info(f"{user_nickname} 向 {nickname} 发送图片【 {url} 】")
sent_res=itchat.send_image(image_storage, toUserName=friend.get("UserName",None))
logger.info(f"{user_nickname} 向 {nickname} 发送图片【 {url} 】{sent_res.get('BaseResponse',{}).get('RawMsg')}")
# // 发送kafka
wx_content_dialogue_message=[{"type": "image_url", "image_url": {"url": url}}]
message=dialogue_message(agent_nickname_data,friend.get("NickName",None),wx_content_dialogue_message)
kafka_helper.kafka_client.produce_message(message)
logger.info("发送对话 %s",message)
time.sleep(10)
# 等待随机时间
time.sleep(random.uniform(30, 60))
#处理文件
elif wx_content.get("type",None) == 'file':
print('处理文件')
@@ -553,16 +554,17 @@ def wx_messages_process_callback(user_nickname,message):

tmp_file_path=save_to_local_from_url(url)

itchat.send_file(tmp_file_path, toUserName=friend.get("UserName",None))
sent_res=itchat.send_file(tmp_file_path, toUserName=friend.get("UserName",None))
logger.info(f'删除本地{ext}文件: {tmp_file_path}')
os.remove(tmp_file_path)
logger.info(f"{user_nickname} 向 {nickname} 发送 {ext} 文件【 {url} 】")
logger.info(f"{user_nickname} 向 {nickname} 发送 {ext} 文件【 {url} 】{sent_res.get('BaseResponse',{}).get('RawMsg')}")
# // 发送kafka
wx_content_dialogue_message=[{"type": "file", "file_url": {"url": url}}]
message=dialogue_message(agent_nickname_data,friend.get("NickName",None),wx_content_dialogue_message)
kafka_helper.kafka_client.produce_message(message)
logger.info("发送对话 %s",message)
time.sleep(10)
# 等待随机时间
time.sleep(random.uniform(30, 60))

elif ext in ['.mp4']:

@@ -577,7 +579,8 @@ def wx_messages_process_callback(user_nickname,message):
message=dialogue_message(agent_nickname_data,friend.get("NickName",None),wx_content_dialogue_message)
kafka_helper.kafka_client.produce_message(message)
logger.info("发送对话 %s",message)
time.sleep(10)
# 等待随机时间
time.sleep(random.uniform(30, 60))
else:
logger.warning(f'暂不支持 {ext} 文件的处理')
@@ -586,6 +589,9 @@ def wx_messages_process_callback(user_nickname,message):
else:
return False




def dialogue_message(nickname_from,nickname_to,wx_content):
"""
构造消息的 JSON 数据


+ 11
- 8
common/kafka_helper.py View File

@@ -23,7 +23,8 @@ class KafkaClient:
'bootstrap.servers': self.bootstrap_servers,
'group.id': self.consumer_group,
'auto.offset.reset': 'earliest',
'enable.auto.commit': False # 禁用自动提交,使用手动提交
# 'enable.auto.commit': False # 禁用自动提交,使用手动提交
'enable.auto.commit': True
})

def delivery_report(self, err, msg):
@@ -84,13 +85,15 @@ class KafkaClient:
# 调用业务处理逻辑
# process_callback(msg.value().decode('utf-8'))
# 调用业务处理逻辑,传递 user_nickname 和消息
if process_callback(user_nickname, msg.value().decode('utf-8')):
# 如果返回 True,表示处理成功,可以提交偏移量
try:
self.consumer.commit(msg)
print(f"Manually committed offset: {msg.offset()}")
except KafkaException as e:
print(f"Error committing offset: {e}")
process_callback(user_nickname, msg.value().decode('utf-8'))
# if process_callback(user_nickname, msg.value().decode('utf-8')):
# # 如果返回 True,表示处理成功,可以提交偏移量
# try:
# # self.consumer.commit(msg)
# self.consumer.commit(message=msg, asynchronous=True)
# print(f"Manually committed offset: {msg.offset()}")
# except KafkaException as e:
# print(f"Error committing offset: {e}")
except KeyboardInterrupt:
print("消费中断")
finally:


+ 114
- 0
common/kafka_helper.txt View File

@@ -0,0 +1,114 @@
from confluent_kafka import Producer, Consumer, KafkaException, KafkaError
import os
from common.singleton import singleton
from config import conf
# 定义全局 redis_helper
kafka_client = None

class KafkaClient:
def __init__(self):
bootstrap_servers=conf().get("kafka_bootstrap_servers")
agent_tel=os.environ.get('tel', '12345678901')
consumer_group=f'aiops-wx_{agent_tel}'
print(f'kafka消费组 {consumer_group}')
topic="topic.aiops.wx"

self.bootstrap_servers = bootstrap_servers
self.consumer_group = consumer_group
self.topic = topic
self.producer = Producer({'bootstrap.servers': self.bootstrap_servers})
self.consumer = Consumer({
'bootstrap.servers': self.bootstrap_servers,
'group.id': self.consumer_group,
'auto.offset.reset': 'earliest',
# 'enable.auto.commit': False # 禁用自动提交,使用手动提交
'enable.auto.commit': True
})

def delivery_report(self, err, msg):
"""
回调函数,用于确认消息是否成功发送
"""
if err is not None:
print(f"Message delivery failed: {err}")
else:
print(f"Message delivered to {msg.topic()} [{msg.partition()}] @ {msg.offset()}")

def produce_messages(self, messages):
"""
发送消息
"""
try:
for message in messages:
self.producer.produce(self.topic, value=message, callback=self.delivery_report)
print(f"Produced: {message}")
self.producer.poll(0)
except Exception as e:
print(f"An error occurred: {e}")
finally:
self.producer.flush()

def produce_message(self, message):
"""
发送消息
"""
try:
self.producer.produce(self.topic, value=message, callback=self.delivery_report)
# print(f"Produced: {message}")
self.producer.poll(0)
except Exception as e:
print(f"An error occurred: {e}")
finally:
self.producer.flush()

def consume_messages(self,process_callback, user_nickname):
"""
消费消息并调用回调处理业务逻辑,只有当回调返回 True 时才提交偏移量
:param process_callback: 业务逻辑回调函数,返回布尔值
:param user_nickname: 用户昵称
"""
self.consumer.subscribe([self.topic])
try:
while True:
msg = self.consumer.poll(0.3)
if msg is None:
continue
if msg.error():
if msg.error().code() == KafkaError._PARTITION_EOF:
print(f"End of partition {msg.partition}, offset {msg.offset()}")
else:
raise KafkaException(msg.error())
else:
# 调用业务处理逻辑
# process_callback(msg.value().decode('utf-8'))
# 调用业务处理逻辑,传递 user_nickname 和消息
process_callback(user_nickname, msg.value().decode('utf-8'))
# if process_callback(user_nickname, msg.value().decode('utf-8')):
# # 如果返回 True,表示处理成功,可以提交偏移量
# try:
# # self.consumer.commit(msg)
# self.consumer.commit(message=msg, asynchronous=True)
# print(f"Manually committed offset: {msg.offset()}")
# except KafkaException as e:
# print(f"Error committing offset: {e}")
except KeyboardInterrupt:
print("消费中断")
finally:
self.consumer.close()

# if __name__ == '__main__':
# kafka_client = KafkaClient(bootstrap_servers='localhost:9092', consumer_group='my-consumer-group', topic='my_topic')
# # 生产消息
# messages_to_produce = [f"Message {i}" for i in range(10)]
# kafka_client.produce_messages(messages_to_produce)
# # 消费消息
# kafka_client.consume_messages()

def start():
global kafka_client
kafka_client = KafkaClient()

+ 2
- 1
common/memory.py View File

@@ -1,3 +1,4 @@
from common.expired_dict import ExpiredDict

USER_IMAGE_CACHE = ExpiredDict(60 * 3)
USER_IMAGE_CACHE = ExpiredDict(60 * 3)
USER_INTERACTIVE_CACHE=ExpiredDict(60 * 1)

+ 15
- 6
config-dev.json View File

@@ -1,11 +1,11 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"open_ai_api_key": "sk-tdi7u0zuLsR0JpPMGBeFZxymOpL0zoFVafX8EEEvEakIDAGQ22NyQ6w",
"model": "",
"open_ai_api_key": "sk-uJDBdKmJVb2cmfldGOvlIY6Qx0AzqWMPD3lS1IzgQYzHNOXv9SKNI",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
"proxy": "",
"hot_reload": false,
"hot_reload": true,
"debug": true,
"single_chat_reply_prefix": "",
"group_chat_prefix": [
@@ -14,14 +14,14 @@
"group_name_white_list": [
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "",
"trigger_by_self": true,
"voice_to_text":"ali",
"speech_recognition": true,
"group_speech_recognition": true,
"voice_reply_voice": false,
"voice_reply_voice": true,
"conversation_max_tokens": 2500,
"expires_in_seconds": 300,
"character_desc": "",
@@ -29,5 +29,14 @@
"subscribe_msg": "",
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
"linkai_app_code": "",

"redis_host":"47.116.142.20",
"redis_port":8090,
"redis_password":"telpo#1234",
"redis_db":3,
"kafka_bootstrap_servers":"192.168.2.121:9092",

"aiops_api":"https://id.ssjlai.com/aiopsadmin"
}

+ 2
- 2
config-origin.json View File

@@ -1,6 +1,6 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"model": "",
"open_ai_api_key": "sk-bQSMCXqpB5sdTMksYUWSFusy1zOQeYPs5Qty0Scw5LoPuvSDa7UFQIOn97mGGsOW",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
@@ -19,7 +19,7 @@
"AI好蕴智能体"
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "您好,我是AI好蕴健康顾问,非常高兴能够陪伴您一起踏上这一段美妙而重要的旅程。怀孕是生命中的一个特殊阶段,不仅承载着新生命的期许,也意味着您和家庭将迎来新的挑战和变化。在这个过程中,了解怀孕的意义、注意事项和如何进行科学的健康管理,对您和宝宝的健康至关重要。\n怀孕期的每一天都是新生命成长的过程,每一次胎动都让人感受到生命的奇迹。对于准妈妈来说,这是一段与宝宝建立深厚联系的时期。与此同时,这段时间也会让您对生活、家庭和未来有更深层次的认识和规划。\n怀孕不仅是生理上的变化,更是心理和情感上的一次洗礼。一个健康乐观的妈妈才能诞生一个阳光天使宝宝!\n通过科学的健康管理和正确的生活方式,您可以为自己和宝宝创造一个健康、安全的环境。我们专门给您设立了独立的服务支撑保证体系,包括各个方面的专家将为您提供贴身呵护和陪伴,为您提供专业的指导和支持,愿您度过一个平安、健康且愉快的孕期。\n如果你有任何健康问题咨询,可@我或输入“小蕴”,呼唤我为你服务!\n 【如果你有任何健康问题咨询,可@我、或语音“小蕴”呼唤我为你服务!】 \n祝您怀孕顺利,宝宝健康成长!",
"trigger_by_self": true,


+ 1
- 1
config-production.json View File

@@ -1,6 +1,6 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"model": "",
"open_ai_api_key": "sk-bQSMCXqpB5sdTMksYUWSFusy1zOQeYPs5Qty0Scw5LoPuvSDa7UFQIOn97mGGsOW",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",


+ 2
- 2
config-test.json View File

@@ -1,6 +1,6 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"model": "",
"open_ai_api_key": "sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
@@ -14,7 +14,7 @@
"group_name_white_list": [
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "",
"trigger_by_self": true,


+ 4
- 4
config.json View File

@@ -1,11 +1,11 @@
{
"channel_type": "wx",
"model": "7374349217580056592",
"open_ai_api_key": "sk-tdi7u0zuLsR0JpPMGBeFZxymOpL0zoFVafX8EEEvEakIDAGQ22NyQ6w",
"model": "",
"open_ai_api_key": "sk-uJDBdKmJVb2cmfldGOvlIY6Qx0AzqWMPD3lS1IzgQYzHNOXv9SKNI",
"open_ai_api_base": "http://106.15.182.218:3000/api/v1",
"claude_api_key": "YOUR API KEY",
"proxy": "",
"hot_reload": false,
"hot_reload": true,
"debug": false,
"single_chat_reply_prefix": "",
"group_chat_prefix": [
@@ -15,7 +15,7 @@
"AI好蕴测试群3"
],
"image_create_prefix": [
"画","识别","看"
],
"group_welcome_msg": "",
"trigger_by_self": true,


+ 11
- 3
docker/Dockerfile.latest View File

@@ -5,8 +5,17 @@ ARG TZ='Asia/Shanghai'

ARG CHATGPT_ON_WECHAT_VER

RUN echo /etc/apt/sources.list
# RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
# RUN echo /etc/apt/sources.list
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

# Set the timezone and configure tzdata
RUN apt-get update \
&& apt-get install -y --no-install-recommends tzdata \
&& ln -sf /usr/share/zoneinfo/$TZ /etc/localtime \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& apt-get clean


ENV BUILD_PREFIX=/app

ADD . ${BUILD_PREFIX}
@@ -19,7 +28,6 @@ RUN apt-get update \
&& pip install --no-cache -r requirements.txt \
&& pip install --no-cache -r requirements-optional.txt \
&& pip install azure-cognitiveservices-speech \
&& pip install --no-cache -r ./plugins/file4upload/requirements.txt \
&& pip install --no-cache -r ./plugins/healthai/requirements.txt

WORKDIR ${BUILD_PREFIX}


+ 0
- 51
docker/entrypoint-bk.sh View File

@@ -1,51 +0,0 @@
#!/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



+ 3
- 1
docker/entrypoint.sh View File

@@ -13,8 +13,10 @@ 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}
elif [ "$environment" == "dev" ]; then
CHATGPT_ON_WECHAT_CONFIG_PATH=${CHATGPT_ON_WECHAT_CONFIG_PATH:-$CHATGPT_ON_WECHAT_PREFIX/config-dev.json}
else
echo "Invalid environment specified. Please set environment to 'test' or 'prod'."
echo "Invalid environment specified. Please set environment to 'test' or 'prod' or 'dev'."
exit 1
fi



+ 0
- 1
plugins/coze4upload/__init__.py View File

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

+ 0
- 54
plugins/coze4upload/config.json View File

@@ -1,54 +0,0 @@
{
"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":""
}
}

+ 0
- 308
plugins/coze4upload/coze4upload-dev.txt View File

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

+ 0
- 372
plugins/coze4upload/coze4upload.py View File

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

+ 0
- 271
plugins/coze4upload/coze4upload.txt View File

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

+ 0
- 279
plugins/coze4upload/coze4upload2.txt View File

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

+ 0
- 336
plugins/coze4upload/coze4upload3.txt View File

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

+ 0
- 374
plugins/coze4upload/coze4upload4.txt View File

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

+ 0
- 9
plugins/coze4upload/requirements.txt View File

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


+ 0
- 1
plugins/file4upload/__init__.py View File

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

+ 0
- 54
plugins/file4upload/config.json View File

@@ -1,54 +0,0 @@
{
"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":""
}
}

+ 0
- 632
plugins/file4upload/file4upload.py View File

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

+ 0
- 619
plugins/file4upload/file4upload.txt View File

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

+ 0
- 523
plugins/file4upload/file4upload0.txt View File

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

+ 0
- 8
plugins/file4upload/requirements.txt View File

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

+ 2
- 2
plugins/healthai/healthai.py View File

@@ -63,10 +63,10 @@ class healthai(Plugin):
self.sessions = SessionManager(ChatGPTSession, model=conf().get("model") or "gpt-3.5-turbo")

# 初始化成功日志
logger.info("[file4upload] inited.")
logger.info("[healthai] inited.")
except Exception as e:
# 初始化失败日志
logger.warn(f"file4upload init failed: {e}")
logger.warn(f"healthai init failed: {e}")


def on_handle_context(self, e_context: EventContext):


+ 0
- 434
plugins/healthai/healthai.txt View File

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

+ 0
- 1
plugins/kimi4upload/__init__.py View File

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

+ 0
- 54
plugins/kimi4upload/config.json View File

@@ -1,54 +0,0 @@
{
"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":""
}
}

+ 0
- 572
plugins/kimi4upload/kimi4upload.py View File

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

+ 0
- 9
plugins/kimi4upload/requirements.txt View File

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


+ 0
- 12
plugins/plugins.json View File

@@ -4,14 +4,6 @@
"enabled": true,
"priority": 999
},
"kimi4upload": {
"enabled": false,
"priority": 998
},
"coze4upload": {
"enabled": false,
"priority": 998
},

"Keyword": {
"enabled": true,
@@ -45,10 +37,6 @@
"enabled": true,
"priority": 998
},
"file4upload": {
"enabled": false,
"priority": -1
},
"Hello": {
"enabled": true,
"priority": -1


Loading…
Cancel
Save