├── __init__.py ├── images └── reply_reference_query.png ├── config.json ├── README.md └── ipartment.py /__init__.py: -------------------------------------------------------------------------------- 1 | from .ipartment import * 2 | -------------------------------------------------------------------------------- /images/reply_reference_query.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangxyd/ipartment/HEAD/images/reply_reference_query.png -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "reply_reference_query": true, 3 | "add_quoter_nickname": true, 4 | "group_at_probability": 0 5 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 插件描述 2 | 3 | 爱情公寓剧本专享chatgpt-on-wechat插件,用于设置群聊中回复消息时@对方的概率、识别对方的身份以及优化引用消息。 4 | 5 | ## 安装方法 6 | 7 | ```sh 8 | #installp https://github.com/wangxyd/ipartment.git 9 | #scanp 10 | ``` 11 | 12 | ## 配置步骤 13 | 14 | 配置文件为`config.json`,可以自行修改,示例如下: 15 | 16 | ```json 17 | { 18 | "group_at_probability": 0, 19 | "add_quoter_nickname": true, 20 | "reply_reference_query": true 21 | } 22 | ``` 23 | 24 | 在以上配置项中: 25 | 26 | - `group_at_probability`: 设置群聊中机器人回复消息时@对方的概率。取值范围0~1,0每次都不@对方,1每次都@对方; 27 | - `add_quoter_nickname`: 是否开启识别对方身份的功能。值为`true`时会将对方的微信昵称添加到对话内容中,以便机器人识别对方的身份并做出有针对性的回答; 28 | - `reply_reference_query`: 是否开启优化引用消息的功能。值为`true`时会告诉机器人,对话内容中哪部分是引用的消息,哪部分是新的问题。 29 | 30 | ## 备注 31 | 32 | chatgpt-on-wechat默认忽略引用的消息,需要修改源代码才会响应引用的消息,然后才可以开启优化引用消息的功能。 33 | 34 | 需要修改的源代码文件为`channel/chat_channel.py`,注释掉或者直接删除下图中红色方框中的4行代码,然后重启chatgpt-on-wechat即可: 35 | 36 | ![修改源码,响应引用的消息](images/reply_reference_query.png) 37 | 38 | 如果您有任何更好的想法或建议,都非常欢迎您积极提出哦~~~ -------------------------------------------------------------------------------- /ipartment.py: -------------------------------------------------------------------------------- 1 | # encoding:utf-8 2 | 3 | import re 4 | import random 5 | 6 | import plugins 7 | from bridge.context import ContextType 8 | from bridge.reply import Reply, ReplyType 9 | from common.log import logger 10 | from plugins import * 11 | 12 | @plugins.register( 13 | name="iPartment", 14 | desire_priority=88, 15 | hidden=True, 16 | desc="爱情公寓剧本专享插件。", 17 | version="1.2", 18 | author="空心菜", 19 | ) 20 | class iPartment(Plugin): 21 | def __init__(self): 22 | super().__init__() 23 | try: 24 | # 加载配置 25 | conf = super().load_config() 26 | if not conf: 27 | raise Exception("[iPartment] config.json not found") 28 | # 优化引用消息 29 | self.reply_reference_query = conf.get('reply_reference_query', False) 30 | if self.reply_reference_query: 31 | logger.info("[iPartment] reply_reference_query is on.") 32 | else: 33 | logger.info("[iPartment] reply_reference_query is off.") 34 | # 识别对方身份 35 | self.add_quoter_nickname = conf.get('add_quoter_nickname', False) 36 | if self.add_quoter_nickname: 37 | logger.info("[iPartment] add_quoter_nickname is on.") 38 | else: 39 | logger.info("[iPartment] add_quoter_nickname is off.") 40 | if self.add_quoter_nickname or self.reply_reference_query: 41 | self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context 42 | # 群聊中机器人回复消息时@对方的概率 43 | self.group_at_probability = conf.get('group_at_probability', 1) 44 | if self.group_at_probability < 1: 45 | self.handlers[Event.ON_DECORATE_REPLY] = self.on_decorate_reply 46 | logger.info("[iPartment] group_at_probability is on.") 47 | else: 48 | logger.info("[iPartment] group_at_probability is off because of group_at_probability>=1.") 49 | logger.info("[iPartment] inited") 50 | except Exception as e: 51 | logger.warn("[iPartment] init failed, ignore.") 52 | raise e 53 | 54 | def on_handle_context(self, e_context: EventContext): 55 | if e_context["context"].type not in [ContextType.TEXT]: 56 | return 57 | content = e_context["context"].content 58 | logger.debug("[iPartment] on_handle_context. content: %s" % content) 59 | try: 60 | # 优化引用消息 61 | if self.reply_reference_query and "」\n- - - - - - -" in content: 62 | content = re.sub(r'」\n(- ){6,}-', '」\n前面「」中的内容是引用的消息,新的问题是:', content) 63 | logger.debug(f"[iPartment] reference query have been modified.") 64 | # 识别对方身份,并在提示词开头添加”xxx对你说:“ 65 | # if self.add_quoter_nickname and e_context["context"].get("isgroup", False): 66 | if self.add_quoter_nickname: 67 | actual_user_nickname = e_context["context"]["msg"].actual_user_nickname or e_context["context"]["msg"].other_user_nickname 68 | add_prefix = f"{actual_user_nickname}对你说:" 69 | # 确保修改操作的幂等性 70 | if not content.startswith(add_prefix): 71 | content = add_prefix + content 72 | logger.debug(f"[iPartment] {add_prefix} have been added.") 73 | if e_context["context"].content != content: 74 | e_context["context"].content = content 75 | except Exception as e: 76 | logger.warn(f"[iPartment] error occurred: {e}.") 77 | 78 | def on_decorate_reply(self, e_context: EventContext): 79 | if e_context["reply"].type != ReplyType.TEXT: 80 | return 81 | logger.debug("[iPartment] on_decorate_reply.") 82 | # 设置群聊中机器人回复消息时@对方的概率 83 | try: 84 | need_at = True if random.random() < self.group_at_probability else False 85 | if not need_at: 86 | e_context["context"]["no_need_at"] = True 87 | logger.debug(f"[iPartment] no_need_at is {not need_at}.") 88 | except Exception as e: 89 | logger.warn(f"[iPartment] error occurred: {e}.") 90 | 91 | def get_help_text(self, **kwargs): 92 | return "爱情公寓剧本专享插件。" 93 | --------------------------------------------------------------------------------